Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions djangoconfig/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__version__ = '0.1.5'
from djangoconfig.deployment import bootstrap, setup_environment
54 changes: 54 additions & 0 deletions djangoconfig/config.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 5 additions & 2 deletions djangoconfig/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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."""
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
@@ -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.


2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from setuptools import setup

version = '0.1.3'
version = '0.1.5'

setup(
name='django-config',
Expand Down