Package cssutils :: Package scripts :: Module cssparse |
[hide private]
[frames] | no frames]

# Source Code for Module cssutils.scripts.cssparse

     1  #!/usr/bin/env python
     2  """utility script to parse given filenames or string
     3  """
     4  __docformat__ = 'restructuredtext'
     5  __version__ = '$Id: cssparse.py 1327 2008-07-08 21:17:12Z cthedot $'
     6
     7  import cssutils
     8  import logging
     9  import optparse
    10  import sys
    11
    12 -def main(args=None):
    13      """
    14      Parses given filename(s) or string (using optional encoding) and prints
    15      the parsed style sheet to stdout.
    16
    17      Redirect stdout to save CSS. Redirect stderr to save parser log infos.
    18      """
    19      usage = """usage: %prog [options] filename1.css [filename2.css ...]
    20          [>filename_combined.css] [2>parserinfo.log] """
    21      p = optparse.OptionParser(usage=usage)
    22      p.add_option('-e', '--encoding', action='store', dest='encoding',
    23          help='encoding of the file')
    24      p.add_option('-d', '--debug', action='store_true', dest='debug',
    25          help='activate debugging output')
    26      p.add_option('-m', '--minify', action='store_true', dest='minify',
    27          help='minify parsed CSS', default=False)
    28      p.add_option('-s', '--string', action='store_true', dest='string',
    29          help='parse given string')
    30
    31      (options, params) = p.parse_args(args)
    32
    33      if not params:
    34          p.error("no filename given")
    35
    36      if options.debug:
    37          p = cssutils.CSSParser(loglevel=logging.DEBUG)
    38      else:
    39          p = cssutils.CSSParser()
    40
    41      if options.minify:
    42          cssutils.ser.prefs.useMinified()
    43
    44      if options.string:
    45          sheet = p.parseString(u''.join(params), encoding=options.encoding)
    46          print sheet.cssText
    47          print
    48          sys.stderr.write('\n')
    49      else:
    50          for filename in params:
    51              sys.stderr.write('=== CSS FILE: "%s" ===\n' % filename)
    52              sheet = p.parseFile(filename, encoding=options.encoding)
    53              print sheet.cssText
    54              print
    55              sys.stderr.write('\n')
    56
    57
    58  if __name__ == "__main__":
    59          sys.exit(main())
    60