예제 #1
0
    def test_write_line_doc(self):
        """Convert CSV files to text lines and back to text lines"""

        all = [
            'example1.csv', 'example2.csv', 'example1-web.csv', 'children.csv',
            'children2.csv', 'issue1.csv'
        ]

        self.maxDiff = None

        for f in all:

            path = test_data(f)

            doc1 = MetatabDoc(path)

            doc1_lines = doc1.as_lines()

            print(doc1_lines)

            doc2 = MetatabDoc(TextRowGenerator(doc1_lines))

            doc2_lines = doc2.as_lines()

            self.assertEqual(doc1_lines, doc2_lines)

            self.compare_dict(doc1.as_dict(), doc2.as_dict())

            self.assertEqual(doc1_lines, doc2_lines)

            self.assertEqual(doc1.as_csv(), doc2.as_csv())
예제 #2
0
파일: test_doc.py 프로젝트: Metatab/metatab
    def test_new(self):

        import metatab.templates as tmpl

        template_path = join(dirname(tmpl.__file__), 'metatab.csv')

        doc = MetatabDoc(template_path)
        doc.cleanse()

        print(doc.as_csv()[:200])
예제 #3
0
파일: cli.py 프로젝트: Metatab/metatab
def metatab():
    import argparse
    parser = argparse.ArgumentParser(prog='metatab',
                                     description='Matatab file parser',
                                     epilog='Cache dir: {}\n'.format(
                                         str(cache.getsyspath('/'))))

    g = parser.add_mutually_exclusive_group()

    g.add_argument(
        '-C',
        '--create',
        action='store',
        nargs='?',
        default=False,
        help=
        "Create a new metatab file, from named template. With no argument, uses the 'metatab' template "
    )

    g.add_argument(
        '-t',
        '--terms',
        default=False,
        action='store_const',
        dest='out_type',
        const='terms',
        help=
        'Parse a file and print out the stream of terms, before interpretation'
    )

    g.add_argument('-j',
                   '--json',
                   default=False,
                   action='store_const',
                   dest='out_type',
                   const='json',
                   help='Parse a file and print out a JSON representation')

    g.add_argument('-y',
                   '--yaml',
                   default=False,
                   action='store_const',
                   dest='out_type',
                   const='yaml',
                   help='Parse a file and print out a YAML representation')

    g.add_argument(
        '-l',
        '--line',
        default=False,
        action='store_const',
        dest='out_type',
        const='line',
        help='Parse a file and print out a Metatab Line representation')

    g.add_argument(
        '-c',
        '--csv',
        default=False,
        action='store_const',
        dest='out_type',
        const='csv',
        help='Parse a file and print out a Metatab Line representation')

    g.add_argument('-p',
                   '--prety',
                   default=False,
                   action='store_const',
                   dest='out_type',
                   const='prety',
                   help='Pretty print the python Dict representation ')

    parser.add_argument(
        '-W',
        '--write-in-place',
        help=
        'When outputting as yaml, json, csv or line, write the file instead of printing it, '
        'to a file with same base name and appropriate extension ',
        action='store_true')

    parser.set_defaults(out_type='csv')

    parser.add_argument(
        '-f',
        '--find-first',
        help='Find and print the first value for a fully qualified term name')

    parser.add_argument(
        '-d',
        '--show-declaration',
        default=False,
        action='store_true',
        help=
        'Parse a declaration file and print out declaration dict. Use -j or -y for the format'
    )

    parser.add_argument('file',
                        nargs='?',
                        default=DEFAULT_METATAB_FILE,
                        help='Path to a Metatab file')

    cli_init()

    args = parser.parse_args(sys.argv[1:])

    # Specing a fragment screws up setting the default metadata file name
    if args.file.startswith('#'):
        args.file = DEFAULT_METATAB_FILE + args.file

    if args.create is not False:
        if new_metatab_file(args.file, args.create):
            prt("Created ", args.file)
        else:
            warn("File", args.file, 'already exists.')

        exit(0)

    metadata_url = parse_app_url(args.file, proto='metatab')
    try:
        doc = MetatabDoc(metadata_url, cache=cache)
    except IOError as e:

        err("Failed to open '{}': {}".format(metadata_url, e))

    def write_or_print(t):
        from pathlib import Path

        if metadata_url.scheme != 'file':
            err("Can only use -w with local files")
            return

        ext = 'txt' if args.out_type == 'line' else args.out_type

        if args.write_in_place:
            with metadata_url.fspath.with_suffix('.' + ext).open('w') as f:
                f.write(t)
        else:
            print(t)

    if args.show_declaration:

        decl_doc = MetatabDoc('', cache=cache, decl=metadata_url.path)

        d = {'terms': decl_doc.decl_terms, 'sections': decl_doc.decl_sections}

        if args.out_type == 'json':
            print(json.dumps(d, indent=4))

        elif args.out_type == 'yaml':
            import yaml
            print(yaml.safe_dump(d, default_flow_style=False, indent=4))

    elif args.find_first:

        t = doc.find_first(args.find_first)
        print(t.value)

    elif args.out_type == 'terms':
        for t in doc._term_parser:
            print(t)

    elif args.out_type == 'json':
        write_or_print(json.dumps(doc.as_dict(), indent=4))

    elif args.out_type == 'yaml':
        import yaml
        from collections import OrderedDict

        def ordered_dump(data, stream=None, Dumper=yaml.Dumper, **kwds):
            class OrderedDumper(Dumper):
                pass

            def _dict_representer(dumper, data):
                return dumper.represent_mapping(
                    yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
                    data.items())

            OrderedDumper.add_representer(OrderedDict, _dict_representer)
            return yaml.dump(data, stream, OrderedDumper, **kwds)

        write_or_print(
            ordered_dump(doc.as_dict(),
                         default_flow_style=False,
                         indent=4,
                         Dumper=yaml.SafeDumper))

    elif args.out_type == 'line':
        write_or_print(doc.as_lines())

    elif args.out_type == 'csv':
        write_or_print(doc.as_csv())

    elif args.out_type == 'prety':
        from pprint import pprint
        pprint(doc.as_dict())

    exit(0)