1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 metadata_namelist = ("title", "lineCount", "lastModified", "idxlastModified")
23 recorded_data = ("id", "title", "lineCount", "lastModified", "idxlastModified")
24 int_data = ("lineCount", "idxlastModified")
26 def _empty_generator():
27 for name in metadata_namelist:
33 default_dict = dict([pair for pair in _empty_generator()])
35 def _tabbed_to_dict(tabbed):
37 def adjust_type(key, value):
45 iterable = misc.tabbed_to_dict_generator(tabbed)
46 iterable = misc.unpack_ifilter(lambda k, v: k in recorded_data, iterable)
47 iterable = itertools.starmap(adjust_type, iterable)
49 dic = default_dict.copy()
50 for key, value in iterable:
55 def _dict_to_tabbed(dic):
57 def validate(name, value):
58 if name in int_data and value > 0:
60 elif name not in int_data and value:
64 iterable = dic.iteritems()
66 # save metadata only if its name exists in metadata_namelist
67 iterable = misc.unpack_ifilter(lambda n,v: n in metadata_namelist,iterable)
69 # no need to save an empty or invalid field
70 iterable = misc.unpack_ifilter(validate, iterable)
72 iterable = itertools.starmap(lambda n,v: "%s=%s\t" % (n, str(v)), iterable)
75 for tabbed in iterable:
79 def do_formatted_to_dict(formatted):
80 dic = _tabbed_to_dict(formatted)
81 if "id" in dic and dic["id"]:
84 def formatted_to_dict(iterable):
85 iterable = itertools.imap(do_formatted_to_dict, iterable)
86 iterable = itertools.ifilter(None, iterable)
89 def do_dict_to_formatted(thread_id, dic):
90 return "id=%s\t%s\n" % (thread_id, _dict_to_tabbed(dic))
92 def dict_to_formatted(iterable):
93 return itertools.starmap(do_dict_to_formatted, iterable)