Commit 2a9fe230 authored by Gerion Entrup's avatar Gerion Entrup
Browse files

settings: docstring and automatic cmdline argument

the settings are now accesible with settings.<name> like before but in
addition to that usable as commandline argument together with a
docstring
parent 419f4b96
......@@ -8,6 +8,7 @@ import logging
# remove it once python supports fully concurrency in sqlite
import sqlitequeue
import model
import settings
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
......@@ -38,12 +39,18 @@ def _init_database(dbfile):
session = sqlitequeue.get_Session(session)
return session
def get_options():
function = type(lambda x:x)
for x in sorted(vars(settings).keys()):
if (x[0] != '_' and type(getattr(settings, x)) != function):
yield x
def main(args):
"""Main method. Parses cmdline arguments and starts the program."""
version = "0.1-alpha"
parser = argparse.ArgumentParser(add_help=True,
description="Create a directory structure of music with" \
"symlinks based on musicbrainz data")
"symlinks based on musicbrainz data. Setting commandline options is only temporary. Change the config file to set option permanently.")
parser.add_argument('--version', '-V', action='version',
version='%(prog)s {0}'.format(version))
parser.add_argument('sourcedir', help="filepath of source file directory")
......@@ -53,12 +60,36 @@ def main(args):
parser.add_argument('--database', '-d', default="music.db", help="path of the database")
parser.add_argument('--logfile', '-l', help="log into a logfile")
# options from settings
for opt in get_options():
t = type(getattr(settings, opt))
h = settings.get_doc(opt)
if t == str:
parser.add_argument('--' + opt, help=h)
elif t == bool:
if getattr(settings, opt):
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)
arg = parser.parse_args(args[1:])
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-5s %(name)-18s %(message)s',
datefmt='%y-%m-%d %H:%M')
logger = logging.getLogger('main')
print(arg.force_clean)
print(arg.scan_interval)
# set settings options
for opt in get_options():
v = getattr(arg, opt)
if v is not None:
setattr(settings, opt, v)
logger.info("initializing database")
session = _init_database(arg.database)
logger.info("database initialized")
......
default_mode_dir = 0o555
default_mode_file = 0o444
import settings
fetcher_cache_length = 10000
fetcher_cache_age = 172800 # 48 hours
_docs = {}
directory_prefix = '/'
force_clean = False
def mk_opt(name, value, doc, after_default=''):
global _docs
setattr(settings, name, value)
# TODO split after 79 chars
doc += '\n\nThe default value is {}'.format(value)
if after_default:
doc += ' ' + after_default
_docs[name] = doc + '.'
scan_interval = 86400 # 24 hours
clean_interval = 86400 # 24 hours
update_interval = 2592000 # 1 month
def get_doc(attr):
global _docs
return _docs[attr]
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.')
mk_opt('fetcher_cache_length', 10000,
'Amount of entries in the fetcher cache. Decrement to save RAM. Increment to reduce network data.')
mk_opt('fetcher_cache_age', 172800,
'Define the maximum age of a fetcher cache entry (time in seconds).',
after_default='(48 hours)')
mk_opt('directory_prefix', '/mnt/tmpmount/',
'Paths of music files will be prefixed with this path and stored relativly to it.')
mk_opt('force_clean', False,
"Automated cleaning won't remove more than half of the files at once. Set this to true to change this.")
mk_opt('scan_interval', 86400,
'Interval for a new scan (time in seconds).',
after_default='(24 hours)')
mk_opt('clean_interval', 86400,
'Interval for clean all files (time in seconds).',
after_default='(24 hours)')
mk_opt('update_interval', 2592000,
'Not used yet.',
after_default='(1 month)')
mk_opt('check_update_interval', 3600,
'Not used yet.',
after_default='(1 hour)')
mk_opt('minimum_cleaning_age', 300,
'Defines the time, after that a music file is threated as old (time in seconds). This should not be set to a value smaller than 300.',
after_default='(5 minutes)')
mk_opt('slash_replacement', '-',
'Defines the letter, which replaces the slash in filenames.')
# this function must not be called again
del mk_opt
del settings
check_update_interval = 3600 # 1 hour
minimum_cleaning_age = 300 # 5 minutes
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