From f4f31e110eb7345c6009e3addf4e7ac7cb0a93c7 Mon Sep 17 00:00:00 2001
From: Daniel <daniel@harvey>
Date: Wed, 12 Jun 2019 11:03:13 +0200
Subject: [PATCH] DOC: Mostly just comments, minor cosmetic code changes.

---
 src/caosadvancedtools/cfood.py          | 45 +++++++++++++++++++++----
 src/caosadvancedtools/crawler.py        |  2 +-
 src/caosadvancedtools/read_md_header.py |  6 +---
 src/programs/example_cfood.py           |  3 ++
 4 files changed, 43 insertions(+), 13 deletions(-)

diff --git a/src/caosadvancedtools/cfood.py b/src/caosadvancedtools/cfood.py
index 05cf6f1c..df66b75b 100644
--- a/src/caosadvancedtools/cfood.py
+++ b/src/caosadvancedtools/cfood.py
@@ -38,13 +38,28 @@ ENTITIES = {}
 
 
 def get_value(prop):
-    if not isinstance(prop.value, db.Entity):
-        return prop.value
-    else:
+    """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 isinstance(prop.value, db.Entity):
         return str(prop.value.id)
+    else:
+        return prop.value
 
 
 def get_entity(name):
+    """Returns the entity with a given name, preferably from a local cache.
+
+    If the local cache does not contain the entity, retrieve it from CaosDB.
+    """
     if name not in ENTITIES:
         ent = db.Entity(name=name)
         ent.retrieve()
@@ -58,7 +73,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
@@ -71,8 +102,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
 
@@ -155,7 +186,7 @@ class AbstractCFood(object):
             query_string = "FIND Record " + entity.get_parents()[0].name
             #import IPython
             # IPython.embed()
-            query_string += " with " + " and ".join(
+            query_string += " WITH " + " AND ".join(
                 ["'" + p.name + "'='"
                  + get_value(p) + "'" for p in entity.get_properties()])
         else:
@@ -171,7 +202,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 0c570ff1..79c4ee07 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 724b6d51..95096ed3 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 20572d16..4b063457 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
-- 
GitLab