name should not have hrefs + cosmetic
[myplc.git] / plc-kml.py
1 #!/usr/bin/env plcsh
2
3 # this script generates a kml file, located under the default location below
4 # you should crontab this job from your myplc image
5 # you can then use the googlemap.js javascript for creating your applet
6 # more on this at http://svn.planet-lab.org/wiki/GooglemapSetup
7
8 import sys
9
10 default_output="/var/www/html/sites/sites.kml"
11
12 class KmlMap:
13
14     def __init__ (self,outputname):
15         self.outputname=outputname
16
17     def open (self):
18         self.output = open(self.outputname,"w")
19
20     def close (self):
21         if self.output:
22             self.output.close()
23         self.output = None
24
25     def refresh (self):
26         self.open()
27         self.write_header()
28         # cache peers 
29         peers = GetPeers({},['peer_id','peername'])
30         for site in GetSites({'enabled':True,'is_public':True}):
31             self.write_site(site,peers)
32         self.write_footer()
33         self.close()
34
35     def write(self,string):
36         self.output.write(string.encode("UTF-8"))
37
38     def write_header (self):
39         self.write("""<?xml version="1.0" encoding="UTF-8"?>
40 <kml xmlns="http://earth.google.com/kml/2.2">
41 <Document>
42 <name> PlanetLab Sites </name>
43 <description> This map shows all sites knows to the PlanetLab testbed. </description>
44 """)
45
46     def write_footer (self):
47         self.write("""</Document></kml>
48 """)
49
50     def peer_name (self,site, peers):
51         if not site['peer_id']:
52             return "local"
53         for peer in peers:
54             if peer['peer_id'] == site['peer_id']:
55                 return peer['peername']
56
57     def write_site (self, site, peers):
58         # discard sites with missing lat or lon
59         if not site['latitude'] or not site['longitude']:
60             return
61         # discard sites with no nodes 
62         if len(site['node_ids']) == 0:
63             return
64
65         site_id=site['site_id']
66         name=site['name']
67         nb_nodes=len(site['node_ids'])
68         nb_slices=len(site['slice_ids'])
69         latitude=site['latitude']
70         longitude=site['longitude']
71         baseurl='https://%s:443'%api.config.PLC_WWW_HOST
72         
73         # open description
74         description='<ul>'
75         # Name and URL
76         description += '<li>'
77         description += '<a href="%(baseurl)s/db/sites/index.php?id=%(site_id)d"> Site page </a>'%locals()
78         if site['url']:
79             site_url=site['url']
80             description += ' -- <a href="%(site_url)s"> %(site_url)s </a>'%locals()
81         description += '</li>'
82         # NODES
83         if nb_nodes:
84             description += '<li>'
85             description += '<a href="%(baseurl)s/db/nodes/index.php?site_id=%(site_id)d">%(nb_nodes)d node(s)</a>'%locals()
86             description += '<a href="%(baseurl)s/db/nodes/comon.php?site_id=%(site_id)d"> (in Comon)</a>'%locals()
87             description += '</li>'
88         else:
89             description += '<li>No node</li>'
90         #SLICES
91         if nb_slices:
92             description += '<li><a href="%(baseurl)s/db/slices/index.php?site_id=%(site_id)d">%(nb_slices)d slice(s)</a></li>'%locals()
93         else:
94             description += '<li>No slice</li>'
95         # close description
96         description +='</ul>'
97
98         # STYLE
99 #        if not site['peer_id']:
100 #            iconfile="google-local.png"
101 #        else:
102 #            iconfile="google-foreign.png"
103 #        iconurl="http://%(baseurl)s/misc/%(iconfile)s"%locals()
104 #        xyspec=""
105
106         if not site['peer_id']:
107             # local sites
108             iconurl="root://icons/palette-3.png"
109             xyspec="<x>0</x><y>0</y><w>32</w><h>32</h>"
110         else:
111             # remote
112             iconurl="root://icons/palette-3.png"
113             xyspec="<x>32</x><y>0</y><w>32</w><h>32</h>"
114             
115         iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
116
117         template="""<Placemark>
118 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
119 <name><![CDATA[%(name)s]]></name>
120 <description><![CDATA[%(description)s]]></description>
121 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
122 </Placemark>
123 """
124         self.write(template%locals())
125
126         
127 #        print 'name',name
128 #        print 'description',description
129 #        print template
130 #        print template%locals()
131
132 if __name__ == "__main__":
133     if len(sys.argv) == 1:
134         out=default_output
135     elif len(sys.argv) == 2:
136         out=sys.argv[1]
137     else:
138         print "Usage: %s [output]"%sys.argv[0]
139         print "default output is %s"%default_output
140         sys.exit(1)
141     KmlMap(out).refresh()