It needs cleanup.
import glob
import os
import zipfile
import sys
def find_path(files):
files2 = [x for x in files if not x.endswith('/')]
paths2 = set([x[:x.rfind('/')+1] for x in files2])
ell = min([len(x) for x in paths2])
path = list(set([x for x in paths2 if len(x)==ell]))[0]
return path
def head(name,styles):
title = '<title>{{=response.title or request.application}}</
title>'
items = '\n'.join(["{{response.files.append(URL
(request.application,'static','p\
lugin_layouts/layouts/%s/%s'))}}" % (name,style) for style in styles])
loc="""<style>
.flash
{
z-index:
2;
position:
absolute;
top:
0px;
right:
20%;
width:
60%;
height:
50px;
opacity:
0.75;
margin: 0
auto;
text-align:
center;
clear:
both;
border: 1px #000000
solid;
color:
black;
font-size:
12pt;
padding: 100px 10px 10px
10px;
cursor:
pointer;
background:
white;
}
div.error
{
background-color:
red;
color:
white;
padding:
3px;
}
</style>"""
return "\n%s\n%s\n{{include 'web2py_ajax.html'}}\n%s" %
(title,items,loc)
def content():
return """<div class="flash">{{=response.flash or ''}}</div>
{{include}}"""
for file in glob.glob('layouts/*'):
name=file[9:]
print name
folder='layouts/%s/' % name
try:
from BeautifulSoup import BeautifulSoup as BS
soup = BS(open(folder+'index.html','rb').read())
styles = [x['href'] for x in soup.findAll('link')]
soup.find('head').contents=BS(head(name,styles))
try:
soup.find('h1').contents=BS('{{=response.title or
request.application}}')
soup.find('h2').contents=BS("{{=response.subtitle or
'=response.subtitle'}}")
except:
pass
for x in (soup.find('div',id='menu'),
soup.find('div',{'class':'menu'}),
soup.find('div',id='nav'),
soup.find('div',{'class':'nav'})):
if x:
x.contents=BS('{{=MENU(response.menu)}}')
break
done=False
for x in (soup.find('div',id='content'),
soup.find('div',{'class':'content'}),
soup.find('div',id='main'),
soup.find('div',{'class':'main'})):
if x:
x.contents=BS(content())
done=True
break
if done:
page = soup.prettify()
page = re.compile("\s*\{\{=response\.flash or ''\}\}
\s*",re.MULTILINE).sub("{{=response.flash or ''}}",page)
open('layouts/'+name+'.html','wb').write(page)
print 'DONE'
else:
raise Exception
except Exception, e:
print e
continue