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