Giter Site home page Giter Site logo

derpconf's Introduction

derpconf

derpconf abstracts loading configuration files for your app. derpconf was extracted from thumbor.

Intalling

Installing derpconf is as easy as:

pip install derpconf

Usage

Using it is as simple as:

from derpconf.config import Config

conf = Config.load('/path/to/my/cfg.conf')

assert conf.MY_KEY == 'MY_VALUE' # assuming there's a key called MY_KEY in
                                 # the configuration file.

Settings Defaults

If you want to set default values for your configurations, just call:

Config.define('MY-KEY', 'DEFAULT VALUE', 'Description for my key', 'Section')

The values that define gets are:

  • the configuration key;
  • the default value for that key if it's not found in the configuration file;
  • the description for this key. This is very useful for generating configuration file examples.
  • the section that this key belongs to. Again very useful for generating configuration file examples.

Using Environment Variables

If you wish to allow environment variables to be used as the value of configuration keys, just call the allow_environment_variables method in your config.py file:

from derpconf.config import Config

Config.allow_environment_variables()

If there's an environment variable with the same name as the given configuration, derpconf will give precedence to it, instead of using the default value or the value in the configuration file.

# called program with SOMETHING=value myprogram.py
assert config.SOMETHING == "value"

# even if the default for 'SOMETHING' or the value in the config file is different from 'value'

Reloading Configurations

After you've loaded configurations from a file, sometimes it's needed to have them reloaded. This is the case when a new module needs to define some new default values.

In order to reload values from a config object, just call reload on it:

from derpconf.config import Config

conf = Config.load('/path/to/my/cfg.conf')

# then later on...

Config.define('SOMENEWFOO', 'bar', 'baz', 'foo')

conf.reload()
assert conf.SOMENEWFOO == 'bar'

Generating Configuration Examples

To generate a configuration example, you just need to call the get_config_text method. Let's see an example:

from derpconf.config import Config

Config.define('foo', 'fooval', 'Foo is always a foo', 'FooValues')
Config.define('bar', 'barval', 'Bar is not always a bar', 'BarValues')
Config.define('baz', 'bazval', 'Baz is never a bar', 'BarValues')

config_sample = Config.get_config_text()
print config_sample # or instead of both, just call generate_config()

The following text will be print into the standard output:

################################## FooValues ###################################

## Foo is always a foo
## Defaults to: fooval
#foo = 'fooval'

################################################################################


################################## BarValues ###################################

## Bar is not always a bar
## Defaults to: barval
#bar = 'barval'

## Baz is never a bar
## Defaults to: bazval
#baz = 'bazval'

################################################################################

A good sample of using derpconf can be seen at thumbor's configuration file.

Verifying a Configuration File

derpconf includes a configuration file verifier. The purpose of this verifier is to help you quickly understand what configuration files are missing what keys and what values will be used for them instead.

Running it is as simple as including a call to verify_config in your config.py file:

verify_config(file_path)

Or you can leave it blank and derpconf will get the file path from sys.argv:

verify_config()

The output of the verifier is something like this:

Configuration "baz" not found in file /Users/bernardo/dev/derpconf/vows/fixtures/missing.conf. Using "bazval" instead.
Configuration "foo" not found in file /Users/bernardo/dev/derpconf/vows/fixtures/missing.conf. Using "fooval" instead.
Configuration "bar" not found in file /Users/bernardo/dev/derpconf/vows/fixtures/missing.conf. Using "barval" instead.

You can see it in use at derpconf's code.

License

derpconf is licensed under the MIT License:

The MIT License

