File: pyfs_context.py

package info (click to toggle)
python-apptools 5.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,552 kB
  • sloc: python: 9,868; makefile: 80
file content (545 lines) | stat: -rw-r--r-- 18,188 bytes parent folder | download
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# (C) Copyright 2005-2025 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" A Python File System context. """


# Standard library imports.
import glob
import logging
import os
from os.path import join, splitext
import pickle

# Enthought library imports.
from apptools.io.api import File
from traits.api import Any, Dict, Instance, Property, Str

# Local imports.
from .address import Address
from .binding import Binding
from .context import Context
from .dir_context import DirContext
from .naming_event import NamingEvent
from .naming_manager import naming_manager
from .object_serializer import ObjectSerializer
from .pyfs_context_factory import PyFSContextFactory
from .pyfs_object_factory import PyFSObjectFactory
from .pyfs_state_factory import PyFSStateFactory
from .reference import Reference
from .referenceable import Referenceable


# Setup a logger for this module.
logger = logging.getLogger(__name__)


# The name of the 'special' file in which we store object attributes.
ATTRIBUTES_FILE = "__attributes__"

# Constants for environment property keys.
FILTERS = "apptools.naming.pyfs.filters"
OBJECT_SERIALIZERS = "apptools.naming.pyfs.object.serializers"


# The default environment.
ENVIRONMENT = {
    #### 'Context' properties #################################################
    # Object factories.
    Context.OBJECT_FACTORIES: [PyFSObjectFactory(), PyFSContextFactory()],
    # State factories.
    Context.STATE_FACTORIES: [PyFSStateFactory()],
    #### 'PyFSContext' properties #############################################
    # Object serializers.
    OBJECT_SERIALIZERS: [ObjectSerializer()],
    # List of filename patterns to ignore.  These patterns are passed to
    # 'glob.glob', so things like '*.pyc' will do what you expect.
    #
    # fixme: We should have a generalized filter mechanism here, and '.svn'
    # should be moved elsewhere!
    FILTERS: [ATTRIBUTES_FILE, ".svn"],
}


