Mercurial > genshi > genshi-test
annotate genshi/util.py @ 857:24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
author | cmlenz |
---|---|
date | Thu, 12 Nov 2009 15:09:26 +0000 |
parents | 1e2be9fb3348 |
children | fbe34d12acde |
rev | line source |
---|---|
274
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
2 # |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
3 # Copyright (C) 2006-2009 Edgewall Software |
274
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
4 # All rights reserved. |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
5 # |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
6 # This software is licensed as described in the file COPYING, which |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
7 # you should have received as part of this distribution. The terms |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
8 # are also available at http://genshi.edgewall.org/wiki/License. |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
9 # |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
10 # This software consists of voluntary contributions made by many |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
11 # individuals. For the exact contribution history, see the revision |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
12 # history and logs, available at http://genshi.edgewall.org/log/. |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
13 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
14 """Various utility classes and functions.""" |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
15 |
856 | 16 try: |
17 import htmlentitydefs as entities | |
18 except ImportError: | |
19 from html import entities | |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
20 import re |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
21 |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
408
diff
changeset
|
22 __docformat__ = 'restructuredtext en' |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
408
diff
changeset
|
23 |
274
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
24 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
25 class LRUCache(dict): |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
26 """A dictionary-like object that stores only a certain number of items, and |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
27 discards its least recently used item when full. |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
28 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
29 >>> cache = LRUCache(3) |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
30 >>> cache['A'] = 0 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
31 >>> cache['B'] = 1 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
32 >>> cache['C'] = 2 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
33 >>> len(cache) |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
34 3 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
35 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
36 >>> cache['A'] |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
37 0 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
38 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
39 Adding new items to the cache does not increase its size. Instead, the least |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
40 recently used item is dropped: |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
41 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
42 >>> cache['D'] = 3 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
43 >>> len(cache) |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
44 3 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
45 >>> 'B' in cache |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
46 False |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
47 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
48 Iterating over the cache returns the keys, starting with the most recently |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
49 used: |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
50 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
51 >>> for key in cache: |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
52 ... print(key) |
274
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
53 D |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
54 A |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
55 C |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
56 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
57 This code is based on the LRUCache class from ``myghtyutils.util``, written |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
58 by Mike Bayer and released under the MIT license. See: |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
59 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
60 http://svn.myghty.org/myghtyutils/trunk/lib/myghtyutils/util.py |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
61 """ |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
62 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
63 class _Item(object): |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
64 def __init__(self, key, value): |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
65 self.prv = self.nxt = None |
274
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
66 self.key = key |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
67 self.value = value |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
68 def __repr__(self): |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
69 return repr(self.value) |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
70 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
71 def __init__(self, capacity): |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
72 self._dict = dict() |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
73 self.capacity = capacity |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
74 self.head = None |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
75 self.tail = None |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
76 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
77 def __contains__(self, key): |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
78 return key in self._dict |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
79 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
80 def __iter__(self): |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
81 cur = self.head |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
82 while cur: |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
83 yield cur.key |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
84 cur = cur.nxt |
274
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
85 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
86 def __len__(self): |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
87 return len(self._dict) |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
88 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
89 def __getitem__(self, key): |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
90 item = self._dict[key] |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
91 self._update_item(item) |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
92 return item.value |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
93 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
94 def __setitem__(self, key, value): |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
95 item = self._dict.get(key) |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
96 if item is None: |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
97 item = self._Item(key, value) |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
98 self._dict[key] = item |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
99 self._insert_item(item) |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
100 else: |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
101 item.value = value |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
102 self._update_item(item) |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
103 self._manage_size() |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
104 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
105 def __repr__(self): |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
106 return repr(self._dict) |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
107 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
108 def _insert_item(self, item): |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
109 item.prv = None |
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
110 item.nxt = self.head |
274
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
111 if self.head is not None: |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
112 self.head.prv = item |
274
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
113 else: |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
114 self.tail = item |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
115 self.head = item |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
116 self._manage_size() |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
117 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
118 def _manage_size(self): |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
119 while len(self._dict) > self.capacity: |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
120 olditem = self._dict[self.tail.key] |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
121 del self._dict[self.tail.key] |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
122 if self.tail != self.head: |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
123 self.tail = self.tail.prv |
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
124 self.tail.nxt = None |
274
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
125 else: |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
126 self.head = self.tail = None |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
127 |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
128 def _update_item(self, item): |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
129 if self.head == item: |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
130 return |
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
131 |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
132 prv = item.prv |
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
133 prv.nxt = item.nxt |
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
134 if item.nxt is not None: |
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
135 item.nxt.prv = prv |
274
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
136 else: |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
137 self.tail = prv |
274
c5ec3146fcb6
Use an LRU cache for caching parsed templates in the `TemplateLoader`. LRU cache implementation is a simplified version of the `LRUCache` class in [http://www.myghty.org/ Myghty].
cmlenz
parents:
diff
changeset
|
138 |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
139 item.prv = None |
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
140 item.nxt = self.head |
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
141 self.head.prv = self.head = item |
357
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
142 |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
143 |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
144 def flatten(items): |
433 | 145 """Flattens a potentially nested sequence into a flat list. |
146 | |
147 :param items: the sequence to flatten | |
357
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
148 |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
149 >>> flatten((1, 2)) |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
150 [1, 2] |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
151 >>> flatten([1, (2, 3), 4]) |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
152 [1, 2, 3, 4] |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
153 >>> flatten([1, (2, [3, 4]), 5]) |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
154 [1, 2, 3, 4, 5] |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
155 """ |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
156 retval = [] |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
157 for item in items: |
580 | 158 if isinstance(item, (frozenset, list, set, tuple)): |
357
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
159 retval += flatten(item) |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
160 else: |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
161 retval.append(item) |
c5684b65c9b7
Improve the way locals (in list comprehensions, lambdas and generator expressions) are handled in template expressions.
cmlenz
parents:
274
diff
changeset
|
162 return retval |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
163 |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
751
diff
changeset
|
164 |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
165 def plaintext(text, keeplinebreaks=True): |
856 | 166 """Return the text with all entities and tags removed. |
433 | 167 |
168 >>> plaintext('<b>1 < 2</b>') | |
169 u'1 < 2' | |
170 | |
171 The `keeplinebreaks` parameter can be set to ``False`` to replace any line | |
172 breaks by simple spaces: | |
173 | |
174 >>> plaintext('''<b>1 | |
175 ... < | |
176 ... 2</b>''', keeplinebreaks=False) | |
177 u'1 < 2' | |
178 | |
179 :param text: the text to convert to plain text | |
180 :param keeplinebreaks: whether line breaks in the text should be kept intact | |
181 :return: the text with tags and entities removed | |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
182 """ |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
183 text = stripentities(striptags(text)) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
184 if not keeplinebreaks: |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
751
diff
changeset
|
185 text = text.replace('\n', ' ') |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
186 return text |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
187 |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
751
diff
changeset
|
188 |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
189 _STRIPENTITIES_RE = re.compile(r'&(?:#((?:\d+)|(?:[xX][0-9a-fA-F]+));?|(\w+);)') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
190 def stripentities(text, keepxmlentities=False): |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
191 """Return a copy of the given text with any character or numeric entities |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
192 replaced by the equivalent UTF-8 characters. |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
193 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
194 >>> stripentities('1 < 2') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
195 u'1 < 2' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
196 >>> stripentities('more …') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
197 u'more \u2026' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
198 >>> stripentities('…') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
199 u'\u2026' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
200 >>> stripentities('…') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
201 u'\u2026' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
202 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
203 If the `keepxmlentities` parameter is provided and is a truth value, the |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
204 core XML entities (&, ', >, < and ") are left intact. |
750 | 205 |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
206 >>> stripentities('1 < 2 …', keepxmlentities=True) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
207 u'1 < 2 \u2026' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
208 """ |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
209 def _replace_entity(match): |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
210 if match.group(1): # numeric entity |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
211 ref = match.group(1) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
212 if ref.startswith('x'): |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
213 ref = int(ref[1:], 16) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
214 else: |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
215 ref = int(ref, 10) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
216 return unichr(ref) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
217 else: # character entity |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
218 ref = match.group(2) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
219 if keepxmlentities and ref in ('amp', 'apos', 'gt', 'lt', 'quot'): |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
751
diff
changeset
|
220 return '&%s;' % ref |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
221 try: |
856 | 222 return unichr(entities.name2codepoint[ref]) |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
223 except KeyError: |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
224 if keepxmlentities: |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
751
diff
changeset
|
225 return '&%s;' % ref |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
226 else: |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
227 return ref |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
228 return _STRIPENTITIES_RE.sub(_replace_entity, text) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
229 |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
751
diff
changeset
|
230 |
655
b090bf737111
The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
cmlenz
parents:
580
diff
changeset
|
231 _STRIPTAGS_RE = re.compile(r'(<!--.*?-->|<[^>]*>)') |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
232 def striptags(text): |
433 | 233 """Return a copy of the text with any XML/HTML tags removed. |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
234 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
235 >>> striptags('<span>Foo</span> bar') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
236 'Foo bar' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
237 >>> striptags('<span class="bar">Foo</span>') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
238 'Foo' |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
239 >>> striptags('Foo<br />') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
240 'Foo' |
433 | 241 |
655
b090bf737111
The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
cmlenz
parents:
580
diff
changeset
|
242 HTML/XML comments are stripped, too: |
b090bf737111
The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
cmlenz
parents:
580
diff
changeset
|
243 |
b090bf737111
The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
cmlenz
parents:
580
diff
changeset
|
244 >>> striptags('<!-- <blub>hehe</blah> -->test') |
b090bf737111
The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
cmlenz
parents:
580
diff
changeset
|
245 'test' |
b090bf737111
The `striptags` function now also removes HTML/XML-style comments. Closes #150. Thanks to Armin Ronacher for the report and suggested fix.
cmlenz
parents:
580
diff
changeset
|
246 |
433 | 247 :param text: the string to remove tags from |
248 :return: the text with tags removed | |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
249 """ |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
357
diff
changeset
|
250 return _STRIPTAGS_RE.sub('', text) |
856 | 251 |
252 | |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
253 def stringrepr(string): |
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
254 slen = len(string) |
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
255 ascii = string.encode('ascii', 'backslashreplace') |
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
256 r = "'" + ascii.replace("'", "\\'") + "'" |
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
257 if (unicode is not str) and (len(ascii) > len(string)): |
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
258 return 'u' + r |
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
259 return r |
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
260 |
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
261 |
856 | 262 # Compatibility fallback implementations for older Python versions |
263 | |
264 try: | |
265 all = all | |
266 any = any | |
267 except NameError: | |
268 def any(S): | |
269 for x in S: | |
270 if x: | |
271 return True | |
272 return False | |
273 | |
274 def all(S): | |
275 for x in S: | |
276 if not x: | |
277 return False | |
278 return True |