Added a --html option to wesnoth_addon_manager.

Soliton hinted that may be useful.
This commit is contained in:
Elias Pschernig 2008-09-27 15:24:38 +00:00
parent b55692a5f3
commit 464c1bd1db
6 changed files with 218 additions and 44 deletions

View file

@ -0,0 +1 @@
# This let's python know about the addon_manager module.

View file

@ -0,0 +1,86 @@
import time, os, glob, sys
def output(path, data):
f = open(path + "/index.html", "w")
def w(x):
f.write(x + "\n")
w("""\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel=stylesheet href=\"style.css\" type=\"text/css\">
</head>
<body>""")
w("""\
<div class="header">
<a href="http://www.wesnoth.org">
<img src="http://www.wesnoth.org/mw/skins/glamdrol/wesnoth-logo.jpg" alt="Wesnoth logo"/>
</a>
</div>
<div class="topnav">
<a href="index.html">Wesnoth Addons</a>
</div>""")
os.system("cp -u data/tools/addon_manager/style.css " + path)
campaigns = data.get_or_create_sub("campaigns")
w("<table id=\"campaigns\">")
w("<tr>")
w("<th></th>")
w("<th>Addon</th>")
w("<th>Size</th>")
w("<th>Traffic</th>")
w("<th>Date</th>")
w("<th>Notes</th>")
w("</tr>")
for campaign in campaigns.get_all("campaign"):
v = campaign.get_text_val
w("<tr>")
icon = v("icon", "")
if icon:
tilde = icon.find("~")
if tilde >= 0: icon = icon[:tilde]
try: os.mkdir(path + "/icons")
except OSError: pass
if "." not in icon: icon += ".png"
src = "./data/core/images/" + icon
if not os.path.exists(src):
src = glob.glob("./data/campaigns/*/images/" + icon)
if src: src = src[0]
if not src or not os.path.exists(src):
sys.stderr.write("Cannot find icon " + icon + "\n")
src = "./data/core/images/units/unknown-unit.png"
imgurl = "icons/unknown-unit.png"
else:
imgurl = "icons/" + os.path.basename(icon)
else:
imgurl = "icons/" + os.path.basename(icon)
os.system("cp -u " + src + " " + path + "/icons")
name = v("name", "unknown").replace("_", " ")
w(('<td><img alt="%s" src="%s" width="72px" height="72px"/>'
) % (icon, imgurl))
w('<div class="desc"><b>%s</b><br/>%s</div></td>' % (
name, v("description", "(no description)")))
w("<td><b>%s</b><br/>" % name)
w("Version: %s<br/>" % v("version", "unknown"))
w("Author: %s</td>" % v("author", "unknown"))
w("<td>%.1fMB</td>" % (float(v("size", "unknown")) / 1024 / 1024))
w("<td><b>%s</b> down<br/>" % v("downloads", "unknown"))
w("%s up</td>" % v("uploads", "unknown"))
t = time.localtime(int(v("timestamp", "unknown")))
w("<td>%s</td>" % time.strftime("%b %d %Y", t))
w("<td>%s</td>" % v("translate", ""))
w("</tr>")
w("</table>")
w("""\
<div id="footer">
<p><a href="http://www.wesnoth.org/wiki/Site_Map">Site map</a></p>
<p><a href="http://www.wesnoth.org/wiki/Wesnoth:Copyrights">Copyright</a> &copy; 2003-2008 The Battle for Wesnoth</p>
<p>Supported by <a href="http://www.jexiste.fr/">Jexiste</a></p>
<p><a href="http://www.ibiblio.org/"><img src="http://www.ibiblio.org/hosted/images/ibiblio_hosted-110x32.png" width="110" height="32" border="0" alt="hosted by ibiblio" /></a></p>
</div>
</body></html>""")

View file

@ -0,0 +1,69 @@
body {
background-color: #fffbf0;
margin: 0px;
}
h1 {
text-align: center;
}
div.header {
background:#444444 url("http://www.wesnoth.org/mw/skins/glamdrol/headerbg.jpg") repeat-x scroll center top;
border-bottom:1px solid #000000;
margin:0pt;
padding:0pt;
width:100%;
text-align: center;
}
div.topnav {
background:#272727 url("http://www.wesnoth.org/mw/skins/glamdrol/navbg.png") repeat-x scroll center bottom;
border-top:1px solid #595959;
margin:0pt;
padding:3px 4px 15px;
text-align:center;
}
div.topnav a {
color:#B48648;
font-family:"Trebuchet MS",sans-serif;
line-height:0.8em;
font-size:0.8em;
font-weight:bold;
text-decoration:none;
}
div.topnav a:hover {
color:#CCCCCC;
}
img {
border: none;
}
div.header img {
vertical-align:middle;
}
div#footer {
clear: both;
font-size: x-small;
border-top: 1px solid black;
padding: 1em;
}
table#campaigns th {
text-align: left;
}
table#campaigns td img {
float: left;
}
div.desc {
display: none;
}
td:hover div.desc {
display: block;
left: 72px;
position: absolute;
width: 20em;
background-color: #ffffff;
border: 1px solid black;
padding: 8px;
}

