From a6afeab07d080319e8e8eca73e9d08c2bc0e85e5 Mon Sep 17 00:00:00 2001 From: dpedu Date: Thu, 18 Jun 2015 20:19:06 -0700 Subject: [PATCH] Allow json or yaml config --- bin/pyircbot | 2 +- pyircbot/pyircbot.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/bin/pyircbot b/bin/pyircbot index 633e3d1..3c27c12 100755 --- a/bin/pyircbot +++ b/bin/pyircbot @@ -24,7 +24,7 @@ if __name__ == "__main__": log.critical("No bot config file specified (-c). Exiting.") sys.exit(0) - botconfig = yaml.load(open(options.config, 'r')) + botconfig = PyIRCBot.load(options.config) log.debug(botconfig) diff --git a/pyircbot/pyircbot.py b/pyircbot/pyircbot.py index 91411f2..90c0229 100644 --- a/pyircbot/pyircbot.py +++ b/pyircbot/pyircbot.py @@ -341,3 +341,20 @@ class PyIRCBot: ob.message = message return ob # return (True, command, args, message) + + @staticmethod + def load(filepath): + """Return an object from the passed filepath + + :param filepath: path to a file of json or yaml format. filename must end with .json or .yml + :type filepath: str + """ + if filepath.endswith(".yml"): + from yaml import load + return load(open(filepath, 'r')) + + elif filepath.endswith(".json"): + from json import load + return load(open(filepath, 'r')) + else: + raise Exception("Unknown config format")