Commit d91675fa authored by Gerion Entrup's avatar Gerion Entrup
Browse files

settings: implement constraints and octal

parent fe7de14d
......@@ -39,6 +39,20 @@ def _init_database(dbfile):
session = sqlitequeue.get_Session(session)
return session
def auto_int(arg):
return int(arg, 0)
def check(opt, f):
def s(value):
if settings.check_constr(opt, f(value)):
return f(value)
else:
raise argparse.ArgumentTypeError("invalid value {}".format(value))
return s
def main(args):
"""Main method. Parses cmdline arguments and starts the program."""
version = "0.1-alpha"
......@@ -58,15 +72,15 @@ def main(args):
for opt, value in settings.get_options():
t = type(value)
h = settings.get_doc(opt)
if t == str:
if t is str:
parser.add_argument('--' + opt, help=h)
elif t == bool:
elif t is bool:
if value:
parser.add_argument('--' + opt, action="store_false", help=h)
else:
parser.add_argument('--' + opt, action="store_true", help=h)
elif t == int:
parser.add_argument('--' + opt, type=int, help=h)
elif issubclass(t, int):
parser.add_argument('--' + opt, type=check(opt, auto_int), help=h)
arg = parser.parse_args(args[1:])
......
import sys
import settings.writer
import settings.parser
self = sys.modules[__name__]
_docs = {}
_constr = {}
def mk_opt(name, value, doc, after_default=''):
class Octal(int):
def __new__(cls, value):
if type(value) is str and value.startswith('0o'):
return super(Octal, cls).__new__(cls, value, 8)
return super(Octal, cls).__new__(cls, value)
def __str__(self):
return oct(self)
def mk_opt(name, value, doc, after_default='', assign_constraint=None):
global _docs
setattr(self, name, value)
if assign_constraint:
_constr[name] = (assign_constraint, value)
# TODO split after 79 chars
doc += '\n\nThe default value is {}'.format(value)
if after_default:
doc += ' ' + after_default
_docs[name] = doc + '.'
def get_options():
global _docs
return [(x, getattr(self, x)) for x in sorted(_docs.keys())]
def get_doc(opt):
global _docs
return _docs[opt]
mk_opt('default_mode_dir', 0o555,
'Default permission for directories. This can only be stronger than the default.')
# TODO implement constraint
# TODO implement octal mode
mk_opt('default_mode_file', 0o444,
'Default permission for files. This can only be stronger than the default.')
def check_constr(opt, value):
if opt in _constr:
f, default = _constr[opt]
return f(value, default)
else:
return True
def check_weaker(value, default):
if value > default:
return False
for i in [0, 3, 6]:
# extract default bit
d = default >> i & 0o7
# compare with value
if not d == (d | (value >> i & 0o7)):
return False
return True
def write(path):
return settings.writer.write(path, self)
def parse(path):
return settings.parser.parse(path, self)
mk_opt('default_mode_dir', Octal(0o555),
'Default permission for directories. This can only be stronger than the default.',
assign_constraint=check_weaker)
mk_opt('default_mode_file', Octal(0o444),
'Default permission for files. This can only be stronger than the default.',
assign_constraint=check_weaker)
mk_opt('fetcher_cache_length', 10000,
'Amount of entries in the fetcher cache. Decrement to save RAM. Increment to reduce network data.')
......
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