Thomas Waldmann avatar Thomas Waldmann committed 880a0da

implement get_names() with workaround code, reduce code duplication

Comments (0)

Files changed (1)

MoinMoin/storage/middleware/indexing.py

 INDEXES = [LATEST_REVS, ALL_REVS, ]
 
 
+def get_names(meta):
+    """
+    Get the (list of) names from meta data and deal with misc. bad things that
+    can happen then (while not all code is fixed to do it correctly).
+
+    TODO make sure meta[NAME] is always a list of unicode
+
+    :param meta: a metadata dictionary that might have a NAME key
+    :return: list of names
+    """
+    msg = "NAME is not a list but %r - fix this! Workaround enabled."
+    names = meta.get(NAME)
+    if names is None:
+        logging.warning(msg % names)
+        names = []
+    elif isinstance(names, str):
+        logging.warning(msg % names)
+        names = [names.decode('utf-8'), ]
+    elif isinstance(names, unicode):
+        logging.warning(msg % names)
+        names = [names, ]
+    elif isinstance(names, tuple):
+        logging.warning(msg % names)
+        names = list(names)
+    elif not isinstance(names, list):
+        raise TypeError("NAME is not a list but %r - fix this!" % names)
+    if not names:
+        # we currently never return an empty list, some code
+        # might not be able to deal with it:
+        names = [u'DoesNotExist', ]
+    return names
+
+
 def backend_to_index(meta, content, schema, wikiname, backend_name):
     """
     Convert backend metadata/data to a whoosh document.
             return self.data.tell(*args, **kw)
 
     if not item_name:
-        names = meta.get(NAME)
-        if names:
-            item_name = names[0]
-        else:
-            item_name = u'anonymous'
+        item_name = get_names(meta)[0]
 
     rev = PseudoRev(meta, data)
     try:
 
     @property
     def names(self):
-        names = self._current.get(NAME)
-        if isinstance(names, tuple):
-            names = list(names)
-        elif not isinstance(names, list):
-            #raise TypeError # for debugging the issues
-            # TODO make sure meta[NAME] is always there, so this never happens
-            # TODO make sure meta[NAME] is always a list of unicode
-            logging.warning("NAME is not a list but %r - fix this! Workaround enabled." % names)
-            if names is None:
-                names = u'DoesNotExist'
-            elif isinstance(names, str):
-                names = unicode(names)
-            names = [names, ]
-            logging.warning("DOC: %r - Workaround returns names = %r" % (self._current, names))
-        assert isinstance(names, list)
-        return names
+        return get_names(self._current)
 
     @property
     def mtime(self):
 
     @property
     def names(self):
-        names = self.meta.get(NAME)
-        if isinstance(names, tuple):
-            names = list(names)
-        elif not isinstance(names, list):
-            #raise TypeError # for debugging the issues
-            # TODO make sure meta[NAME] is always there, so this never happens
-            # TODO make sure meta[NAME] is always a list of unicode
-            logging.warning("NAME is not a list but %r - fix this! Workaround enabled." % names)
-            if names is None:
-                names = u'DoesNotExist'
-            elif isinstance(names, str):
-                names = unicode(names)
-            names = [names, ]
-            logging.warning("DOC: %r - Workaround returns names = %r" % (self.meta, names))
-        return names
+        return get_names(self.meta)
 
     @property
     def name(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.