def configure(cfgpath=None): """ Configure lexibank. :return: a pair (config, logger) """ cfgpath = Path(cfgpath) \ if cfgpath else Path(user_config_dir(pylexibank.__name__)) / 'config.ini' if not cfgpath.exists(): print(""" {0} You seem to be running lexibank for the first time. Your system configuration will now be written to a config file to be used whenever lexibank is run lateron. """.format(colored('Welcome to lexibank!', 'blue', attrs=['bold', 'reverse']))) if not cfgpath.parent.exists(): cfgpath.parent.mkdir(parents=True) cfg = Config() cfg['paths'] = {k: get_path(src) for k, src in REPOS} cfg.write(cfgpath) print(""" Configuration has been written to: {0} You may edit this file to adapt to changes in your system or to reconfigure settings such as the logging level.""".format(cfgpath.resolve())) else: cfg = Config.from_file(cfgpath) try: cfg.glottolog except (FileNotFoundError, ValueError): raise ParserError( 'Misconfigured Glottolog path in {0}'.format(cfgpath)) if not Path(cfg['paths']['concepticon']).exists(): raise ParserError( 'Misconfigured Concepticon path in {0}'.format(cfgpath)) # Print the configuration directory for reference: print("Using configuration file at:") print(str(cfgpath) + '\n') return cfg
class API(UnicodeMixin): """An API base class to provide programmatic access to data in a git repository.""" # A light-weight way to specifiy a default repository location (without having to # overwrite __init__) __repos_path__ = None def __init__(self, repos=None): self.repos = Path(repos or self.__repos_path__) def __unicode__(self): name = self.repos.resolve().name if self.repos.exists( ) else self.repos.name return '<{0} repository {1} at {2}>'.format(name, git_describe(self.repos), self.repos) def path(self, *comps): return self.repos.joinpath(*comps) @property def appdir(self): return self.path('app') @property def appdatadir(self): return self.appdir.joinpath('data') @classmethod def app_wrapper(cls, func): @wraps(func) def wrapper(args): api = cls(args.repos) if not api.appdatadir.exists() or '--recreate' in args.args: api.appdatadir.mkdir(exist_ok=True) args.api = api func(args) index = api.appdir / 'index.html' if index.exists(): webbrowser.open(index.resolve().as_uri()) return wrapper