+++ /dev/null
-#!/usr/bin/python
-
-# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
-#
-# This file is part of ChemDB.
-#
-# ChemDB is free software: you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by the
-# Free Software Foundation, either version 3 of the License, or (at your
-# option) any later version.
-#
-# ChemDB is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with ChemDB. If not, see <http://www.gnu.org/licenses/>.
-
-from sys import stdout, stdin, stderr
-
-import chemdb.chemdb
-
-from chemdb.db.text import TextDB, DBPrettyPrinter
-
-
-def open_IOfiles(ifilename=None, ofilename=None, debug=False):
- if ifilename:
- if debug: print >> stderr, "open input file '%s'" % ifilename
- ifile = file(ifilename, 'r')
- else:
- ifile = stdin
- if ofilename:
- if debug: print >> stderr, "open output file '%s'" % ofilename
- ofile = file(ofilename, 'w')
- else:
- ofile = stdout
- return (ifile, ofile)
-
-def close_IOfiles(ifilename=None, ifile=stdin,
- ofilename=None, ofile=stdout,
- debug=False):
- if ifilename:
- if debug: print >> stderr, "close input file '%s'" % ifilename
- ifile.close()
- if ofilename:
- if debug: print >> stderr, "close output file '%s'" % ofilename
- ofile.close()
-
-if __name__ == '__main__':
- from optparse import OptionParser
-
- parser = OptionParser(usage='usage: %prog [options]', version='%prog 0.1')
-
- parser.add_option('-f', '--input-file', dest='ifilename',
- help='Read input from FILE (default stdin)',
- type='string', metavar='FILE')
- parser.add_option('-o', '--output-file', dest='ofilename',
- help='Write output to FILE (default stdout)',
- type='string', metavar='FILE')
- parser.add_option('-d', '--delimiter', dest='FS', # field seperator
- help="Set field delimiter (default '%default')",
- type='string', metavar='DELIM', default='\t')
- parser.add_option('-p', '--print-fields', dest='print_fields',
- help='Only print certain fields (e.g. 0,3,4,2)',
- type='string', metavar='FIELDS')
- parser.add_option('-r', '--print-records', dest='print_records',
- help='Only print certain records (e.g. 0:3)',
- type='string', metavar='RECORDS')
- parser.add_option('-w', '--column-width', dest='width',
- help='Set column width for short-format output.',
- type='string', metavar='WIDTH')
- parser.add_option('-L', '--long-format', dest='long_format',
- help='Print long format (several lines per record)',
- action='store_true', default=False)
- parser.add_option('-l', '--short-format', dest='long_format',
- help='Print short format (default) (one lines per record)',
- action='store_false', default=False)
- parser.add_option('--valid-record', dest='valid_record',
- help="Select fields where True == lambda r : eval(EXPRESSION). default '%default'",
- type='string', metavar='EXPRESSION', default="r['Disposed'] == ''")
- parser.add_option('--sort-field', dest='sort_field',
- help="Sort matching records by FIELD (defauly '%default')",
- type='string', metavar='FIELD', default='db_id')
- parser.add_option('--pdf-title', dest='pdf_title',
- help='Override the default PDF title',
- type='string', metavar='TITLE')
- parser.add_option('--inventory', dest='inventory',
- help='Output a PDF inventory of matching records',
- action='store_true', default=False)
- parser.add_option('--door-warning', dest='door_warning',
- help='Output a PDF door warning of matching records',
- action='store_true', default=False)
- parser.add_option('-t', '--test', dest='test',
- help='Run docutils tests on db.py',
- action='store_true', default=False)
- parser.add_option('--list-locations', dest='locations',
- help='List all currently used locations (no other output)',
- action='store_true', default=False)
- parser.add_option('-V', '--validate', dest='validate',
- help='Validate CAS#s (no other output)',
- action='store_true', default=False)
- parser.add_option('-A', '--audit', dest='audit',
- help='Search for troublesome entries (no other output)',
- action='store_true', default=False)
- parser.add_option('-v', '--verbose', dest='verbose',
- help='Print lots of debugging information',
- action='store_true', default=False)
-
- (options, args) = parser.parse_args()
- parser.destroy()
-
- ifile,ofile = open_IOfiles(options.ifilename, options.ofilename,
- options.verbose)
-
- if options.test:
- _test()
- elif options.locations:
- db = TextDB(filename=None)
- pp = DBPrettyPrinter(db)
-
- # read in and parse the file
- db._parse(ifile.read())
-
- locations = []
- for record in db.records():
- if len(record['Location']) > 0 and record['Location'] not in locations:
- locations.append(record['Location'])
- locations.sort()
- print >> ofile, '\n'.join(locations)
- elif options.validate:
- db = TextDB(filename=None)
- pp = DBPrettyPrinter(db)
-
- # read in and parse the file
- db._parse(ifile.read())
-
- CAS_DELIM = ',' # seperate CAS entries for chemicals with multiple CAS numbers
- PERCENT_DELIM = ':' # seperate CAS number from ingredient percentage
- for record in db.records():
- valid = True
- cas = record['CAS#']
- if len(cas.split(CAS_DELIM)) == 0 : # cas = 'N...N-NN-N'
- if not chemdb.chemdb.valid_CASno(cas, options.verbose):
- valid = False
- print >> ofile, "Invalid CAS# in record: '%s'" % cas
- else : # cas = 'N...N-NN-N:X%,N...N-NN-N:Y%,...'
- for casterm in cas.split(CAS_DELIM) : # casterm = 'N...N-NN-N:X%'
- c = casterm.split(PERCENT_DELIM)[0] # c = 'N...N-NN-N'
- if not chemdb.chemdb.valid_CASno(c, options.verbose):
- valid = False
- print >> ofile, "Invalid CAS* in record: '%s'" % c
- if not valid:
- print >> ofile, (
- "in record %s: %s" % (record['ID'], record['Name']))
- #pp.full_record_string(record)
- elif options.audit:
- db = TextDB(filename=None)
- pp = DBPrettyPrinter(db)
-
- # read in and parse the file
- db._parse(ifile.read())
-
- for record in db.records():
- # check for extra spaces
- for key,value in record.items():
- if (isinstance(value, types.StringTypes)
- and value.strip() != value):
- print >> ofile, (
- "Extra whitespace for %s - %s field %s : '%s'"
- % (record['ID'], record['Name'], key, value))
- # make sure we know the location of all current chemicals
- if len(record['Disposed']) == 0 and len(record['Location']) == 0:
- print >> ofile, (
- "Misplaced record: %s - %s"
- % (record['ID'], record['Name']))
- elif options.inventory:
- db = TextDB(filename=None)
- pp = DBPrettyPrinter(db)
-
- # read in and parse the file
- db._parse(ifile.read())
-
- dgen = chemdb.chemdb.DocGen(db)
- def valid_record(r):
- return eval(options.valid_record, # expression
- {'__builtins__':None}, # globals
- {'r':r}) # locals
- path = dgen.inventory(title=options.pdf_title,
- namewidth=40,
- sort_field=options.sort_field,
- valid_record=valid_record)
- print >> ofile, '\n', path
- elif options.door_warning:
- db = TextDB(filename=None)
- pp = DBPrettyPrinter(db)
-
- # read in and parse the file
- db._parse(ifile.read())
-
- dgen = chemdb.chemdb.DocGen(db)
- def valid_record(r):
- return eval(options.valid_record, # expression
- {'__builtins__':None}, # globals
- {'r':r}) # locals
- path = dgen.door_warning(valid_record=valid_record)
- print >> ofile, '\n', path
- else:
- db = TextDB(filename=None)
-
- # read in and parse the file
- db._parse(ifile.read())
- pp = DBPrettyPrinter(db)
- if options.long_format:
- for id in pp._norm_record_ids(options.print_records):
- string = pp.full_record_string_id(id)
- else:
- # pythonize the width option
- if options.width == None or options.width == 'a':
- width = options.width
- elif len(options.width.split(':')) == 1:
- width = int(options.width)
- elif len(options.width.split(':')) > 1:
- width = {}
- for kv in options.width.split(','):
- spl = kv.split(':')
- assert len(spl) == 2, 'invalid width "%s" in "%s"' % (kv, options.width)
- if spl[1] == 'a':
- width[spl[0]] = spl[1]
- else:
- width[spl[0]] = int(spl[1])
-
- string = pp.multi_record_string(options.print_records,
- options.print_fields,
- width,
- options.FS)
- print >> ofile, string,
-
- close_IOfiles(options.ifilename, ifile,
- options.ofilename, ofile, options.verbose)