Skip to content

Commit

Permalink
#58: Back to reading file in a simple way.
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermechapiewski committed Oct 6, 2009
1 parent 040e397 commit e86b1bc
Showing 1 changed file with 8 additions and 48 deletions.
56 changes: 8 additions & 48 deletions src/config.py
Expand Up @@ -36,59 +36,19 @@ def _parse_migrations_dir(self, dirs, config_dir=''):
class FileConfig(Config):

class SettingsFile(object):
@staticmethod
def get_file_imports(filename):
imports = []
try:
f = open(filename)
for line in f:
line = line.rstrip()
if line.startswith('import') or line.startswith('from'):
imports.append(line)
finally:
f.close()
return imports

@staticmethod
def exec_import(import_line, global_variables, local_variables):
if import_line.startswith('import'):
modules = import_line.lstrip('import').split(',')
for module in modules:
__import__(module.strip(), global_variables, local_variables, [], -1)
else:
match = re.search(r'from (.*) import (.*)', import_line)
modules = [m.strip() for m in match.groups()[1].split(',')]
__import__(match.groups()[0], global_variables, local_variables, modules, -1)

@staticmethod
def import_file(full_filename):
path, filename = os.path.split(full_filename)

# add settings dir from path
sys.path.insert(0, path)

# read imports from config file
# try:
# modules = FileConfig.SettingsFile.get_file_imports(full_filename)
# for module in modules:
# FileConfig.SettingsFile.exec_import(module, globals(), locals())
# except Exception, e:
# raise Exception("error executing imports from config file '%s': %s" % (filename, str(e)))

# read config file
local_variables = locals()
local_variables['__file__'] = full_filename
try:
execfile(full_filename, globals(), local_variables)
execfile(full_filename)
except IOError:
raise Exception("%s: file not found" % filename)
raise Exception("%s: file not found" % full_filename)
except Exception, e:
raise Exception("error interpreting config file '%s': %s" % (filename, str(e)))

# remove settings dir from path
sys.path.remove(path)

return local_variables
return locals()

def __init__(self, config_file="simple-db-migrate.conf"):
self._config = {}
Expand All @@ -97,18 +57,18 @@ def __init__(self, config_file="simple-db-migrate.conf"):
# read configuration
settings = FileConfig.SettingsFile.import_file(config_file)

config_path = os.path.split(config_file)[0]
self.put("db_host", self.get_variable(settings, 'DATABASE_HOST', 'HOST'))
self.put("db_user", self.get_variable(settings, 'DATABASE_USER', 'USERNAME'))
self.put("db_password", self.get_variable(settings, 'DATABASE_PASSWORD', 'PASSWORD'))
self.put("db_name", self.get_variable(settings, 'DATABASE_NAME', 'DATABASE'))

migrations_dir = self.get_variable(settings, 'DATABASE_MIGRATIONS_DIR', 'MIGRATIONS_DIR')
self.put("migrations_dir", self._parse_migrations_dir(migrations_dir, config_path))
config_dir = os.path.split(config_file)[0]
self.put("migrations_dir", self._parse_migrations_dir(migrations_dir, config_dir))

def get_variable(self, local_variables_dict, name, old_name):
if name in local_variables_dict or old_name in local_variables_dict:
return local_variables_dict.get(name, local_variables_dict.get(old_name))
def get_variable(self, settings, name, old_name):
if name in settings or old_name in settings:
return settings.get(name, settings.get(old_name))
else:
raise NameError("config file error: name '%s' is not defined" % name)

Expand Down

0 comments on commit e86b1bc

Please sign in to comment.