File: cif_printout_Python

package info (click to toggle)
cod-tools 3.11.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 159,136 kB
  • sloc: perl: 58,707; sh: 41,323; ansic: 7,268; xml: 1,982; yacc: 1,117; makefile: 731; python: 166
file content (88 lines) | stat: -rwxr-xr-x 3,574 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------
#$Author: antanas $
#$Revision: 8230 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.11.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)