profiles

Most important method in this module is cssutils.profiles.Profiles.addProfile() to add new properties to cssutils and the setting of defaultprofile.

Example of how to add a new profile:

>>> import cssutils
>>> sheet = cssutils.parseString('x { -test-x: 1 }')
>>> print sheet.cssRules[0].style.getProperties()[0].valid
False
>>> P1 = {
...    '-test-x': '{num}',
...    '-test-y': '{ident}|{percentage}',
...    # custom validation function
...    '-test-z': lambda(v): int(v) > 0}
>>> cssutils.profiles.profiles.addProfile('test', P1)
>>> sheet = cssutils.parseString('x { -test-x: 1 }')
>>> print sheet.cssRules[0].style.getProperties()[0].valid
True

This may change again, please beware!

A per CSSStyleSheet setting of a profile may be added soon.

Current status in release 0.9.6a1.

profiles

A global cssutils.profiles.Profile object which is used by all CSS properties for validation. Add or remove new profile definitions here.

defaultprofile

defaultprofile = None
defaultprofile is used for validation. To use e.g. the CSS2 profile set cssutils.profiles.defaultprofile = cssutils.profiles.Profiles.CSS_LEVEL_2

Profiles

class cssutils.profiles.Profiles

All profiles used for validation. cssutils.profiles.profiles is a preset object of this class and used by all properties for validation.

Predefined profiles are (use propertiesByProfile() to get a list of defined properties):

CSS_LEVEL_2
Properties defined by CSS2.1
CSS_COLOR_LEVEL_3
CSS 3 color properties
CSS_BOX_LEVEL_3
Currently overflow related properties only
addProfile(profile, properties, macros=None)

Add a new profile with name profile (e.g. ‘CSS level 2’) and the given properties.

Parameters:
  • profile – the new profile‘s name
  • properties

    a dictionary of { property-name: propery-value } items where property-value is a regex which may use macros defined in given macros or the standard macros Profiles.tokens and Profiles.generalvalues.

    propery-value may also be a function which takes a single argument which is the value to validate and which should return True or False. Any exceptions which may be raised during this custom validation are reported or raised as all other cssutils exceptions depending on cssutils.log.raiseExceptions which e.g during parsing normally is False so the exceptions would be logged only.

  • macros – may be used in the given properties definitions. There are some predefined basic macros which may always be used in Profiles.basicmacros and Profiles.generalmacros.
knownnames
All known property names of all profiles.
profiles
Names of all profiles.
propertiesByProfile(profiles=None)

Generator: Yield property names, if no profiles is given all profile’s properties are used.

Parameters:
  • profiles – a single profile name or a list of names.
removeProfile(profile=None, all=False)

Remove profile or remove all profiles.

Parameters:
  • profile – profile name to remove
  • all – if True removes all profiles to start with a clean state
Exceptions:
  • cssutils.profiles.NoSuchProfileException: If given profile cannot be found.
validate(name, value)

Check if value is valid for given property name using any profile.

Parameters:
  • name – a property name
  • value – a CSS value (string)
Returns:

if the value is valid for the given property name in any profile

validateWithProfile(name, value, profiles=None)

Check if value is valid for given property name returning (valid, profile).

Parameters:
  • name – a property name
  • value – a CSS value (string)
Returns:

valid, profiles where valid is if the value is valid for the given property name in any profile of given profiles and profiles the profile names for which the value is valid (or [] if not valid at all)

Example: You might expect a valid Profiles.CSS_LEVEL_2 value but e.g. validateWithProfile('color', 'rgba(1,1,1,1)') returns (True, Profiles.CSS_COLOR_LEVEL_3)

Table Of Contents

Previous topic

scripts

Next topic

logging

This Page

Quick search