Pastebin

Paste #8897: 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

cfg = None


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)))



----------- output
C:\wc\git\rat\main\venv\Scripts\python.exe C:/wc/git/rat/main/argtest.py config2.yml
config2.yml
Traceback (most recent call last):
  File "C:/wc/git/rat/main/argtest.py", line 24, in <module>
    main()
  File "C:/wc/git/rat/main/argtest.py", line 19, in main
    config.global_load(path=config_file_path)
  File "C:\wc\git\rat\main\config.py", line 21, in global_load
    cfg = yaml.safe_load(configfile, Loader=Loader)
TypeError: safe_load() got an unexpected keyword argument 'Loader'

Process finished with exit code 1

New Paste


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

Go to most recent paste.