#!/usr/bin/python3
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------
#$Author: antanas $
#$Revision: 8230 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.8.0/tests/scripts/cif_printout_Python $
#$Date: 2020-07-20 16:43:00 +0300 (Mon, 20 Jul 2020) $
#$Id: cif_printout_Python 8230 2020-07-20 13:43:00Z antanas $
#------------------------------------------------------------------------------
#*
#* Parse CIF file and print out the structure generated by CIF parser.
#**

import argparse
import pprint
import sys
from pycodcif import parse, CifParserException

def version():
    import os
    from os.path import realpath,dirname
    from subprocess import call
    directory = os.path.dirname(os.path.realpath(__file__))
    call( "{}/cod-tools-version".format(directory) )
    sys.exit()

parser = argparse.ArgumentParser(description='Parse CIF file and print out '
                                             'the structure generated by '
                                             'the CIF parser')

parser.add_argument("--do-not-unprefix-text",
                    dest='do_not_unprefix_text',
                    action='store_const', const=1)
parser.add_argument("--do-not-unfold-text",
                    dest='do_not_unfold_text',
                    action='store_const', const=1)
parser.add_argument("--fix-errors",
                    dest='fix_errors',
                    action='store_const', const=1)
parser.add_argument("--fix-duplicate-tags-with-same-values",
                    dest='fix_duplicate_tags_with_same_values',
                    action='store_const', const=1)
parser.add_argument("--fix-duplicate-tags-with-empty-values",
                    dest='fix_duplicate_tags_with_empty_values',
                    action='store_const', const=1)
parser.add_argument("--fix-data-header",
                    dest='fix_data_header',
                    action='store_const', const=1)
parser.add_argument("--fix-datablock-names",
                    dest='fix_datablock_names',
                    action='store_const', const=1)
parser.add_argument("--fix-string-quotes",
                    dest='fix_string_quotes',
                    action='store_const', const=1)
parser.add_argument("--fix-missing-closing-double-quote",
                    dest='fix_missing_closing_double_quote',
                    action='store_const', const=1)
parser.add_argument("--fix-missing-closing-single-quote",
                    dest='fix_missing_closing_single_quote',
                    action='store_const', const=1)
parser.add_argument("--fix-ctrl-z",
                    dest='fix_ctrl_z',
                    action='store_const', const=1)
parser.add_argument("--allow-uqstring-brackets",
                    dest='allow_uqstring_brackets',
                    action='store_const', const=1)
parser.add_argument("--version", dest='version',
                    action='store_const', const=1)
parser.add_argument("files", nargs='*')

options = vars(parser.parse_args(args=sys.argv[1:]))
options = {key: options[key] for key in options.keys() if options[key]}

files = options.pop('files',[])
if not files:
    files = ['-']

if 'version' in options.keys():
    version()

for filename in files:
    try:
        data, errcount, _ = parse(filename, options)
        pprint.PrettyPrinter().pprint(data)
        print(len(data[0]['tags'][0]))
        sys.stderr.write("{} error(s) detected\n".format(errcount))
    except CifParserException:
        sys.exit(1)
