CVS: ht2html ht2html.py,2.1,2.2

"Fred L. Drake" <[email protected]> Thu, 24 Jul 2003 22:20:20 -0700
Newsgroups gmane.comp.web.ht2html.devel
Message-ID <[email protected]>
Update of /cvsroot/ht2html/ht2html
In directory sc8-pr-cvs1:/tmp/cvs-serv13554

Modified Files:
	ht2html.py 
Log Message:
substantial refactoring to allow portions of this to be re-used


Index: ht2html.py
===================================================================
RCS file: /cvsroot/ht2html/ht2html/ht2html.py,v
retrieving revision 2.1
retrieving revision 2.2
diff -C2 -d -r2.1 -r2.2
*** ht2html.py	22 Mar 2002 18:03:40 -0000	2.1
--- ht2html.py	25 Jul 2003 05:20:18 -0000	2.2
***************
*** 69,91 ****
  
  
! 
! def usage(code, msg=''):
!     print __doc__ % globals()
!     if msg:
!         print msg
!     sys.exit(code)
! 
! 
! 
! def main():
!     try:
!         opts, args = getopt.getopt(
!             sys.argv[1:],
!             'hr:s:bx:fqv',
!             ['help', 'rootdir=', 'style=', 'backup', 'backupext=',
!              'force', 'quiet', 'version'])
! 
!     except getopt.error, msg:
!         usage(1, msg)
  
      rootdir = '.'
--- 69,73 ----
  
  
! class Command:
  
      rootdir = '.'
***************
*** 96,104 ****
      quiet = 0
  
!     for opt, arg in opts:
          if opt in ('-h', '--help'):
!             usage(0)
          elif opt in ('-v', '--version'):
!             print 'ht2html version', __version__
              sys.exit(0)
          elif opt in ('-r', '--rootdir'):
--- 78,107 ----
      quiet = 0
  
!     def __init__(self):
!         self.short_opts = 'hr:s:qv'
!         self.long_opts = ['help', 'rootdir=', 'style=', 'quiet', 'version']
! 
!     def usage(self, code, msg=''):
!         import __main__
!         if code:
!             f = sys.stderr
!         else:
!             f = sys.stdout
!         print >>f, __main__.__doc__ % globals()
!         if msg:
!             print >>f, msg
!         sys.exit(code)
! 
!     def add_option(self, short, long):
!         if short:
!             self.short_opts = self.short_opts + short
!         if long:
!             self.long_opts.append(long)
! 
!     def handle_option(self, opt, arg):
          if opt in ('-h', '--help'):
!             self.usage(0)
          elif opt in ('-v', '--version'):
!             print program, 'version', __version__
              sys.exit(0)
          elif opt in ('-r', '--rootdir'):
***************
*** 107,149 ****
  ##                rootdir = os.path.join(os.getcwd(), rootdir)
  ##            rootdir = os.path.normpath(rootdir)
!             rootdir = arg
          elif opt in ('-s', '--style'):
!             classmod = arg
!         elif opt in ('-b', '--backup'):
!             backup = 1
!         elif opt in ('-x', '--backupext'):
!             backupext = arg
!         elif opt in ('-f', '--force'):
!             force = 1
          elif opt in ('-q', '--quiet'):
!             quiet = 1
  
!     # find current dir relative to rootdir
!     absroot = os.path.abspath(rootdir)
!     curdir = os.path.abspath('.')
!     prefix = os.path.commonprefix([absroot, curdir])
!     if prefix <> absroot:
!         usage(1, 'Root directory must be relative to current directory')
!     relthis = curdir[len(prefix)+1:]
!     if not relthis:
!         relthis = '.'
  
!     # get the generator class
!     m = __import__(classmod)
!     GenClass = getattr(m, classmod)
  
!     # process all the files on the command line
!     for file in args:
!         if not quiet:
!             print 'Processing %s...' % file
!         # get the target filename
!         root, ext = os.path.splitext(file)
!         htmlfile = root + '.html'
          try:
