Exemplo n.º 1
0
def _ascii_node(n, level, last, maxlevel, prefix):
    if maxlevel:
        if (isinstance(maxlevel, EnumSymbol) and n.level > maxlevel) or \
                (not isinstance(maxlevel, EnumSymbol) and level > maxlevel):
            return
    s = '\u2514' if last else '\u251c'
    s += '\u2500 '

    if not level:
        for i, node in enumerate(n.ancestors):
            util.sprint('{0}{1}{2} [{3}]', prefix, s if i else '', node.name,
                        node.id)
            prefix = '   ' + prefix

    nprefix = prefix + ('   ' if last else '\u2502  ')

    color = 'red' if not level else (
        'green' if n.level == objects.Level.language else
        ('blue' if n.level == objects.Level.dialect else None))

    util.sprint('{0}{1}{2} [{3}]', prefix, s if level else
                (s if n.ancestors else ''),
                colored(n.name, color) if color else n.name,
                colored(n.id, color) if color else n.id)
    for i, c in enumerate(sorted(n.children, key=lambda nn: nn.name)):
        _ascii_node(c, level + 1, i == len(n.children) - 1, maxlevel, nprefix)
Exemplo n.º 2
0
def refsearch(args):
    """Search Glottolog references

    glottolog refsearch "QUERY"

    E.g.:
    - glottolog refsearch "Izi provider:hh"
    - glottolog refsearch "author:Haspelmath provider:wals"
    """
    count, results = fts.search(args.repos, args.args[0])
    table = Table('ID', 'Author', 'Year', 'Title')
    for res in results:
        table.append([res.id, res.author, res.year, res.title])
    sprint(table.render(tablefmt='simple'))
    print('({} matches)'.format(count))
Exemplo n.º 3
0
def run(args):
    root = get_languoid(args, args.root)

    if args.maxlevel is not None:
        try:
            args.maxlevel = int(args.maxlevel)
        except Exception:
            args.maxlevel = args.repos.languoid_levels[args.maxlevel] \
                if args.maxlevel in args.repos.languoid_levels else None
    if args.newick:
        sprint(
            args.repos.newick_tree(root,
                                   template=args.template,
                                   maxlevel=args.maxlevel))
    else:
        if not root:
            raise ParserError(
                'Root languoid must be specified for non-newick serialization!'
            )
        args.repos.ascii_tree(root, maxlevel=args.maxlevel)
Exemplo n.º 4
0
def langsearch(args):
    """Search Glottolog languoids

    glottolog langsearch "QUERY"
    """
    def highlight(text):
        res, i = '', 0
        for m in re.finditer('\[\[(?P<m>[^\]]+)\]\]', text):
            res += text[i:m.start()]
            res += colored(m.group('m'), 'red', attrs=['bold'])
            i = m.end()
        res += text[i:]
        return res + '\n'

    count, results = fts.search_langs(args.repos, args.args[0])
    cwd = os.getcwd()
    print('{} matches'.format(count))
    for res in results:
        try:
            p = Path(res.fname).relative_to(Path(cwd))
        except ValueError:
            p = res.fname
        sprint('{0.name} [{0.id}] {0.level}'.format(res),
               color=None,
               attrs=['bold'])
        sprint(p, color='green')
        sprint(highlight(res.highlights) if res.highlights else '')
    print('{} matches'.format(count))
Exemplo n.º 5
0
def run(args):
    def highlight(text):
        res, i = '', 0
        for m in re.finditer(r'\[\[(?P<m>[^]]+)]]', text):
            res += text[i:m.start()]
            res += colored(m.group('m'), 'red', attrs=['bold'])
            i = m.end()
        res += text[i:]
        return res + '\n'

    count, results = fts.search_langs(args.repos, args.query)
    print('{} matches'.format(count))
    for res in results:
        try:
            p = pathlib.Path(res.fname).relative_to(pathlib.Path.cwd())
        except ValueError:
            p = res.fname
        sprint('{0.name} [{0.id}] {0.level}'.format(res),
               color=None,
               attrs=['bold'])
        sprint(p, color='green')
        sprint(highlight(res.highlights) if res.highlights else '')
    print('{} matches'.format(count))
Exemplo n.º 6
0
def newick(args):
    """Print the classification tree starting at a specific languoid in Newick format.

    glottolog newick <GLOTTOCODE>|<ISO-CODE> [MAXLEVEL]
    """
    sprint(args.repos.newick_tree(args.args[0] if args.args else None))
Exemplo n.º 7
0
def show(args):
    """Display details of a Glottolog object.

    glottolog show <GLOTTOCODE>|<ISO-CODE>|<BIBTEXKEY>
    """
    if args.args and ':' in args.args[0]:
        if args.args[0].startswith('**'):
            ref = Reference.from_string(args.args[0])
        else:
            ref = Reference(key=args.args[0])
        sprint('Glottolog reference {0}'.format(ref),
               attrs=['bold', 'underline'])
        print()
        src = ref.get_source(args.repos)
        sprint(src.text())
        print()
        sprint(src)
        return
    lang = existing_lang(args)
    print()
    sprint('Glottolog languoid {0}'.format(lang.id),
           attrs=['bold', 'underline'])
    print()
    sprint('Classification:', attrs=['bold', 'underline'])
    args.repos.ascii_tree(lang, maxlevel=1)
    print()
    sprint('Info:', attrs=['bold', 'underline'])
    sprint('Path: {0}'.format(lang.fname), 'green', attrs=['bold'])
    sources = lang.sources
    if sources:
        del lang.cfg['sources']['glottolog']
        del lang.cfg['sources']
    for line in lang.cfg.write_string().split('\n'):
        if not line.startswith('#'):
            sprint(line, None, attrs=['bold'] if line.startswith('[') else [])
    sprint('Sources:', attrs=['bold', 'underline'])
    for src in sources:
        src = src.get_source(args.repos)
        sprint(src.id, color='green')
        sprint(src.text())
        print()
Exemplo n.º 8
0
def run(args):
    if ':' in args.object:
        if args.object.startswith('**'):
            ref = Reference.from_string(args.object)
        else:
            ref = Reference(key=args.object)
        sprint('Glottolog reference {0}'.format(ref),
               attrs=['bold', 'underline'])
        print()
        src = ref.get_source(args.repos)
        sprint(src.text())
        print()
        sprint(src)
        return

    lang = get_languoid(args, args.object)
    print()
    sprint('Glottolog languoid {0}'.format(lang.id),
           attrs=['bold', 'underline'])
    print()
    sprint('Classification:', attrs=['bold', 'underline'])
    args.repos.ascii_tree(lang, maxlevel=1)
    print()
    sprint('Info:', attrs=['bold', 'underline'])
    sprint('Path: {0}'.format(lang.fname), 'green', attrs=['bold'])
    sources = lang.sources
    if sources:
        del lang.cfg['sources']['glottolog']
        del lang.cfg['sources']
    for line in lang.cfg.write_string().split('\n'):
        if not line.startswith('#'):
            sprint(line, None, attrs=['bold'] if line.startswith('[') else [])
    sprint('Sources:', attrs=['bold', 'underline'])
    for src in sources:
        src = src.get_source(args.repos)
        sprint(src.id, color='green')
        sprint(src.text())
        print()