diff --git a/kivy/parser.py b/kivy/parser.py index 49fa5a890..57bc894a0 100644 --- a/kivy/parser.py +++ b/kivy/parser.py @@ -2,7 +2,7 @@ Parser utilities ================ -Used specially for CSS +Helper functions used for CSS ''' __all__ = ('parse_image', 'parse_color', 'parse_int', 'parse_float', @@ -17,9 +17,9 @@ from kivy.core.svg import Svg def parse_filename(filename): - '''Parse a filename, and search inside resource if exist. - If we haven't found it, just return the name. - ''' + '''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: @@ -28,11 +28,12 @@ def parse_filename(filename): def parse_image(filename): - '''Parse a filename to load an image ro svg''' + '''Parse a filename and load it in a Widget (Image or Svg). + Svg is used only if the file extension is '.svg', otherwise Image.''' filename = parse_filename(filename) if filename in (None, 'None', u'None'): return None - if filename.endswith('.svg'): + if filename.lower().endswith('.svg'): return Svg(filename) else: return Image(filename) @@ -41,7 +42,7 @@ def parse_image(filename): def parse_color(text): - '''Parse a text color to a kivy color. Format supported are : + '''Parse a string to a kivy color. Supported formats : * rgb(r, g, b) * rgba(r, g, b, a) * #aaa @@ -58,14 +59,15 @@ def parse_color(text): 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 != ''] + '(?i)([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''' + '''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'): @@ -74,14 +76,14 @@ def parse_bool(text): def parse_string(text): - '''Parse a string to a string (remove quotes and double-quotes)''' + '''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 integer with exactly 2 number + '''Parse a string to a list of exactly 2 integers >>> print parse_int2("12 54") 12, 54 @@ -90,16 +92,16 @@ def parse_int2(text): 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) + raise Exception('Invalid int2 format: %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))) + raise Exception('Too many values in %s: %s' % (text, str(value))) return value def parse_float4(text): - '''Parse a string to a float with exactly 4 floats + '''Parse a string to a list of exactly 4 floats >>> parse_float4('54 87. 35 0') 54, 87., 35, 0 @@ -108,7 +110,7 @@ def parse_float4(text): 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) + raise Exception('Invalid float4 format: %s' % text) elif len(value) == 1: return map(lambda x: value[0], range(4)) elif len(value) == 2: @@ -117,7 +119,7 @@ def parse_float4(text): # ambigous case! return [value[0], value[1], value[0], value[2]] elif len(value) > 4: - raise Exception('Too much value in %s' % text) + raise Exception('Too many values in %s' % text) return value parse_int = int