Mercurial > hg > decoupage
comparison decoupage/formatters.py @ 68:4094bee13154
quick cleanup
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 03 Aug 2012 23:46:29 -0700 |
parents | fdb77c57bd22 |
children | 262fb90a54b4 |
comparison
equal
deleted
inserted
replaced
67:fdb77c57bd22 | 68:4094bee13154 |
---|---|
7 | 7 |
8 ### abstract base classes for formatters | 8 ### abstract base classes for formatters |
9 | 9 |
10 class FormatterBase(object): | 10 class FormatterBase(object): |
11 """ | 11 """ |
12 abstract base class if you want to use __init__ methods | 12 abstract base class if you want to use __init__ methods |
13 in the form of | 13 in the form of |
14 'arg1, arg2, arg3, kw1=foo, kw2=bar, kw3=baz | 14 'arg1, arg2, arg3, kw1=foo, kw2=bar, kw3=baz |
15 """ | 15 """ |
16 | 16 |
17 defaults = {} # default values for attrs to be set on the instance | 17 defaults = {} # default values for attrs to be set on the instance |
18 | 18 |
19 def __init__(self, string): | 19 def __init__(self, string): |
20 args = [ i.strip() for i in string.split(',')] | 20 args = [ i.strip() for i in string.split(',')] |
21 for index, arg in enumerate(args): | 21 for index, arg in enumerate(args): |
22 if '=' in arg: | 22 if '=' in arg: |
23 break | 23 break |
35 | 35 |
36 ### formatters | 36 ### formatters |
37 | 37 |
38 class Ignore(object): | 38 class Ignore(object): |
39 """ | 39 """ |
40 ignore files of a glob patterns. | 40 ignore files of a glob patterns. |
41 These files will not be linked to in the template. | 41 These files will not be linked to in the template. |
42 e.g. /ignore = .* *.pdf # don't list dotfiles and PDFs | 42 e.g. /ignore = .* *.pdf # don't list dotfiles and PDFs |
43 """ | 43 """ |
44 | 44 |
45 def __init__(self, ignore): | 45 def __init__(self, ignore): |
56 data['files'] = _files | 56 data['files'] = _files |
57 | 57 |
58 | 58 |
59 class All(object): | 59 class All(object): |
60 """ | 60 """ |
61 only pass files of a certain pattern; | 61 only pass files of a certain pattern; |
62 the inverse of ignore | 62 the inverse of ignore |
63 calling all with no arguments means only files with descriptions are used | 63 calling all with no arguments means only files with descriptions are used |
64 """ | 64 """ |
65 | 65 |
66 def __init__(self, pattern): | 66 def __init__(self, pattern): |
67 self.match = pattern.split() | 67 self.match = pattern.split() |
68 | 68 |
69 def __call__(self, request, data): | 69 def __call__(self, request, data): |
70 _files = [] | 70 _files = [] |
77 else: | 77 else: |
78 # use only files where the description is not None | 78 # use only files where the description is not None |
79 if f['description'] is not None: | 79 if f['description'] is not None: |
80 _files.append(f) | 80 _files.append(f) |
81 data['files'] = _files | 81 data['files'] = _files |
82 | 82 |
83 class Sort(FormatterBase): | 83 class Sort(FormatterBase): |
84 """ | 84 """ |
85 determines how to sort the files in a directory; | 85 determines how to sort the files in a directory; |
86 right now only by case-insensitive alphabetically | 86 right now only by case-insensitive alphabetically |
87 * reverse : reverse the order of the sorting | 87 * reverse : reverse the order of the sorting |
88 """ | 88 """ |
89 defaults = {'order': 'name'} | 89 defaults = {'order': 'name'} |
90 | 90 |
91 def __init__(self, pattern): | 91 def __init__(self, pattern): |
92 FormatterBase.__init__(self, pattern) | 92 FormatterBase.__init__(self, pattern) |
93 self.orders = {'name': self.name, | 93 self.orders = {'name': self.name, |
94 'random': self.random, | 94 'random': self.random, |
95 } | 95 } |
96 | 96 # TODO: date |
97 | |
97 def __call__(self, request, data): | 98 def __call__(self, request, data): |
98 | 99 |
99 sort = self.orders.get(request.GET.get('order', self.order), self.name) | 100 sort = self.orders.get(request.GET.get('order', self.order), self.name) |
100 data['files'] = sort(data['files']) | 101 data['files'] = sort(data['files']) |
101 | 102 |
119 key, value = pattern.split('=', 1) | 120 key, value = pattern.split('=', 1) |
120 assert key == 'file' | 121 assert key == 'file' |
121 self.file = value | 122 self.file = value |
122 else: | 123 else: |
123 self.order = [i.strip() for i in pattern.split(',')] | 124 self.order = [i.strip() for i in pattern.split(',')] |
124 | 125 |
125 def __call__(self, request, data): | 126 def __call__(self, request, data): |
126 | 127 |
127 if self.file: | 128 if self.file: |
128 raise NotImplementedError | 129 raise NotImplementedError |
129 | 130 |
160 f['description'] = description | 161 f['description'] = description |
161 | 162 |
162 | 163 |
163 class TitleDescription(FormatterBase): | 164 class TitleDescription(FormatterBase): |
164 """ | 165 """ |
165 splits a description into a title and a description via a separator in | 166 splits a description into a title and a description via a separator in |
166 the description. The template will now have an additional variable, | 167 the description. The template will now have an additional variable, |
167 'title', per file | 168 'title', per file |
168 Arguments: | 169 Arguments: |
169 * separator: what separator to use (':' by default) | 170 * separator: what separator to use (':' by default) |
170 """ | 171 """ |
171 # XXX what about setting the page title? | 172 # XXX what about setting the page title? |
172 | 173 |
173 defaults = { 'separator': ':' } | 174 defaults = { 'separator': ':' } |
174 | 175 |
175 def __call__(self, request, data): | 176 def __call__(self, request, data): |
176 for f in data['files']: | 177 for f in data['files']: |
177 if f['description'] and self.separator in f['description']: | 178 if f['description'] and self.separator in f['description']: |
178 title, description = f['description'].split(self.separator, 1) | 179 title, description = f['description'].split(self.separator, 1) |
179 title = title.strip() | 180 title = title.strip() |
189 class Links(FormatterBase): | 190 class Links(FormatterBase): |
190 """ | 191 """ |
191 allow list of links per item: | 192 allow list of links per item: |
192 foo.html = description of foo; [PDF]=foo.pdf; [TXT]=foo.txt | 193 foo.html = description of foo; [PDF]=foo.pdf; [TXT]=foo.txt |
193 """ | 194 """ |
194 | 195 |
195 defaults = { 'separator': ';' } | 196 defaults = { 'separator': ';' } |
196 | 197 |
197 def __call__(self, request, data): | 198 def __call__(self, request, data): |
198 for f in data['files']: | 199 for f in data['files']: |
199 if f['description'] and self.separator in f['description']: | 200 if f['description'] and self.separator in f['description']: |
211 /up = .. | 212 /up = .. |
212 """ | 213 """ |
213 | 214 |
214 def __init__(self, arg): | 215 def __init__(self, arg): |
215 self.up = arg.strip() | 216 self.up = arg.strip() |
216 | 217 |
217 def __call__(self, request, data): | 218 def __call__(self, request, data): |
218 path = request.path_info | 219 path = request.path_info |
219 if (path != '/') and self.up: | 220 if (path != '/') and self.up: |
220 data['files'].insert(0, {'path': '..', | 221 data['files'].insert(0, {'path': '..', |
221 'type': 'directory', | 222 'type': 'directory', |
252 def __init__(self, arg): | 253 def __init__(self, arg): |
253 self.include = arg | 254 self.include = arg |
254 def __call__(self, request, data): | 255 def __call__(self, request, data): |
255 data['include'] = self.include | 256 data['include'] = self.include |
256 | 257 |
257 | 258 |
258 def formatters(): | 259 def formatters(): |
259 formatters = {} | 260 formatters = {} |
260 for entry_point in iter_entry_points('decoupage.formatters'): | 261 for entry_point in iter_entry_points('decoupage.formatters'): |
261 try: | 262 try: |
262 formatter = entry_point.load() | 263 formatter = entry_point.load() |