OSDN Git Service

Replace PangoLayout with ResLayout.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / board_plugins.py
1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
3 #
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.
8 #
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.
13 #
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
17
18 import os.path
19 from glob import glob
20 import imp
21 import traceback
22 import itertools
23
24 import config
25 from board_plugin_base import BoardPluginBase
26
27 base_path = os.path.join(config.get_config_dir_path(), "scripts")
28 version = (0, 2)
29
30 def generate_board_plugin():
31     plugin_list = []
32     for module_path in glob(os.path.join(base_path, "board_*.py")):
33         module_name, ext = os.path.splitext(os.path.basename(module_path))
34         try:
35             try:
36                 file, filename, description = imp.find_module(
37                     module_name, [base_path])
38                 module = imp.load_module(
39                     module_name, file, filename, description)
40             finally:
41                 if file:
42                     file.close()
43         except ImportError:
44             traceback.print_exc()
45         else:
46             yield module
47
48 def check_compatible(plugin_version):
49     if version[0] > plugin_version[0]:
50         return False
51     elif version[0] < plugin_version[0]:
52         return True
53     else:
54         return version[1] <= plugin_version[1]
55
56 def load_plugin(plugin_module, widget_tree):
57     try:
58         if check_compatible(plugin_module.version):
59             klass = plugin_module.BoardPlugin
60             if issubclass(klass, BoardPluginBase):
61                 klass(widget_tree)
62             else:
63                 print klass, "is not subclass of BoardPluginBase"
64         else:
65             print plugin_module.__file__, "not compatible", \
66                   plugin_module.version
67     except:
68         traceback.print_exc()
69     
70 def load(widget_tree):
71     for i in itertools.imap(lambda e: load_plugin(e, widget_tree),
72                             generate_board_plugin()): -1