~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/directory_ui.py

[rs=mwhudson] Merge from trunk, 1.6 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
import datetime
20
20
import logging
21
 
import stat
22
 
 
23
 
from bzrlib import branch, errors
24
 
 
25
 
from loggerhead import util
 
21
import os
 
22
 
 
23
from bzrlib import branch
 
24
 
26
25
from loggerhead.controllers import TemplatedBranchView
27
26
 
28
 
 
29
27
class DirEntry(object):
30
 
 
31
28
    def __init__(self, dirname, parity, branch):
32
29
        self.dirname = dirname
33
30
        self.parity = parity
35
32
        if branch is not None:
36
33
            # If a branch is empty, bzr raises an exception when trying this
37
34
            try:
38
 
                self.last_change = datetime.datetime.fromtimestamp(
39
 
                    branch.repository.get_revision(
40
 
                        branch.last_revision()).timestamp)
 
35
                self.last_change =  datetime.datetime.fromtimestamp(
 
36
                    branch.repository.get_revision(branch.last_revision()).timestamp)
41
37
            except:
42
38
                self.last_change = None
43
39
 
44
 
 
45
40
class DirectoryUI(TemplatedBranchView):
46
41
    """
47
42
    """
48
43
 
49
44
    template_path = 'loggerhead.templates.directory'
50
45
 
51
 
    def __init__(self, static_url_base, transport, name):
52
 
 
 
46
    def __init__(self, static_url_base, path, name):
53
47
        class _branch(object):
54
48
            context_url = 1
55
 
 
56
 
            @staticmethod
57
 
            def static_url(path):
58
 
                return self._static_url_base + path
59
49
        self._branch = _branch
60
 
        self._history_callable = lambda: None
 
50
        self._history = None
 
51
        self._path = path
61
52
        self._name = name
62
53
        self._static_url_base = static_url_base
63
 
        self.transport = transport
64
54
        self.log = logging.getLogger('')
65
55
 
66
 
    def get_values(self, path, kwargs, response):
67
 
        listing = [d for d in self.transport.list_dir('.')
68
 
                   if not d.startswith('.')]
 
56
    def get_values(self, h, args, kwargs, response):
 
57
        listing = [d for d in os.listdir(self._path)
 
58
                   if not d.startswith('.')
 
59
                   and os.path.isdir(os.path.join(self._path, d))]
69
60
        listing.sort(key=lambda x: x.lower())
70
61
        dirs = []
71
62
        parity = 0
 
63
        def static_url(path):
 
64
            return self._static_url_base + path
72
65
        for d in listing:
 
66
            p = os.path.join(self._path, d)
73
67
            try:
74
 
                b = branch.Branch.open_from_transport(self.transport.clone(d))
75
 
                if b.get_config().get_user_option('http_serve') == 'False':
76
 
                    continue
 
68
                b = branch.Branch.open(p)
77
69
            except:
78
 
                try:
79
 
                    if not stat.S_ISDIR(self.transport.stat(d).st_mode):
80
 
                        continue
81
 
                except errors.NoSuchFile:
82
 
                    continue
83
70
                b = None
84
71
            dirs.append(DirEntry(d, parity, b))
85
72
            parity = 1 - parity
86
 
        # Create breadcrumb trail
87
 
        directory_breadcrumbs = util.directory_breadcrumbs(
88
 
                self._name,
89
 
                False,
90
 
                'directory')
91
73
        return {
92
74
            'dirs': dirs,
93
75
            'name': self._name,
94
 
            'directory_breadcrumbs': directory_breadcrumbs,
 
76
            'static_url': static_url,
95
77
            }