瀏覽代碼

JSON -> YAML

tags/v0.1^2
Ben Kurtovic 12 年之前
父節點
當前提交
202464d9fb
共有 2 個文件被更改,包括 9 次插入7 次删除
  1. +1
    -0
      .gitignore
  2. +8
    -7
      earwigbot/config.py

+ 1
- 0
.gitignore 查看文件

@@ -3,6 +3,7 @@


# Ignore bot-specific config file: # Ignore bot-specific config file:
config.json config.json
config.yml


# Ignore logs directory: # Ignore logs directory:
logs/ logs/


+ 8
- 7
earwigbot/config.py 查看文件

@@ -21,7 +21,7 @@
# SOFTWARE. # SOFTWARE.


""" """
EarwigBot's JSON Config File Parser
EarwigBot's YAML Config File Parser


This handles all tasks involving reading and writing to our config file, This handles all tasks involving reading and writing to our config file,
including encrypting and decrypting passwords and making a new config file from including encrypting and decrypting passwords and making a new config file from
@@ -45,11 +45,12 @@ Additionally, _BotConfig has some functions used in config loading:
variables; won't work if passwords aren't encrypted variables; won't work if passwords aren't encrypted
""" """


import json
import logging import logging
import logging.handlers import logging.handlers
from os import mkdir, path from os import mkdir, path


import yaml

from earwigbot import blowfish from earwigbot import blowfish


__all__ = ["config"] __all__ = ["config"]
@@ -90,7 +91,7 @@ class _BotConfig(object):
def __init__(self): def __init__(self):
self._script_dir = path.dirname(path.abspath(__file__)) self._script_dir = path.dirname(path.abspath(__file__))
self._root_dir = path.split(self._script_dir)[0] self._root_dir = path.split(self._script_dir)[0]
self._config_path = path.join(self._root_dir, "config.json")
self._config_path = path.join(self._root_dir, "config.yml")
self._log_dir = path.join(self._root_dir, "logs") self._log_dir = path.join(self._root_dir, "logs")
self._decryption_key = None self._decryption_key = None
self._data = None self._data = None
@@ -105,12 +106,12 @@ class _BotConfig(object):
self._metadata] self._metadata]


def _load(self): def _load(self):
"""Load data from our JSON config file (config.json) into _config."""
"""Load data from our JSON config file (config.yml) into _config."""
filename = self._config_path filename = self._config_path
with open(filename, 'r') as fp: with open(filename, 'r') as fp:
try: try:
self._data = json.load(fp)
except ValueError as error:
self._data = yml.load(fp)
except yaml.YAMLError as error:
print "Error parsing config file {0}:".format(filename) print "Error parsing config file {0}:".format(filename)
print error print error
exit(1) exit(1)
@@ -158,7 +159,7 @@ class _BotConfig(object):


def _make_new(self): def _make_new(self):
"""Make a new config file based on the user's input.""" """Make a new config file based on the user's input."""
encrypt = raw_input("Would you like to encrypt passwords stored in config.json? [y/n] ")
encrypt = raw_input("Would you like to encrypt passwords stored in config.yml? [y/n] ")
if encrypt.lower().startswith("y"): if encrypt.lower().startswith("y"):
is_encrypted = True is_encrypted = True
else: else:


Loading…
取消
儲存