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.
fromcss_parser.stylesheetimportStyleSheet# parse css stringtext='body {margin:0;} p.indent{text-indent:1em;}'stylesheet=StyleSheet.from_string(text)
# parse css filestylesheet=StyleSheet.from_file('path/to/css/file.css')
# write to stringtxt=stylesheet.to_string()
# write to filestylesheet.to_file('path/to/css/file.css')
# get all rulesallrules=stylesheet.get_rules()
# get rules by querysomerules=stylesheet.get_rules('p.indent')
# remove rulestylesheet.remove_rule(rule)
# append rulestylesheet.append_rule(newrule)
# append rule after existing rulestylesheet.append_rule(newrule, existingrule)
# prepend rulestylesheet.prepend_rule(newrule)
# prepend rule before existing rulestylesheet.prepend_rule(newrule, existingrule)
# insert rule into media-querymediaquery=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.'''fromcss_parser.ruleimportRule# create rule from stringtext='blockquote {margin:1em 5% 1em 5%;}'rule=Rule.from_string(text)
# media-queries are retainedtext='@media all {blockquote {margin:1em 5% 1em 5%;} }'rule=Rule.from_string(text)
# multiple rules can be generated in this way, if desiredtext='/* 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 bepreserved, use StyleSheet.from_string() rather than Rule.from_string().'''# write single rule to string# includes media-query, if applicablerule.to_string()
# get all declarationsalldeclarations=rule.get_declarations()
# get declarations by querysomedeclarations=rule.get_declarations('margin')
# remove declarationrule.remove_declaration(declaration)
# append declarationrule.append_declaration(declaration)
# insert declaration after existing declarationrule.append_declaration(declaration, existingdeclaration)
# prepend declarationrule.append_declaration(declaration)
# insert declaration before existing declarationrule.prepend_declaration(declaration, existingdeclaration)fromcss_parser.ruleimportRule# create declaration from stringtext='margin:1em 5% 1em 5%;'declaration=Declaration.from_string(text)
# get propertyprop_string=declaration.get_property()
# get valueval_string=declaration.get_value()