Python:cssutils: CSS handling …

(op)hdknr@debuniid:~/.virtualenvs/op/src/gposterous$ pip install cssutils
Downloading/unpacking cssutils
  Downloading cssutils-0.9.6.zip (522Kb): 522Kb downloaded
  Running setup.py egg_info for package cssutils
    /home/hdknr/.virtualenvs/op/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg/setuptools/command/sdist.py:4: UserWarning: Module pkg_resources was already imported from /home/hdknr/.virtualenvs/op/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg/pkg_resources.py, but /usr/lib/python2.5/site-packages is being added to sys.path
      import os, re, sys, pkg_resources
    /home/hdknr/.virtualenvs/op/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg/setuptools/command/sdist.py:4: UserWarning: Module setuptools was already imported from /home/hdknr/.virtualenvs/op/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg/setuptools/__init__.py, but /usr/lib/python2.5/site-packages is being added to sys.path
      import os, re, sys, pkg_resources
    /home/hdknr/.virtualenvs/op/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg/setuptools/command/sdist.py:4: UserWarning: Module site was already imported from /home/hdknr/.virtualenvs/op/lib/python2.5/site.pyc, but /usr/lib/python2.5/site-packages is being added to sys.path
      import os, re, sys, pkg_resources
Installing collected packages: cssutils
  Running setup.py install for cssutils
    /home/hdknr/.virtualenvs/op/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg/setuptools/command/sdist.py:4: UserWarning: Module pkg_resources was already imported from /home/hdknr/.virtualenvs/op/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg/pkg_resources.py, but /usr/lib/python2.5/site-packages is being added to sys.path
      import os, re, sys, pkg_resources
    /home/hdknr/.virtualenvs/op/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg/setuptools/command/sdist.py:4: UserWarning: Module setuptools was already imported from /home/hdknr/.virtualenvs/op/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg/setuptools/__init__.py, but /usr/lib/python2.5/site-packages is being added to sys.path
      import os, re, sys, pkg_resources
    /home/hdknr/.virtualenvs/op/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg/setuptools/command/sdist.py:4: UserWarning: Module site was already imported from /home/hdknr/.virtualenvs/op/lib/python2.5/site.pyc, but /usr/lib/python2.5/site-packages is being added to sys.path
      import os, re, sys, pkg_resources
    Installing csscombine script to /home/hdknr/.virtualenvs/op/bin
    Installing cssparse script to /home/hdknr/.virtualenvs/op/bin
    Installing csscapture script to /home/hdknr/.virtualenvs/op/bin
Successfully installed cssutils

