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

# Source Code for Module cssutils.scripts.csscapture

     1  #!/usr/bin/env python
     2  """Retrieve all CSS stylesheets including embedded for a given URL.
     3  Retrieve as StyleSheetList or save to disk - raw, parsed or minified version.
     4
     5  TODO:
     6  - maybe use DOM 3 load/save?
     7  - logger class which handles all cases when no log is given...
     8  - saveto: why does urllib2 hang?
     9  """
    10  __all__ = ['CSSCapture']
    11  __docformat__ = 'restructuredtext'
    12  __version__ = '$Id: csscapture.py 1332 2008-07-09 13:12:56Z cthedot $'
    13
    14  import logging
    15  import optparse
    16  import sys
    17  from cssutils.script import CSSCapture
    18
    19 -def main(args=None):
    20      usage = "usage: %prog [options] URL"
    21      parser = optparse.OptionParser(usage=usage)
    22      parser.add_option('-d', '--debug', action='store_true', dest='debug',
    23          help='show debug messages during capturing')
    24      parser.add_option('-m', '--minified', action='store_true', dest='minified',
    25          help='saves minified version of captured files')
    26      parser.add_option('-n', '--notsave', action='store_true', dest='notsave',
    27          help='if given files are NOT saved, only log is written')
    28  #    parser.add_option('-r', '--saveraw', action='store_true', dest='saveraw',
    29  #        help='if given saves raw css otherwise cssutils\' parsed files')
    30      parser.add_option('-s', '--saveto', action='store', dest='saveto',
    31          help='saving retrieved files to "saveto", defaults to "_CSSCapture_SAVED"')
    32      parser.add_option('-u', '--useragent', action='store', dest='ua',
    33          help='useragent to use for request of URL, default is urllib2s default')
    34      options, url = parser.parse_args()
    35
    36      # TODO:
    37      options.saveraw = False
    38
    39      if not url:
    40          parser.error('no URL given')
    41      else:
    42          url = url[0]
    43
    44      if options.debug:
    45          level = logging.DEBUG
    46      else:
    47          level = logging.INFO
    48
    49      # START
    50      c = CSSCapture(ua=options.ua, defaultloglevel=level)
    51
    52      stylesheetlist = c.capture(url)
    53
    54      if options.notsave is None or not options.notsave:
    55          if options.saveto:
    56              saveto = options.saveto
    57          else:
    58              saveto = u'_CSSCapture_SAVED'
    59          c.saveto(saveto, saveraw=options.saveraw, minified=options.minified)
    60      else:
    61          for i, s in enumerate(stylesheetlist):
    62              print u'''%s.
    63      encoding: %r
    64      title: %r
    65      href: %r''' % (i + 1, s.encoding, s.title, s.href)
    66
    67
    68  if __name__ == "__main__":
    69      sys.exit(main())
    70