Added subdir argument, cleaned up arg handling
[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 lxml import etree
8 from lxml.builder import ElementMaker
9 from time import time
10 from treecutter import constants as const
11 from treecutter.trie import Trie
12 from treecutter.link import Link
13 from treecutter.tools import ssh_cmd, publish, mkdir_p
14
15 class Sitemap():
16     """Class keeping the internal site structure"""
17     def __init__(self,args):
18         self._output = args.output
19         self._style = args.style
20         self._subdir = args.subdir
21         self._file = 'sitemap.txt'
22         self._tree = Trie()
23         self._sitelang = set()
24         self._isocode = etree.parse('/usr/share/xml/iso-codes/iso_639_3.xml')
25         self._tranlang = {}
26         self._tmptarget = tempfile.mkdtemp()+'/'
27
28     # The sitemap uses a trie structure to keep track of links
29     # A link represents the path to the document and the link
30     # representing the text on the site.
31     # A link can have several pages in different languages.
32     def add_link(self, link):
33         tokens = filter(None,re.split(r'(^/[\w-]*/|[\w-]*/)',link))
34         self._tree.add(tokens,Link(link))
35
36     def write_map(self):
37         f = open(self._file,'w')
38         f.write('\n'.join(link.link() for link in self._tree))
39         f.close()
40
41     def read_map(self):
42         try:
43             f = open(self._file)
44             sml = f.read().split()
45             f.close()
46             for line in sml:
47                 self.add_link(line)
48         except IOError, what_error:
49             print 'INFO: Could not read sitemap.txt - one will be created'
50
51     # Create a set of the current tree for comparison with the
52     # directory scan
53     def set(self):
54         return set(link.link() for link in self._tree)
55
56     # Main driver in the application processing the documents
57     # in the collected sitemap
58     def process(self):
59         t1 = time()
60         print "Prepareing the input"
61         for link in self._tree:
62             link.prepare()
63         t2 = time()
64         print "Prepare  [%5.2f s]" % (round(t2-t1,2))
65         for link in self._tree:
66             self._sitelang = self._sitelang.union(set(link.languages()))
67         for tran in self._sitelang:
68             if tran != 'en':
69                 self._tranlang[tran] = gettext.translation('iso_639_3',
70                                                            languages=[tran])
71         t3 = time()
72         print "Language [%5.2f s]" % (round(t3-t2,2))
73         for link in self._tree:
74             link.render(self._style)
75         t4 = time()
76         print "Render   [%5.2f s]" % (round(t4-t3,2))
77         for link in self._tree:
78             link.template(self, self._style, self._tmptarget)
79         t5 = time()
80         print "Template [%5.2f s]" % (round(t5-t4,2))
81         t6 = time()
82         res = set()
83         # Collect all files used by the documents
84         for link in self._tree:
85             res = res.union(link.resources())
86         for f in res:
87             outfile = self._tmptarget+f
88             mkdir_p(os.path.dirname(outfile))
89             shutil.copyfile(f,outfile)
90         print "Resources[%5.2f s]" % (round(t6-t5,2))
91         # TODO: Improve the sitemap, it is a page that is generated from
92         #       the ground up and added a bit adhoc.
93         sitmaplink = Link('/sitemap')
94         for l in self._sitelang:
95             sitmaplink.add_page((l,'/sitemap.'+l+'.xml'))
96         for l in self._sitelang:
97             sitmaplink.page(l).set_article(self.gen_menu(l,None,"tree sitemap"))
98             sitmaplink.page(l).template(self,self._style,self._tmptarget)
99         t7 = time()
100         print "Sitemap  [%5.2f s]" % (round(t7-t6,2))
101
102     def graph(self):
103         self._tree.graph()
104
105     def gen_menu(self,lang,page,cssclass):
106         return self._tree.menu(lang,page,cssclass,self._subdir)
107
108     def lang_menu(self,lang,link):
109         html = ElementMaker()
110         menu = html.ul()
111         for l in link.languages():
112             isoxml = u"//iso_639_3_entry[@*='"+l+"']"
113             ln = self._isocode.xpath(isoxml)[0].get('name')
114             if lang != 'en':
115                 ln = self._tranlang[lang].gettext(ln)
116             p = link.link()
117             if p[-1] == '/':
118                 p = p +'index'
119             p = p+'.'+l
120             li = html.li(html.a(ln.decode('utf-8'),
121                                 href=self._subdir+p,hreflang=l))
122             menu.append(li)
123         return etree.tostring(menu,encoding='UTF-8',pretty_print=False)
124
125     def publish(self):
126         ssh_cmd(self._output,"mkdir -p")
127         publish(self._tmptarget, self._output)
128         for res in ["css","images","js","fonts","favicon.ico"]:
129             if (os.path.exists(self._style+res)):
130                 publish(self._style+res, self._output)
131         ssh_cmd(self._output,"chmod a+rx")