class PyFSContext(DirContext, Referenceable):
    """A Python File System context.

    This context represents a directory on a local file system.

    """

    # The name of the 'special' file in which we store object attributes.
    ATTRIBUTES_FILE = ATTRIBUTES_FILE

    # Environment property keys.
    FILTERS = FILTERS
    OBJECT_SERIALIZERS = OBJECT_SERIALIZERS

    #### 'Context' interface ##################################################

    # The naming environment in effect for this context.
    environment = Dict(ENVIRONMENT)

    # The name of the context within its own namespace.
    namespace_name = Property(Str)

    #### 'PyFSContext' interface ##############################################

    # The name of the context (the last component of the path).
    name = Str

    # The path name of the directory on the local file system.
    path = Str

    #### 'Referenceable' interface ############################################

    # The object's reference suitable for binding in a naming context.
    reference = Property(Instance(Reference))

    #### Private interface ####################################################

    # A mapping from bound name to the name of the corresponding file or
    # directory on the file system.
    _name_to_filename_map = Dict  # (Str, Str)

    # The attributes of every object in the context.  The attributes for the
    # context itself have the empty string as the key.
    #
    # {str name : dict attributes}
    #
    # fixme: Don't use 'Dict' here as it causes problems when pickling because
    # trait dicts have a reference back to the parent object (hence we end up
    # pickling all kinds of things that we don't need or want to!).
    _attributes = Any

    ###########################################################################
    # 'object' interface.
    ###########################################################################

    def __init__(self, **traits):
        """ Creates a new context. """

        # Base class constructor.
        super(PyFSContext, self).__init__(**traits)

        # We cache each object as it is looked up so that all accesses to a
        # serialized Python object return a reference to exactly the same one.
        self._cache = {}

    ###########################################################################
    # 'PyFSContext' interface.
    ###########################################################################

    #### Properties ###########################################################

    def _get_namespace_name(self):
        """ Returns the name of the context within its own namespace. """

        # fixme: clean this up with an initial context API!
        if "root" in self.environment:
            root = self.environment["root"]

            namespace_name = self.path[len(root) + 1:]

        else:
            namespace_name = self.path

        # fixme: This is a bit dodgy 'cos we actually return a name that can
        # be looked up, and not the file system name...
        namespace_name = "/".join(namespace_name.split(os.path.sep))

        return namespace_name

    #### methods ##############################################################

    def refresh(self):
        """ Refresh the context to reflect changes in the file system. """

        # fixme: This needs more work 'cos if we refresh a context then we
        # will load new copies of serialized Python objects!

        # This causes the initializer to run again the next time the trait is
        # accessed.
        self.reset_traits(["_name_to_filename_map"])

        # Clear out the cache.
        self._cache = {}

        # fixme: This is a bit hacky since the context in the binding may
        # not be None!
        self.context_changed = NamingEvent(
            new_binding=Binding(name=self.name, obj=self, context=None)
        )

    ###########################################################################
    # 'Referenceable' interface.
    ###########################################################################

    #### Properties ###########################################################

    def _get_reference(self):
        """ Returns a reference to this object suitable for binding. """

        abspath = os.path.abspath(self.path)

        reference = Reference(
            class_name=self.__class__.__name__,
            addresses=[Address(type="pyfs_context", content=abspath)],
        )

        return reference

    ###########################################################################
    # Protected 'Context' interface.
    ###########################################################################

    def _is_bound(self, name):
        """ Is a name bound in this context? """

        return name in self._name_to_filename_map

    def _lookup(self, name):
        """ Looks up a name in this context. """

        if name in self._cache:
            obj = self._cache[name]

        else:
            # Get the full path to the file.
            path = join(self.path, self._name_to_filename_map[name])

            # If the file contains a serialized Python object then load it.
            for serializer in self._get_object_serializers():
                if serializer.can_load(path):
                    try:
                        state = serializer.load(path)

                    # If the load fails then we create a generic file resource
                    # (the idea being that it might be useful to have access to
                    # the file to see what went wrong).
                    except:  # noqa: E722
                        state = File(path)
                        logger.exception("Error loading resource at %s" % path)

                    break

            # Otherwise, it must just be a file or folder.
            else:
                # Directories are contexts.
                if os.path.isdir(path):
                    state = self._context_factory(name, path)

                # Files are just files!
                elif os.path.isfile(path):
                    state = File(path)

                else:
                    raise ValueError("unrecognized file for %s" % name)

            # Get the actual object from the naming manager.
            obj = naming_manager.get_object_instance(state, name, self)

            # Update the cache.
            self._cache[name] = obj

        return obj

    def _bind(self, name, obj):
        """ Binds a name to an object in this context. """

        # Get the actual state to bind from the naming manager.
        state = naming_manager.get_state_to_bind(obj, name, self)

        # If the object is actually an abstract file then we don't have to
        # do anything.
        if isinstance(state, File):
            if not state.exists:
                state.create_file()

            filename = name

        # Otherwise we are binding an arbitrary Python object, so find a
        # serializer for it.
        else:
            for serializer in self._get_object_serializers():
                if serializer.can_save(obj):
                    path = serializer.save(join(self.path, name), obj)
                    filename = os.path.basename(path)
                    break

            else:
                raise ValueError("cannot serialize object %s" % name)

        # Update the name to filename map.
        self._name_to_filename_map[name] = filename

        # Update the cache.
        self._cache[name] = obj

        return state

    def _rebind(self, name, obj):
        """ Rebinds a name to an object in this context. """

        self._bind(name, obj)

    def _unbind(self, name):
        """ Unbinds a name from this context. """

        # Get the full path to the file.
        path = join(self.path, self._name_to_filename_map[name])

        # Remove it!
        f = File(path)
        f.delete()

        # Update the name to filename map.
        del self._name_to_filename_map[name]

        # Update the cache.
        if name in self._cache:
            del self._cache[name]

        # Remove any attributes.
        if name in self._attributes:
            del self._attributes[name]
            self._save_attributes()

    def _rename(self, old_name, new_name):
        """ Renames an object in this context. """

        # Get the old filename.
        old_filename = self._name_to_filename_map[old_name]
        old_file = File(join(self.path, old_filename))

        # Lookup the object bound to the old name.  This has the side effect
        # of adding the object to the cache under the name 'old_name'.
        obj = self._lookup(old_name)

        # We are renaming a LOCAL context (ie. a folder)...
        if old_file.is_folder:
            # Create the new filename.
            new_filename = new_name
            new_file = File(join(self.path, new_filename))

            # Move the folder.
            old_file.move(new_file)

            # Update the 'Context' object.
            obj.path = new_file.path

            # Update the cache.
            self._cache[new_name] = obj
            del self._cache[old_name]

            # Refreshing the context makes sure that all of its contents
            # reflect the new name (i.e., sub-folders and files have the
            # correct path).
            #
            # fixme: This currently results in new copies of serialized
            # Python objects!  We need to be a bit more judicious in the
            # refresh.
            obj.refresh()

        # We are renaming a file...
        elif isinstance(obj, File):
            # Create the new filename.
            new_filename = new_name
            new_file = File(join(self.path, new_filename))

            # Move the file.
            old_file.move(new_file)

            # Update the 'File' object.
            obj.path = new_file.path

            # Update the cache.
            self._cache[new_name] = obj
            del self._cache[old_name]

        # We are renaming a serialized Python object...
        else:
            # Create the new filename.
            new_filename = new_name + old_file.ext
            new_file = File(join(self.path, new_filename))

            old_file.delete()

            # Update the cache.
            if old_name in self._cache:
                self._cache[new_name] = self._cache[old_name]
                del self._cache[old_name]

            # Force the creation of the new file.
            #
            # fixme: I'm not sure that this is really the place for this.  We
            # do it because often the 'name' of the object is actually an
            # attribute of the object itself, and hence we want the serialized
            # state to reflect the new name... Hmmm...
            self._rebind(new_name, obj)

        # Update the name to filename map.
        del self._name_to_filename_map[old_name]
        self._name_to_filename_map[new_name] = new_filename

        # Move any attributes over to the new name.
        if old_name in self._attributes:
            self._attributes[new_name] = self._attributes[old_name]
            del self._attributes[old_name]
            self._save_attributes()

    def _create_subcontext(self, name):
        """ Creates a sub-context of this context. """

        path = join(self.path, name)

        # Create a directory.
        os.mkdir(path)

        # Create a sub-context that represents the directory.
        sub = self._context_factory(name, path)

        # Update the name to filename map.
        self._name_to_filename_map[name] = name

        # Update the cache.
        self._cache[name] = sub

        return sub

    def _destroy_subcontext(self, name):
        """ Destroys a sub-context of this context. """

        return self._unbind(name)

    def _list_names(self):
        """ Lists the names bound in this context. """

        return list(self._name_to_filename_map.keys())

    # fixme: YFI this is not part of the protected 'Context' interface so
    # what is it doing here?
    def get_unique_name(self, name):

        ext = splitext(name)[1]

        # specially handle '.py' files
        if ext != ".py":
            return super(PyFSContext, self).get_unique_name(name)

        body = splitext(name)[0]
        names = self.list_names()
        i = 2
        unique = name
        while unique in names:
            unique = body + "_" + str(i) + ".py"
            i += 1

        return unique

    ###########################################################################
    # Protected 'DirContext' interface.
    ###########################################################################

    def _get_attributes(self, name):
        """ Returns the attributes of an object in this context. """

        attributes = self._attributes.setdefault(name, {})

        return attributes.copy()

    def _set_attributes(self, name, attributes):
        """ Sets the attributes of an object in this context. """

        self._attributes[name] = attributes
        self._save_attributes()

    ###########################################################################
    # Private interface.
    ###########################################################################

    def _get_filters(self):
        """ Returns the filters for this context. """

        return self.environment.get(self.FILTERS, [])

    def _get_object_serializers(self):
        """ Returns the object serializers for this context. """

        return self.environment.get(self.OBJECT_SERIALIZERS, [])

    def _context_factory(self, name, path):
        """ Create a sub-context. """

        return self.__class__(path=path, environment=self.environment)

    def _save_attributes(self):
        """ Saves all attributes to the attributes file. """

        path = join(self.path, self.ATTRIBUTES_FILE)

        f = open(path, "wb")
        pickle.dump(self._attributes, f, 1)
        f.close()

    #### Trait initializers ###################################################

    def __name_to_filename_map_default(self):
        """ Initializes the '_name_to_filename' trait. """

        # fixme: We should have a generalized filter mechanism (instead of
        # just 'glob' patterns we should have filter objects that can be a bit
        # more flexible in how they do the filtering).
        patterns = [join(self.path, filter) for filter in self._get_filters()]

        name_to_filename_map = {}
        for filename in os.listdir(self.path):
            path = join(self.path, filename)
            for pattern in patterns:
                if path in glob.glob(pattern):
                    break

            else:
                for serializer in self._get_object_serializers():
                    if serializer.can_load(filename):
                        # fixme: We should probably get the name from the
                        # serializer instead of assuming that we can just
                        # drop the file exension.
                        name, ext = os.path.splitext(filename)
                        break

                else:
                    name = filename

                name_to_filename_map[name] = filename

        return name_to_filename_map

    def __attributes_default(self):
        """ Initializes the '_attributes' trait. """

        attributes_file = File(join(self.path, self.ATTRIBUTES_FILE))
        if attributes_file.is_file:
            f = open(attributes_file.path, "rb")
            attributes = pickle.load(f)
            f.close()

        else:
            attributes = {}

        return attributes

    #### Trait event handlers #################################################

    def _path_changed(self):
        """ Called when the context's path has changed. """

        basename = os.path.basename(self.path)

        self.name, ext = os.path.splitext(basename)