language: added full translation support
[treecutter.git] / treecutter / directory.py
1 #!/usr/bin/python
2 import os
3 import fnmatch
4 from lxml import etree
5 import treecutter.constants as const
6 from treecutter.docbook import Docbook
7 import re
8 from itertools import chain
9
10 class Directory():
11     """Class containing the state of the directory with articles"""
12     def __init__(self):
13         self._cwd = u'.'
14         self._translations = []
15         self._tree = []
16         self._basepath = re.compile('[/\w\._-]*/[\w-]+',re.UNICODE)
17
18     def translations(self, directory):
19         paths = (self._cwd, directory)
20         for dirname, dirnames, filenames in chain.from_iterable(os.walk(path) for path in paths):
21             for filename in filenames:
22                 if fnmatch.fnmatch(filename, '*.xlf'):
23                     file_ = os.path.join(dirname,filename)
24                     self._translations.append(file_)
25         return self._translations
26
27
28     def scan(self, draftflag, levelflag):
29         for dirname, dirnames, filenames in os.walk(self._cwd):
30             for filename in filenames:
31                 if fnmatch.fnmatch(filename, '*.xml'):
32                     file_ = os.path.join(dirname,filename)
33                     doc = Docbook(file_)
34                     (title, menu) = doc.title()
35                     draft = doc.status() == "draft"
36                     level = doc.userlevel()
37
38 #                    doc = etree.parse(file_)
39 #                    title = doc.xpath(u'/db:article/db:info/db:title',namespaces=const.XPATH)
40 #                    menu  = doc.xpath(u'/db:article/db:info/db:titleabbrev',namespaces=const.XPATH)
41 #                    draft = doc.xpath(u'/db:article[@status="draft"]',namespaces=const.XPATH)
42                     if draft and draftflag:
43                         draft = False
44                     if title and menu and not draft and level <= levelflag:
45                         base = self._basepath.match(file_).group()
46                         link = base.replace('index','')[1:]
47                         self._tree.append(link)
48
49     def set(self):
50         return set(self._tree)