#!/usr/bin/env python
# -*- coding: utf-8 -*-
from xml.dom.minidom import parse
from os.path import exists
from os.path import getmtime
import sys, codecs, re
from optparse import OptionParser
commentmatch = re.compile('^\s*(.+)\s*')
ampmatch = re.compile('&')
citydic = {}
class restaurant:
def __init__(self, info):
self.info = info
def __lt__(self, rhs):
if self.info['city'] < rhs.info['city']:
return True
elif self.info['city'] == rhs.info['city'] and self.info['name'] < rhs.info['name']:
return True
else:
return False
def getcity(self):
return self.info['city']
def htmldump(self, f, idt):
closed = False
if self.info.has_key('obsolete') and self.info['obsolete'] == 'yes':
closed = True
idt2 = idt * 2
f.write(idt + '
San Francisco Bay Areaのレストランリスト
\n''')
f.write(' ' + jintro + '\n
\n
\n ' + eintro + '\n
\n')
f.write('Last Updated: .
\n')
f.write(str(nrestaurant) + ' restaurants are listed as of that date.\n')
f.write('
\n\n')
def printfooter(f):
f.write(u'''
Email: jinmei at wide dot ad dot jp
Search
''')
f.write('''
\n''')
def printindex(f):
f.write('
City Index
\n
\n')
citylist = citydic.keys()
citylist.sort()
for c in citylist:
f.write('' + c + '')
if c != citylist[-1]:
f.write(' / ')
f.write('\n
\n')
if __name__ == '__main__':
usage = '''usage: %prog [options] inputfile outputfile'''
parser = OptionParser(usage = usage)
parser.add_option("-f", "--force", action="store_true",
dest="force", default=False,
help="force to output [default: %default]")
parser.add_option("-l", "--local", action="store_true",
dest="local", default=False,
help="use local links for testing [default: %default]")
parser.add_option("-q", "--quiet", action="store_true",
dest="quiet", default=False,
help="less verbose [default: %default]")
(options, args) = parser.parse_args()
if len(args) < 2:
parser.error('missing argument')
if not options.force and exists(args[1]) and getmtime(args[1]) >= getmtime(args[0]):
if not options.quiet:
sys.stderr.write('input file is older than output, exiting\n')
sys.exit(0)
xmlobj = parse(args[0])
restaurants = xmlobj.getElementsByTagName('restaurants')[0]
node = xmlobj.getElementsByTagName('japaneseintro')[0]
for child in node.childNodes:
if child.nodeType == node.TEXT_NODE:
jintro = child.data
m = commentmatch.search(jintro)
jintro = m.group(1)
node = xmlobj.getElementsByTagName('englishintro')[0]
for child in node.childNodes:
if child.nodeType == node.TEXT_NODE:
eintro = child.data
m = commentmatch.search(eintro)
eintro = m.group(1)
f = codecs.open(args[1], 'w', 'utf-8')
if restaurants:
reslist = []
make_list(restaurants, reslist)
printheader(f, jintro, eintro, len(reslist), options.local)
printindex(f)
reslist.sort()
currentcity = ''
for r in reslist:
city = r.getcity()
if city != currentcity:
if currentcity != '':
f.write('\n')
f.write('
' + city + ' (')
f.write(str(citydic[city]) + ' restaurant')
if citydic[city] != 1:
f.write('s')
f.write(')
\n')
f.write('
\n')
currentcity = city
r.htmldump(f, ' ')
f.write('
\n')
printfooter(f)