Beispiel #1
0
#!/usr/bin/env python


import sys
import json
import urllib.parse

import micro
import rfc3986

service = micro.Service(name='uritool')


def flatten(x):
    if len(x) == 1:
        x = x[0]
    else:
        x = x

    return x


def do_parse(uri):
    # Parse the incoming URI
    return rfc3986.urlparse(uri)


def do_query(uri):
    parsed = do_parse(uri)

    collection = {}
Beispiel #2
0
def service(micro):
    return micro.Service(name='service')
Beispiel #3
0
            return uri_or_doc
    else:
        return uri_or_doc


def convert(doc: str, format: str, *, output: str) -> str:
    """Converts a document, using Pandoc."""

    doc = fetch_or_not(doc)

    tf = NamedTemporaryFile(prefix='pandoc-', suffix=f'-{format}.txt').name
    with open(tf, 'w') as f:
        f.write(doc)

    valid_input = delegator.run('pandoc --list-input-formats').out.split()
    valid_output = delegator.run('pandoc --list-output-formats').out.split()

    assert format in valid_input
    assert output in valid_output

    p = delegator.run(f'pandoc --from={format} --to={output} {tf}')
    os.remove(tf)

    return p.out


if __name__ == '__main__':
    service = micro.Service(name='pandoc')
    service.add(f=convert)
    service.serve(ensure=True)