Functions | |
| def | normalize |
| def | replaceWord |
| def | make_qoid |
| def | genId |
| def | replace_lower_accents |
| def | replace_upper_accents |
| def | replaceAccents |
| def | __cartesianProduct |
| def | getAllPermutations |
| def | listflatten |
| def | decode |
| def | decode_if_string |
| def | decode_utf8_if_string |
| def | encode |
| def | encode_if_unicode |
| def | filesize |
| def | datetime_from_iso |
| def | find_file_matching |
| def | file_id |
| def | mime_from_name |
| def | ext_from_mime |
| def | parse_version |
| def | is_version_newer |
| def | build_matching_set |
| def | shift_exc_info |
| def | exec_bg |
Variables | |
| list | __lower_accents |
| list | __upper_accents = [] |
| list | version_sub = [ 'alpha', 'beta', 'pre', 'rc', 'r' ] |
| def py::qo::utils::__cartesianProduct | ( | pivot, | ||
| l | ||||
| ) | [private] |
00083 : 00084 def prepend_pivot( sl ) : return [ pivot ] + sl 00085 return map( prepend_pivot, l ) 00086 def getAllPermutations( elts, max = 4 ) :
| def py::qo::utils::build_matching_set | ( | value | ) |
00259 : 00260 return set(re.sub("[^a-z0-9]", " ", replace_lower_accents(decode_if_string(value).lower())).split()) 00261 00262 00263 # 00264 # Python helpers 00265 # 00266 def shift_exc_info( exc_info, shift=1 ) :
| def py::qo::utils::datetime_from_iso | ( | iso | ) |
Convert a string representing a datetime in isoformat (YYYY-MM-DD hh:mm:ss) to a datetime object
00166 : 00167 """ 00168 Convert a string representing a datetime in isoformat (YYYY-MM-DD hh:mm:ss) to a datetime object 00169 """ 00170 d = re.match( "^(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})$", iso ) 00171 if d is not None : 00172 return datetime.datetime( *[ int(x) for x in d.groups() ] ) 00173 return None 00174 def find_file_matching( basedir, regexp ) :
| def py::qo::utils::decode | ( | s, | ||
error = 'replace' | ||||
| ) |
Returns the unicode representation of given locale encoded string s
00108 : 00109 """ 00110 Returns the unicode representation of given locale encoded string s 00111 """ 00112 return s.decode(locale.getpreferredencoding(), error) 00113 def decode_if_string( value, error='replace' ) :
| def py::qo::utils::decode_if_string | ( | value, | ||
error = 'replace' | ||||
| ) |
If value is decodable (is a string), decode it
00114 : 00115 """ 00116 If value is decodable (is a string), decode it 00117 """ 00118 if isinstance(value, str) : 00119 return decode(value, error) 00120 return value 00121 def decode_utf8_if_string( value, error='replace' ) :
| def py::qo::utils::decode_utf8_if_string | ( | value, | ||
error = 'replace' | ||||
| ) |
If value is decodable (is a string), decode it from utf-8
00122 : 00123 """ 00124 If value is decodable (is a string), decode it from utf-8 00125 """ 00126 if isinstance(value, str) : 00127 return value.decode('utf-8', error) 00128 return value 00129 def encode( s ) :
| def py::qo::utils::encode | ( | s | ) |
Returns the locale encoded representation of given unicode string s
00130 : 00131 """ 00132 Returns the locale encoded representation of given unicode string s 00133 """ 00134 try : 00135 return codecs.getencoder( locale.getpreferredencoding() )( s, 'replace' )[ 0 ] 00136 except : 00137 traceback.print_exc() 00138 return s 00139 def encode_if_unicode( value, error='replace' ) :
| def py::qo::utils::encode_if_unicode | ( | value, | ||
error = 'replace' | ||||
| ) |
If value is unicode, encode it
00140 : 00141 """ 00142 If value is unicode, encode it 00143 """ 00144 if isinstance(value, unicode) : 00145 return value.encode(locale.getpreferredencoding(), error) 00146 return value 00147 def filesize( size ) :
| def py::qo::utils::exec_bg | ( | commandline | ) |
| def py::qo::utils::ext_from_mime | ( | mime | ) |
| def py::qo::utils::file_id | ( | path | ) |
00181 : 00182 return ".".join(os.path.basename(path).split('.')[:-1]) 00183 def mime_from_name( name ) :
| def py::qo::utils::filesize | ( | size | ) |
Returns a human readable filesize given in bytes
00148 : 00149 """ 00150 Returns a human readable filesize given in bytes 00151 """ 00152 units = [ "o", "Ko", "Mo", "Go" ] 00153 unit = units.pop( 0 ) 00154 for x in units : 00155 if size >= 1024 : 00156 size = size / 1024 00157 unit = x 00158 else : 00159 break 00160 if unit not in [ "o", "Ko" ] : 00161 size = round( size, 1 ) 00162 else : 00163 size = int( size ) 00164 return str( size ) + unit 00165 def datetime_from_iso( iso ) :
| def py::qo::utils::find_file_matching | ( | basedir, | ||
| regexp | ||||
| ) |
00175 : 00176 m = re.compile( regexp ) 00177 def file_matching( x ) : 00178 return os.path.isfile( os.path.join( basedir, x ) ) and m.match( x ) is not None 00179 return filter( file_matching, os.listdir( basedir ) ) 00180 def file_id( path ) :
| def py::qo::utils::genId | ( | s, | ||
replace_accent = False | ||||
| ) |
| def py::qo::utils::getAllPermutations | ( | elts, | ||
max = 4 | ||||
| ) |
Get all permutations for the list elts. ( len(elts) must be <= max parameter )
00087 : 00088 """ 00089 Get all permutations for the list elts. ( len(elts) must be <= max parameter ) 00090 """ 00091 if len( elts ) > max : return [ elts ] # complexity is high! 00092 done = [] 00093 for pivot in elts : 00094 def remove_pivot( x ) : return x != pivot 00095 done += __cartesianProduct( pivot, getAllPermutations( filter( remove_pivot, elts ), max ) ) 00096 if len( done ) == 0 : done.append( [] ) 00097 return done 00098 def listflatten( l ) :
| def py::qo::utils::is_version_newer | ( | test, | ||
| ref | ||||
| ) |
return True if test is strictly newer than ref
00223 : 00224 """return True if test is strictly newer than ref""" 00225 00226 def __is_version_newer( cur, new ) : 00227 ( v1, v2 ) = ( cur[ 0 ], new[ 0 ] ) 00228 i = 0 00229 while i < len(v1) and i < len(v2) : 00230 if v1[ i ] > v2[ i ] : return False 00231 if v1[ i ] < v2[ i ] : return True 00232 i = i + 1 00233 if len(v1) > len(v2 ) : return False 00234 if len(v1) < len(v2 ) : return True 00235 00236 # test for alpha, beta, ... 00237 if cur[ 1 ] > new[ 1 ] : return False 00238 if cur[ 1 ] < new[ 1 ] : return True 00239 if cur[ 2 ] > new[ 2 ] : return False 00240 if cur[ 2 ] < new[ 2 ] : return True 00241 return False 00242 00243 ref = ref.split( ',' ) 00244 if len( ref ) == 1 : ref.append( "0" ) 00245 test = test.split( ',' ) 00246 if len( test ) == 1 : test.append( "0" ) 00247 00248 ref_major = parse_version( ref[ 0 ] ) 00249 test_major = parse_version( test[ 0 ] ) 00250 ref_minor = parse_version( ref[ 1 ] ) 00251 test_minor = parse_version( test[ 1 ] ) 00252 00253 if __is_version_newer( ref_major, test_major ) : return True 00254 if ref_major == test_major and __is_version_newer( ref_minor, test_minor ) : return True 00255 00256 return False 00257 00258 def build_matching_set( value ) :
| def py::qo::utils::listflatten | ( | l | ) |
[[a, b], [c, d]] -> [a, b, c, d]
00099 : 00100 """ 00101 [[a, b], [c, d]] -> [a, b, c, d] 00102 """ 00103 def extend(a, b) : 00104 a.extend(b) 00105 return a 00106 return reduce(lambda a, b : extend(a, b), l, []) 00107 def decode( s, error='replace' ) :
| def py::qo::utils::make_qoid | ( | value, | ||
replace_accents = True | ||||
| ) |
Convert @param value to a qoid value
00041 : 00042 """ 00043 Convert @param value to a qoid value 00044 """ 00045 value = value.upper() 00046 if replace_accents : 00047 value = replace_upper_accents(value) 00048 return re.sub("[^A-Z0-9]", "", value) 00049 00050 def genId( s, replace_accent = False ) :
| def py::qo::utils::mime_from_name | ( | name | ) |
00184 : 00185 ext = name.lower().split('.') 00186 if ext[-2] in [ 'tar' ] and ext[-1] in [ 'bz2' ] or ext[-1] in [ 'tbz2' ] : 00187 return "application/x-tbz" 00188 elif ext[-1] in ['bz2'] : 00189 return "application/x-bzip2" 00190 else : 00191 return "text/plain" 00192 def ext_from_mime( mime ) :
| def py::qo::utils::normalize | ( | s | ) |
Normalize spaces in the string s
00029 : 00030 """ 00031 Normalize spaces in the string s 00032 """ 00033 return re.sub( " \\s+", " ", s.strip() ) 00034 def replaceWord( s, orig, new ) :
| def py::qo::utils::parse_version | ( | version, | ||
show_sub = False | ||||
| ) |
00203 : 00204 def int0( value ) : 00205 if value == "" or value == u"" : 00206 return 0 00207 return int(value) 00208 def make_version( num, sub, subnum ) : 00209 return (tuple(map(int0, num.split('.'))), show_sub and sub or version_sub.index(sub), subnum) 00210 has_sub = False 00211 for t in version_sub : 00212 _version = version.split(t) 00213 if len(_version) == 2 : 00214 version = make_version(_version[0], t, _version[1]) 00215 has_sub = True 00216 break 00217 if not has_sub : # assume r0 00218 version = make_version(version, "r", "0") 00219 00220 return version 00221 00222 def is_version_newer( test, ref ) :
| def py::qo::utils::replace_lower_accents | ( | s | ) |
00069 : 00070 for ( a, l ) in __lower_accents : 00071 s = re.sub( "[%s]" % a, l, s ) 00072 return s def replace_upper_accents( value ) :
| def py::qo::utils::replace_upper_accents | ( | value | ) |
00073 : 00074 for a, l in __upper_accents : 00075 value = re.sub("[%s]"%a, l, value) 00076 return value 00077 def replaceAccents( s ) :
| def py::qo::utils::replaceAccents | ( | s | ) |
00078 : 00079 for (a,l) in __lower_accents + __upper_accents : 00080 s = re.sub( "[%s]" % a, l, s ) 00081 return s 00082 def __cartesianProduct( pivot, l ) :
| def py::qo::utils::replaceWord | ( | s, | ||
| orig, | ||||
| new | ||||
| ) |
Replace the word orig by new in string s
00035 : 00036 """ 00037 Replace the word orig by new in string s 00038 """ 00039 return re.sub( "\\b"+orig+"\\b", new, s ) 00040 def make_qoid( value, replace_accents = True ) :
| def py::qo::utils::shift_exc_info | ( | exc_info, | ||
shift = 1 | ||||
| ) |
remove @param shift traceback from exc_info
00267 : 00268 """ 00269 remove @param shift traceback from exc_info 00270 """ 00271 exc_info = list(exc_info) 00272 for i in range(shift) : 00273 exc_info[2] = exc_info[2].tb_next 00274 return tuple(exc_info) 00275 def exec_bg( commandline ) :
list py::qo::utils::__lower_accents [static] |
Initial value:
[
(u"àäâ", 'a' ),
(u"ç", 'c' ),
(u"éèêë", 'e' ),
(u"ïî", 'i' ),
(u"ñ", "n"),
]
list py::qo::utils::__upper_accents = [] [static] |
list py::qo::utils::version_sub = [ 'alpha', 'beta', 'pre', 'rc', 'r' ] [static] |
1.5.3