def action(args): engine = sqlalchemy.create_engine(args.url, echo=args.verbosity > 2) tax = Taxonomy(engine, schema=args.schema) records = list(yaml.load_all(args.new_nodes)) log.info('adding new nodes') retval = None for rec in records: try: record_type = rec.pop('type') if record_type not in {'node', 'name'}: raise ValueError except (KeyError, ValueError): log.error( ('Error in record for tax_id {tax_id}: "type" is ' 'required and must be one of "node" or "name"').format(**rec)) retval = 1 continue tax_id = rec['tax_id'] rec['source_name'] = rec.get('source_name') or args.source_name try: if record_type == 'node': if not rec['source_name']: log.error('Error: record has no source_name:\n{}'.format( pprint.pformat(rec))) raise ValueError if tax.has_node(tax_id): log.info('updating *node* "{tax_id}"'.format(**rec)) tax.update_node(**rec) else: log.info('new *node* "{tax_id}"'.format(**rec)) tax.add_node(**rec) elif record_type == 'name': for name in rec['names']: name['tax_id'] = tax_id # source_name may be provided at the record or name level name['source_name'] = name.get( 'source_name') or rec['source_name'] if not name['source_name']: log.error( 'Error: record has no source_name:\n {}'.format( pprint.pformat(rec))) raise ValueError log.info('new *name* for "{tax_id}": "{tax_name}"'.format( **name)) tax.add_name(**name) except (ValueError, TypeError): log.error('Error in record with tax_id {}'.format(rec['tax_id'])) log.error(''.join(traceback.format_exception(*sys.exc_info()))) retval = 1 engine.dispose() if retval: log.error('Error: some records were malformed') return retval
def action(args): engine = sqlalchemy.create_engine(args.url, echo=args.verbosity > 2) tax = Taxonomy(engine, schema=args.schema) nodes = list(get_new_nodes(args.new_nodes)) # check if there are any new ranks and exit if needed node_ranks = set(n['rank'] for n in nodes) for r in node_ranks: if r not in tax.ranks: msg = 'adding new ranks to taxonomy is not yet supported' raise TaxonIntegrityError(msg) ranksdict = tax.ranksdict() ranksdict.update(dict([(n['tax_id'], n['rank']) for n in nodes])) nodes = [verify_rank_integrity(n, ranksdict, tax.ranks) for n in nodes] nodes = [verify_lineage_integrity(n, ranksdict, tax.ranks, tax) for n in nodes] log.info('adding new nodes') for d in nodes: if args.source_name: d['source_name'] = args.source_name try: tax.add_node(**d) except sqlalchemy.exc.IntegrityError: if args.update: tax.update_node(**d) else: log.warn('node with tax_id %(tax_id)s already exists' % d) else: log.info('added new node with tax_id %(tax_id)s' % d) engine.dispose()
def action(args): engine = sqlalchemy.create_engine(args.url, echo=args.verbosity > 2) tax = Taxonomy(engine, schema=args.schema) records = list(yaml.load_all(args.new_nodes)) log.info('adding new nodes') retval = None for rec in records: try: record_type = rec.pop('type') if record_type not in {'node', 'name'}: raise ValueError except (KeyError, ValueError): log.error(('Error in record for tax_id {tax_id}: "type" is ' 'required and must be one of "node" or "name"').format(**rec)) retval = 1 continue tax_id = rec['tax_id'] rec['source_name'] = rec.get('source_name') or args.source_name try: if record_type == 'node': if not rec['source_name']: log.error('Error: record has no source_name:\n{}'.format( pprint.pformat(rec))) raise ValueError if tax.has_node(tax_id): log.info('updating *node* "{tax_id}"'.format(**rec)) tax.update_node(**rec) else: log.info('new *node* "{tax_id}"'.format(**rec)) tax.add_node(**rec) elif record_type == 'name': for name in rec['names']: name['tax_id'] = tax_id # source_name may be provided at the record or name level name['source_name'] = name.get('source_name') or rec['source_name'] if not name['source_name']: log.error( 'Error: record has no source_name:\n {}'.format( pprint.pformat(rec))) raise ValueError log.info('new *name* for "{tax_id}": "{tax_name}"'.format(**name)) tax.add_name(**name) except (ValueError, TypeError): log.error('Error in record with tax_id {}'.format(rec['tax_id'])) log.error(''.join(traceback.format_exception(*sys.exc_info()))) retval = 1 engine.dispose() if retval: log.error('Error: some records were malformed') return retval