Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/develop-activity/activity_model.py
blob: d20690a3dbc7f298b10522d039502ed68d255283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Copyright 2008 Paul Swartz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gtk
import gobject
import os
import os.path
import logging
from gettext import gettext as _
import fnmatch

unwantedPaths = [
    '.*',  # hidden files
    '*~',  # emacs backups
    '*.pyc',  # compiled python
    '*.bak',  # SPE backup file
    '.git',  # git repository info
    'CVS', ]  # CVS repository info


def _nodefilter(node):
    notmatched = True
    for path in unwantedPaths:
        if fnmatch.fnmatch(node.filename, path):
            notmatched = False
            break
    return notmatched


def inmanifestfn(bundle):
    activity_path = bundle.get_path()
    logging.error('activity path = %s', activity_path)

    allfiles = []

    def walk_callback(allfiles, directory, files):
        for file_name in files:
            notmatched = True
            for path in unwantedPaths:
                if fnmatch.fnmatch(file_name, path):
                    notmatched = False
                    break
            if notmatched:
                allfiles.append(os.path.join(directory, file_name))

    os.path.walk(activity_path, walk_callback, allfiles)

    #logging.error('allfiles %s', allfiles)

    #allfiles = bundle.get_files()
    def nodefilterfn(node):
        if os.path.isdir(node.path):
            return True
        for innerpath in allfiles[:-1]:  # truncate MANIFEST
            if os.path.join(node.model.root, innerpath) == node.path:
                return True
        return False
    return nodefilterfn


def get_selected_file(treeview):
    selection = treeview.get_selection()
    model, _iter = selection.get_selected()
    if not _iter:
        return
    value = model.get_value(_iter, 0)
    return value


def get_selected_file_path(treeview):
    value = get_selected_file(treeview)
    if value:
        return value['path']


class DirectoryAndExtraModel(gtk.GenericTreeModel):

    columns = (gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)

    def __init__(self, root, extra_paths=None, nodefilter=_nodefilter):
        self.root = root
        self.extra_paths = extra_paths
        self.nodefilter = nodefilter
        self.refresh()
        gtk.GenericTreeModel.__init__(self)

    def refresh(self):
        self.files = list(
            n for n in
            (ActivityNode(filename, self, None, self.nodefilter)
             for filename in sorted(os.listdir(self.root)))
            if self.nodefilter(n))

        if self.extra_paths:
            self.files.extend(ActivityNode(
                filename, self, None,
                self.nodefilter) for filename in self.extra_paths)

    def get_iter_from_filepath(self, filepath):
        if filepath.startswith(self.root):
            inpath = os.path.split(filepath[len(self.root):])[1:]
            #os.path.split gives empty first element
        else:
            inpath = os.path.split(filepath)
        files = self.files
        outpath = []
        try:
            for node in inpath:
                nodeindex = files.index(node)
                files = files[nodeindex]._files
                outpath.append(nodeindex)
        except ValueError:
            print files
            print filepath, inpath, outpath
        tree_iter = self.get_iter(tuple(outpath))
        return tree_iter

    def on_get_flags(self):
        return 0

    def on_get_n_columns(self):
        return len(self.columns)

    def on_get_column_type(self, n):
        return self.columns[n]

    def on_get_iter(self, path):
        x = self.files
        for part in path:
            x = x[part]
        return x

    def on_get_path(self, rowref):
        return rowref.getTreePath()

    def on_get_value(self, rowref, n):
        if n == 0:
            return {'name': rowref.filename,
                    'path': rowref.path}
        else:
            return rowref.filename

    def on_iter_next(self, rowref):
        if rowref.parent is not None:
            files = rowref.parent
        else:
            files = self.files
        index = files.index(rowref) + 1
        if index < len(files):
            return files[index]

    def on_iter_has_child(self, rowref):
        if rowref is not None:
            return rowref.isDirectory
        else:
            return bool(len(self.files))

    def on_iter_n_chilren(self, rowref):
        logging.critical('n children: %s' % rowref)
        if rowref is not None:
            if rowref.isDirectory:
                logging.critical('res: %i' % len(rowref))
                return len(rowref)
            else:
                return
        else:
            return len(self.files)

    def on_iter_nth_child(self, rowref, n):
        if rowref is not None:
            if not rowref.isDirectory:
                return
            files = rowref
        else:
            files = self.files
        if n < len(files):
            return files[n]

    def on_iter_parent(self, child):
        return child.parent

    def on_iter_children(self, rowref):
        if rowref is not None:
            if rowref.isDirectory and len(rowref):
                return rowref[0]
            else:
                return
        else:
            return self.files[0]


class ActivityNode(object):

    def __init__(self, filename, model, parent, nodefilter):
        self.filename = filename
        self.model = model
        self.nodefilter = nodefilter
        if parent is not None:
            self.path = os.path.join(parent.path, filename)
        else:
            self.path = os.path.join(model.root, filename)
        self.parent = parent
        self.isDirectory = os.path.isdir(self.path)
        self._files = None

    def _get_files(self):
        if not self.isDirectory:
            return None
        if self._files is None:
            files = sorted(os.listdir(self.path))
            self._files = filter(
                self.nodefilter,
                (ActivityNode(filename, self.model, self, self.nodefilter)
                 for filename in files))
        if not self._files:
            self._files = (DummyActivityNode(self),)
        return self._files
    files = property(_get_files)  # TODO: use a decorator

    def __eq__(self, other):
        if not isinstance(other, ActivityNode):
            if isinstance(other, str):
                return self.filename == other
            else:
                return False
        return self.path == other.path

    def __len__(self):
        return len(self.files)

    def index(self, item):
        return self.files.index(item)

    def __getitem__(self, n):
        return self.files[n]

    def getTreePath(self):
        if self.parent is None:
            return (self.model.files.index(self),)
        parentPath = self.parent.getTreePath()
        return parentPath + (self.parent.files.index(self),)

    def __hash__(self):
        return hash(self.path)

    def __str__(self):
        return '<ActivityNode %s>' % self.path

    __repr__ = __str__

    def __eq__(self, other):
        return other == self.path or other == self.filename


class DummyActivityNode(ActivityNode):
    files = ()
    filename = _("<No visible files>")
    path = ""
    isDirectory = False

    def __init__(self, parent):
        self.parent = parent