Pastebin

Paste #8895: No description

< previous paste - next paste>

Pasted by Anonymous Coward

Download View as text

--------------main.py
import os

import sys
import config


def usage():
    print("Usage: {name} <config file>".format(name=sys.argv[0]))
    sys.exit(1)


def main():
    try:
        config_file_path = sys.argv[1]
        print(config_file_path)
    except IndexError:
        # If no config file was provided use 'config.yml' in same folder as main
        config_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.yml")
        config.global_load(path=config_file_path)

    print("Main: ", config.cfg)

if __name__ == "__main__":
    main()



--------------- config.py
import sys
import yaml


class ConfigurationError(Exception):
    pass


def global_load(path):
    global cfg

    try:
        from yaml import CLoader as Loader, CDumper as Dumper
    except ImportError:
        from yaml import Loader, Dumper

    try:
        with open(path, 'r') as configfile:
            cfg = yaml.safe_load(configfile, Loader=Loader)
    except IOError:
        raise ConfigurationError("Failed to open config file: {}".format(repr(path)))


New Paste


Do not write anything in this field if you're a human.

Go to most recent paste.