1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 Misc utils for internal use
19 """
20
21 __id__ = __revision__ = "$Id: utils.py 103 2007-07-12 13:10:13Z the.pythy $"
22 __url__ = "$URL: https://pythy.googlecode.com/svn/tags/pytils/0_2_2/pytils/utils.py $"
23
24 import sys
25
26
28 """
29 Provide Unicode from text
30
31 @param stext: text
32 @type stext: C{str}
33
34 @param encoding: encoding if input text
35 @type encoding: C{str}
36
37 @return: C{unicode}
38 """
39 try:
40 utext = str(stext).decode(encoding)
41 except UnicodeDecodeError, err:
42 utext = default % {'error': err, 'value': u""}
43 return utext
44
45
47 """
48 Provide text from Unicode
49
50 @param utext: unicode text
51 @type utext: C{unicode}
52
53 @param encoding: encoding of output text
54 @type encoding: C{str}
55
56 @return: C{str}
57 """
58 try:
59 stext = unicode(utext).encode(encoding)
60 except UnicodeEncodeError, err:
61 stext = default % {'error': err, 'value': ""}
62 return stext
63
64
66 """
67 Return value of variable by it's name
68
69 @param variable_name: name of variable
70 @type variable_name: C{str}
71
72 @param depth: stack depth
73 @type depth: C{int}
74
75 @raise RuntimeError: when unable to fetch variable
76 """
77 try:
78 variable_value = sys._getframe(depth).f_locals[variable_name]
79 except KeyError:
80 raise RuntimeError("Unable to fetch variable %s (depth %d)" % \
81 (variable_name, depth))
82 return variable_value
83
84
86 """
87 Checks type of variable
88
89 @param variable_name: name of variable
90 @type variable_name: C{str}
91
92 @param typ: type checking for
93 @type typ: C{type} or C{tuple} of types
94
95 @return: None when check successful
96
97 @raise TypeError: check failed
98 """
99 variable_value = get_value_by_name(variable_name, 2)
100 if not isinstance(variable_value, typ):
101 raise TypeError("%s must be %s, not %s" % \
102 (variable_name, str(typ), type(variable_value)))
103
104
106 """
107 Checks length of variable's value
108
109 @param variable_name: name of variable
110 @type variable_name: C{str}
111
112 @param length: length checking for
113 @type length: C{int}
114
115 @return: None when check successful
116
117 @raise ValueError: check failed
118 """
119 variable_value = get_value_by_name(variable_name, 2)
120 _length = len(variable_value)
121 if _length != length:
122 raise ValueError("%s's length must be %d, but it %d" % \
123 (variable_name, length, _length))
124
125
127 """
128 Checks if variable is positive
129
130 @param variable_name: name of variable
131 @type variable_name: C{str}
132
133 @return: None when check successful
134
135 @raise ValueError: check failed
136 """
137 variable_value = get_value_by_name(variable_name, 2)
138 if not strict and variable_value < 0:
139 raise ValueError("%s must be positive or zero, not %s" % \
140 (variable_name, str(variable_value)))
141 if strict and variable_value <= 0:
142 raise ValueError("%s must be positive, not %s" % \
143 (variable_name, str(variable_value)))
144
146 """
147 Splits unicode string with separator C{sep},
148 but skips escaped separator.
149
150 @param ustring: string to split
151 @type ustring: C{unicode}
152
153 @param sep: separator (default to u',')
154 @type sep: C{unicode}
155
156 @return: tuple of splitted elements
157 """
158 assert isinstance(ustring, unicode), "uvalue must be unicode, not %s" % type(ustring)
159
160
161
162 ustring_marked = ustring.replace(u'\,', u'\uffff')
163 items = tuple([i.strip().replace(u'\uffff', u',') for i in ustring_marked.split(sep)])
164 return items
165