This is a a CSS modeling utility written in pure Python.
- [✓] Create from file
- [✓] Create from string
- [✓] Write to file
- [✓] Write to string
- [✓] Get rules by query string
- [✓] Remove rule
- [✓] Prepend rule
- [✓] Append rule
- [✓] Add rule before/after existing rule
- Comment out rule
- [✓] Add rule to media-query
- Add rule with new media-query
- Pretty-Printing
- [✓] Create Rule from string
- [✓] Create multiple rules from a string
- [✓] Write Rule to string
- [✓] Get declarations by property
- [✓] Remove declaration
- [✓] Append declaration
- [✓] Prepend declaration
- [✓] Add declaration before/after existing declaration
- Comment out declaration
- Pretty-Printing
It has no dependencies outside of the Python standard library.
Developed and tested in v2.7.5.
from css_parser.stylesheet import StyleSheet
# parse css string
text = 'body {margin:0;} p.indent{text-indent:1em;}'
stylesheet = StyleSheet.from_string(text)
# parse css file
stylesheet = StyleSheet.from_file('path/to/css/file.css')
# write to string
txt = stylesheet.to_string()
# write to file
stylesheet.to_file('path/to/css/file.css')
# get all rules
allrules = stylesheet.get_rules()
# get rules by query
somerules = stylesheet.get_rules('p.indent')
# remove rule
stylesheet.remove_rule(rule)
# append rule
stylesheet.append_rule(newrule)
# append rule after existing rule
stylesheet.append_rule(newrule, existingrule)
# prepend rule
stylesheet.prepend_rule(newrule)
# prepend rule before existing rule
stylesheet.prepend_rule(newrule, existingrule)
# insert rule into media-query
mediaquery = existingrule.get_mediaquery()
newrule.set_mediaquery(mediaquery)
stylesheet.append(newrule, mediaquery)
'''
If the media-queries don't match, the new rule
will be added before/after the media-query,
depending on whether prepend_rule or
append_rule is used.
'''
from css_parser.rule import Rule
# create rule from string
text = 'blockquote {margin:1em 5% 1em 5%;}'
rule = Rule.from_string(text)
# media-queries are retained
text = '@media all {blockquote {margin:1em 5% 1em 5%;} }'
rule = Rule.from_string(text)
# multiple rules can be generated in this way, if desired
text = '/* multiple rules */' \
'@media all {' \
' blockquote {' \
' margin:1em 5% 1em 5%;' \
' }' \
' p {' \
' color:blue;' \
' }' \
'}'
rule = Rule.from_string(text)
'''
Note: If it is important that comments and whitespace be
preserved, use StyleSheet.from_string() rather than Rule.from_string().
'''
# write single rule to string
# includes media-query, if applicable
rule.to_string()
# get all declarations
alldeclarations = rule.get_declarations()
# get declarations by query
somedeclarations = rule.get_declarations('margin')
# remove declaration
rule.remove_declaration(declaration)
# append declaration
rule.append_declaration(declaration)
# insert declaration after existing declaration
rule.append_declaration(declaration, existingdeclaration)
# prepend declaration
rule.append_declaration(declaration)
# insert declaration before existing declaration
rule.prepend_declaration(declaration, existingdeclaration)
from css_parser.rule import Rule
# create declaration from string
text = 'margin:1em 5% 1em 5%;'
declaration = Declaration.from_string(text)
# get property
prop_string = declaration.get_property()
# get value
val_string = declaration.get_value()