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