A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

47 linhas
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])