A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

47 行
1.3 KiB

  1. # -*- coding: utf-8 -*-
  2. ## EarwigBot's Config File Parser
  3. from collections import defaultdict
  4. import ConfigParser as configparser
  5. import os
  6. main_cfg_path = os.path.join("config", "main.cfg")
  7. secure_cfg_path = os.path.join("config", "secure.cfg")
  8. config = dict()
  9. def load_config_file(filename):
  10. parser = configparser.SafeConfigParser()
  11. parser.optionxform = str # don't lowercase option names automatically
  12. parser.read(filename)
  13. return parser
  14. def make_new_config():
  15. print "You haven't configured the bot yet!"
  16. choice = raw_input("Would you like to do this now? [y/n] ")
  17. if choice.lower().startswith("y"):
  18. pass
  19. else:
  20. exit()
  21. def dump_config_to_dict(parsers):
  22. global config
  23. for parser in parsers:
  24. for section in parser.sections():
  25. for option in parser.options(section):
  26. try:
  27. config[section][option] = parser.get(section, option)
  28. except KeyError:
  29. config[section] = defaultdict(lambda: None)
  30. config[section][option] = parser.get(section, option)
  31. def load():
  32. if not os.path.exists(main_cfg_path):
  33. make_new_config()
  34. main_cfg = load_config_file(main_cfg_path)
  35. secure_cfg = load_config_file(secure_cfg_path)
  36. dump_config_to_dict([main_cfg, secure_cfg])