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 1227 2008-05-19 19:59:31Z 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('-s', '--string', action='store_true', dest='string',
    27          help='parse given string')
    28
    29      (options, params) = p.parse_args(args)
    30
    31      if not params:
    32          p.error("no filename given")
    33
    34      if options.debug:
    35          p = cssutils.CSSParser(loglevel=logging.DEBUG)
    36      else:
    37          p = cssutils.CSSParser()
    38
    39      if options.string:
    40          sheet = p.parseString(u''.join(params), encoding=options.encoding)
    41          print sheet.cssText
    42          print
    43          sys.stderr.write('\n')
    44      else:
    45          for filename in params:
    46              sys.stderr.write('=== CSS FILE: "%s" ===\n' % filename)
    47              sheet = p.parseFile(filename, encoding=options.encoding)
    48              print sheet.cssText
    49              print
    50              sys.stderr.write('\n')
    51
    52
    53  if __name__ == "__main__":
    54          sys.exit(main())
    55