Git Product home page Git Product logo

pyyamlcfg's Introduction

yamlcfg

Hierarchical YAML configuration utility for Python

yamlcfg makes it easier to have different levels of YAML configuration files, with prioritization that you declare, based on the order of the paths keyword argument.

It first checks your environment for the variable, and if it exists, it uses that over anything else. Then it checks the first file in paths, or path, and then the next in order until the variable is found. If not found, it returns None.

Access is allowed via normal attribute access via the dot operator, or from an index such as config['myattr'].

To dump the full configuration that was loaded back to file (first path in paths), just invoke write().

Example:

from yamlcfg import YamlConfig
config = YamlConfig(path='~/.some_config.yml')
print(config.foo)
config.foo = 'bar'
config.write()

fifo_configs = YamlConfig(paths=
  ('.myconfig.yml', '~/.userconfig.yml', '/etc/myconfig/defaultconfig.yml')
)
# First checks .myconfig.yml, and if it doesn't exist there, it checks
# ~/.userconfig.yml, and so on. If an environment variable of the same name
# is set, it will use that first.
print(fifo_configs.some_var)
# Dumps to the first path in paths, with every variable it found in order.
fifo_configs.write()

pyyamlcfg's People

Contributors

johannestaas avatar

Stargazers

Gil Klein avatar otakuSiD avatar

Watchers

Brandon Dixon avatar  avatar  avatar  avatar James Cloos avatar sha avatar Darren Spruell avatar Jonas avatar  avatar

pyyamlcfg's Issues

Should os.makedirs before reading/writing

I'm getting this kind of error:

Traceback (most recent call last):
  File "/home/ale/dev/rocket-api/rock-cli/venv/bin/rock", line 9, in <module>
    load_entry_point('rock-cli==0.1', 'console_scripts', 'rock')()
  File "/home/ale/dev/rocket-api/rock-cli/venv/lib/python3.4/site-packages/click/core.py", line 700, in __call__
    return self.main(*args, **kwargs)
  File "/home/ale/dev/rocket-api/rock-cli/venv/lib/python3.4/site-packages/click/core.py", line 680, in main
    rv = self.invoke(ctx)
  File "/home/ale/dev/rocket-api/rock-cli/venv/lib/python3.4/site-packages/click/core.py", line 1027, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/ale/dev/rocket-api/rock-cli/venv/lib/python3.4/site-packages/click/core.py", line 873, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/ale/dev/rocket-api/rock-cli/venv/lib/python3.4/site-packages/click/core.py", line 508, in invoke
    return callback(*args, **kwargs)
  File "/home/ale/dev/rocket-api/rock-cli/venv/lib/python3.4/site-packages/click/decorators.py", line 16, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/ale/dev/rocket-api/rock-cli/rock_cli/cli.py", line 30, in register
    ctx.obj['config'].write()
  File "/home/ale/dev/rocket-api/rock-cli/venv/lib/python3.4/site-packages/yamlcfg/yml.py", line 107, in write
    with open(path, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/home/ale/.config/rock-cli/config.yml'

I think that's because the /home/ale/.config/rock-cli/ directory doesn't exist. It would've been great if some mkdirp stuff was done before trying to read or write the config.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.