Commit e6efee0d authored by Martin Schrön's avatar Martin Schrön Committed by Martin Schrön
Browse files

Improved config functions (wip)

parent 72d50523
......@@ -3,12 +3,69 @@ CoRNy Config
Deal with Configuration files
"""
from corny import *
#from corny import *
import os
import configparser
from corny.basics import Progress, safe_cast, xsplit, str2dt
from corny.io import format_path, make_dir
class Config():
def __init__(self, file=None, chdir=True,
format_path = [('input','path'),('output','out_path')],
make_dir = [('output','out_path')]):
if file is None: file = 'config.cfg'
self.file = file
self._chdir = chdir
self._format_path = format_path
self._make_dir = make_dir
self.config = None
def read(self):
self.config = read_config(self.file)
if self.config:
if self._chdir:
config_dir, config_file_name = os.path.split(self.file)
os.chdir(os.path.normpath(config_dir))
if self._make_dir:
for sec, key in self._make_dir:
make_dir(self.get(sec, key))
if self._format_path:
for sec, key in self._format_path:
self.config[sec][key] = format_path(self.get(sec, key))
return(self)
def check(self, sec, key, value=None):
return(cc(self.config, sec, key, value))
def get(self, sec, key, alt=None, dtype=None, range=None, types='str'):
return(gc(self.config, sec, key, alt, dtype, range, types))
def str( self, sec, key, alt=None):
return(self.get(sec, key, alt, dtype=str))
def int( self, sec, key, alt=None):
return(self.get(sec, key, alt, dtype=int))
def float( self, sec, key, alt=None):
return(self.get(sec, key, alt, dtype=float))
def bool( self, sec, key, alt=None):
return(self.get(sec, key, alt, dtype=bool))
def list( self, sec, key, alt=None, range=None, types='str'):
return(self.get(sec, key, alt, dtype=list, range=range, types=types))
def date( self, sec, key, alt=None):
return(self.get(sec, key, alt, dtype='date'))
def multi( self, sec, key, alt=None):
return(self.get(sec, key, alt, dtype='multi'))
def read_config(file, chdir=False):
# Config
config = configparser.ConfigParser()
if file is None: file = 'config.cfg'
""" Deprecated
if not os.path.isfile(file):
print(cprintf('! Config file required! Either specify it as an argument or provide ./config.cfg', 'red'))
return()
......@@ -16,7 +73,15 @@ def read_config(file, chdir=False):
print("i Working with %s" % file)
temp = config.read(file)
"""
with Progress('> Loading config: %s' % file) as Pr:
if not os.path.isfile(file):
Pr.fail()
print('! Config file required! Either specify it as an argument or provide config.cfg.')
return()
else:
temp = config.read(file)
if chdir:
# Change to this directory
config_dir, config_file_name = os.path.split(file)
......@@ -27,9 +92,16 @@ def read_config(file, chdir=False):
# add trailing slash
config['input']['path'] = os.path.join(config['input']['path'], '')
# check whether output path exists
""" Deprecated
if config['output']['out_path']:
if not os.path.exists(config['output']['out_path']):
os.makedirs(config['output']['out_path'])
"""
output_path = gc(config,'output','out_path',alt='output')
if not os.path.exists(output_path):
with Progress('< Creating folder %s' % output_path):
os.makedirs(output_path)
# add trailing slash
config['output']['out_path'] = os.path.join(config['output']['out_path'], '')
......@@ -49,7 +121,7 @@ def check_config(config, section, key, value=None):
cc = check_config
def get_config(config, section, key, alt=None, dtype=None):
def get_config(config, section, key, alt=None, dtype=None, range=None, types=str):
# return None if item does not exist or is empty
r = alt
if section in config:
......@@ -61,17 +133,24 @@ def get_config(config, section, key, alt=None, dtype=None):
if not dtype is None:
if dtype=='multi':
r = [item for item in r.split('\n')]
elif dtype=='date':
r = str2dt(r)
elif dtype==list:
r = xsplit(r, range=range, type=types)
if not r:
r = []
elif dtype==bool:
r = yesno2bool(r)
else:
r = safe_cast(r, dtype)
return(r)
gc = get_config
def yesno2bool(s):
if isinstance(s, str):
s = s.lower()
if s == 'yes' or s == 'y':
s = s.lower().strip()
if s == 'yes' or s == 'y' or s=='true' or s=='1':
return(True)
else:
return(False)
......
import os
#from .basics import cprintf
from .basics import Progress
def file_save_msg(file, name='File', end=''):
size = os.stat(file).st_size
......@@ -10,3 +10,15 @@ def file_save_msg(file, name='File', end=''):
#rc = cprintf(r, 'green')
print(r, end=end)
#return(rc)
def make_dir(dir):
if not os.path.exists(dir):
with Progress('Creating folder %s' % dir):
os.makedirs(dir)
def format_path(dir):
if os.path.isdir(dir):
# add trailing slash
return(os.path.join(dir, ''))
else:
return(dir)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment