1. Victor Kotseruba
  2. python-sass

Commits

Victor Kotseruba  committed 739a8cf

sass.py splited

  • Participants
  • Parent commits 9a560af
  • Branches default

Comments (0)

Files changed (5)

File color.py

View file
+# -*- coding: utf-8 -*-
+
+import re
+
+class Color(object):
+
+    full_color_re = re.compile(r'^#[\da-f]{6}$', re.I)
+    compact_color_re = re.compile(r'^#[\da-f]{3}$', re.I)
+    rgb_color_re = re.compile(r'^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$', re.I)
+    
+    def __init__(self, color):
+        self.color = color
+        self._full_color = None
+        self._compact_color = None
+        self._rgb_color = None
+        if self.full_color_re.match(color):
+            self._full_color = color.upper()
+        elif self.compact_color_re.match(color):
+            self._compact_color = color.lower()
+        elif self.rgb_color_re.match(color):
+            _parts = []
+            for part in map(int, self.rgb_color_re.findall(self.color)[0]):
+                part = hex(part)[2:].upper()
+                if len(part) == 1:
+                    part = '0' + part
+                _parts.append(part)
+            self._full_color = '#' + ''.join(_parts)
+            self._rgb_color = re.sub('\s+', '', color).replace(',', ', ')
+            self.color = self._full_color
+        else:
+            raise self.UnknownType
+
+    @property
+    def rgb_color(self):
+        if self._rgb_color:
+            return self._rgb_color
+        color = self.full_color
+        return 'rgb(%i, %i, %i)' % (int(color[1:3], 16),
+                                    int(color[3:5], 16),
+                                    int(color[5:7], 16))
+    
+    @property
+    def compact_color(self):
+        if self._compact_color:
+            return self._compact_color
+        color = self.color.lower()
+        if color[1] == color[2] and color[3] == color[4] and color[5] == color[6]:
+            return '#%s%s%s' % (color[1], color[3], color[5])
+        return color
+    
+    @property
+    def full_color(self):
+        if self._full_color:
+            return self._full_color
+        color = self.color.upper()
+        return '#%s%s%s' % (color[1]*2, color[2]*2, color[3]*2)
+    
+    class UnknownType(Exception): pass

File context.py

View file
+# -*- coding: utf-8 -*-
+
+
+class Context(object):
+    
+    def __init__(self, *args):
+        self.dicts = args or [{}]
+    
+    def __getitem__(self, name):
+        for d in self.dicts:
+            if name in d:
+                return d[name]
+        raise KeyError(name)
+    
+    def __setitem__(self, name, value):
+        self.dicts[0][name] = value
+    
+    def __contains__(self, name):
+        for d in self.dicts:
+            if name in d:
+                return True
+        return False
+    
+    def extend(self, d):
+        return self.__class__(d, *self.dicts)    
+    
+    def copy(self):
+        return self.__cls__([d.copy() for d in self.dicts])

File expression.py

View file
+# -*- coding: utf-8 -*-
+
+import re
+from number import Number
+
+
+class EvaluatedExpression(str):
+            
+    def evaluate(self, context):
+        return str(self)
+
+
+class Expression(object):
+
+    number_re = re.compile(r'(?<!Number\()(\d+(?:\.\d+)?(?:em|px|%|pt))')
+    variable_re = re.compile(r'(?P<name>!\w+)')
+    
+    compact_res = (re.compile(r'\s*(?=\+|-|/|\*|\))'),
+                   re.compile(r'(?<=\+|-|/|\*|\()\s*'))
+    
+    def __init__(self, expr):
+        self.expr = expr        
+    
+    
+    def split(self):
+        expr = self.expr
+        for re in self.compact_res:
+            expr = re.sub('', expr)
+        return expr.split(' ')
+        
+    
+    def evaluate(self, context):
+        #print 'original expr %r' % self.expr
+        exprs = self.split()
+        if len(exprs) > 1:
+            return ' '.join(Expression(e).evaluate(context) for e in exprs)
+        expr = exprs[0]
+        #print 'compact expr %r' % expr
+        for op in '+-/*!':
+            if op in expr:
+                break
+        else:
+            #print 'should not eval'
+            #print 
+            return expr
+        def replace_variable(name):
+            name = name.groupdict()['name']
+            name = name.strip('!')
+            if name in context:
+                value = context[name]
+                #print 'value of !%s is %r' % (name, value)
+                if type(value) in (int, float):
+                    return repr(value)
+                return str(context[name])
+            raise Exception('variable "%s" not found' % name)
+        expr = self.variable_re.sub(replace_variable, expr)
+        expr = self.number_re.sub(r"Number('\1')", expr)
+        #print 'expr to eval %r' % expr
+        result = str(eval(expr))
+        #print 'result %r' % result
+        #print
+        return result
+    
+    def __repr__(self):
+        return self.expr

File number.py

View file
+# -*- coding: utf-8 -*-
+
+import re
+
+
+class Number(object):
+    
+    number_re = re.compile(r'^(?P<value>\d+(?:\.\d+)?)(?P<points>em|px|%|pt)$')
+    
+    def __init__(self, expr):
+        data = self.number_re.match(expr).groupdict()
+        if '.' in data['value']:
+            self.number = float(data['value'])
+        else:
+            self.number = int(data['value'])
+        self.points = data['points']
+
+    def __str__(self):
+        return '%s%s' % (self.number, self.points)
+
+    def __add__(self, value):
+        if type(value) is str:
+            return '%s%s' % (self, value)
+        if type(value) in (int, float):
+            self.number += value
+            return self
+        if type(value) is Number and value.points == self.points:
+            self.number += value.number
+            return self
+        raise Exception("can't process: %s + %s" % (self, value))
+    
+    def __sub__(self, value):
+        if type(value) in (int, float):
+            self.number -= value
+            return self
+        if type(value) is Number and value.points == self.points:
+            self.number -= value.number
+            return self
+        raise Exception("can't process: %s - %s" % (self, value))
+    
+    def __mul__(self, value):
+        if type(value) in (int, float):
+            self.number *= value
+            return self
+        raise Exception("can't process: %s * %s" % (self, value))
+    
+    def __div__(self, value):
+        if type(value) in (int, float):
+            self.number /= value
+            return self
+        raise Exception("can't process: %s / %s" % (self, value))
+
+    def __repr__(self):
+        return "Number('%s%s')" % (self.number, self.points)

File sass.py

View file
 # -*- coding: utf-8 -*-
 
 import re
-
-
-class Context(object):
-    
-    def __init__(self, *args):
-        self.dicts = args or [{}]
-    
-    def __getitem__(self, name):
-        for d in self.dicts:
-            if name in d:
-                return d[name]
-        raise KeyError(name)
-    
-    def __setitem__(self, name, value):
-        self.dicts[0][name] = value
-    
-    def __contains__(self, name):
-        for d in self.dicts:
-            if name in d:
-                return True
-        return False
-    
-    def extend(self, d):
-        return self.__class__(d, *self.dicts)    
-    
-    def copy(self):
-        return self.__cls__([d.copy() for d in self.dicts])
-
-
-class EvaluatedExpression(str):
-            
-    def evaluate(self, context):
-        return str(self)
-
-
-class Expression(object):
-
-    number_re = re.compile(r'(?<!Number\()(\d+(?:\.\d+)?(?:em|px|%|pt))')
-    variable_re = re.compile(r'(?P<name>!\w+)')
-    
-    compact_res = (re.compile(r'\s*(?=\+|-|/|\*|\))'),
-                   re.compile(r'(?<=\+|-|/|\*|\()\s*'))
-    
-    def __init__(self, expr):
-        self.expr = expr        
-    
-    
-    def split(self):
-        expr = self.expr
-        for re in self.compact_res:
-            expr = re.sub('', expr)
-        return expr.split(' ')
-        
-    
-    def evaluate(self, context):
-        #print 'original expr %r' % self.expr
-        exprs = self.split()
-        if len(exprs) > 1:
-            return ' '.join(Expression(e).evaluate(context) for e in exprs)
-        expr = exprs[0]
-        #print 'compact expr %r' % expr
-        for op in '+-/*!':
-            if op in expr:
-                break
-        else:
-            #print 'should not eval'
-            #print 
-            return expr
-        def replace_variable(name):
-            name = name.groupdict()['name']
-            name = name.strip('!')
-            if name in context:
-                value = context[name]
-                #print 'value of !%s is %r' % (name, value)
-                if type(value) in (int, float):
-                    return repr(value)
-                return str(context[name])
-            raise Exception('variable "%s" not found' % name)
-        expr = self.variable_re.sub(replace_variable, expr)
-        expr = self.number_re.sub(r"Number('\1')", expr)
-        #print 'expr to eval %r' % expr
-        result = str(eval(expr))
-        #print 'result %r' % result
-        #print
-        return result
-    
-    def __repr__(self):
-        return self.expr
-
-
-class Number(object):
-    
-    number_re = re.compile(r'^(?P<value>\d+(?:\.\d+)?)(?P<points>em|px|%|pt)$')
-    
-    def __init__(self, expr):
-        data = self.number_re.match(expr).groupdict()
-        if '.' in data['value']:
-            self.number = float(data['value'])
-        else:
-            self.number = int(data['value'])
-        self.points = data['points']
-
-    def __str__(self):
-        return '%s%s' % (self.number, self.points)
-
-    def __add__(self, value):
-        if type(value) is str:
-            return '%s%s' % (self, value)
-        if type(value) in (int, float):
-            self.number += value
-            return self
-        if type(value) is Number and value.points == self.points:
-            self.number += value.number
-            return self
-        raise Exception("can't process: %s + %s" % (self, value))
-    
-    def __sub__(self, value):
-        if type(value) in (int, float):
-            self.number -= value
-            return self
-        if type(value) is Number and value.points == self.points:
-            self.number -= value.number
-            return self
-        raise Exception("can't process: %s - %s" % (self, value))
-    
-    def __mul__(self, value):
-        if type(value) in (int, float):
-            self.number *= value
-            return self
-        raise Exception("can't process: %s * %s" % (self, value))
-    
-    def __div__(self, value):
-        if type(value) in (int, float):
-            self.number /= value
-            return self
-        raise Exception("can't process: %s / %s" % (self, value))
-
-    def __repr__(self):
-        return "Number('%s%s')" % (self.number, self.points)
+from color import Color
+from expression import Expression, EvaluatedExpression
+from context import Context
 
 
 class Property(object):
             yield Property(prop.key, prop.value.evaluate(context))
 
 
-class Color(object):
-
-    full_color_re = re.compile(r'^#[\da-f]{6}$', re.I)
-    compact_color_re = re.compile(r'^#[\da-f]{3}$', re.I)
-    rgb_color_re = re.compile(r'^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$', re.I)
-    
-    def __init__(self, color):
-        self.color = color
-        self._full_color = None
-        self._compact_color = None
-        self._rgb_color = None
-        if self.full_color_re.match(color):
-            self._full_color = color.upper()
-        elif self.compact_color_re.match(color):
-            self._compact_color = color.lower()
-        elif self.rgb_color_re.match(color):
-            _parts = []
-            for part in map(int, self.rgb_color_re.findall(self.color)[0]):
-                part = hex(part)[2:].upper()
-                if len(part) == 1:
-                    part = '0' + part
-                _parts.append(part)
-            self._full_color = '#' + ''.join(_parts)
-            self._rgb_color = re.sub('\s+', '', color).replace(',', ', ')
-            self.color = self._full_color
-        else:
-            raise self.UnknownType
-
-    @property
-    def rgb_color(self):
-        if self._rgb_color:
-            return self._rgb_color
-        color = self.full_color
-        return 'rgb(%i, %i, %i)' % (int(color[1:3], 16),
-                                    int(color[3:5], 16),
-                                    int(color[5:7], 16))
-    
-    @property
-    def compact_color(self):
-        if self._compact_color:
-            return self._compact_color
-        color = self.color.lower()
-        if color[1] == color[2] and color[3] == color[4] and color[5] == color[6]:
-            return '#%s%s%s' % (color[1], color[3], color[5])
-        return color
-    
-    @property
-    def full_color(self):
-        if self._full_color:
-            return self._full_color
-        color = self.color.upper()
-        return '#%s%s%s' % (color[1]*2, color[2]*2, color[3]*2)
-    
-    class UnknownType(Exception): pass
 
 
 class SassConvertor(object):