Commits

Brent Tubbs committed 1a2235f

init

Comments (0)

Files changed (4)

+syntax: glob
+settings.yaml
+*pyc
+*swp
+env/*
+._*
+*egg-info*
+tmp/*
+*pem
+*der
+In Velociraptor, application config is passed into apps by setting an
+APP_SETTINGS_YAML environment variable that contains the path to the app's
+yaml-formatted settings file.
+
+This module provides helpers for reading and accessing that configuration
+information.
+
+Loading config::
+
+    import appsettings
+
+    my_config = appsettings.read()
+
+Using config::
+
+    In [1]: import appsettings
+
+    In [2]: config = appsettings.read()
+
+    In [3]: config.DEBUG
+    Out[3]: True
+
+    In [4]: config.DATABASES.default.HOST
+    Out[4]: 'vdeploydb.paix.yougov.local'
+
+import os
+
+import yaml
+
+
+class SettingsStruct(object):
+    """
+    A container for dot-notated access to a dictionary of config data like
+    you'd get from parsing a safe yaml file.
+    """
+    def __init__(self, **entries):
+        for k, v in entries.items():
+            self.__dict__[k] = self.structify(v)
+
+    @classmethod
+    def structify(cls, item):
+        """
+        Convert possibly-nested YAML-safe type into dot-notated struct.
+
+        Dictionaries are made into objects, lists are checked for dictionaries,
+        and all other types are passed through.
+        """
+        if isinstance(item, dict):
+            return cls(**item)
+
+        if isinstance(item, list):
+            return [cls.structify(i) for i in item]
+
+        return item
+
+
+def read(env_var='APP_SETTINGS_YAML'):
+    """
+    Given the name of an environment variable that should contain a path to a
+    yaml file with a dictionary full of settings, return a SettingsStruct that
+    provides dot-style access to those settings.
+    """
+    return SettingsStruct(**yaml.safe_load(open(os.environ[env_var])))
+
+
+def unstrict_read(env_var='APP_SETTINGS_YAML'):
+    """
+    Just like appsettings.read(), but don't choke on a missing env var or file.
+    Return an empty struct in those cases.
+    """
+    try:
+        return read(env_var)
+    except (KeyError, IOError):
+        # IF the env var isn't set, or the file doesn't exist, just return an
+        # empty dict.
+        return SettingsStruct(**{})
+from setuptools import setup
+
+setup(
+    name='appsettings',
+    author='Brent Tubbs',
+    author_email='brent.tubbs@gmail.com',
+    version='0.1',
+    url='http://bits.btubbs.com/appsettings/',
+    py_modules=['appsettings'],
+    description=('A tiny helper to read the yaml file specified in the '
+                 'APP_SETTINGS_YAML env var and return an object with '
+                 'the parsed contents'),
+    long_description=open('README.rst').read(),
+    install_requires=[
+        'pyyaml==3.10',
+    ],
+    zip_safe=False,
+)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.