OSDN Git Service

Seperate restore code to board_states.py.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Sun, 24 Sep 2006 09:22:26 +0000 (18:22 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Sun, 24 Sep 2006 09:22:26 +0000 (18:22 +0900)
src/FukuiNoNamari/board_states.py [new file with mode: 0644]
src/FukuiNoNamari/board_window.py

diff --git a/src/FukuiNoNamari/board_states.py b/src/FukuiNoNamari/board_states.py
new file mode 100644 (file)
index 0000000..0bd40a8
--- /dev/null
@@ -0,0 +1,113 @@
+# Copyright (C) 2006 by Aiwota Programmer
+# aiwotaprog@tetteke.tk
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+import gconf
+import traceback
+import itertools
+
+import misc
+import config
+
+interest_dict = dict([
+    ("columns", (list, str)), 
+    ("widths", (list, int)),
+    ("sort_column", (str, None)),
+    ("sort_reverse", (bool, None)),
+    ("window_height", (int , None)),
+    ("window_width", (int, None)),
+    ("toolbar_visible", (bool, None)),
+    ("statusbar_visible", (bool, None)),
+    ("filterbar_visible", (bool, None))
+    ])
+
+default_states_dict = dict([
+    ("columns", ()),
+    ("widths", ()),
+    ("sort_column", "num"),
+    ("sort_reverse", False),
+    ("window_height", 600),
+    ("window_width", 600),
+    ("toolbar_visible", True),
+    ("statusbar_visible", True),
+    ("filterbar_visible", False)])
+
+def states_file_to_dict_generator(bbs_type):
+    def adjust_type(key, value):
+
+        def to_int(value):
+            try:
+                return int(value)
+            except:
+                return 0
+
+        key_type, key_type_extra = interest_dict[key]
+        if key_type == str:
+            return key, value
+        elif key_type == int:
+            return key, to_int(value)
+        elif key_type == bool:
+            return key, value == "True"
+        elif key_type == list:
+            if key_type_extra == str:
+                return key, value.split(",")
+            elif key_type_extra == int:
+                return key, [to_int(i) for i in value.split(",")]
+            else:
+                print key, "not supported",
+                key_type, key_type_extra, value
+        else:
+            print key, "not supported", key_type, value
+
+    iterable = file(misc.get_board_states_path(bbs_type))
+    iterable = itertools.imap(lambda l: l.rstrip(), iterable)
+    iterable = itertools.imap(misc.split_key_and_value, iterable)
+    iterable = itertools.ifilter(None, iterable)
+    iterable = misc.unpack_ifilter(lambda k,v: k in interest_dict, iterable)
+    iterable = itertools.starmap(adjust_type, iterable)
+    iterable = itertools.ifilter(None, iterable)
+    return iterable
+
+def load_gconf_generator():
+    key_base = config.gconf_app_key_base() + "/board_states"
+    gconf_client = gconf.client_get_default()
+    width = gconf_client.get_int(key_base + "/window_width")
+    if width != 0:
+        yield "window_width", width
+    height = gconf_client.get_int(key_base + "/window_height")
+    if height != 0:
+        yield "window_height", height
+    yield "toolbar_visible", gconf_client.get_bool(key_base + "/toolbar")
+    yield "statusbar_visible", gconf_client.get_bool(
+        key_base + "/statusbar")
+    yield "filterbar_visible", gconf_client.get_bool(
+        key_base + "/filterbar")
+
+def states_file_to_dict(bbs_type):
+    states_dict = default_states_dict.copy()
+
+    iterable = itertools.chain(
+        load_gconf_generator(), states_file_to_dict_generator(bbs_type))
+
+    try:
+        for key, value in iterable:
+            states_dict[key] = value
+    except IOError:
+        pass
+    except:
+        traceback.print_exc()
+
+    return states_dict
index 4245dd7..820ca08 100644 (file)
@@ -40,6 +40,7 @@ import bookmark_list
 import bookmark_window
 import board_plugins
 import board_column
+import board_states
 
 GLADE_FILENAME = "board_window.glade"
 
@@ -269,93 +270,8 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
             traceback.print_exc()
 
     def restore(self):
-        states_dict = dict([
-            ("columns", ()), ("widths", ()), ("sort_column", "num"),
-            ("sort_reverse", False), ("window_height", 600),
-            ("window_width", 600), ("toolbar_visible", True),
-            ("statusbar_visible", True), ("filterbar_visible", False)])
-
-        interest_list = [
-            "columns", "widths", "sort_column", "sort_reverse",
-            "window_height", "window_width", "toolbar_visible",
-            "statusbar_visible", "filterbar_visible"]
-
-        interest_type_list = (
-            (list, str), (list, int), (str, None), (bool, None), (int, None),
-            (int, None), (bool, None), (bool, None), (bool, None))
-
-        def key_value_generator():
-
-            def generate_pair():
-                states_path = misc.get_board_states_path(self.bbs_type)
-                try:
-                    for line in file(states_path):
-                        key_equal_value = line.rstrip()
-                        try:
-                            index = key_equal_value.index("=")
-                        except ValueError:
-                            pass
-                        else:
-                            key = key_equal_value[:index]
-                            if key in interest_list:
-                                value = key_equal_value[index+1:]
-                                yield key, value
-                except IOError:
-                    traceback.print_exc()
-
-            def to_int(value):
-                try:
-                    return int(value)
-                except:
-                    return 0
-
-            for key, value in generate_pair():
-                list_index = interest_list.index(key)
-                key_type, key_type_extra = interest_type_list[list_index]
-                if key_type == str:
-                    yield key, value
-                elif key_type == int:
-                    yield key, to_int(value)
-                elif key_type == bool:
-                    value = value == "True"
-                    yield key, value
-                elif key_type == list:
-                    if key_type_extra == str:
-                        yield key, value.split(",")
-                    elif key_type_extra == int:
-                        yield key, [i for i in itertools.imap(
-                            to_int, value.split(","))]
-                    else:
-                        print key, "not supported",
-                        key_type, key_type_extra, value
-                else:
-                    print key, "not supported", key_type, value
-
-        def load_gconf():
-            key_base = config.gconf_app_key_base() + "/board_states"
-            gconf_client = gconf.client_get_default()
-            width = gconf_client.get_int(key_base + "/window_width")
-            height = gconf_client.get_int(key_base + "/window_height")
-            states_dict["toolbar_visible"] = gconf_client.get_bool(
-                key_base + "/toolbar")
-            states_dict["statusbar_visible"] = gconf_client.get_bool(
-                key_base + "/statusbar")
-            states_dict["filterbar_visible"] = gconf_client.get_bool(
-                key_base + "/filterbar")
-
-            if width != 0:
-                states_dict["window_width"] = width
-            if height != 0:
-                states_dict["window_height"] = height
-
         try:
-            try:
-                load_gconf()
-            except:
-                traceback.print_exc()
-
-            for key, value in key_value_generator():
-                states_dict[key] = value
+            states_dict = board_states.states_file_to_dict(self.bbs_type)
 
             self.treeview.get_model().sort(
                 states_dict["sort_column"], True, states_dict["sort_reverse"])