diff --git a/src/caosadvancedtools/cfood.py b/src/caosadvancedtools/cfood.py index 617a8c7acb907fe225b83692cc7ee14570e1a9cc..23e60d8e7d75d62ed41a4d482abb2c1b2e252eca 100644 --- a/src/caosadvancedtools/cfood.py +++ b/src/caosadvancedtools/cfood.py @@ -38,22 +38,28 @@ ENTITIES = {} def get_value(prop): - """returns the value of a Property + """ Returns the value of a Property + + Parameters + ---------- + prop : The property of which the value shall be returned. + + Returns + ------- + out : The value of the property; if the value is an entity, its ID. - If the value is an Entity itself, its id is returned instead """ - if not isinstance(prop.value, db.Entity): - return prop.value - else: + if isinstance(prop.value, db.Entity): return prop.value.id + else: + return prop.value def get_entity(name): - """implements a mini cache of RecordTypes + """ Returns the entity with a given name, preferably from a local cache. - If and only if a RecordType (identified by its name) is not in the - dictionary, it is retrieved from CaosDB. + If the local cache does not contain the entity, retrieve it from CaosDB. """ if name not in ENTITIES: @@ -69,7 +75,23 @@ class AbstractCFood(object): # a match # instances shall be used to keep track of a match; i.e. entities can be # object variable + def __init__(self, pattern, use_cache=False, access=lambda x: x): + """Abstract base class for Crawler food (CFood). + + Parameters + ---------- + pattern : str + The regex pattern for matching against file names. + + use_cache : bool, optional + Whether to use caching (not re-inserting probably existing + objects into CaosDB), defaults to False. + + access : callable, optional + Only used by child classes? + + """ self.pattern = re.compile(pattern) self.use_cache = use_cache self.access = access @@ -85,8 +107,8 @@ class AbstractCFood(object): for key, identifiable in entities.items(): if identifiable is None: - print("THIS IS STRANGE") - + print("THIS IS STRANGE. No identifiables found in {}.".format( + crawled_file}) continue existing = None @@ -175,7 +197,7 @@ class AbstractCFood(object): if entity.name is None: # TODO multiple parents are ignored! Sufficient? query_string = "FIND Record " + entity.get_parents()[0].name - query_string += " with " + " and ".join( + query_string += " WITH " + " AND ".join( ["'" + p.name + "'='" + str(get_value(p)) + "'" for p in entity.get_properties()]) else: @@ -193,7 +215,7 @@ class AbstractCFood(object): if r is not None: print("Found Entity with id:", r.id) else: - print("Did not find an existing entity") + print("Did not find an existing entity.") return r diff --git a/src/caosadvancedtools/crawler.py b/src/caosadvancedtools/crawler.py index 0c570ff14cb76bdafd866d299087014d1dd783a1..79c4ee071e129173dbca9289dbea522eb1c7b3de 100644 --- a/src/caosadvancedtools/crawler.py +++ b/src/caosadvancedtools/crawler.py @@ -54,7 +54,7 @@ class Crawler(object): (path if path.endswith("/") else path + "/") + "**" print("FILES QUERY: " + query_str) files = db.execute_query(query_str) - print(str(len(files)) + " FILES TO BE PROCESSED.") + print("{} FILES TO BE PROCESSED.".format(len(files))) return files diff --git a/src/caosadvancedtools/read_md_header.py b/src/caosadvancedtools/read_md_header.py index 724b6d51d2fcf07528f42119e25a1fb02c6e9546..95096ed32d21ec4b58031fad3fc29f94fb0b2756 100644 --- a/src/caosadvancedtools/read_md_header.py +++ b/src/caosadvancedtools/read_md_header.py @@ -55,11 +55,7 @@ def _clean_header(header): class NoValidHeader(Exception): - def __init__(self, filename, *args, **kwargs): - msg = ("Header missing in {}\nFix this with the modify subcommand " - "using -f option".format(filename)) - super().__init__(msg, *args, **kwargs) - + pass def get_header(filename): """Open an md file identified by filename and read out the yaml diff --git a/src/programs/example_cfood.py b/src/programs/example_cfood.py index 20572d16689a50b45cc8a183188990127e1d3f13..4b06345775d5f8758303bf4ffab92a1f9b138364 100644 --- a/src/programs/example_cfood.py +++ b/src/programs/example_cfood.py @@ -23,10 +23,13 @@ # ** end header # +import caosdb as db + from caosadvancedtools.cfood import AbstractCFood class ExampleCFood(AbstractCFood): def create_identifiables(self, crawled_file, match): + print("create_identifiables") entities = {} entities["exp"] = db.Record() #import IPython