Refactoring, cleaining up imports, setting filemode
[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
22     def add_link(self, link):
23         tokens = filter(None,re.split(r'(^/[\w-]*/|[\w-]*/)',link))
24         self._tree.add(tokens,Link(link))
25
26     def write_map(self):
27         f = open(self._file,'w')
28         f.write('\n'.join(link.link() for link in self._tree))
29         f.close()
30
31     def read_map(self):
32         try:
33             f = open(self._file)
34             sml = f.read().split()
35             f.close()
36             for line in sml:
37                 self.add_link(line)
38         except IOError, what_error:
39             print 'INFO: Could not read sitemap.txt - one will be created'
40
41     def set(self):
42         return set(link.link() for link in self._tree)
43
44     def process(self):
45         t1 = time.time()
46         for link in self._tree:
47             link.prepare()
48         t2 = time.time()
49         print "Prepare  [%5.2f s]" % (round(t2-t1,2))
50         for link in self._tree:
51             self._sitelang = self._sitelang.union(set(link.languages()))
52         for tran in self._sitelang:
53             if tran != 'en':
54                 self._tranlang[tran] = gettext.translation('iso_639_3',
55                                                            languages=[tran])
56         t3 = time.time()
57         print "Language [%5.2f s]" % (round(t3-t2,2))
58         for link in self._tree:
59             link.render()
60         t4 = time.time()
61         print "Render   [%5.2f s]" % (round(t4-t3,2))
62         for link in self._tree:
63             link.template(self)
64         t5 = time.time()
65         print "Template [%5.2f s]" % (round(t5-t4,2))
66         t6 = time.time()
67         res = set()
68         cwd = os.getcwd()
69         for link in self._tree:
70             res = res.union(link.resources())
71         for f in res:
72             outfile = tmptarget+f
73             mkdir_p(os.path.dirname(outfile))
74             shutil.copyfile(f,outfile)
75         print "Resources[%5.2f s]" % (round(t6-t5,2))
76         sitmaplink = Link('/sitemap')
77         for l in self._sitelang:
78             sitmaplink.add_page((l,'/sitemap.'+l+'.xml'))
79         for l in self._sitelang:
80             sitmaplink.page(l).set_article(self.gen_menu(l,None,"tree sitemap"))
81             sitmaplink.page(l).template(self)
82         t7 = time.time()
83         print "Sitemap  [%5.2f s]" % (round(t7-t6,2))
84
85     def graph(self):
86         self._tree.graph()
87
88     def gen_menu(self,lang,page,cssclass):
89         return self._tree.menu(lang,page,cssclass)
90
91     def lang_menu(self,lang,link):
92         html = "<ul>"
93         for l in link.languages():
94             isoxml = u"//iso_639_3_entry[@*='"+l+"']"
95             ln = self._isocode.xml_select(isoxml)[0].name
96             if lang != 'en':
97                 ln = self._tranlang[lang].gettext(ln)
98             p = link.link()
99             if p[-1] == '/':
100                 p = p +'index'
101             p = p+'.'+l
102             html += '<li><a href="%s" hreflang="%s">%s</a></li>' % (p, l, ln)
103         html += "</ul>"
104         return html
105
106     def publish(self):
107         ssh_cmd(args.output,"mkdir -p")
108         publish(tmptarget, args.output)
109         for res in ["css","images","js","favicon.ico"]:
110             if (os.path.exists(args.style+res)):
111                 publish(args.style+res, args.output)
112         ssh_cmd(args.output,"chmod a+rx")