Example #1
0
 def _parse(self, params: List[str]) -> None:
     '''
 todo: docs
 '''
     if not params[0].isdigit(): raise TypeError()
     instance.config.set('api', 'port', params[0])
     logger().info('Set api port to %s', params[0])
Example #2
0
File: index.py Project: cceh/kosh
 def delete(cls, elex: Dict[str, Any]) -> None:
     '''
 todo: docs
 '''
     idxs = connections.get_connection().indices
     logger().debug('Dropping index %s', elex.uid)
     idxs.delete(ignore=404, index=elex.uid)
Example #3
0
File: index.py Project: cceh/kosh
 def create(cls, elex: Dict[str, Any]) -> None:
     '''
 todo: docs
 '''
     idxs = connections.get_connection().indices
     logger().debug('Creating index %s', elex.uid)
     idxs.create(index=elex.uid, body=cls.__schema(elex))
Example #4
0
  def deploy(self, wapp: Flask) -> None:
    '''
    todo: docs
    '''
    view = ql.as_view(self.path, schema = self.__schema(), graphiql = True)
    logger().debug('Deploying GraphQL endpoint %s', self.path)

    wapp.add_url_rule(self.path, view_func = view)
Example #5
0
File: index.py Project: cceh/kosh
 def update(cls, elex: Dict[str, Any]) -> None:
     '''
 todo: docs
 '''
     logger().info('Updating index %s', elex.uid)
     cls.delete(elex)
     cls.create(elex)
     cls.append(elex)
Example #6
0
  def _parse(self, params: List[str]) -> None:
    '''
    todo: docs
    '''
    try: instance.config.read_string(params[0])
    except: exit('Invalid config string {}'.format(params[0]))

    logger().info('Read config string %s', params[0])
Example #7
0
    def _parse(self, params: List[str]) -> None:
        '''
    todo: docs
    '''
        try:
            logger().setLevel(getLevelName(params[0]))
        except:
            exit('Invalid log level {}'.format(params[0]))

        instance.config.set('logs', 'elvl', params[0])
        logger().info('Set log level to %s', params[0])
Example #8
0
File: index.py Project: cceh/kosh
    def lookup(cls, root: str, spec: str) -> List[Dict[str, Any]]:
        '''
    todo: docs
    '''
        idxs = []

        logger().debug('Looking for dict definitions in %s', root)
        for file in glob('{}/**/{}'.format(root, spec), recursive=True):
            try:
                idxs += cls.__parser(file)
            except:
                logger().warn('Corrupt dict definition in %s', file)

        return idxs
Example #9
0
  def parse(self, file: str) -> List[Document]:
    '''
    todo: docs
    '''
    docs = []
    name = path.basename(file)
    xmap = self.elex.schema.mappings._meta._xpaths

    logger().debug('Parsing file %s/%s', self.elex.uid, name)
    tree = etree.parse(file, etree.XMLParser(remove_blank_text = True))
    for elem in tree.xpath(xmap.root, namespaces = ns()):
      docs += [self.__record(elem)]

    return docs
Example #10
0
    def append(cls, elex: Dict[str, Any]) -> None:
        '''
    todo: docs
    '''
        edef = entry(elex)
        size = 0

        for file in elex.files:
            bulk = [i.to_dict(include_meta=True) for i in edef.parse(file)]
            logger().debug('Adding %i entries to index %s', len(bulk),
                           elex.uid)
            helpers.bulk(connections.get_connection(), bulk)
            size += len(bulk)

        logger().info('Found %i entries for dictionary %s', size, elex.uid)
Example #11
0
File: index.py Project: cceh/kosh
    def notify(cls, root: str, spec: str) -> Callable:
        '''
    todo: docs
    '''
        from inotify.adapters import InotifyTree
        from inotify.constants import IN_CLOSE_WRITE, IN_CREATE

        task = InotifyTree(root, IN_CLOSE_WRITE | IN_CREATE)
        uniq = lambda i: (i[2], int(time() / 60))

        for tick, _ in groupby(task.event_gen(yield_nones=0), key=uniq):
            file = '{}/{}'.format(tick[0], spec)

            if not '.git' in file and path.isfile(file):
                logger().debug('Observed change in %s', tick[0])
                yield lambda i=file: cls.__parser(i)
Example #12
0
    def deploy(self, wapp: Flask) -> None:
        '''
    todo: docs
    '''
        path = lambda p: '{}/{}'.format(self.path, p)
        logger().debug('Deploying RESTful endpoint %s', self.path)

        wapp.add_url_rule(path('entries'), path('entries'), self.entries)
        wapp.add_url_rule(path('ids'), path('ids'), self.ids)
        wapp.add_url_rule(path('spec'), path('spec'), self.spec)

        wapp.register_blueprint(get_swaggerui_blueprint(
            self.path,
            path('spec'),
            blueprint_name=self.elex.uid,
            config={'layout': 'BaseLayout'}),
                                url_prefix=self.path)
Example #13
0
    def main(self) -> None:
        '''
    todo: docs
    '''
        try:
            instance.config = ConfigParser()
            instance.config.read_dict(defaultconfig())
            logger().info('Started kosh with pid %s', getpid())

            root = '{}/{}'.format(path.dirname(__file__), 'api')
            mods = [i for _, i, _ in iter_modules([root]) if not i[0] is ('_')]
            instance.echoes = [
                mod('kosh.api.{}'.format(i)).__dict__[i] for i in mods
            ]
            logger().info('Loaded API endpoint modules %s', mods)

            for i in [i for i in argv if i.startswith('--')]:
                try:
                    mod('kosh.param.{}'.format(i[2:])).__dict__[i[2:]](argv)
                except:
                    exit('Invalid parameter or argument to {}'.format(i[2:]))

            conf = dotdict(instance.config['data'])
            connections.create_connection(hosts=[conf.host])
            instance.elexes = {
                i.uid: i
                for i in index.lookup(conf.root, conf.spec)
            }
            for elex in instance.elexes.values():
                index.update(elex)

            self.serve()
            self.watch() if strtobool(conf.sync) else pause()

        except KeyboardInterrupt:
            print('\N{bomb}')
        except Exception as exception:
            logger().exception(exception)
        except SystemExit as exception:
            logger().critical(str(exception))

        finally:
            logger().info('Stopped kosh with pid %s', getpid())
Example #14
0
 def _parse(self, params: List[str]) -> None:
     '''
 todo: docs
 '''
     instance.config.set('data', 'sync', str(strtobool(params[0])))
     logger().info('Set data sync to %r', bool(strtobool(params[0])))
Example #15
0
 def _parse(self, params: List[str]) -> None:
     '''
 todo: docs
 '''
     instance.config.set('data', 'host', params[0])
     logger().info('Set data host to %s', params[0])
Example #16
0
 def run(self) -> None:
     logger().info('Deploying web server at %s:%s', conf.host,
                   conf.port)
     wapp.run(host=conf.host, port=conf.port)
Example #17
0
 def run(self) -> None:
     logger().info('Starting data sync in %s', conf.root)
     for call in index.notify(conf.root, conf.spec):
         tick.put(call)