sitemap: support for draft, levels and templates
[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
9 class Directory():
10     """Class containing the state of the directory with articles"""
11     def __init__(self):
12         self._cwd = u'.'
13         self._tree = []
14         self._basepath = re.compile('[/\w\._-]*/[\w-]+',re.UNICODE)
15
16     def scan(self):
17         for dirname, dirnames, filenames in os.walk(self._cwd):
18             for filename in filenames:
19                 if fnmatch.fnmatch(filename, '*.xml'):
20                     file_ = os.path.join(dirname,filename)
21                     doc = Docbook(file_)
22                     (title, menu) = doc.title()
23                     draft = doc.status() == "draft"
24                     level = doc.userlevel()
25
26 #                    doc = etree.parse(file_)
27 #                    title = doc.xpath(u'/db:article/db:info/db:title',namespaces=const.XPATH)
28 #                    menu  = doc.xpath(u'/db:article/db:info/db:titleabbrev',namespaces=const.XPATH)
29 #                    draft = doc.xpath(u'/db:article[@status="draft"]',namespaces=const.XPATH)
30                     if draft and draftflag:
31                         draft = False
32                     if title and menu and not draft and level <= levelflag:
33                         base = self._basepath.match(file_).group()
34                         link = base.replace('index','')[1:]
35                         self._tree.append(link)
36
37     def set(self):
38         return set(self._tree)