OSDN Git Service

Use gconf for getting board host name from board id
[fukui-no-namari/fukui-no-namari.git] / src / Hage1 / brdlist.py
index 7c89b50..99b07f2 100644 (file)
 
 import os.path
 import ConfigParser
+import gconf
 
 import config
 
-_config_2ch = ConfigParser.RawConfigParser()
-_config_2ch.add_section("morningcoffee")
-_config_2ch.set("morningcoffee", "host", "ex11.2ch.net")
-_config_2ch.set("morningcoffee", "name", "morningcoffee")
-_config_list = {"2ch": {"config": _config_2ch, "filename": "brdlist.2ch.cfg"}}
+# key: bbs id, value: board dict
+# board dict:: key: board id ,value: attr dict
+# attr dict:: key: attr name, value attr value
+# /apps/hage1/boards
+#   /2ch
+#      /morningcoffee
+#          /host
+#          /name
+#      /ainotane
+#          /host
+#          /name
 
-def get_brdlist_path(bbs):
-    return os.path.join(config.get_config_dir_path(),
-                        _config_list[bbs]["filename"])
+brd_list = None
+key_root = None
 
-def read(bbs):
-    _config_list[bbs]["config"].read(get_brdlist_path(bbs))
+def get(bbs, board, attr):
+    if not bbs or not board or not attr:
+        raise ValueError, "parameter must not be empty"
 
-def get(bbs, board, option):
-    return _config_list[bbs]["config"].get(board, option)
+    if brd_list and \
+       bbs in brd_list and brd_list[bbs] and \
+       board in brd_list[bbs] and brd_list[bbs][board] and \
+       attr in brd_list[bbs][board]:
+        return brd_list[bbs][board][attr]
+    else:
+        raise RuntimeError, \
+              "brdlist.get: "+bbs+"::"+board+"::"+attr+" not found"
+
+def split_key(key):
+    """Returns (bbs, board, attribute)"""
+    global key_root
+
+    if not key:
+        print "no key"
+        return None, None, None
+    if not key.startswith(key_root+"/"):
+        print "invalid key", key
+        return None, None, None
+    key_unpref = key[len(key_root+"/"):]
+    list = key_unpref.split("/", 2)
+    bbs = None
+    board = None
+    attr = None
+    if len(list) > 0:
+        bbs = list[0]
+    if len(list) > 1:
+        board = list[1]
+    if len(list) > 2:
+        attr = list[2]
+    return bbs, board, attr
+
+def remove_attr(bbs, board, attr):
+    if not bbs or not board or not attr:
+        return
+
+    if brd_list and bbs in brd_list and board in brd_list[bbs] \
+       and attr in brd_list[bbs][board]:
+        print "delete", bbs, board, attr
+        del brd_list[bbs][board][attr]
+        if not brd_list[bbs][board]:
+            print "delete", bbs, board
+            del brd_list[bbs][board]
+            if not brd_list[bbs]:
+                print "delete", bbs
+                del brd_list[bbs]
+
+def modify_attr(bbs, board, attr, value):
+    if not bbs or not board or not attr:
+        return
+
+    global brd_list
+
+    if not brd_list:
+        print "create root brd_list"
+        brd_list = {}
+    if bbs not in brd_list or not brd_list[bbs]:
+        print "create", bbs
+        brd_list[bbs] = {}
+    if board not in brd_list[bbs] or not brd_list[bbs][board]:
+        print "create", bbs, board
+        brd_list[bbs][board] = {}
+
+    print "modified", attr, value
+    brd_list[bbs][board][attr] = str(value)
+
+def key_changed_callback(client, cnxn_id, entry, user_data = None):
+    print "notify", cnxn_id, entry.key
+
+    bbs, board, attr = split_key(entry.key)
+    if not bbs or not board or not attr:
+        print "key is not /apps/{appname}/boards/{bbs}/{board}/{attr}"
+        return
+
+    if not entry.value:
+        # entry.key is unset
+        print "unset"
+        remove_attr(bbs, board, attr)
+    elif entry.value.type != gconf.VALUE_STRING:
+        # entry value is invalid type
+        print "invalid type"
+        remove_attr(bbs, board, attr)
+    else:
+        # entry.key is modified, or new
+        value = entry.value.to_string()
+        print "modified or new", value
+        modify_attr(bbs, board, attr, value)
+
+def init():
+    global key_root
+
+    key_root = "/apps/" + config.APPNAME.lower() + "/boards"
+    client = gconf.client_get_default()
+
+    # init
+    print "init brd_list for gconf"
+    for bbs_dir in client.all_dirs(key_root):
+        for board_dir in client.all_dirs(bbs_dir):
+            # host
+            key = board_dir + "/host"
+            value = client.get_string(key)
+            if value:
+                bbs, board, attr = split_key(key)
+                modify_attr(bbs, board, attr, value)
+            # name
+            key = board_dir + "/name"
+            value = client.get_string(key)
+            if value:
+                bbs, board, attr = split_key(key)
+                modify_attr(bbs, board, attr, value)
+
+    # register notification
+    print "register gconf notification"
+    client.add_dir(key_root, gconf.CLIENT_PRELOAD_NONE)
+    client.notify_add(key_root, key_changed_callback)