Skip to content

Instantly share code, notes, and snippets.

@harlo
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harlo/d4c45902ef5551fe112c to your computer and use it in GitHub Desktop.
Save harlo/d4c45902ef5551fe112c to your computer and use it in GitHub Desktop.
conf tools i use a lot
import os, json, re
from collections import namedtuple
from fabric.operations import prompt
BASE_DIR = os.path.abspath(os.path.join(os.path.join(__file__, os.pardir), os.pardir))
DUtilsKey = namedtuple("DUtilsKey", ["label", "description", "default", "default_str", "value_transform"])
DUtilsTransforms = {
'PORT_TO_INT' : lambda p : int(p.strip()),
'NONE_IF_EMPTY' : lambda s : None if len(s) == 0 else s
}
def is_acceptable_str(str):
try:
return re.match(r'.*[\s]+', str) is None
except Exception as e:
print e, type(e)
return False
def get_directive(args, flags):
if type(flags) in [str, unicode]:
flags = [flags]
if type(flags) is not list:
return None
directives = []
if len(args) >= 2:
for a in args[1:]:
if re.match(r'^\-\-', a) is None:
continue
directive = a.split("=")
if directive[0][2:] in flags:
directives.append(directive[1])
if len(directives) == 0:
return None
elif len(directives) == 1:
directives = directives[0]
return directives
def build_config(config_keys, with_config=None):
config = {}
if with_config is not None:
if not os.path.exists(with_config):
print "cannot find file at %s" % with_config
print "Not a valid config file"
else:
try:
with open(with_config, 'rb') as c:
config = json.loads(c.read())
except Exception as e:
print e, type(e)
print "Not a valid config file"
config = parse_config_keys(config_keys, config)
return config
def parse_config_keys(config_keys, config=None):
if config is None:
config = {}
for c in config_keys:
if c.label not in config.keys():
print c.description
value = prompt("[ENTER for default ( %s )]: " % c.default_str)
if len(value) > 0:
if c.value_transform is None:
if not is_acceptable_str(value.strip()):
value = c.default
else:
value = c.value_transform(value)
config[c.label] = value
else:
print "USING DEFAULT: %s" % c.default
config[c.label] = c.default
return config
def save_config(config, with_config=None, return_config=False):
if with_config is None:
with_config = os.path.join(BASE_DIR, "config.json")
try:
with open(with_config, 'wb+') as c:
c.write(json.dumps(config))
if return_config:
return True, config
return True
except Exception as e:
print "COULD NOT SAVE CONFIG:"
print e, type(e)
return False
def append_to_config(append_to_config, with_config=None, return_config=False):
if with_config is None:
with_config = os.path.join(BASE_DIR, "config.json")
try:
with open(with_config, 'rb') as c:
config = json.loads(c.read())
config.update(append_to_config)
with open(with_config, 'wb+') as c:
c.write(json.dumps(config))
if return_config:
return True, config
return True
except Exception as e:
print e, type(e)
return False
def load_config(with_config=None):
if with_config is None:
with_config = os.path.join(BASE_DIR, "config.json")
try:
with open(with_config, 'rb') as c:
return json.loads(c.read())
except Exception as e:
print "__load_config Error:"
print e, type(e)
return None
def get_config(keys, with_config=None):
if with_config is None:
with_config = os.path.join(BASE_DIR, "config.json")
if type(with_config) not in [dict, str, unicode]:
return None
if type(with_config) in [str, unicode]:
with_config = __load_config(with_config)
if with_config is None:
return False
try:
if type(keys) in [str, unicode]:
return with_config[keys]
elif type(keys) is list:
return [with_config[key] for key in keys]
except Exception as e:
try:
print e, type(e)
except Exception as e_:
print e, type(e)
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment