File: style.py

package info (click to toggle)
python-vispy 0.15.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,868 kB
  • sloc: python: 59,799; javascript: 6,800; makefile: 69; sh: 6
file content (59 lines) | stat: -rw-r--r-- 1,680 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2014, Nicolas P. Rougier
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------

from . color import Color
from . number import Number
from . length import Length

_converters = {
    "fill": Color,
    "fill-opacity": Number,
    "stroke": Color,
    "stroke-opacity": Number,
    "opacity": Number,
    "stroke-width": Length,
    #    "stroke-miterlimit": Number,
    #    "stroke-dasharray":  Lengths,
    #    "stroke-dashoffset": Length,
}


class Style(object):

    def __init__(self):
        self._unset = True
        for key in _converters.keys():
            key_ = key.replace("-", "_")
            self.__setattr__(key_, None)

    def update(self, content):
        if not content:
            return

        self._unset = False
        items = content.strip().split(";")
        attributes = dict([item.strip().split(":") for item in items if item])
        for key, value in attributes.items():
            if key in _converters:
                key_ = key.replace("-", "_")
                self.__setattr__(key_, _converters[key](value))

    @property
    def xml(self):
        return self._xml()

    def _xml(self, prefix=""):
        if self._unset:
            return ""

        s = 'style="'
        for key in _converters.keys():
            key_ = key.replace("-", "_")
            value = self.__getattribute__(key_)
            if value is not None:
                s += '%s:%s ' % (key, value)
        s += '"'
        return s