(op)hdknr@debuniid:~/.virtualenvs/op/src/gposterous$ python
Python 2.5.4 (r254:67916, Sep 26 2009, 08:19:36)
[GCC 4.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pygments.formatters import *
>>> fmt=get_formatter_by_name(‘html’,style=’colorful’)

>>> fmt.get_style_defs()

‘.hll { background-color: #ffffcc }\n.c { color: #808080 } /* Comment */\n.err { color: #F00000; background-color: #F0A0A0 } /* Error */\n.k { color: #008000; font-weight: bold } /* Keyword */\n.o { color: #303030 } /* Operator */\n.cm { color: #808080 } /* Comment.Multiline */\n.cp { color: #507090 } /* Comment.Preproc */\n.c1 { color: #808080 } /* Comment.Single */\n.cs { color: #cc0000; font-weight: bold } /* Comment.Special */\n.gd { color: #A00000 } /* Generic.Deleted */\n.ge { font-style: italic } /* Generic.Emph */\n.gr { color: #FF0000 } /* Generic.Error */\n.gh { color: #000080; font-weight: bold } /* Generic.Heading */\n.gi { color: #00A000 } /* Generic.Inserted */\n.go { color: #808080 } /* Generic.Output */\n.gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */\n.gs { font-weight: bold } /* Generic.Strong */\n.gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n.gt { color: #0040D0 } /* Generic.Traceback */\n.kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n.kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n.kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n.kp { color: #003080; font-weight: bold } /* Keyword.Pseudo */\n.kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n.kt { color: #303090; font-weight: bold } /* Keyword.Type */\n.m { color: #6000E0; font-weight: bold } /* Literal.Number */\n.s { background-color: #fff0f0 } /* Literal.String */\n.na { color: #0000C0 } /* Name.Attribute */\n.nb { color: #007020 } /* Name.Builtin */\n.nc { color: #B00060; font-weight: bold } /* Name.Class */\n.no { color: #003060; font-weight: bold } /* Name.Constant */\n.nd { color: #505050; font-weight: bold } /* Name.Decorator */\n.ni { color: #800000; font-weight: bold } /* Name.Entity */\n.ne { color: #F00000; font-weight: bold } /* Name.Exception */\n.nf { color: #0060B0; font-weight: bold } /* Name.Function */\n.nl { color: #907000; font-weight: bold } /* Name.Label */\n.nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */\n.nt { color: #007000 } /* Name.Tag */\n.nv { color: #906030 } /* Name.Variable */\n.ow { color: #000000; font-weight: bold } /* Operator.Word */\n.w { color: #bbbbbb } /* Text.Whitespace */\n.mf { color: #6000E0; font-weight: bold } /* Literal.Number.Float */\n.mh { color: #005080; font-weight: bold } /* Literal.Number.Hex */\n.mi { color: #0000D0; font-weight: bold } /* Literal.Number.Integer */\n.mo { color: #4000E0; font-weight: bold } /* Literal.Number.Oct */\n.sb { background-color: #fff0f0 } /* Literal.String.Backtick */\n.sc { color: #0040D0 } /* Literal.String.Char */\n.sd { color: #D04020 } /* Literal.String.Doc */\n.s2 { background-color: #fff0f0 } /* Literal.String.Double */\n.se { color: #606060; font-weight: bold; background-color: #fff0f0 } /* Literal.String.Escape */\n.sh { background-color: #fff0f0 } /* Literal.String.Heredoc */\n.si { background-color: #e0e0e0 } /* Literal.String.Interpol */\n.sx { color: #D02000; background-color: #fff0f0 } /* Literal.String.Other */\n.sr { color: #000000; background-color: #fff0ff } /* Literal.String.Regex */\n.s1 { background-color: #fff0f0 } /* Literal.String.Single */\n.ss { color: #A06000 } /* Literal.String.Symbol */\n.bp { color: #007020 } /* Name.Builtin.Pseudo */\n.vc { color: #306090 } /* Name.Variable.Class */\n.vg { color: #d07000; font-weight: bold } /* Name.Variable.Global */\n.vi { color: #3030B0 } /* Name.Variable.Instance */\n.il { color: #0000D0; font-weight: bold } /* Literal.Number.Integer.Long */’

>>> import cssutils
>>> css = cssutils.parseString(fmt.get_style_defs())
>>> type(css)
<class ‘cssutils.css.cssstylesheet.CSSStyleSheet’>
>>> type(css.cssRules)
<class ‘cssutils.css.cssrulelist.CSSRuleList’>
>>> type(css.cssRules[0])
<class ‘cssutils.css.cssstylerule.CSSStyleRule’>
>>> css.cssRules[0].cssText
u’.hll {\n    background-color: #ffc\n    }’
>>> css.cssRules[0].type
1
>>> css.cssRules[0].typeString
‘STYLE_RULE’
>>> css.cssRules[0].wellformed
True

>>> rules=dict([(x.selectorText,x.style.cssText.replace(‘\n’,”)) for x in css.cssRules if x.typeString == ‘STYLE_RULE’])
>>> for x in rules:
…   print x ," : ", rules[x]

.sh  :  background-color: #fff0f0
.go  :  color: #808080
.kd  :  color: #008000;font-weight: bold
.sc  :  color: #0040D0
.si  :  background-color: #e0e0e0
.cp  :  color: #507090
.cs  :  color: #c00;font-weight: bold
.kn  :  color: #008000;font-weight: bold
.kp  :  color: #003080;font-weight: bold
.kr  :  color: #008000;font-weight: bold
.kc  :  color: #008000;font-weight: bold
.kt  :  color: #303090;font-weight: bold

カテゴリー: 未分類 パーマリンク

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト / 変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト / 変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト / 変更 )

Google+ フォト

Google+ アカウントを使ってコメントしています。 ログアウト / 変更 )

%s と連携中