File: attributes.py

package info (click to toggle)
python-enable 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,280 kB
  • ctags: 13,899
  • sloc: cpp: 48,447; python: 28,502; ansic: 9,004; makefile: 315; sh: 44
file content (44 lines) | stat: -rw-r--r-- 1,387 bytes parent folder | download | duplicates (3)
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
"""
    Parsers for specific attributes
"""
import urlparse
from pyparsing import (Literal,
    Optional, oneOf, Group, StringEnd, Combine, Word, alphas, hexnums,
    CaselessLiteral, SkipTo
)
from css.colour import colourValue
import string

##Paint values
none = CaselessLiteral("none").setParseAction(lambda t: ["NONE", ()])
currentColor = CaselessLiteral("currentColor").setParseAction(lambda t: ["CURRENTCOLOR", ()])

def parsePossibleURL(t):
    possibleURL, fallback = t[0]
    return [urlparse.urlsplit(possibleURL), fallback]

#Normal color declaration
colorDeclaration = none | currentColor | colourValue

urlEnd = (
    Literal(")").suppress() +
    Optional(Group(colorDeclaration), default=()) +
    StringEnd()
)

url = (
    CaselessLiteral("URL")
    +
    Literal("(").suppress()+
    Group(SkipTo(urlEnd, include=True).setParseAction(parsePossibleURL))
)

#paint value will parse into a (type, details) tuple.
#For none and currentColor, the details tuple will be the empty tuple
#for CSS color declarations, it will be (type, (R,G,B))
#for URLs, it will be ("URL", ((url tuple), fallback))
#The url tuple will be as returned by urlparse.urlsplit, and can be
#an empty tuple if the parser has an error
#The fallback will be another (type, details) tuple as a parsed
#colorDeclaration, but may be the empty tuple if it is not present
paintValue = url | colorDeclaration