10
10
11
11
12
12
class SlidingWindowMapBuffer (object ):
13
- """A buffer like object which allows direct byte-wise object and slicing into
13
+ """A buffer like object which allows direct byte-wise object and slicing into
14
14
memory of a mapped file. The mapping is controlled by the provided cursor.
15
-
16
- The buffer is relative, that is if you map an offset, index 0 will map to the
15
+
16
+ The buffer is relative, that is if you map an offset, index 0 will map to the
17
17
first byte at the offset you used during initialization or begin_access
18
-
18
+
19
19
**Note:** Although this type effectively hides the fact that there are mapped windows
20
20
underneath, it can unfortunately not be used in any non-pure python method which
21
21
needs a buffer or string"""
22
22
__slots__ = (
23
23
'_c' , # our cursor
24
24
'_size' , # our supposed size
25
25
)
26
-
27
-
28
- def __init__ (self , cursor = None , offset = 0 , size = sys .maxsize , flags = 0 ):
26
+
27
+ def __init__ (self , cursor = None , offset = 0 , size = sys .maxsize , flags = 0 ):
29
28
"""Initalize the instance to operate on the given cursor.
30
29
:param cursor: if not None, the associated cursor to the file you want to access
31
- If None, you have call begin_access before using the buffer and provide a cursor
30
+ If None, you have call begin_access before using the buffer and provide a cursor
32
31
:param offset: absolute offset in bytes
33
32
:param size: the total size of the mapping. Defaults to the maximum possible size
34
33
From that point on, the __len__ of the buffer will be the given size or the file size.
@@ -44,10 +43,10 @@ def __init__(self, cursor = None, offset = 0, size = sys.maxsize, flags = 0):
44
43
45
44
def __del__ (self ):
46
45
self .end_access ()
47
-
46
+
48
47
def __len__ (self ):
49
48
return self ._size
50
-
49
+
51
50
def __getitem__ (self , i ):
52
51
if isinstance (i , slice ):
53
52
return self .__getslice__ (i .start or 0 , i .stop or self ._size )
@@ -59,10 +58,10 @@ def __getitem__(self, i):
59
58
c .use_region (i , 1 )
60
59
# END handle region usage
61
60
return c .buffer ()[i - c .ofs_begin ()]
62
-
61
+
63
62
def __getslice__ (self , i , j ):
64
63
c = self ._c
65
- # fast path, slice fully included - safes a concatenate operation and
64
+ # fast path, slice fully included - safes a concatenate operation and
66
65
# should be the default
67
66
assert c .is_valid ()
68
67
if i < 0 :
@@ -91,18 +90,18 @@ def __getslice__(self, i, j):
91
90
return bytes ().join (md )
92
91
# END fast or slow path
93
92
#{ Interface
94
-
95
- def begin_access (self , cursor = None , offset = 0 , size = sys .maxsize , flags = 0 ):
93
+
94
+ def begin_access (self , cursor = None , offset = 0 , size = sys .maxsize , flags = 0 ):
96
95
"""Call this before the first use of this instance. The method was already
97
96
called by the constructor in case sufficient information was provided.
98
-
97
+
99
98
For more information no the parameters, see the __init__ method
100
- :param path: if cursor is None the existing one will be used.
99
+ :param path: if cursor is None the existing one will be used.
101
100
:return: True if the buffer can be used"""
102
101
if cursor :
103
102
self ._c = cursor
104
103
#END update our cursor
105
-
104
+
106
105
# reuse existing cursors if possible
107
106
if self ._c is not None and self ._c .is_associated ():
108
107
res = self ._c .use_region (offset , size , flags ).is_valid ()
@@ -114,27 +113,25 @@ def begin_access(self, cursor = None, offset = 0, size = sys.maxsize, flags = 0)
114
113
if size > self ._c .file_size ():
115
114
size = self ._c .file_size () - offset
116
115
#END handle size
117
- self ._size = size
116
+ self ._size = size
118
117
#END set size
119
118
return res
120
119
# END use our cursor
121
120
return False
122
-
121
+
123
122
def end_access (self ):
124
- """Call this method once you are done using the instance. It is automatically
123
+ """Call this method once you are done using the instance. It is automatically
125
124
called on destruction, and should be called just in time to allow system
126
125
resources to be freed.
127
-
126
+
128
127
Once you called end_access, you must call begin access before reusing this instance!"""
129
128
self ._size = 0
130
129
if self ._c is not None :
131
130
self ._c .unuse_region ()
132
131
#END unuse region
133
-
132
+
134
133
def cursor (self ):
135
134
""":return: the currently set cursor which provides access to the data"""
136
135
return self ._c
137
-
138
- #}END interface
139
-
140
136
137
+ #}END interface
0 commit comments