Package XPyTools :: Package scripts :: Module pep_parse
[hide private]
[frames] | no frames]

Source Code for Module XPyTools.scripts.pep_parse

  1  #!/usr/bin/env python
 
  2  # -*- coding: utf-8 -*-
 
  3  #--------------------------------------------------------------------
 
  4  # Filname: pep_parse.py
 
  5  # Author: Mazhugin Aleksey
 
  6  # Created: 2007/11/29
 
  7  # ID: $Id: $
 
  8  # URL: $URL: $
 
  9  # Copyright: Copyright (c) 2007, Mazhugin Aleksey.
 
 10  # License: BSD
 
 11  #--------------------------------------------------------------------
 
 12  
 
 13  """
 
 14  Rename PEPs html and generate index page "pep_index.html".
 
 15  
 
 16  Read folder contens (*.html files) and if they like PEP html then
 
 17  rename they to "PEP 0000 -- Title". Escape all not filename symbols ("*?:/\\").
 
 18  """ 
 19  
 
 20  # Importing
 
 21  ##import XPyLIB.dbg as dbg ##@
 
 22  import XPyLIB 
 23  import re 
 24  import sys 
 25  import glob 
 26  import getopt 
 27  import os 
 28  import string 
 29  
 
 30  ##:LOGGING:start
 
 31  import XPyLIB.xlogging as _xlog 
 32  from functools import wraps 
 33  _log=_xlog.getLogger('XPyTools.scripts.pep_parser') ##__name__) 
 34  _log.disabled=1 
 35  _log_u=_xlog.IndentFormatter.lvlup 
 36  _log_d=_xlog.IndentFormatter.lvldown 
 37  _log_r=_xlog.IndentFormatter.lvlreset 
 38  _log_w=_xlog.IndentFormatter.lvlwrap 
39 -def _log_wl(fun):
40 @wraps(fun) 41 def wrapper(*args, **kwds): 42 return _xlog.IndentFormatter.lvlwraplog(fun,_log)(*args, **kwds)
43 return wrapper 44 ##:LOGGING:end 45 46 _log.debug('logging enabled') 47 48 # Start Implementation 49
50 -def fail(fn='', errstr='Error.'):
51 """ 52 Fail reporting. 53 """ 54 print 'FAIL: file="%s". %s\n' % (fn, errstr)
55
56 -def usage():
57 """ 58 Output help info. 59 """ 60 print r''' 61 Usage: 62 i, indir= - input dir for pep *.html files. 63 o, outdir= - output dir, copy renamed pep files and create index.html file. 64 h,?, help - output this help info. 65 '''
66
67 -def do():
68 """ 69 Pars peps. 70 """ 71 72 # Parse options. 73 opt = 'i:o:h?' 74 lopt = ['indir=', 'outdir=', 'help'] 75 76 try: 77 opts, args = getopt.getopt(sys.argv[1:], opt, lopt) 78 except getopt.GetoptError, err: 79 # print help information and exit: 80 print str(err) # will print something like "option -a not recognized" 81 usage() 82 sys.exit(2) 83 84 indir = os.getcwdu() 85 outdir = os.path.join(indir, 'peps') 86 87 for o,v in opts: 88 if o in ('?','-h','--help'): 89 usage() 90 sys.exit(0) 91 elif o in ('-i', '--indir'): 92 indir=v 93 elif o in ('-o', '--outdir'): 94 outdir=v 95 else: 96 assert False, "unhandled option" ##@@ 97 98 if not os.path.exists(indir): 99 print 'Input dir "%s" not exist.' % (indir,) 100 sys.exit(2) 101 102 if not os.path.exists(outdir): 103 try: 104 os.makedirs(outdir) 105 except: 106 print 'Output dir "%s" not exist, and can\'t be created.' % (outdir,) 107 sys.exit(2) 108 109 re_pep = re.compile(r'<tr["=\w -]*>\s*<th["=\w \-]*>PEP:[&nbsp; ]*</th>\s*<td["=\w \-]*>(?P<pepid> *\d+ *)</td>\s*</tr>\s*<tr["=\w \-]*><th["=\w \-]*>Title:[&nbsp; ]*</th>\s*<td["=\w \-]*>(?P<peptitle>[\S ]+)</td>\s*</tr>') 110 111 tt=string.maketrans(':\\/*+?\'"&<>',' ') 112 113 _log.debug('pattern=%s' % (os.path.join(indir,'*.htm'),) ) 114 fl = glob.glob(os.path.join(indir,'*.htm')) 115 _log.debug('fl=%s' % (str(fl),) ) 116 # Index for files: [(Id, Title, path)]. 117 idx = [] 118 119 # Main loop. 120 for fn in fl: 121 _log.debug( 'try %s\n' % (fn,) ) 122 try: 123 f=open(fn,'r') 124 ln=f.read() 125 except Exception, err: 126 fail( fn, 'READ EXCEPTION=%s.' % (str(err),) ) 127 if f: 128 f.close() 129 f=None 130 continue 131 f.close() 132 f=None 133 134 s = re_pep.search(ln) 135 if not s: 136 _log.debug('fn search fail\n') 137 continue 138 _log.debug('fn search ok\n') 139 d = s.groupdict() 140 if not ( d.has_key('pepid') and d.has_key('peptitle') ): 141 fail(fn, 'No PEP id and/or title.\n') 142 try: 143 id=int(d['pepid']) 144 except: 145 fail( fn, 'Wrong PEP id="%s".\n' % (d['pepid'],) ) 146 continue 147 title=d['peptitle'].strip() 148 tn=title.translate(tt) 149 name='PEP %d -- %s.html' % (id, tn ) 150 p=os.path.join(outdir, name) 151 idx.append((id, title, name)) 152 _log.debug('try write path: %s\n' % (p,) ) 153 try: 154 f=open(p,'w') 155 f.write(ln) 156 _log.debug('\nwritten=%s\n' % (ln[:256],)) 157 except Exception, err: 158 fail( fn, 'WRITE EXCEPTION=%s.' % (str(err),) ) 159 if f: 160 f.close() 161 f=None 162 continue 163 f.close() 164 f=None 165 166 ##end for fn 167 168 _log.debug('generate index.html\n') 169 170 ois=r'''<html> 171 <head> 172 <title> PEPs index</title> 173 </head> 174 175 <body> 176 <table border=1><thead><th>Id</th><th>Title</th></thead> 177 <tbody>''' 178 179 oie=r'''</tbody></table> 180 </body> 181 </html> 182 ''' 183 184 idx=sorted(idx, lambda x,y: cmp(x[0],y[0]) ) 185 htm=ois 186 for i in idx: 187 htm+='<tr>\n<td><a href="%s">%d</a></td>\n<td>%s</td>\n</tr>\n' % (i[2],i[0],i[1]) 188 189 htm+=oie 190 191 try: 192 f=open(os.path.join(outdir, 'index.html'),'w') 193 f.write(htm) 194 except Exception, err: 195 print 'WRITE INDEX EXCEPTION=%s.' % (str(err),) 196 if f: 197 f.close()
198 199 200 ##end def do 201 202 # End Implemetation 203 204 #-DEBUG-START------------------------------------------------------------------- 205
206 -def _debug():
207 """ 208 """ 209 ## _log.debug('Debug message') 210 pass
211 212 #-DEBUG-END--------------------------------------------------------------------- 213 214 if __name__=='__main__': 215 try: 216 do() 217 except Exception, err: 218 print 'ERROR=%s.' % (str(err),) 219 else: 220 print '\nWork complete.\n' 221