diff --git a/djangoconfig/__init__.py b/djangoconfig/__init__.py index 8b7b179..6ab42a7 100644 --- a/djangoconfig/__init__.py +++ b/djangoconfig/__init__.py @@ -1 +1,2 @@ +__version__ = '0.1.5' from djangoconfig.deployment import bootstrap, setup_environment \ No newline at end of file diff --git a/djangoconfig/config.py b/djangoconfig/config.py new file mode 100644 index 0000000..084ab21 --- /dev/null +++ b/djangoconfig/config.py @@ -0,0 +1,54 @@ +COMMAND_CACHE = 'command_cache' +AVAILABLE_COMMANDS = 'available_commands' + +CONFIG_IDENTIFIER = 'config_identifier' + +CACHE_DIR='config' +CACHE_NAME='django_config_cache.cfg' + +import ConfigParser +import os + + + +def get_config_identifier(path, default): + cache_dir = os.path.join(path, CACHE_DIR) + cache_file = os.path.abspath(os.path.join(cache_dir,CACHE_NAME)) + with open(cache_file,'rb') as configfile: + config = ConfigParser.ConfigParser() + config.readfp(configfile) + + if config.has_section(CONFIG_IDENTIFIER): + os.environ['CONFIG_IDENTIFIER'] = config.get(CONFIG_IDENTIFIER,CONFIG_IDENTIFIER) + return config.get(CONFIG_IDENTIFIER,CONFIG_IDENTIFIER) + + return default or '' + +def get_commands(path): + cache_dir = os.path.join(path, CACHE_DIR) + cache_file = os.path.abspath(os.path.join(cache_dir,CACHE_NAME)) + with open(cache_file,'rb') as configfile: + config = ConfigParser.ConfigParser() + config.readfp(configfile) + + if config.has_section(COMMAND_CACHE): + return config.get(COMMAND_CACHE,AVAILABLE_COMMANDS).split(',') + + return [] +def cache_commands(path): + from django.core.management import get_commands + cache_dir = os.path.join(path, CACHE_DIR) + cache_file = os.path.abspath(os.path.join(cache_dir,CACHE_NAME)) + # If dir dosen't exist, fail silently. + with open(cache_file,'r+wb') as configfile: + + config = ConfigParser.ConfigParser() + config.readfp(configfile) + configfile.seek(0) + + if not config.has_section(COMMAND_CACHE): + config.add_section(COMMAND_CACHE) + for command in get_commands().keys(): + config.set(COMMAND_CACHE,AVAILABLE_COMMANDS,','.join(get_commands().keys())) + + config.write(configfile) \ No newline at end of file diff --git a/djangoconfig/deployment.py b/djangoconfig/deployment.py index ec33cb9..9abe636 100644 --- a/djangoconfig/deployment.py +++ b/djangoconfig/deployment.py @@ -3,6 +3,7 @@ import sys import hashlib import getpass +from djangoconfig.config import get_commands, cache_commands, get_config_identifier def bootstrap_script(path, project_path_components): project_path = os.path.join(find_parent_path(path, project_path_components), *project_path_components) @@ -19,6 +20,7 @@ def bootstrap(path): sys.exit(1) management.setup_environ(settings) + cache_commands(find_settings_path(path)) def find_settings_path(path): """Legacy support.""" @@ -53,10 +55,11 @@ def get_config_identifiers(path): def setup_environment(path): IDENTIFIERS = get_config_identifiers(path) - CONFIG_IDENTIFIER = os.environ.get('CONFIG_IDENTIFIER', '') + available_commands = get_commands(find_settings_path(path)) + CONFIG_IDENTIFIER = os.environ.get('CONFIG_IDENTIFIER', get_config_identifier(find_settings_path(path),'')) # If we are trying to use runserver, then look for existing configuration so that auto reload does not keep propmpting for the configuration file. - if len(sys.argv) >= 2 and sys.argv[1] in ('runserver', 'runserver_plus',): + if len(sys.argv) >= 2 and sys.argv[1] in available_commands: if 'CONFIG_IDENTIFIER' in os.environ and CONFIG_IDENTIFIER in [name for id, name in IDENTIFIERS]: bootstrap(path) return diff --git a/docs/config.rst b/docs/config.rst new file mode 100644 index 0000000..a5db550 --- /dev/null +++ b/docs/config.rst @@ -0,0 +1,36 @@ +###### +ConFiG +###### +An architecture for maintaining multiple settings files in Django + +Purpose +======== +To cache the availble commands and allow for an easy file configuration. +Django config is ran before the django-env can be initalized, this file is here to define settings for django config. + + +Available Settings +================== +>>> command_cache +>>> config_identifier + +------------- +Command Cache +------------- +* Command Cache is a read only setting. + +>>> [command_cache] +>>> available_commands = runserver,shell,shell_plus + + +----------------- +Config Identifier +----------------- +>>> [config_identifier] +>>> config_identifier = monty + +Known Bugs +========== +1. File sturcture is moved around everytime an app is started. + + diff --git a/setup.py b/setup.py index 1afe703..252e8ca 100755 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup -version = '0.1.3' +version = '0.1.5' setup( name='django-config',