View file

@ -7,8 +7,6 @@ import wesnoth.wmlparser as wmlparser
# src/addon_management.cpp
# src/network.cpp
BWML_PREFIXES = "\x00\x01\x02\x03"
dumpi = 0
class CampaignClient:
# First port listed will be used as default.
@ -85,12 +83,12 @@ class CampaignClient:
Return True if packet is encoded as binary WML. Else
return False.
"""
if packet[0] in BWML_PREFIXES:
if packet[0] in "\x00\x01\x02\x03":
return True
return False
def makePacket( self, doc ):
def make_packet(self, doc):
root = wmldata.DataSub("WML")
root.insert(doc)
return root.make_string()
@ -113,6 +111,7 @@ class CampaignClient:
"""
Read binary data from the server.
"""
sys.stderr.write("read_packet\n")
packet = ""
while len(packet) < 4 and not self.canceled:
packet += self.sock.recv(4 - len(packet))
@ -120,6 +119,9 @@ class CampaignClient:
return None
self.length = l = struct.unpack("!i", packet)[0]
sys.stderr.write("Receiving %d bytes.\n" % self.length)
packet = ""
while len(packet) < l and not self.canceled:
packet += self.sock.recv(l - len(packet))
@ -127,6 +129,8 @@ class CampaignClient:
if self.canceled:
return None
sys.stderr.write("Received %d bytes.\n" % len(packet))
if packet.startswith("\x1F\x8B"):
if self.verbose:
sys.stderr.write("GZIP compression found...\n")
@ -316,7 +320,7 @@ class CampaignClient:
if self.error:
return None
request = wmldata.DataSub("request_campaign_list")
self.send_packet( self.makePacket( request ) )
self.send_packet(self.make_packet(request))
return self.decode(self.read_packet())
@ -327,9 +331,9 @@ class CampaignClient:
request = wmldata.DataSub("validate_scripts")
request.set_text_val("name", name)
request.set_text_val("master_password", passphrase)
self.send_packet( self.makePacket( request ) )
self.send_packet(self.make_packet(request))
return self.decode( self.read_packet() )
return self.decode(self.read_packet())
def delete_campaign(self, name, passphrase):
"""
@ -339,7 +343,7 @@ class CampaignClient:
request.set_text_val("name", name)
request.set_text_val("passphrase", passphrase)
self.send_packet(self.makePacket(request))
self.send_packet(self.make_packet(request))
return self.decode(self.read_packet())
def change_passphrase(self, name, old, new):
@ -351,7 +355,7 @@ class CampaignClient:
request.set_text_val("passphrase", old)
request.set_text_val("new_passphrase", new)
self.send_packet( self.makePacket( request ) )
self.send_packet(self.make_packet(request))
return self.decode(self.read_packet())
def get_campaign_raw(self, name):
@ -360,7 +364,7 @@ class CampaignClient:
"""
request = wmldata.DataSub("request_campaign")
request.insert(wmldata.DataText("name", name))
self.send_packet(self.makePacket(request))
self.send_packet(self.make_packet(request))
raw_packet = self.read_packet()
if self.canceled:
@ -392,31 +396,30 @@ class CampaignClient:
The directory is the name of the campaign's directory.
"""
request = wmldata.DataSub("upload")
request.set_text_val("title", title)
request.set_text_val("name", name)
request.set_text_val("author", author)
request.set_text_val("passphrase", passphrase)
request.set_text_val("description", description)
request.set_text_val("name", name)
request.set_text_val("passphrase", passphrase)
request.set_text_val("title", title)
request.set_text_val("version", version)
request.set_text_val("icon", icon)
dataNode = wmldata.DataSub( "data" )
request.insert( dataNode )
def put_file(name, f):
fileNode = wmldata.DataSub("file")
# Order in which we apply escape sequences matters.
contents = f.read()
contents = contents.replace("\x01", "\x01\x02" )
contents = contents.replace("\x00", "\x01\x01")
contents = contents.replace("\x0d", "\x01\x0e")
fileContents = wmldata.DataText( "contents", contents )
fileNode.insert(fileContents)
fileNode.set_text_val("name", name)
# Order we apply escape sequences matters
contents = f.read().replace( "\x01", "\x01\x02" ).replace( "\x00", "\x01\x01" )
## import time
## contents = str(time.ctime())
fileContents = wmldata.DataText( "contents", contents )
fileNode.insert( fileContents )
return fileNode
def put_dir(name, path):
print "put_dir(", name, path, ")"
dataNode = wmldata.DataSub("dir")
dataNode.set_text_val("name", name)
for fn in glob.glob(path + "/*"):
@ -432,18 +435,17 @@ class CampaignClient:
# Only used if it's an old-style campaign directory
# with an external config.
if os.path.exists(cfgfile):
dataNode.insert( put_file(name + ".cfg", file(cfgfile)) )
request.insert(put_file(name + ".cfg", file(cfgfile)))
print "putting dir", name, os.path.basename(directory)
dataNode.insert( put_dir(name, directory) )
sys.stderr.write("Adding directory %s as %s.\n" % (directory, name))
request.insert(put_dir(name, directory))
print
## print "packet:", self.makePacket( request )
print "packet len:", len(self.makePacket( request ))
print
self.send_packet( self.makePacket( request ) )
packet = self.make_packet(request)
sys.stderr.write("Packet length is %d bytes.\n" % len(packet))
open("packet.dump", "wb").write(packet)
self.send_packet(packet)
return self.decode( self.read_packet( True ) )
return self.decode(self.read_packet())
def get_campaign_raw_async(self, name):
"""
@ -451,7 +453,7 @@ class CampaignClient:
doing server communications in a background thread.
"""
class MyThread(threading.Thread):
def __init__( self, name, client ):
def __init__(self, name, client):
threading.Thread.__init__( self, name=name )
self.name = name
self.cs = client
@ -480,7 +482,7 @@ class CampaignClient:
doing server communications in a background thread.
"""
class MyThread(threading.Thread):
def __init__( self, name, cs, args ):
def __init__(self, name, cs, args):
threading.Thread.__init__( self, name=name )
self.name = name
self.cs = cs
@ -488,7 +490,7 @@ class CampaignClient:
self.data = None
self.event = threading.Event()
def run(self):
self.data = self.cs.put_campaign(*self.args)
self.event.set()
@ -498,7 +500,7 @@ class CampaignClient:
self.event.set()
self.cs.async_cancel()
mythread = MyThread( args[1], self, args )
mythread = MyThread(args[1], self, args)
mythread.start()
return mythread
@ -524,8 +526,8 @@ class CampaignClient:
if contents:
contents = contents.get_value()
if verbose:
print i * " " + name + " (" +\
str(len(contents)) + ")"
sys.stderr.write(i * " " + name + " (" +
str(len(contents)) + ")")
save = file( os.path.join(path, name), "wb")
# We MUST un-escape our data
@ -539,7 +541,7 @@ class CampaignClient:
shutil.rmtree(os.path.join(path, name), True)
os.mkdir(os.path.join(path, name))
if verbose:
print i * " " + name
sys.stderr.write(i * " " + name + "\n")
self.unpackdir(dir, os.path.join(path, name), i + 2, verbose)
# vim: tabstop=4: shiftwidth=4: expandtab: softtabstop=4: autoindent:

View file

@ -187,7 +187,7 @@ class DataSub(Data):
elif isinstance(item, DataSub):
result.append(" " * indent)
result.append("[%s]\n" % item.name)
result.append( item.make_string(indent + 4, textdomain) )
result.append(item.make_string(indent + 4, textdomain))
result.append(" " * indent)
close = item.name
if close[0] == "+": close = close[1:]
@ -245,7 +245,13 @@ class DataSub(Data):
if ifdef:
result.append("#endif\n")
return "".join(result)
bytes = ""
for r in result:
if r != None:
# For networking, we need actual bytesteam here, not unicode.
bytes += str(r)
return bytes
def is_empty(self):
return len(self.data) == 0

View file

@ -27,6 +27,8 @@ if __name__ == "__main__":
optionparser = optparse.OptionParser()
optionparser.add_option("-a", "--address", help = "specify server address",
default = "add-ons.wesnoth.org")
optionparser.add_option("--html",
help = "Output HTML overview into the givendirectory.",)
optionparser.add_option("-p", "--port",
help = "specify server port or BfW version (%s)" % " or ".join(
map(lambda x: x[1], CampaignClient.portmap)),
@ -141,7 +143,16 @@ if __name__ == "__main__":
version = info.get_or_create_sub("info").get_text_val("version", "")
return uploads, version
if options.list:
if options.html:
cs = CampaignClient(address)
data = cs.list_campaigns()
if data:
import addon_manager.html
addon_manager.html.output(options.html, data)
else:
sys.stderr.write("Could not connect.\n")
elif options.list:
cs = CampaignClient(address)
data = cs.list_campaigns()
if data:
@ -250,8 +261,7 @@ if __name__ == "__main__":
pbl.get_text_val("description"),
pbl.get_text_val("version"),
pbl.get_text_val("icon"),
options.upload.replace(".pbl", ".cfg"),
wmldir)
options.upload.replace(".pbl", ".cfg"), wmldir)
pcounter = 0
while not mythread.event.isSet():