Copyright (c) 2012 globo.com [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

derpconf's People

Contributors

cezarsa avatar dhardy92 avatar gcirne avatar guilhermef avatar heynemann avatar marcelometal avatar morpheu avatar rfloriano avatar wichert avatar wking avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

derpconf's Issues

Config class vs Config instances

Is it really necessary to configure the Config class rather than creating Config instances?

I can think of scenarios where different libraries use derpconf to setup themselves and one config interferes with the other.

I think that global Config instances are fine, but configuring the Config class itself sounds dangerous. If you have discussed this before, please write me comment with your thoughts on this.

Tests fail once in a while even with no changes

I ran make test and it failed, and then I ran it more a couple of times and it succeeded. Shell output follows:

$ make test
WARNING:root:Option STORAGE_ALIAS_ALIAS is marked as deprecated please use STORAGE instead.
WARNING:root:Option OTHER_ENGINE is marked as deprecated please use ENGINE instead.
WARNING:root:Option STORAGE_ALIAS_ALIAS is marked as deprecated please use STORAGE instead.
WARNING:root:Option STORAGE_ALIAS is marked as deprecated please use STORAGE instead.
WARNING:root:Option OTHER_ENGINE is marked as deprecated please use ENGINE instead.
WARNING:root:Option LOADER_ALIAS is marked as deprecated please use LOADER instead.

 ============
 Vows Results
 ============


    Configuration
      When verifying
      ✗ should be lengthy
          Expected topic([('UBERFOO', 'baz'), ('some_key', 'default')]) to have 1 of length, but it has 2

           found in /Users/hugo/projects/derpconf/vows/config_vows.py at line 125

  ✗ OK » 21 honored • 1 broken (0.006755s)

make: *** [test] Error 1

$ make test
WARNING:root:Option STORAGE_ALIAS_ALIAS is marked as deprecated please use STORAGE instead.
WARNING:root:Option OTHER_ENGINE is marked as deprecated please use ENGINE instead.
WARNING:root:Option STORAGE_ALIAS_ALIAS is marked as deprecated please use STORAGE instead.
WARNING:root:Option STORAGE_ALIAS is marked as deprecated please use STORAGE instead.
WARNING:root:Option OTHER_ENGINE is marked as deprecated please use ENGINE instead.
WARNING:root:Option LOADER_ALIAS is marked as deprecated please use LOADER instead.

 ============
 Vows Results
 ============


    Configuration
      When verifying
      ✗ should be lengthy
          Expected topic([('UBERFOO', 'baz'), ('some_key', 'default')]) to have 1 of length, but it has 2

           found in /Users/hugo/projects/derpconf/vows/config_vows.py at line 125

  ✗ OK » 21 honored • 1 broken (0.006917s)

make: *** [test] Error 1

$ make test
WARNING:root:Option STORAGE_ALIAS_ALIAS is marked as deprecated please use STORAGE instead.
WARNING:root:Option OTHER_ENGINE is marked as deprecated please use ENGINE instead.
WARNING:root:Option LOADER_ALIAS is marked as deprecated please use LOADER instead.
WARNING:root:Option STORAGE_ALIAS_ALIAS is marked as deprecated please use STORAGE instead.
WARNING:root:Option STORAGE_ALIAS is marked as deprecated please use STORAGE instead.
WARNING:root:Option OTHER_ENGINE is marked as deprecated please use ENGINE instead.

 ============
 Vows Results
 ============

  ✓ OK » 22 honored • 0 broken (0.006623s)


 =============
 Code Coverage
 =============

 ✗ derpconf/version   0.0%                                                     11
 ✗ derpconf/config    ••••••••••••••••••••••••••••••• 62.6%                    97, 100, 112, and 74 more

 ✗ OVERALL            ••••••••••••••••••••••••••••••• 62.3%

I didn't touch any code, only ran make test multiple times. I am running it on Mac OS X 10.9.1 and Python 2.7.6.

python 2 support

Is the python 2 support maintained intentionally? I guess some analogue of ecbf82e could be considered.

Message when configuration not found has thumbor.conf as conf name for everyone

Legacy of having extracted this from thumbor.

Example:

File "/Users/heynemann/.virtualenvs/splitsecond/site-packages/derpconf/config.py", line 154, in validates_presence_of
    raise ConfigurationError('Configuration %s was not found and does not have a default value. Please verify your thumbor.conf file' % arg)
ConfigurationError: Configuration LOADER was not found and does not have a default value. Please verify your thumbor.conf file

Don't use exec()

I found this out by reading the source code - apparently a derpconf .conf file is actually a python file, and derpconf reads and executes it.

That's awfully insecure, given that local users might have access to write to the file.

In addition the conf_name and lookup_paths options allow loading the config from a list of paths rather than a well-defined absolute path, meaning that all an attacker has to do is be able to write a file into one of the directories in the list to get arbitrary code execution.

cyclops uses this to load configs from . and ~ if a relative path is specified, which seems like a bad idea.

i'm not 100% sure on how to best solve this, but my 5minute 2-cents-worth: seems like most configs should be securely parseable using a very simple parser (read lines, split on =, then parse expressions using ast.literal_eval() ). This would allow you to support a sane range of python expressions, without allowing arbitrary execution. It makes multi-line expressions a bit tricky though.

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.