Skip to content
This repository
Newer
Older
100644 153 lines (140 sloc) 5.362 kb
759a8b1a »
2010-03-30 Change most config options to be hierarchical
1 import sys
a81ded65 »
2010-04-07 Allow filters to by loaded from modules (not just standalone .py file…
2 import os
f9bf7717 »
2009-10-07 Initial filter code.
3 import logging
2356c1b1 »
2011-03-10 Adds filter support to plugins.
4 import imp
5 import uuid
f9bf7717 »
2009-10-07 Initial filter code.
6
fb99e2b6 »
2011-03-13 Python 2.x is long since deprecated.. let's use Python 3 for real thi…
7 from . import util
f9bf7717 »
2009-10-07 Initial filter code.
8
9 logger = logging.getLogger("blogofile.filter")
10
fb99e2b6 »
2011-03-13 Python 2.x is long since deprecated.. let's use Python 3 for real thi…
11 from .cache import bf
12 from .cache import HierarchicalCache
13 from . import exception
759a8b1a »
2010-03-30 Change most config options to be hierarchical
14
2356c1b1 »
2011-03-10 Adds filter support to plugins.
15 bf.filter = sys.modules['blogofile.filter']
6710fc21 »
2009-12-01 Filters work in blog posts now.
16
4094a784 »
2010-04-09 using the string "None" as the default in filter/controller configs w…
17 default_filter_config = {"name" : None,
18 "description" : None,
19 "author" : None,
20 "url" : None}
b580695a »
2010-04-07 filter modifications
21
f9bf7717 »
2009-10-07 Initial filter code.
22 def run_chain(chain, content):
6710fc21 »
2009-12-01 Filters work in blog posts now.
23 """Run content through a filter chain.
24
25 Works with either a string or a sequence of filters"""
2356c1b1 »
2011-03-10 Adds filter support to plugins.
26 if chain is None:
6710fc21 »
2009-12-01 Filters work in blog posts now.
27 return content
fb99e2b6 »
2011-03-13 Python 2.x is long since deprecated.. let's use Python 3 for real thi…
28 if isinstance(chain, str):
6710fc21 »
2009-12-01 Filters work in blog posts now.
29 chain = parse_chain(chain)
30 for fn in chain:
2356c1b1 »
2011-03-10 Adds filter support to plugins.
31 f = get_filter(fn)
f2cee02f »
2010-09-21 Started tidying/PEP-8 cleanup/Py2.6ing of blogofile source
32 logger.debug("Applying filter: " + fn)
f9bf7717 »
2009-10-07 Initial filter code.
33 content = f.run(content)
f2cee02f »
2010-09-21 Started tidying/PEP-8 cleanup/Py2.6ing of blogofile source
34 logger.debug("Content: " + content)
d867099b »
2009-12-13 Unicode and Path bug fixes
35 return util.force_unicode(content)
f9bf7717 »
2009-10-07 Initial filter code.
36
6710fc21 »
2009-12-01 Filters work in blog posts now.
37 def parse_chain(chain):
38 """Parse a filter chain into a sequence of filters"""
39 parts = []
40 for p in chain.split(","):
41 p = p.strip()
42 if p.lower() == "none":
43 continue
f2cee02f »
2010-09-21 Started tidying/PEP-8 cleanup/Py2.6ing of blogofile source
44 if len(p) > 0:
6710fc21 »
2009-12-01 Filters work in blog posts now.
45 parts.append(p)
46 return parts
47
2356c1b1 »
2011-03-10 Adds filter support to plugins.
48 def preload_filters(namespace=None, directory="_filters"):
49 #Find all the standalone .py files and modules in the directory
50 #specified and load them into namespace specified.
51 if namespace is None:
52 namespace = bf.config.filters
53 if(not os.path.isdir(directory)):
a81ded65 »
2010-04-07 Allow filters to by loaded from modules (not just standalone .py file…
54 return
55 for fn in os.listdir(directory):
56 p = os.path.join(directory,fn)
2356c1b1 »
2011-03-10 Adds filter support to plugins.
57 if (os.path.isfile(p) and fn.endswith(".py")):
58 #Load a single .py file:
59 load_filter(fn[:-3],module_path=p,namespace=namespace)
60 elif os.path.isdir(p) and os.path.isfile(os.path.join(p, "__init__.py")):
61 #Load a package:
62 load_filter(fn,module_path=p,namespace=namespace)
f2cee02f »
2010-09-21 Started tidying/PEP-8 cleanup/Py2.6ing of blogofile source
63
2356c1b1 »
2011-03-10 Adds filter support to plugins.
64 def init_filters(namespace=None):
f2cee02f »
2010-09-21 Started tidying/PEP-8 cleanup/Py2.6ing of blogofile source
65 """Filters have an optional init method that runs before the site is
66 built"""
2356c1b1 »
2011-03-10 Adds filter support to plugins.
67 if namespace is None:
68 namespace=bf.config.filters
fb99e2b6 »
2011-03-13 Python 2.x is long since deprecated.. let's use Python 3 for real thi…
69 for name, filt in list(namespace.items()):
33af5004 »
2011-02-06 Makes sure we don't catch exceptions we didn't want to catch in contr…
70 if "mod" in filt \
71 and type(filt.mod).__name__ == "module"\
72 and not filt.mod.__initialized:
73 try:
74 init_method = filt.mod.init
75 except AttributeError:
5a1e0573 »
2011-02-05 Makes sure filters only get initialized once.
76 filt.mod.__initialized = True
33af5004 »
2011-02-06 Makes sure we don't catch exceptions we didn't want to catch in contr…
77 continue
78 logger.debug("Initializing filter: "+name)
79 init_method()
80 filt.mod.__initialized = True
f2cee02f »
2010-09-21 Started tidying/PEP-8 cleanup/Py2.6ing of blogofile source
81
2356c1b1 »
2011-03-10 Adds filter support to plugins.
82 def get_filter(name, namespace=None):
83 #Return an already loaded filter:
84 if namespace is None:
85 namespace = bf.config.filters
fb99e2b6 »
2011-03-13 Python 2.x is long since deprecated.. let's use Python 3 for real thi…
86 if name in namespace and "mod" in namespace[name]:
2356c1b1 »
2011-03-10 Adds filter support to plugins.
87 logger.debug("Retrieving already loaded filter: " + name)
88 return namespace[name]['mod']
89 else:
90 raise exception.FilterNotLoaded("Filter not loaded: {0}".format(name))
91
92 def load_filter(name, module_path, namespace=None):
a81ded65 »
2010-04-07 Allow filters to by loaded from modules (not just standalone .py file…
93 """Load a filter from the site's _filters directory"""
b6c7e49f »
2010-04-08 Adds filter and controller init hooks
94 #Don't generate pyc files in the _filters directory
95 #Reset the original sys.dont_write_bytecode setting where we're done
2356c1b1 »
2011-03-10 Adds filter support to plugins.
96 if namespace is None:
97 namespace = bf.config.filters
b6c7e49f »
2010-04-08 Adds filter and controller init hooks
98 try:
99 initial_dont_write_bytecode = sys.dont_write_bytecode
100 except KeyError:
101 initial_dont_write_bytecode = False
f9bf7717 »
2009-10-07 Initial filter code.
102 try:
2356c1b1 »
2011-03-10 Adds filter support to plugins.
103 #Don't generate .pyc files in the _filters directory
104 sys.dont_write_bytecode = True
105 if module_path.endswith(".py"):
106 mod = imp.load_source("{0}_{1}".format(name,uuid.uuid4()), module_path)
107 else:
108 mod = imp.load_package("{0}_{1}".format(name,uuid.uuid4()), module_path)
109 logger.debug("Loaded filter for first time: {0}".format(module_path))
110 mod.__initialized = False
111 #Overwrite anything currently in this namespace:
9bb3268b »
2011-02-05 Fixes issue where _filters directory was getting added to sys.path mu…
112 try:
2356c1b1 »
2011-03-10 Adds filter support to plugins.
113 del namespace[name]
114 except KeyError:
115 pass
116 #If the filter defines it's own configuration, use that as
117 #it's own namespace:
118 if hasattr(mod, "config") and \
119 isinstance(mod.config, HierarchicalCache):
120 namespace[name] = mod.config
121 #Load the module into the namespace
122 namespace[name].mod = mod
123 #If the filter has any aliases, load those as well
124 try:
125 for alias in mod.config['aliases']:
126 namespace[alias] = namespace[name]
127 except:
128 pass
129 #Load the default blogofile config for filters:
fb99e2b6 »
2011-03-13 Python 2.x is long since deprecated.. let's use Python 3 for real thi…
130 for k, v in list(default_filter_config.items()):
2356c1b1 »
2011-03-10 Adds filter support to plugins.
131 namespace[name][k] = v
132 #Load any filter defined defaults:
133 try:
134 filter_config = getattr(mod, "config")
fb99e2b6 »
2011-03-13 Python 2.x is long since deprecated.. let's use Python 3 for real thi…
135 for k, v in list(filter_config.items()):
2356c1b1 »
2011-03-10 Adds filter support to plugins.
136 if "." in k:
137 #This is a hierarchical setting
138 tail = namespace[name]
139 parts = k.split(".")
140 for part in parts[:-1]:
141 tail = tail[part]
142 tail[parts[-1]] = v
143 else:
144 namespace[name][k] = v
145 except AttributeError:
146 pass
147 return mod
148 except:
149 logger.error("Cannot load filter: "+name)
150 raise
151 finally:
152 sys.dont_write_bytecode = initial_dont_write_bytecode
Something went wrong with that request. Please try again.