OSDN Git Service

Re implement cachefile.py
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / cachefile.py
index 53184d1..8077d81 100644 (file)
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
-import os
+import itertools
 
 import misc
 
-metadata_namelist = ["title", "lineCount", "lastModified", "idxlastModified"]
-
-def load_cache(bbs_type, func):
-    """ Loads metadata from .cache file
-
-    loads metadata and invokes handler per one thread
-
-    bbs: bbs id
-
-    board: board id
-
-    func: invoked at loading one thread's metadata
-    should be of the form:
-    def handler_example(id, metadata_dic):
-    where the first argument is thread id and
-    the second metadata dic which key is in metadata_namelist
-    """
-
-    # nothing to do if .cache file does not exist
-    cachefile_path = misc.get_board_cache_path(bbs_type)
-    if not os.path.exists(cachefile_path):
-        return
-
-    f = open(cachefile_path, "r")
-    try:
-        line = f.readline()
-        while line:
-            metadatas = line.split("\t")
-            dic = {}
-            for field in metadatas:
-                for name in ["id", "title", "lineCount",
-                             "lastModified", "idxlastModified"]:
-                    if field.startswith(name+"="):
-                        value = field[len(name)+1:]
-                        if name is "lineCount" or name is "idxlastModified":
-                            try:
-                                dic[name] = int(value)
-                            except:
-                                dic[name] = 0
-                        else:
-                            dic[name] = value
-            # invoke func only if id exists
-            if "id" in dic and dic["id"]:
-                # if metadata in metadata_namelist does not exist,
-                # set empty str or 0
-                for name in metadata_namelist:
-                    if name not in dic:
-                        if name is "lineCount" or name is "idxlastModified":
-                            dic[name] = 0
-                        else:
-                            dic[name] = ""
-                func(dic["id"], dic)
-            line = f.readline()
-    finally:
-        f.close()
-
-def save_cache(bbs_type, dic):
-    """ Saves metadata list to .cache file
-
-    bbs: bbs id
-
-    board: board id
-
-    dic: dictionary of thread id and metadata dictionary
-    which key is in metadata_namelist
-    """
-    # nothing to do if dic is empty
-    if not dic:
-        return
-
-    cachefile_path = misc.get_board_cache_path(bbs_type)
-
-    # create a directroy where .cache file is if not exists
-    basedir = os.path.dirname(cachefile_path)
-    if not os.path.isdir(basedir):
-        os.makedirs(basedir)
-
-    f = open(cachefile_path, "w")
-    for id, metadata_list in dic.iteritems():
-        # save only if id is not empty
-        if id:
-            line = "id=" + id + "\t"
-            # save metadata only if exists in metadata_namelist
-            for name in metadata_namelist:
-                # save metadata only if exists in metadata_list
-                # and no need to save an empty or invalid field
-                if name in metadata_list:
-                    if name is "lineCount" or name is "idxlastModified":
-                        if metadata_list[name] > 0:
-                            line += name + "=" + str(metadata_list[name]) + "\t"
-                    else:
-                        if metadata_list[name]:
-                            line += name + "=" + metadata_list[name] + "\t"
-            line += "\n"
-            f.write(line)
-    f.close()
+metadata_namelist = ("title", "lineCount", "lastModified", "idxlastModified")
+recorded_data = ("id", "title", "lineCount", "lastModified", "idxlastModified")
+int_data = ("lineCount", "idxlastModified")
+
+def _empty_generator():
+    for name in metadata_namelist:
+        if name in int_data:
+            yield name, 0
+        else:
+            yield name, ""
+
+default_dict = dict([pair for pair in _empty_generator()])
+
+def _tabbed_to_dict(tabbed):
+
+    def adjust_type(key, value):
+        if key in int_data:
+            try:
+                value = int(value)
+            except:
+                value = 0
+        return key, value
+
+    iterable = misc.tabbed_to_dict_generator(tabbed)
+    iterable = misc.unpack_ifilter(lambda k, v: k in recorded_data, iterable)
+    iterable = itertools.starmap(adjust_type, iterable)
+
+    dic = default_dict.copy()
+    for key, value in iterable:
+        dic[key] = value
+
+    return dic
+
+def _dict_to_tabbed(dic):
+
+    def validate(name, value):
+        if name in int_data and value > 0:
+            return True
+        elif name not in int_data and value:
+            return True
+        return False
+        
+    iterable = dic.iteritems()
+
+    # save metadata only if its name exists in metadata_namelist
+    iterable = misc.unpack_ifilter(lambda n,v: n in metadata_namelist,iterable)
+
+    # no need to save an empty or invalid field
+    iterable = misc.unpack_ifilter(validate, iterable)
+
+    iterable = itertools.starmap(lambda n,v: "%s=%s\t" % (n, str(v)), iterable)
+
+    line = ""
+    for tabbed in iterable:
+        line += tabbed
+    return line
+
+def do_formatted_to_dict(formatted):
+    dic = _tabbed_to_dict(formatted)
+    if "id" in dic and dic["id"]:
+        return dic
+    
+def formatted_to_dict(iterable):
+    iterable = itertools.imap(do_formatted_to_dict, iterable)
+    iterable = itertools.ifilter(None, iterable)
+    return iterable
+
+def do_dict_to_formatted(thread_id, dic):
+    return "id=%s\t%s\n" % (thread_id, _dict_to_tabbed(dic))
+
+def dict_to_formatted(iterable):
+    return itertools.starmap(do_dict_to_formatted, iterable)