/usr/lib/python2.7/dist-packages/cssutils/stylesheets/mediaquery.py is in python-cssutils 1.0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | """Implements a DOM for MediaQuery, see
http://www.w3.org/TR/css3-mediaqueries/.
A cssutils implementation, not defined in official DOM.
"""
__all__ = ['MediaQuery']
__docformat__ = 'restructuredtext'
__version__ = '$Id$'
from cssutils.prodparser import *
from cssutils.helper import normalize, pushtoken
import cssutils
import re
import xml.dom
class MediaQuery(cssutils.util._NewBase):#cssutils.util.Base):
"""
A Media Query consists of one of :const:`MediaQuery.MEDIA_TYPES`
and one or more expressions involving media features.
Format::
media_query
: [ONLY | NOT]? S* media_type S* [ AND S* expression ]*
| expression [ AND S* expression ]*
;
media_type
: IDENT
;
expression
: '(' S* media_feature S* [ ':' S* expr ]? ')' S*
;
media_feature
: IDENT
;
"""
MEDIA_TYPES = ['all', 'braille', 'handheld', 'print', 'projection',
'speech', 'screen', 'tty', 'tv', 'embossed']
def __init__(self, mediaText=None, readonly=False, _partof=False):
"""
:param mediaText:
unicodestring of parsable media
# _standalone: True if new from ML parser
"""
super(MediaQuery, self).__init__()
self._wellformed = False
self._mediaType = u''
self._partof = _partof
if mediaText:
self.mediaText = mediaText # sets self._mediaType too
self._partof = False
self._readonly = readonly
def __repr__(self):
return "cssutils.stylesheets.%s(mediaText=%r)" % (
self.__class__.__name__, self.mediaText)
def __str__(self):
return "<cssutils.stylesheets.%s object mediaText=%r at 0x%x>" % (
self.__class__.__name__, self.mediaText, id(self))
def _getMediaText(self):
return cssutils.ser.do_stylesheets_mediaquery(self)
def _setMediaText(self, mediaText):
"""
:param mediaText:
a single media query string, e.g. ``print and (min-width: 25cm)``
:exceptions:
- :exc:`~xml.dom.SyntaxErr`:
Raised if the specified string value has a syntax error and is
unparsable.
- :exc:`~xml.dom.InvalidCharacterErr`:
Raised if the given mediaType is unknown.
- :exc:`~xml.dom.NoModificationAllowedErr`:
Raised if this media query is readonly.
media_query
: [ONLY | NOT]? S* media_type S* [ AND S* expression ]*
| expression [ AND S* expression ]*
;
media_type
: IDENT
;
expression
: '(' S* media_feature S* [ ':' S* expr ]? ')' S*
;
media_feature
: IDENT
;
"""
self._checkReadonly()
expression = lambda: Sequence(PreDef.char(name='expression', char=u'('),
Prod(name=u'media_feature',
match=lambda t, v: t == PreDef.types.IDENT
),
Sequence(PreDef.char(name='colon', char=u':'),
cssutils.css.value.MediaQueryValueProd(self),
minmax=lambda: (0, 1) # optional
),
PreDef.char(name='expression END', char=u')',
stopIfNoMoreMatch=self._partof
)
)
prods = Choice(Sequence(Prod(name=u'ONLY|NOT', # media_query
match=lambda t, v: t == PreDef.types.IDENT and
normalize(v) in (u'only', u'not'),
optional=True,
toStore='not simple'
),
Prod(name=u'media_type',
match=lambda t, v: t == PreDef.types.IDENT and
normalize(v) in self.MEDIA_TYPES,
stopIfNoMoreMatch=True,
toStore='media_type'
),
Sequence(Prod(name=u'AND',
match=lambda t, v: t == PreDef.types.IDENT and
normalize(v) == 'and',
toStore='not simple'
),
expression(),
minmax=lambda: (0, None)
)
),
Sequence(expression(),
Sequence(Prod(name=u'AND',
match=lambda t, v: t == PreDef.types.IDENT and
normalize(v) == 'and'
),
expression(),
minmax=lambda: (0, None)
)
)
)
# parse
ok, seq, store, unused = ProdParser().parse(mediaText,
u'MediaQuery',
prods)
self._wellformed = ok
if ok:
try:
media_type = store['media_type']
except KeyError, e:
pass
else:
if 'not simple' not in store:
self.mediaType = media_type.value
# TODO: filter doubles!
self._setSeq(seq)
mediaText = property(_getMediaText, _setMediaText,
doc="The parsable textual representation of the media list.")
def _setMediaType(self, mediaType):
"""
:param mediaType:
one of :attr:`MEDIA_TYPES`
:exceptions:
- :exc:`~xml.dom.SyntaxErr`:
Raised if the specified string value has a syntax error and is
unparsable.
- :exc:`~xml.dom.InvalidCharacterErr`:
Raised if the given mediaType is unknown.
- :exc:`~xml.dom.NoModificationAllowedErr`:
Raised if this media query is readonly.
"""
self._checkReadonly()
nmediaType = normalize(mediaType)
if nmediaType not in self.MEDIA_TYPES:
self._log.error(
u'MediaQuery: Syntax Error in media type "%s".' % mediaType,
error=xml.dom.SyntaxErr)
else:
# set
self._mediaType = mediaType
# update seq
for i, x in enumerate(self._seq):
if isinstance(x.value, basestring):
if normalize(x.value) in (u'only', u'not'):
continue
else:
# TODO: simplify!
self._seq[i] = (mediaType, 'IDENT', None, None)
break
else:
self._seq.insert(0, mediaType, 'IDENT')
mediaType = property(lambda self: self._mediaType, _setMediaType,
doc="The media type of this MediaQuery (one of "
":attr:`MEDIA_TYPES`) but only if it is a simple MediaType!")
wellformed = property(lambda self: self._wellformed)
|