Adding classes Address and Coord, moving functions into the classes.
[treecutter.git] / treecutter / sitemap.py
1 #!/usr/bin/python
2 import os
3 import re
4 import shutil
5 import gettext
6 import tempfile
7 from amara import bindery
8 from time import time
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()
47         print "Prepareing the input"
48         for link in self._tree:
49             link.prepare()
50         t2 = time()
51         print "Prepare  [%5.2f s]" % (round(t2-t1,2))
52         for link in self._tree:
53             self._sitelang = self._sitelang.union(set(link.languages()))
54         for tran in self._sitelang:
55             if tran != 'en':
56                 self._tranlang[tran] = gettext.translation('iso_639_3',
57                                                            languages=[tran])
58         t3 = time()
59         print "Language [%5.2f s]" % (round(t3-t2,2))
60         for link in self._tree:
61             link.render(style)
62         t4 = time()
63         print "Render   [%5.2f s]" % (round(t4-t3,2))
64         for link in self._tree:
65             link.template(self, style, self._tmptarget)
66         t5 = time()
67         print "Template [%5.2f s]" % (round(t5-t4,2))
68         t6 = time()
69         res = set()
70         cwd = os.getcwd()
71         for link in self._tree:
72             res = res.union(link.resources())
73         for f in res:
74             outfile = self._tmptarget+f
75             mkdir_p(os.path.dirname(outfile))
76             shutil.copyfile(f,outfile)
77         print "Resources[%5.2f s]" % (round(t6-t5,2))
78         sitmaplink = Link('/sitemap')
79         for l in self._sitelang:
80             sitmaplink.add_page((l,'/sitemap.'+l+'.xml'))
81         for l in self._sitelang:
82             sitmaplink.page(l).set_article(self.gen_menu(l,None,"tree sitemap"))
83             sitmaplink.page(l).template(self,style,self._tmptarget)
84         t7 = time()
85         print "Sitemap  [%5.2f s]" % (round(t7-t6,2))
86
87     def graph(self):
88         self._tree.graph()
89
90     def gen_menu(self,lang,page,cssclass):
91         return self._tree.menu(lang,page,cssclass)
92
93     def lang_menu(self,lang,link):
94         html = "<ul>"
95         for l in link.languages():
96             isoxml = u"//iso_639_3_entry[@*='"+l+"']"
97             ln = self._isocode.xml_select(isoxml)[0].name
98             if lang != 'en':
99                 ln = self._tranlang[lang].gettext(ln)
100             p = link.link()
101             if p[-1] == '/':
102                 p = p +'index'
103             p = p+'.'+l
104             html += '<li><a href="%s" hreflang="%s">%s</a></li>' % (p, l, ln)
105         html += "</ul>"
106         return html
107
108     def publish(self,output,style):
109         ssh_cmd(output,"mkdir -p")
110         publish(self._tmptarget, output)
111         for res in ["css","images","js","fonts","favicon.ico"]:
112             if (os.path.exists(style+res)):
113                 publish(style+res, output)
114         ssh_cmd(output,"chmod a+rx")