11467dec2ca14ed447be4d5583b55948bf263f5d
[treecutter.git] / treecutter / sitemap.py
1 #!/usr/bin/python
2 import os
3 import fnmatch
4 import subprocess
5 import amara
6 import re
7 import tempfile
8 import errno
9 import time
10 import argparse
11 import shutil
12 import pygraphviz as pgv
13 import glob
14 import gettext
15 import shutil
16 from amara import bindery
17 from amara.xslt import transform
18 from Cheetah.Template import Template
19
20 class Sitemap():
21     """Class keeping the internal site structure"""
22     def __init__(self):
23         self._file = 'sitemap.txt'
24         self._tree = Trie()
25         self._sitelang = set()
26         self._isocode = bindery.parse('/usr/share/xml/iso-codes/iso_639_3.xml')
27         self._tranlang = {}
28
29     def add_link(self, link):
30         tokens = filter(None,re.split(r'(^/[\w-]*/|[\w-]*/)',link))
31         self._tree.add(tokens,Link(link))
32
33     def write_map(self):
34         f = open(self._file,'w')
35         f.write('\n'.join(link.link() for link in self._tree))
36         f.close()
37
38     def read_map(self):
39         try:
40             f = open(self._file)
41             sml = f.read().split()
42             f.close()
43             for line in sml:
44                 self.add_link(line)
45         except IOError, what_error:
46             print 'INFO: Could not read sitemap.txt - one will be created'
47
48     def set(self):
49         return set(link.link() for link in self._tree)
50
51     def process(self):
52         t1 = time.time()
53         for link in self._tree:
54             link.prepare()
55         t2 = time.time()
56         print "Prepare  [%5.2f s]" % (round(t2-t1,2))
57         for link in self._tree:
58             self._sitelang = self._sitelang.union(set(link.languages()))
59         for tran in self._sitelang:
60             if tran != 'en':
61                 self._tranlang[tran] = gettext.translation('iso_639_3',
62                                                            languages=[tran])
63         t3 = time.time()
64         print "Language [%5.2f s]" % (round(t3-t2,2))
65         for link in self._tree:
66             link.render()
67         t4 = time.time()
68         print "Render   [%5.2f s]" % (round(t4-t3,2))
69         for link in self._tree:
70             link.template(self)
71         t5 = time.time()
72         print "Template [%5.2f s]" % (round(t5-t4,2))
73         t6 = time.time()
74         res = set()
75         cwd = os.getcwd()
76         for link in self._tree:
77             res = res.union(link.resources())
78         for f in res:
79             outfile = tmptarget+f
80             mkdir_p(os.path.dirname(outfile))
81             shutil.copyfile(f,outfile)
82         print "Resources[%5.2f s]" % (round(t6-t5,2))
83         sitmaplink = Link('/sitemap')
84         for l in self._sitelang:
85             sitmaplink.add_page((l,'/sitemap.'+l+'.xml'))
86         for l in self._sitelang:
87             sitmaplink.page(l).set_article(self.gen_menu(l,None,"tree sitemap"))
88             sitmaplink.page(l).template(self)
89         t7 = time.time()
90         print "Sitemap  [%5.2f s]" % (round(t7-t6,2))
91
92     def graph(self):
93         self._tree.graph()
94
95     def gen_menu(self,lang,page,cssclass):
96         return self._tree.menu(lang,page,cssclass)
97
98     def lang_menu(self,lang,link):
99         html = "<ul>"
100         for l in link.languages():
101             isoxml = u"//iso_639_3_entry[@*='"+l+"']"
102             ln = self._isocode.xml_select(isoxml)[0].name
103             if lang != 'en':
104                 ln = self._tranlang[lang].gettext(ln)
105             p = link.link()
106             if p[-1] == '/':
107                 p = p +'index'
108             p = p+'.'+l
109             html += '<li><a href="%s" hreflang="%s">%s</a></li>' % (p, l, ln)
110         html += "</ul>"
111         return html
112
113     def publish(self):
114         ssh_cmd(args.output,"mkdir -p")
115         publish(tmptarget, args.output)
116         for res in ["css","images","js","favicon.ico"]:
117             if (os.path.exists(args.style+res)):
118                 publish(args.style+res, args.output)
119         ssh_cmd(args.output,"chmod a+rx")