def common_interpret_object_ids(pipeline_param_full, args=None): """ Given command line arguments and the parameters of the pipeline, clean the 'object_ids' field to be a list of object ids """ pipeline_param = pipeline_param_full['parameters'] # read the object_ids object_ids = None if args: objs = [args.__dict__, pipeline_param] else: objs = [pipeline_param] for obj in objs: ids = obj.get('object_ids', None) names = obj.get('object_names', None) if ids is None and names is None: continue db_params = pipeline_param.get('db', {}) db_type = db_params.get('type', '') if db_type.lower() not in core_db_types(): continue # initialize the DB if isinstance(ids, str) and ids != 'all' and ids != 'missing': ids = eval(ids) if isinstance(names, str) and names != 'all' and names != 'missing': names = eval(names) if not ids and not names: break if object_ids is None: object_ids = set() db = dbtools.db_params_to_db(ObjectDbParameters(db_params)) if 'all' in (ids, names): object_ids = set([ str(x.id) for x in models.Object.all(db) ]) # unicode without the str() break if 'missing' in (ids, names): tmp_object_ids = set([ str(x.id) for x in models.Object.all(db) ]) tmp_object_ids_from_names = set([ str(x.object_id) for x in models.Model.all(db) ]) object_ids.update(tmp_object_ids.difference(tmp_object_ids_from_names)) if ids and ids != 'missing': object_ids.update(ids) if names and names != 'missing': for object_name in names: object_ids.update([str(x.id) for x in models.objects_by_name(db, object_name)]) # if we got some ids through the command line, just stop here if object_ids: break if isinstance(object_ids, set): pipeline_param['object_ids'] = list(object_ids) else: pipeline_param['object_ids'] = []
def interpret_object_ids(db_params, ids=[], names=[]): """ Given db parameters, clean the 'object_ids' field to be a list of object ids (as it could be the string 'all') """ db_type = eval(db_params).get('type', '') if db_type.lower() not in core_db_types(): return [] # initialize the DB if isinstance(ids, str) and ids != 'all' and ids != 'missing': ids = eval(ids) if isinstance(names, str) and names != 'all' and names != 'missing': names = eval(names) if not ids and not names: return [] object_ids = set() db = dbtools.db_params_to_db(ObjectDbParameters(db_params)) import models if 'all' in (ids, names): return set([str(x.id) for x in models.Object.all(db) ]) # unicode without the str() if 'missing' in (ids, names): tmp_object_ids = set([str(x.id) for x in models.Object.all(db)]) tmp_object_ids_from_names = set( [str(x.object_id) for x in models.Model.all(db)]) object_ids.update(tmp_object_ids.difference(tmp_object_ids_from_names)) if ids and ids != 'missing': object_ids.update(ids) if names and names != 'missing': for object_name in names: object_ids.update( [str(x.id) for x in models.objects_by_name(db, object_name)]) if isinstance(object_ids, set): return list(object_ids) else: return []
def interpret_object_ids(db_params, ids=[], names=[]): """ Given db parameters, clean the 'object_ids' field to be a list of object ids (as it could be the string 'all') """ db_type = eval(db_params).get('type', '') if db_type.lower() not in core_db_types(): return [] # initialize the DB if isinstance(ids, str) and ids != 'all' and ids != 'missing': ids = eval(ids) if isinstance(names, str) and names != 'all' and names != 'missing': names = eval(names) if not ids and not names: return [] object_ids = set() db = dbtools.db_params_to_db(ObjectDbParameters(db_params)) import models if 'all' in (ids, names): return set([ str(x.id) for x in models.Object.all(db) ]) # unicode without the str() if 'missing' in (ids, names): tmp_object_ids = set([ str(x.id) for x in models.Object.all(db) ]) tmp_object_ids_from_names = set([ str(x.object_id) for x in models.Model.all(db) ]) object_ids.update(tmp_object_ids.difference(tmp_object_ids_from_names)) if ids and ids != 'missing': object_ids.update(ids) if names and names != 'missing': for object_name in names: object_ids.update([str(x.id) for x in models.objects_by_name(db, object_name)]) if isinstance(object_ids, set): return list(object_ids) else: return []
#!/usr/bin/env python from object_recognition_core.db import ObjectDbTypes, ObjectDb, ObjectDbParameters from object_recognition_core.db.tools import db_params_to_db from object_recognition_core.db.object_db import core_db_types print 'Existing core types: ' + str(core_db_types()) str_to_enum = {'CouchDB': ObjectDbTypes.COUCHDB, 'filesystem': ObjectDbTypes.FILESYSTEM, 'empty': ObjectDbTypes.EMPTY } # test default parameters for db_params_raw in [{'type': 'CouchDB', 'root': 'http://localhost:5984', 'collection': 'object_recognition'}, {'path': '/tmp', 'type': 'filesystem', 'collection': 'object_recognition'}, {'type': 'empty'}]: type_str = db_params_raw['type'] print 'starting type ' + type_str db_params = ObjectDbParameters(db_params_raw) db = ObjectDb(db_params) db_params_new = db.parameters().raw for dic1, dic2 in [(db_params_raw, db_params_new), (db_params_new, db_params_raw)]: for k, v in dic1.items(): if (k not in dic2) or (k in dic2 and dic2[k] != v): raise RuntimeError('Key "%s" in %s but not in %s' % (str(k), str(dic1), str(dic2))) if str_to_enum[type_str] != db_params.type: raise RuntimeError('The "type" argument in db_params are wrong for db of type %s' % type_str) print 'ending type ' + type_str