| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Copyright (c) 2017, the cclib development team |
| 5 |
# |
| 6 |
# This file is part of cclib (http://cclib.github.io) and is distributed under |
| 7 |
# the terms of the BSD 3-Clause License. |
| 8 |
|
| 9 |
"""Script for loading data from computational chemistry files.""" |
| 10 |
|
| 11 |
|
| 12 |
from __future__ import print_function |
| 13 |
|
| 14 |
import glob |
| 15 |
import logging |
| 16 |
import os.path |
| 17 |
import sys |
| 18 |
from functools import partial |
| 19 |
from pprint import pprint |
| 20 |
|
| 21 |
import numpy |
| 22 |
|
| 23 |
from cclib.parser import ccData |
| 24 |
from cclib.io import ccread, URL_PATTERN |
| 25 |
|
| 26 |
|
| 27 |
# Set up options for pretty-printing output. |
| 28 |
if sys.version_info < (3, 4): |
| 29 |
pprint = partial(pprint, width=120) |
| 30 |
else: |
| 31 |
pprint = partial(pprint, width=120, compact=True) |
| 32 |
numpy.set_printoptions(linewidth=120) |
| 33 |
|
| 34 |
|
| 35 |
def ccget(): |
| 36 |
"""Parse files with cclib based on command line arguments.""" |
| 37 |
|
| 38 |
import argparse |
| 39 |
|
| 40 |
parser = argparse.ArgumentParser() |
| 41 |
|
| 42 |
parser.add_argument( |
| 43 |
"attribute_or_compchemlogfile", nargs="+", |
| 44 |
help="one or more attributes to be parsed from one ore more logfiles", |
| 45 |
) |
| 46 |
|
| 47 |
group = parser.add_mutually_exclusive_group() |
| 48 |
|
| 49 |
group.add_argument( |
| 50 |
"--list", "-l", |
| 51 |
action="store_true", |
| 52 |
help="print a list of attributes available in each file", |
| 53 |
) |
| 54 |
group.add_argument( |
| 55 |
"--json", "-j", |
| 56 |
action="store_true", |
| 57 |
help="the given logfile is in CJSON format", |
| 58 |
) |
| 59 |
group.add_argument( |
| 60 |
"--multi", "-m", |
| 61 |
action="store_true", |
| 62 |
help="parse multiple input files as one input stream", |
| 63 |
) |
| 64 |
|
| 65 |
parser.add_argument( |
| 66 |
"--verbose", "-v", |
| 67 |
action="store_true", |
| 68 |
help="more verbose parsing output (only errors by default)", |
| 69 |
) |
| 70 |
parser.add_argument( |
| 71 |
"--future", "-u", |
| 72 |
action="store_true", |
| 73 |
help="use experimental features (currently optdone_as_list)", |
| 74 |
) |
| 75 |
parser.add_argument( |
| 76 |
"--full", "-f", |
| 77 |
action="store_true", |
| 78 |
help="toggle full print behaviour for attributes", |
| 79 |
) |
| 80 |
|
| 81 |
args = parser.parse_args() |
| 82 |
|
| 83 |
arglist = args.attribute_or_compchemlogfile |
| 84 |
showattr = args.list |
| 85 |
cjsonfile = args.json |
| 86 |
multifile = args.multi |
| 87 |
verbose = args.verbose |
| 88 |
future = args.future |
| 89 |
full = args.full |
| 90 |
|
| 91 |
# Toggle full print behaviour for numpy arrays. |
| 92 |
if full: |
| 93 |
numpy.set_printoptions(threshold=numpy.nan) |
| 94 |
|
| 95 |
# We need at least one attribute and the filename, so two arguments, or |
| 96 |
# just one filename if we want to list attributes that can be extracted. |
| 97 |
# In multifile mode, we generally want at least two filenames, so the |
| 98 |
# expected number of arguments is a bit different. |
| 99 |
if not multifile: |
| 100 |
correct_number = (not showattr and len(arglist) > 1) or (showattr and len(arglist) > 0) |
| 101 |
else: |
| 102 |
correct_number = (not showattr and len(arglist) > 2) or (showattr and len(arglist) > 1) |
| 103 |
if not correct_number: |
| 104 |
print("The number of arguments does not seem to be correct.") |
| 105 |
parser.print_usage() |
| 106 |
parser.exit(1) |
| 107 |
|
| 108 |
# Figure out which are the attribute names and which are the filenames or links. |
| 109 |
# Note that in Linux, the shell expands wild cards, but not so in Windows, |
| 110 |
# so try to do that here using glob. |
| 111 |
attrnames = [] |
| 112 |
filenames = [] |
| 113 |
for arg in arglist: |
| 114 |
if arg in ccData._attrlist: |
| 115 |
attrnames.append(arg) |
| 116 |
elif URL_PATTERN.match(arg) or os.path.isfile(arg): |
| 117 |
filenames.append(arg) |
| 118 |
else: |
| 119 |
wildcardmatches = glob.glob(arg) |
| 120 |
if wildcardmatches: |
| 121 |
filenames.extend(wildcardmatches) |
| 122 |
else: |
| 123 |
print("%s is neither a filename nor an attribute name." % arg) |
| 124 |
parser.print_usage() |
| 125 |
parser.exit(1) |
| 126 |
|
| 127 |
# Since there is some ambiguity to the correct number of arguments, check |
| 128 |
# that there is at least one filename (or two in multifile mode), and also |
| 129 |
# at least one attribute to parse if the -l option was not passed. |
| 130 |
if len(filenames) == 0: |
| 131 |
print("No logfiles given") |
| 132 |
parser.exit(1) |
| 133 |
if multifile and len(filenames) == 1: |
| 134 |
print("Expecting at least two logfiles in multifile mode") |
| 135 |
parser.exit(1) |
| 136 |
if not showattr and len(attrnames) == 0: |
| 137 |
print("No attributes given") |
| 138 |
parser.exit(1) |
| 139 |
|
| 140 |
# This should be sufficient to correctly handle multiple files, that is to |
| 141 |
# run the loop below only once with all logfiles in the variable `filename`. |
| 142 |
# Although, perhaps it would be clearer to abstract the contents of the loop |
| 143 |
# into another function. |
| 144 |
if multifile: |
| 145 |
filenames = [filenames] |
| 146 |
|
| 147 |
# Now parse each file and print out the requested attributes. |
| 148 |
for filename in filenames: |
| 149 |
|
| 150 |
if multifile: |
| 151 |
name = ", ".join(filename[:-1]) + " and " + filename[-1] |
| 152 |
else: |
| 153 |
name = filename |
| 154 |
|
| 155 |
# The keyword dictionary are not used so much. but could be useful for |
| 156 |
# passing options downstream. For example, we might use --future for |
| 157 |
# triggering experimental or alternative behavior (as with optdone). |
| 158 |
kwargs = {} |
| 159 |
if verbose: |
| 160 |
kwargs['verbose'] = True |
| 161 |
kwargs['loglevel'] = logging.INFO |
| 162 |
else: |
| 163 |
kwargs['verbose'] = False |
| 164 |
kwargs['loglevel'] = logging.ERROR |
| 165 |
if future: |
| 166 |
kwargs['future'] = True |
| 167 |
if cjsonfile: |
| 168 |
kwargs['cjson'] = True |
| 169 |
|
| 170 |
print("Attempting to read %s" % name) |
| 171 |
data = ccread(filename, **kwargs) |
| 172 |
|
| 173 |
if data is None: |
| 174 |
print("Cannot figure out the format of '%s'" % name) |
| 175 |
print("Report this to the cclib development team if you think it is an error.") |
| 176 |
print("\n" + parser.format_usage()) |
| 177 |
parser.exit(1) |
| 178 |
|
| 179 |
if showattr: |
| 180 |
print("cclib can parse the following attributes from %s:" % name) |
| 181 |
if cjsonfile: |
| 182 |
for key in data: |
| 183 |
print(key) |
| 184 |
break |
| 185 |
for attr in data._attrlist: |
| 186 |
if hasattr(data, attr): |
| 187 |
print(" %s" % attr) |
| 188 |
else: |
| 189 |
invalid = False |
| 190 |
for attr in attrnames: |
| 191 |
if cjsonfile: |
| 192 |
if attr in data: |
| 193 |
print("%s:\n%s" % (attr, data[attr])) |
| 194 |
continue |
| 195 |
else: |
| 196 |
if hasattr(data, attr): |
| 197 |
print(attr) |
| 198 |
attr_val = getattr(data, attr) |
| 199 |
# List of attributes to be printed with new lines |
| 200 |
if attr in data._listsofarrays and full: |
| 201 |
for val in attr_val: |
| 202 |
pprint(val) |
| 203 |
else: |
| 204 |
pprint(attr_val) |
| 205 |
continue |
| 206 |
|
| 207 |
print("Could not parse %s from this file." % attr) |
| 208 |
invalid = True |
| 209 |
if invalid: |
| 210 |
parser.print_help() |
| 211 |
|
| 212 |
|
| 213 |
if __name__ == "__main__": |
| 214 |
|
| 215 |
ccget() |