-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.py
198 lines (124 loc) · 5.12 KB
/
resource.py
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
# -*- coding: utf-8 -*-
# This file is part of the pymfony package.
#
# (c) Alexandre Quercia <[email protected]>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
from __future__ import absolute_import;
import os.path;
import re;
from pickle import dumps as serialize;
from pickle import loads as unserialize;
from pymfony.component.system import Object;
from pymfony.component.system import SerializableInterface;
from pymfony.component.system.oop import interface;
"""
"""
@interface
class ResourceInterface(Object):
"""ResourceInterface is the interface that must be implemented
by all Resource classes.
@author Fabien Potencier <[email protected]>
"""
def __str__(self):
"""Returns a string representation of the Resource.
@return string A string representation of the Resource
"""
pass;
def isFresh(self, timestamp):
"""Returns true if the resource has not been updated
since the given timestamp.
@param timestamp: int The last time the resource was loaded
@return: Boolean True if the resource has not been updated, false otherwise
"""
pass;
def getResource(self):
"""Returns the resource tied to this Resource.
@return: mixed The resource
"""
pass;
class FileResource(ResourceInterface, SerializableInterface):
"""FileResource represents a resource stored on the filesystem.
The resource can be a file or a directory.
@author Fabien Potencier <[email protected]>
"""
def __init__(self, resource):
"""Constructor.
@param string $resource The file path to the resource
"""
if resource:
self.__resource = str(os.path.realpath(str(resource)));
else:
self.__resource = '';
def __str__(self):
"""Returns a string representation of the Resource.
@return string A string representation of the Resource
"""
return self.__resource;
def getResource(self):
"""Returns the resource tied to this Resource.
@return mixed The resource
"""
return self.__resource;
def isFresh(self, timestamp):
"""Returns true if the resource has not been updated since the given timestamp.
@param timestamp: integer The last time the resource was loaded
@return Boolean true if the resource has not been updated, false otherwise
"""
if not os.path.exists(self.__resource):
return False;
return os.path.getmtime(self.__resource) < timestamp;
def serialize(self):
return serialize(self.__resource);
def unserialize(self, serialized):
self.__resource = unserialize(serialized);
class DirectoryResource(ResourceInterface, SerializableInterface):
"""DirectoryResource represents a resources stored in a subdirectory tree.
@author Fabien Potencier <[email protected]>
"""
def __init__(self, resource, pattern = None):
"""Constructor.
@param string resource The file path to the resource
@param string pattern A pattern to restrict monitored files
"""
self.__resource = None;
self.__pattern = None;
self.__resource = resource;
self.__pattern = pattern;
def __str__(self):
"""Returns a string representation of the Resource.
@return string A string representation of the Resource
"""
return str(self.__resource);
def getResource(self):
"""Returns the resource tied to this Resource.
@return mixed The resource
"""
return self.__resource;
def getPattern(self):
return self.__pattern;
def isFresh(self, timestamp):
"""Returns True if the resource has not been updated since the given timestamp.:
@param integer timestamp The last time the resource was loaded
@return Boolean True if the resource has not been updated, False otherwise:
"""
if ( not os.path.isdir(self.__resource)) :
return False;
newestMTime = os.path.getmtime(self.__resource);
for root, dirs, files in os.walk(self.__resource, followlinks=True):
for filename in files + dirs:
filename = '/'.join([root, filename]);
# if regex filtering is enabled only check matching files:
if (self.__pattern and os.path.isfile(filename) and not re.search(self.__pattern, os.path.basename(filename))) :
continue;
# always monitor directories for changes, except the .. entries
# (otherwise deleted files wouldn't get detected)
if os.path.isdir(filename) and '/..' == filename[-3:] :
continue;
newestMTime = max(os.path.getmtime(filename), newestMTime);
return newestMTime < timestamp;
def serialize(self):
return serialize([self.__resource, self.__pattern]);
def unserialize(self, serialized):
self.__resource, self.__pattern = unserialize(serialized);