gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / cssutils / helper.py @ 475
History | View | Annotate | Download (3.89 KB)
1 |
"""cssutils helper TEST
|
---|---|
2 |
"""
|
3 |
__docformat__ = 'restructuredtext'
|
4 |
__version__ = '$Id: errorhandler.py 1234 2008-05-22 20:26:12Z cthedot $'
|
5 |
|
6 |
import os |
7 |
import re |
8 |
import sys |
9 |
import urllib |
10 |
|
11 |
class Deprecated(object): |
12 |
"""This is a decorator which can be used to mark functions
|
13 |
as deprecated. It will result in a warning being emitted
|
14 |
when the function is used.
|
15 |
|
16 |
It accepts a single paramter ``msg`` which is shown with the warning.
|
17 |
It should contain information which function or method to use instead.
|
18 |
"""
|
19 |
def __init__(self, msg): |
20 |
self.msg = msg
|
21 |
|
22 |
def __call__(self, func): |
23 |
def newFunc(*args, **kwargs): |
24 |
import warnings |
25 |
warnings.warn("Call to deprecated method %r. %s" %
|
26 |
(func.__name__, self.msg),
|
27 |
category=DeprecationWarning,
|
28 |
stacklevel=2)
|
29 |
return func(*args, **kwargs)
|
30 |
newFunc.__name__ = func.__name__ |
31 |
newFunc.__doc__ = func.__doc__ |
32 |
newFunc.__dict__.update(func.__dict__) |
33 |
return newFunc
|
34 |
|
35 |
# simple escapes, all non unicodes
|
36 |
_simpleescapes = re.compile(ur'(\\[^0-9a-fA-F])').sub
|
37 |
def normalize(x): |
38 |
"""
|
39 |
normalizes x, namely:
|
40 |
|
41 |
- remove any \ before non unicode sequences (0-9a-zA-Z) so for
|
42 |
x=="c\olor\" return "color" (unicode escape sequences should have
|
43 |
been resolved by the tokenizer already)
|
44 |
- lowercase
|
45 |
"""
|
46 |
if x:
|
47 |
def removeescape(matchobj): |
48 |
return matchobj.group(0)[1:] |
49 |
x = _simpleescapes(removeescape, x) |
50 |
return x.lower()
|
51 |
else:
|
52 |
return x
|
53 |
|
54 |
def path2url(path): |
55 |
"""Return file URL of `path`"""
|
56 |
return u'file:' + urllib.pathname2url(os.path.abspath(path)) |
57 |
|
58 |
def pushtoken(token, tokens): |
59 |
"""Return new generator starting with token followed by all tokens in
|
60 |
``tokens``"""
|
61 |
# TODO: may use itertools.chain?
|
62 |
yield token
|
63 |
for t in tokens: |
64 |
yield t
|
65 |
|
66 |
def string(value): |
67 |
"""
|
68 |
Serialize value with quotes e.g.::
|
69 |
|
70 |
``a \'string`` => ``'a \'string'``
|
71 |
"""
|
72 |
# \n = 0xa, \r = 0xd, \f = 0xc
|
73 |
value = value.replace(u'\n', u'\\a ').replace( |
74 |
u'\r', u'\\d ').replace( |
75 |
u'\f', u'\\c ').replace( |
76 |
u'"', u'\\"') |
77 |
|
78 |
if value.endswith(u'\\'): |
79 |
value = value[:-1] + u'\\\\' |
80 |
|
81 |
return u'"%s"' % value |
82 |
|
83 |
def stringvalue(string): |
84 |
"""
|
85 |
Retrieve actual value of string without quotes. Escaped
|
86 |
quotes inside the value are resolved, e.g.::
|
87 |
|
88 |
``'a \'string'`` => ``a 'string``
|
89 |
"""
|
90 |
return string.replace(u'\\'+string[0], string[0])[1:-1] |
91 |
|
92 |
_match_forbidden_in_uri = re.compile(ur'''.*?[\(\)\s\;,'"]''', re.U).match
|
93 |
def uri(value): |
94 |
"""
|
95 |
Serialize value by adding ``url()`` and with quotes if needed e.g.::
|
96 |
|
97 |
``"`` => ``url("\"")``
|
98 |
"""
|
99 |
if _match_forbidden_in_uri(value):
|
100 |
value = string(value) |
101 |
return u'url(%s)' % value |
102 |
|
103 |
def urivalue(uri): |
104 |
"""
|
105 |
Return actual content without surrounding "url(" and ")"
|
106 |
and removed surrounding quotes too including contained
|
107 |
escapes of quotes, e.g.::
|
108 |
|
109 |
``url("\"")`` => ``"``
|
110 |
"""
|
111 |
uri = uri[uri.find('(')+1:-1].strip() |
112 |
if uri and (uri[0] in '\'"') and (uri[0] == uri[-1]): |
113 |
return stringvalue(uri)
|
114 |
else:
|
115 |
return uri
|
116 |
|
117 |
#def normalnumber(num):
|
118 |
# """
|
119 |
# Return normalized number as string.
|
120 |
# """
|
121 |
# sign = ''
|
122 |
# if num.startswith('-'):
|
123 |
# sign = '-'
|
124 |
# num = num[1:]
|
125 |
# elif num.startswith('+'):
|
126 |
# num = num[1:]
|
127 |
#
|
128 |
# if float(num) == 0.0:
|
129 |
# return '0'
|
130 |
# else:
|
131 |
# if num.find('.') == -1:
|
132 |
# return sign + str(int(num))
|
133 |
# else:
|
134 |
# a, b = num.split('.')
|
135 |
# if not a:
|
136 |
# a = '0'
|
137 |
# return '%s%s.%s' % (sign, int(a), b)
|