Package lib :: Module plugin
[hide private]
[frames] | no frames]

Source Code for Module lib.plugin

 1  # -*- coding: utf-8 -*- 
 2   
 3  #    callirhoe - high quality calendar rendering 
 4  #    Copyright (C) 2012-2014 George M. Tzoumas 
 5   
 6  #    This program is free software: you can redistribute it and/or modify 
 7  #    it under the terms of the GNU General Public License as published by 
 8  #    the Free Software Foundation, either version 3 of the License, or 
 9  #    (at your option) any later version. 
10  # 
11  #    This program is distributed in the hope that it will be useful, 
12  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  #    GNU General Public License for more details. 
15  # 
16  #    You should have received a copy of the GNU General Public License 
17  #    along with this program.  If not, see http://www.gnu.org/licenses/ 
18   
19  # ***************************************** 
20  #                                         # 
21  """        plugin handling routines     """ 
22  #                                         # 
23  # ***************************************** 
24   
25  import sys 
26  import os.path 
27  import glob 
28   
29  try: 
30      import resources 
31  except: 
32      resources = None 
33   
34 -def available_files(parent, dir, fmatch = None):
35 """find parent/dir/*.py files to be used for plugins 36 37 @rtype: [str,...] 38 @note: 39 1. __init__.py should exist 40 2. files starting with underscore are ignored 41 3. if fnmatch is defined (base name), it matches a single file 42 """ 43 good = False 44 res = [] 45 pattern = parent + "/" + dir + "/*.py" 46 for x in glob.glob(pattern) if not parent.startswith('resource:') else resources.resource_list[dir]: 47 basex = os.path.basename(x) 48 if basex == "__init__.py": good = True 49 elif basex.startswith('_'): 50 # ignore files aimed for internal use 51 # safer than [a-z]-style matching... 52 continue 53 else: 54 base = os.path.splitext(basex)[0] 55 if base and ((not fmatch) or (fmatch == base)): res.append((base,parent)) 56 return res if good else []
57
58 -def plugin_list(cat):
59 """return a sequence of available plugins, using L{available_files()} and L{get_plugin_paths()} 60 61 @rtype: [str,...] 62 """ 63 plugin_paths = get_plugin_paths() 64 result = [] 65 for path in plugin_paths: 66 result += available_files(path, cat) 67 return result
68 69 # cat = lang (category) 70 # longcat = language 71 # longcat2 = languages 72 # listopt = --list-lang 73 # preset = "EN" 74
75 -def get_plugin_paths():
76 """return the plugin search paths 77 78 @rtype: [str,str,..] 79 """ 80 result = [ os.path.expanduser("~/.callirhoe"), sys.path[0] if sys.path[0] else "." ] 81 if resources: 82 result.append("resource:") 83 return result
84