/usr/share/pyshared/kivy/parser.py is in python-kivy 1.7.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | '''
Parser utilities
================
Helper functions used for CSS
'''
__all__ = ('parse_color', 'parse_int', 'parse_float',
'parse_string', 'parse_bool', 'parse_int2',
'parse_float4', 'parse_filename')
import re
from kivy.logger import Logger
from kivy.resources import resource_find
class ColorException(Exception):
pass
def parse_filename(filename):
'''Parse a filename and search for it using `resource_find()`.
If found, the resource path is returned, otherwise return the unmodified
filename (as specified by the caller).'''
filename = parse_string(filename)
result = resource_find(filename)
if result is None:
Logger.error('Resource: unable to find <%s>' % filename)
return result or filename
def color_error(text):
# show warning and return a sane value
Logger.warning(text)
return (0, 0, 0, 1)
def parse_color(text):
'''Parse a string to a kivy color. Supported formats :
* rgb(r, g, b)
* rgba(r, g, b, a)
* aaa
* rrggbb
* #aaa
* #rrggbb
'''
value = [1, 1, 1, 1]
if text.startswith('rgb'):
res = re.match('rgba?\((.*)\)', text)
if res:
try:
# default r/g/b values to 1 if greater than 255 else x/255
value = [1 if int(x) > 255. else (int(x) / 255.)
for x in re.split(',\ ?', res.groups()[0])]
if len(value) < 3:
#in case of invalid input like rgb()/rgb(r)/rgb(r, g)
raise ValueError
except ValueError:
return color_error('ColorParser: Invalid color for %r' % text)
except AttributeError:
return color_error('ColorParser: Invalid color for %r' % text)
else:
return color_error('ColorParser: Invalid color for %r' % text)
if len(value) == 3:
value.append(1.)
elif len(text):
res = text
if text[0] == '#':
res = text[1:]
lres = len(res)
if lres == 3 or lres == 4:
res = ''.join([x + x for x in res])
elif lres != 6 and lres != 8:
#raise ColorException('Invalid color format for %r' % text)
return color_error(
'ColorParser: Invalid color format for %r' % text)
try:
value = [int(res[i:i + 2], 16) / 255.
for i in xrange(0, len(res), 2)]
except ValueError:
return color_error('ColorParser: Invalid color for %r' % text)
if lres == 6:
value.append(1.)
return value
def parse_bool(text):
'''Parse a string to a boolean, ignoring case. "true"/"1" is True,
"false"/"0" is False. Anything else throws an exception.'''
if text.lower() in ('true', '1'):
return True
elif text.lower() in ('false', '0'):
return False
raise Exception('Invalid boolean: %s' % text)
def parse_string(text):
'''Parse a string to a string (remove single and double quotes)'''
if len(text) >= 2 and text[0] in ('"', "'") and text[-1] in ('"', "'"):
text = text[1:-1]
return text.strip()
def parse_int2(text):
'''Parse a string to a list of exactly 2 integers
>>> print parse_int2("12 54")
12, 54
'''
texts = [x for x in text.split(' ') if x.strip() != '']
value = map(parse_int, texts)
if len(value) < 1:
raise Exception('Invalid int2 format: %s' % text)
elif len(value) == 1:
return [value[0], value[0]]
elif len(value) > 2:
raise Exception('Too many values in %s: %s' % (text, str(value)))
return value
def parse_float4(text):
'''Parse a string to a list of exactly 4 floats
>>> parse_float4('54 87. 35 0')
54, 87., 35, 0
'''
texts = [x for x in text.split(' ') if x.strip() != '']
value = map(parse_float, texts)
if len(value) < 1:
raise Exception('Invalid float4 format: %s' % text)
elif len(value) == 1:
return map(lambda x: value[0], range(4))
elif len(value) == 2:
return [value[0], value[1], value[0], value[1]]
elif len(value) == 3:
# ambigous case!
return [value[0], value[1], value[0], value[2]]
elif len(value) > 4:
raise Exception('Too many values in %s' % text)
return value
parse_int = int
parse_float = float
|