From f932df138aa7847c95f56e3994e5b939df01429f Mon Sep 17 00:00:00 2001 From: Qua-non Date: Fri, 9 Mar 2012 20:33:37 +0530 Subject: [PATCH] Replace invalid values with sane ones for color and bb codes in color-parser and markup respectively to allow for dynamic input. --- kivy/core/text/markup.py | 7 +++++-- kivy/parser.py | 27 ++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/kivy/core/text/markup.py b/kivy/core/text/markup.py index 4d2e586a1..680fe6ef4 100644 --- a/kivy/core/text/markup.py +++ b/kivy/core/text/markup.py @@ -93,7 +93,7 @@ class MarkupLabel(MarkupLabelBase): self._style_stack[k].append(self.options[k]) def _pop_style(self, k): - if len(self._style_stack[k]) == 0: + if k not in self._style_stack or len(self._style_stack[k]) == 0: Logger.warning('Label: pop style stack without push') return v = self._style_stack[k].pop() @@ -135,7 +135,10 @@ class MarkupLabel(MarkupLabelBase): spop('italic') self.resolve_font_name() elif item[:6] == '[size=': - size = int(item[6:-1]) + try: + size = int(item[6:-1]) + except ValueError: + size = options['font_size'] spush('font_size') options['font_size'] = size elif item == '[/size]': diff --git a/kivy/parser.py b/kivy/parser.py index 24b3a6726..1c645958b 100644 --- a/kivy/parser.py +++ b/kivy/parser.py @@ -29,6 +29,12 @@ def parse_filename(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) @@ -41,7 +47,18 @@ def parse_color(text): value = [1, 1, 1, 1] if text.startswith('rgb'): res = re.match('rgba?\((.*)\)', text) - value = [int(x) / 255. for x in re.split(',\ ?', res.groups()[0])] + 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, AttributeError: + return color_error('Color Parser:Invalid color for %r' % text) + else: + return color_error('Color Parser:Invalid color for %r' % text) if len(value) == 3: value.append(1.) elif len(text): @@ -52,8 +69,12 @@ def parse_color(text): 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) - value = [int(res[i:i+2], 16) / 255. for i in xrange(0, len(res), 2)] + #raise ColorException('Invalid color format for %r' % text) + return color_error('Color Parser: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('Color Parser:Invalid color for %r' % text) if lres == 6: value.append(1.) return value