<commit_before># -*- coding: utf-8 -*-

import sys

from .compat import StringIO
import seria

from docopt import docopt

docopt_args = """Seria - Serialization for humans

Usage:
  seria [-h]
  seria ([--json | -j ] | [--xml | -x] | [--yaml | -y]) (INPUT|"[-]")

Arguments:
  INPUT  Input stream for serialization.  Can be file stdin stream or uri
Options:
  -h --help             show this help message and exit
  -j --json             Output json
  -x --xml              Output xml
  -y --yaml             Output yaml
"""


def main():
    _serialized_obj = None
    args = docopt(docopt_args)
    if args['INPUT'] != '-':
        with open(args['INPUT'], 'rb') as f:
            _serialized_obj = seria.load(f)

    elif args['INPUT'] == '-':
        _serialized_obj = StringIO()
        for l in sys.stdin:
            try:
                _serialized_obj.write(str(l))
            except TypeError:
                _serialized_obj.write(bytes(l, 'utf-8'))

        _serialized_obj = seria.load(_serialized_obj)

    if args['--json']:
        sys.stdout.write(_serialized_obj.dump('json'))
    if args['--xml']:
        sys.stdout.write(_serialized_obj.dump('xml'))
    if args['--yaml']:
        sys.stdout.write(_serialized_obj.dump('yaml'))


if __name__ == "__main__":
    main()<commit_msg>Move to click for cli implmentation<commit_after># -*- coding: utf-8 -*-

import click
from .compat import StringIO
import seria


CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])


@click.command(context_settings=CONTEXT_SETTINGS)
@click.option('--xml', 'out_fmt', flag_value='xml')
@click.option('--yaml', 'out_fmt', flag_value='yaml')
@click.option('--json', 'out_fmt', flag_value='json')
@click.argument('input', type=click.File('rb'), default='-')
@click.argument('output', type=click.File('wb'), default='-')
def cli(out_fmt, input, output):
    """Converts text."""
    _input = StringIO()
    for l in input:
        try:
            _input.write(str(l))
        except TypeError:
            _input.write(bytes(l, 'utf-8'))
    _serialized_obj = seria.load(_input)
    print output
    output.write(_serialized_obj.dump(out_fmt))


if __name__ == '__main__':
    cli(out_fmt, input, output)

{'fraction_non_alphanumeric': 0.1604882459312839, 'fraction_numerical': 0.0018083182640144665, 'mean_word_length': 2.719327731092437, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1620011', 'n_tokens_mistral': 745, 'n_tokens_neox': 734, 'n_words': 172}