/usr/share/pyshared/pymt/parser.py is in python-pymt 0.5.1-0ubuntu3.
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 | '''
Parser: default parser from string to special type
Used specially for CSS
'''
__all__ = ('parse_image', 'parse_color', 'parse_int', 'parse_float',
'parse_string', 'parse_bool', 'parse_int2',
'parse_float4')
import re
from pymt.core.image import Image
from pymt.core.svg import Svg
def parse_image(filename):
'''Parse a filename to load an image ro svg'''
filename = parse_string(filename)
if filename in (None, 'None', u'None'):
return None
if filename.endswith('.svg'):
return Svg(filename)
else:
return Image(filename)
raise Exception('Error trying to load image specified in css: %s' \
% filename)
def parse_color(text):
'''Parse a text color to a pymt color. Format supported are :
* rgb(r, g, b)
* rgba(r, g, b, a)
* #aaa
* #rrggbb
'''
value = [1, 1, 1, 1]
if text.startswith('rgb'):
res = re.match('rgba?\((.*)\)', text)
value = map(lambda x: int(x) / 255., re.split(',\ ?', res.groups()[0]))
if len(value) == 3:
value.append(1.)
elif text.startswith('#'):
res = text[1:]
if len(res) == 3:
res = ''.join(map(lambda x: x+x, res))
value = [int(x, 16) / 255. for x in re.split(
'([0-9a-f]{2})', res) if x != '']
if len(value) == 3:
value.append(1.)
return value
def parse_bool(text):
'''Parse a string to a boolean'''
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 quotes 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 integer with exactly 2 number
>>> 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 format int2 for %s' % text)
elif len(value) == 1:
return [value[0], value[0]]
elif len(value) > 2:
raise Exception('Too much value in %s : %s' % (text, str(value)))
return value
def parse_float4(text):
'''Parse a string to a float with 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 format float4 for %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 much value in %s' % text)
return value
parse_int = int
parse_float = float
|