133 lines
3.6 KiB
Python
133 lines
3.6 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
"""\
|
|
Material database.
|
|
"""
|
|
__author__ = "Daniel Weschke"
|
|
__copyright__ = "Copyright 2019 Daniel Weschke"
|
|
__credits__ = ["Daniel Weschke"]
|
|
__license__ = "MIT"
|
|
__version__ = "2019.02.03"
|
|
__maintainer__ = "Daniel Weschke"
|
|
__email__ = "daniel.weschke@directbox.de"
|
|
__status__ = "Production" # "Prototype", "Development", "Production"
|
|
|
|
VERSION = """\
|
|
%(prog)s version {version} {copyright}
|
|
""".format(
|
|
version=__version__, copyright=__copyright__)
|
|
|
|
EPILOG = """\
|
|
"""
|
|
|
|
import os
|
|
|
|
|
|
def list():
|
|
from os import listdir
|
|
|
|
for data in listdir("./data"):
|
|
file_name = data.split('.')[0]
|
|
if file_name != "INFO":
|
|
print(file_name)
|
|
|
|
|
|
def read(input, search_keys=None):
|
|
import json
|
|
from pprint import pprint
|
|
|
|
def find_values(id, json_repr):
|
|
results = []
|
|
|
|
def _decode_dict(a_dict):
|
|
try: results.append(a_dict[id])
|
|
except KeyError: pass
|
|
return a_dict
|
|
|
|
json.loads(json_repr, object_hook=_decode_dict) # return value ignored
|
|
return results
|
|
|
|
try:
|
|
with open(os.path.join('data', input + '.json')) as data_file:
|
|
if search_keys is not None:
|
|
data = data_file.read()
|
|
for search_key in search_keys:
|
|
found_values = find_values(search_key, data)
|
|
#print(found_values)
|
|
for value in found_values:
|
|
#print('%-5s' % value[0] + ' ' + value[1])
|
|
print(search_key + ' = ' + '%s' % value[0] + ' ' + value[1])
|
|
|
|
#def getLength(element):
|
|
# try:
|
|
# element.__iter__
|
|
# return sum([getLength(i) for i in element])
|
|
# except:
|
|
# return 1
|
|
#print(len(data))
|
|
#print(getLength(json.loads(data)))
|
|
|
|
else:
|
|
data = json.load(data_file)
|
|
pprint(data)
|
|
|
|
except (OSError, IOError) as err:
|
|
print(str(err))
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
def main():
|
|
"""\
|
|
Main function
|
|
"""
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser(
|
|
description=__doc__, prefix_chars='-', epilog=EPILOG,
|
|
#usage="%(prog)s [OPTION]... NAME",
|
|
formatter_class=argparse.RawTextHelpFormatter,
|
|
)
|
|
parser.add_argument('-v', '--verbose', action="store_true", help="Verbose output")
|
|
parser.add_argument('-V', '--version', action='version', version=VERSION)
|
|
parser.add_argument('-D', '--debug', dest='debug', action='store_true', help=argparse.SUPPRESS)
|
|
|
|
subparsers = parser.add_subparsers(help='commands', dest='command')
|
|
|
|
# list materials
|
|
parser_list = subparsers.add_parser('list',
|
|
description='List materials.',
|
|
help='list all available materials')
|
|
|
|
# material
|
|
parser_material = subparsers.add_parser('material',
|
|
help='the material to get information from')
|
|
parser_material.add_argument('materialname', action='store',
|
|
help='list available material information')
|
|
parser_material.add_argument('-r', '--rho', dest='const_collection',
|
|
action='append_const', const="rho",
|
|
help='get density from material')
|
|
parser_material.add_argument('-T', '--Tm', dest='const_collection',
|
|
action='append_const', const="Tm",
|
|
help='get melting point temperature')
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.debug:
|
|
print(args)
|
|
|
|
if args.command == 'list':
|
|
list()
|
|
|
|
elif args.command == 'material':
|
|
if args.const_collection:
|
|
read(args.materialname, search_keys=args.const_collection)
|
|
else:
|
|
read(args.materialname)
|
|
|
|
return 0
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
sys.exit(main())
|