!             g = GenClass(file, rootdir, relthis)
          except IOError, msg:
              print 'The source file is unreadable, skipping:', file
              print msg
!             continue
          # deal with backups, first load the original file
          try:
--- 110,191 ----
  ##                rootdir = os.path.join(os.getcwd(), rootdir)
  ##            rootdir = os.path.normpath(rootdir)
!             self.rootdir = arg
          elif opt in ('-s', '--style'):
!             self.classmod = arg
          elif opt in ('-q', '--quiet'):
!             self.quiet = 1
  
!     def parse_options(self, args):
!         try:
!             opts, args = getopt.getopt(args, self.short_opts, self.long_opts)
!         except getopt.error, err:
!             self.usage(2, str(err))
!         else:
!             self.args = args
!             for opt, arg in opts:
!                 self.handle_option(opt, arg)
  
!     def main(self, args=None):
!         if args is None:
!             args = sys.argv[1:]
!         self.parse_options(args)
  
!         # find current dir relative to rootdir
!         self.absroot = os.path.abspath(self.rootdir)
!         self.curdir = os.path.abspath(os.curdir)
!         self.prefix = os.path.commonprefix([self.absroot, self.curdir])
!         if self.prefix != self.absroot:
!             usage(1, 'Root directory must be relative to current directory')
!         self.relthis = self.curdir[len(self.prefix)+1:]
!         if not self.relthis:
!             self.relthis = os.curdir
! 
!         # get the generator class
!         m = __import__(self.classmod)
!         self.GenClass = getattr(m, self.classmod)
! 
!         self.run()
! 
!     def get_generator(self, file):
          try:
!             return self.GenClass(file, self.rootdir, self.relthis)
          except IOError, msg:
              print 'The source file is unreadable, skipping:', file
              print msg
!             return None
! 
!     def run(self):
!         # process all the files on the command line
!         for file in self.args:
!             self.process_file(file)
! 
! 
! class HT2HTML(Command):
! 
!     def __init__(self):
!         Command.__init__(self)
!         self.add_option('b', 'backup')
!         self.add_option('x', 'backupext=')
!         self.add_option('f', 'force')
! 
!     def handle_option(self, opt, arg):
!         if opt in ('-b', '--backup'):
!             self.backup = 1
!         elif opt in ('-x', '--backupext'):
!             self.backupext = arg
!         elif opt in ('-f', '--force'):
!             self.force = 1
!         else:
!             Command.handle_option(self, opt, arg)
! 
!     def process_file(self, file):
!         if not self.quiet:
!             print 'Processing %s...' % file
!         # get the target filename
!         root, ext = os.path.splitext(file)
!         htmlfile = root + '.html'
!         g = self.get_generator(file)
!         if g is None:
!             return
          # deal with backups, first load the original file
          try:
***************
*** 157,169 ****
          if origfound and newtext == data:
              # the file hasn't changed.  only write it if forced to
!             if not force:
!                 continue
          try:
              omask = os.umask(002)
!             if origfound and backup:
                  fp = open(htmlfile + '.generated', 'w')
                  fp.write(newtext)
                  fp.close()
!                 os.rename(htmlfile, htmlfile + backupext)
                  os.rename(htmlfile + '.generated', htmlfile)
              else:
--- 199,211 ----
          if origfound and newtext == data:
              # the file hasn't changed.  only write it if forced to
!             if not self.force:
!                 return
          try:
              omask = os.umask(002)
!             if origfound and self.backup:
                  fp = open(htmlfile + '.generated', 'w')
                  fp.write(newtext)
                  fp.close()
!                 os.rename(htmlfile, htmlfile + self.backupext)
                  os.rename(htmlfile + '.generated', htmlfile)
              else:
***************
*** 177,180 ****
                  raise
  
  if __name__ == '__main__':
!     main()
--- 219,223 ----
                  raise
  
+ 
  if __name__ == '__main__':
!     HT2HTML().main()



-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01