language: added full translation support
[treecutter.git] / treecutter / directory.py
index 0c633a784b11f04e158fbfdd723e8cb59168bab5..88390bb0ef9039a19f98878d2ecf121842edb51e 100644 (file)
@@ -1,26 +1,49 @@
 #!/usr/bin/python
 import os
 import fnmatch
-from amara import bindery
-import treecutter.const as const
+from lxml import etree
+import treecutter.constants as const
+from treecutter.docbook import Docbook
+import re
+from itertools import chain
 
 class Directory():
     """Class containing the state of the directory with articles"""
     def __init__(self):
-        self._cwd = '.'
+        self._cwd = u'.'
+        self._translations = []
         self._tree = []
+        self._basepath = re.compile('[/\w\._-]*/[\w-]+',re.UNICODE)
 
-    def scan(self):
+    def translations(self, directory):
+        paths = (self._cwd, directory)
+        for dirname, dirnames, filenames in chain.from_iterable(os.walk(path) for path in paths):
+            for filename in filenames:
+                if fnmatch.fnmatch(filename, '*.xlf'):
+                    file_ = os.path.join(dirname,filename)
+                    self._translations.append(file_)
+        return self._translations
+
+
+    def scan(self, draftflag, levelflag):
         for dirname, dirnames, filenames in os.walk(self._cwd):
             for filename in filenames:
                 if fnmatch.fnmatch(filename, '*.xml'):
                     file_ = os.path.join(dirname,filename)
-                    doc = bindery.parse(file_, prefixes=const.PREFIXES)
-                    title = doc.xml_select(u'/db:article/db:info/db:title')
-                    menu  = doc.xml_select(u'/db:article/db:info/db:titleabbrev')
-                    if title and menu:
-                        base = file_.split('.')[1]
-                        link = base.replace('index','')
+                    doc = Docbook(file_)
+                    (title, menu) = doc.title()
+                    draft = doc.status() == "draft"
+                    level = doc.userlevel()
+
+#                    doc = etree.parse(file_)
+#                    title = doc.xpath(u'/db:article/db:info/db:title',namespaces=const.XPATH)
+#                    menu  = doc.xpath(u'/db:article/db:info/db:titleabbrev',namespaces=const.XPATH)
+#                    draft = doc.xpath(u'/db:article[@status="draft"]',namespaces=const.XPATH)
+                    if draft and draftflag:
+                        draft = False
+                    if title and menu and not draft and level <= levelflag:
+                        base = self._basepath.match(file_).group()
+                        link = base.replace('index','')[1:]
                         self._tree.append(link)
 
     def set(self):