4
4
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php
5
5
"""Module with basic data structures - they are designed to be lightweight and fast"""
6
6
from gitdb .util import bin_to_hex , suppress
7
+ from collections import namedtuple
8
+
7
9
8
10
from gitdb .fun import (
9
11
type_id_to_type_map ,
17
19
#{ ODB Bases
18
20
19
21
20
- class OInfo (tuple ):
22
+ class OInfo (namedtuple ( 'OInfo' , 'binsha, type, size' ) ):
21
23
22
24
"""Carries information about an object in an ODB, providing information
23
25
about the binary sha of the object, the type_string as well as the uncompressed size
@@ -30,109 +32,57 @@ class OInfo(tuple):
30
32
assert dbi[2] == dbi.size
31
33
32
34
The type is designed to be as lightweight as possible."""
33
- __slots__ = tuple ()
34
-
35
- def __new__ (cls , sha , type , size ):
36
- return tuple .__new__ (cls , (sha , type , size ))
37
-
38
- def __init__ (self , * args ):
39
- tuple .__init__ (self )
40
-
41
- #{ Interface
42
- @property
43
- def binsha (self ):
44
- """:return: our sha as binary, 20 bytes"""
45
- return self [0 ]
35
+ __slots__ = ()
46
36
47
37
@property
48
38
def hexsha (self ):
49
39
""":return: our sha, hex encoded, 40 bytes"""
50
- return bin_to_hex (self [0 ])
51
-
52
- @property
53
- def type (self ):
54
- return self [1 ]
40
+ return bin_to_hex (self .binsha )
55
41
56
42
@property
57
43
def type_id (self ):
58
- return type_to_type_id_map [self [ 1 ] ]
44
+ return type_to_type_id_map [self . type ]
59
45
60
- @property
61
- def size (self ):
62
- return self [2 ]
63
- #} END interface
64
46
65
-
66
- class OPackInfo (tuple ):
47
+ class OPackInfo (namedtuple ('OPackInfo' , 'pack_offset, type_id, size' )):
67
48
68
49
"""As OInfo, but provides a type_id property to retrieve the numerical type id, and
69
50
does not include a sha.
70
51
71
52
Additionally, the pack_offset is the absolute offset into the packfile at which
72
53
all object information is located. The data_offset property points to the absolute
73
54
location in the pack at which that actual data stream can be found."""
74
- __slots__ = tuple ()
75
-
76
- def __new__ (cls , packoffset , type , size ):
77
- return tuple .__new__ (cls , (packoffset , type , size ))
78
-
79
- def __init__ (self , * args ):
80
- tuple .__init__ (self )
81
-
82
- #{ Interface
83
-
84
- @property
85
- def pack_offset (self ):
86
- return self [0 ]
55
+ __slots__ = ()
87
56
88
57
@property
89
58
def type (self ):
90
- return type_id_to_type_map [self [1 ]]
91
-
92
- @property
93
- def type_id (self ):
94
- return self [1 ]
95
-
96
- @property
97
- def size (self ):
98
- return self [2 ]
99
-
100
- #} END interface
59
+ return type_id_to_type_map [self .type_id ]
101
60
102
61
103
- class ODeltaPackInfo (OPackInfo ):
62
+ class ODeltaPackInfo (namedtuple ( 'ODeltaPackInfo' , 'pack_offset, type_id, size, delta_info' ) ):
104
63
105
64
"""Adds delta specific information,
106
65
Either the 20 byte sha which points to some object in the database,
107
66
or the negative offset from the pack_offset, so that pack_offset - delta_info yields
108
67
the pack offset of the base object"""
109
- __slots__ = tuple ()
110
-
111
- def __new__ (cls , packoffset , type , size , delta_info ):
112
- return tuple .__new__ (cls , (packoffset , type , size , delta_info ))
68
+ __slots__ = ()
113
69
114
- #{ Interface
115
70
@property
116
- def delta_info (self ):
117
- return self [3 ]
118
- #} END interface
119
-
71
+ def type (self ):
72
+ return type_id_to_type_map [self .type_id ]
120
73
121
- class OStream (OInfo ):
122
74
75
+ class OStream (namedtuple ('OStream' , 'binsha type size stream' )):
123
76
"""Base for object streams retrieved from the database, providing additional
124
77
information about the stream.
125
- Generally, ODB streams are read-only as objects are immutable"""
126
- __slots__ = tuple ()
78
+ Generally, ODB streams are read-only as objects are immutable
127
79
128
- def __new__ ( cls , sha , type , size , stream , * args , ** kwargs ) :
129
- """Helps with the initialization of subclasses"""
130
- return tuple . __new__ ( cls , ( sha , type , size , stream ))
80
+ .. Note :
81
+ Is NOTE a :class:`OInfo` instance; for the effort required, see:
82
+ see http://stackoverflow.com/questions/20794182/how-to-make-a-file-like-class-work-with-isinstancecls-io-iobase
131
83
132
- def __init__ (self , * args , ** kwargs ):
133
- tuple .__init__ (self )
134
-
135
- #{ Stream Reader Interface
84
+ """
85
+ __slots__ = ()
136
86
137
87
def __enter__ (self ):
138
88
return self
@@ -148,38 +98,26 @@ def read(self, size=-1):
148
98
return self .stream .read (size )
149
99
150
100
@property
151
- def stream (self ):
152
- return self [3 ]
101
+ def hexsha (self ):
102
+ """:return: our sha, hex encoded, 40 bytes"""
103
+ return bin_to_hex (self .binsha )
153
104
154
- #} END stream reader interface
105
+ @property
106
+ def type_id (self ):
107
+ return type_to_type_id_map [self .type ]
155
108
156
109
157
110
class ODeltaStream (OStream ):
158
-
159
- """Uses size info of its stream, delaying reads"""
160
-
161
- def __new__ (cls , sha , type , size , stream , * args , ** kwargs ):
162
- """Helps with the initialization of subclasses"""
163
- return tuple .__new__ (cls , (sha , type , size , stream ))
164
-
165
- #{ Stream Reader Interface
166
-
167
111
@property
168
112
def size (self ):
169
113
return self [3 ].size
170
114
171
- #} END stream reader interface
172
-
173
115
174
- class OPackStream (OPackInfo ):
116
+ class OPackStream (namedtuple ( 'OPackStream' , 'pack_offset, type_id, size, stream' ) ):
175
117
176
118
"""Next to pack object information, a stream outputting an undeltified base object
177
119
is provided"""
178
- __slots__ = tuple ()
179
-
180
- def __new__ (cls , packoffset , type , size , stream , * args ):
181
- """Helps with the initialization of subclasses"""
182
- return tuple .__new__ (cls , (packoffset , type , size , stream ))
120
+ __slots__ = ()
183
121
184
122
def __enter__ (self ):
185
123
return self
@@ -191,23 +129,18 @@ def __exit__(self, exc_type, exc_value, traceback):
191
129
def close (self ):
192
130
self .stream .close ()
193
131
194
- #{ Stream Reader Interface
195
132
def read (self , size = - 1 ):
196
133
return self .stream .read (size )
197
134
198
135
@property
199
- def stream (self ):
200
- return self [3 ]
201
- #} END stream reader interface
136
+ def type (self ):
137
+ return type_id_to_type_map [self .type_id ]
202
138
203
139
204
- class ODeltaPackStream (ODeltaPackInfo ):
140
+ class ODeltaPackStream (namedtuple ( 'ODeltaPackStream' , 'pack_offset, type_id, size, delta_info stream' ) ):
205
141
206
142
"""Provides a stream outputting the uncompressed offset delta information"""
207
- __slots__ = tuple ()
208
-
209
- def __new__ (cls , packoffset , type , size , delta_info , stream ):
210
- return tuple .__new__ (cls , (packoffset , type , size , delta_info , stream ))
143
+ __slots__ = ()
211
144
212
145
def __enter__ (self ):
213
146
return self
@@ -219,14 +152,12 @@ def __exit__(self, exc_type, exc_value, traceback):
219
152
def close (self ):
220
153
self .stream .close ()
221
154
222
- #{ Stream Reader Interface
223
155
def read (self , size = - 1 ):
224
156
return self .stream .read (size )
225
157
226
158
@property
227
- def stream (self ):
228
- return self [4 ]
229
- #} END stream reader interface
159
+ def type (self ):
160
+ return type_id_to_type_map [self .type_id ]
230
161
231
162
232
163
class IStream (list ):
@@ -238,7 +169,7 @@ class IStream(list):
238
169
to blend in without prior conversion.
239
170
240
171
The only method your content stream must support is 'read'"""
241
- __slots__ = tuple ()
172
+ __slots__ = ()
242
173
243
174
def __new__ (cls , type , size , stream , sha = None ):
244
175
return list .__new__ (cls , (sha , type , size , stream , None ))
@@ -325,7 +256,7 @@ class InvalidOInfo(tuple):
325
256
"""Carries information about a sha identifying an object which is invalid in
326
257
the queried database. The exception attribute provides more information about
327
258
the cause of the issue"""
328
- __slots__ = tuple ()
259
+ __slots__ = ()
329
260
330
261
def __new__ (cls , sha , exc ):
331
262
return tuple .__new__ (cls , (sha , exc ))
@@ -350,7 +281,7 @@ def error(self):
350
281
class InvalidOStream (InvalidOInfo ):
351
282
352
283
"""Carries information about an invalid ODB stream"""
353
- __slots__ = tuple ()
284
+ __slots__ = ()
354
285
355
286
def __enter__ (self ):
356
287
return self
0 commit comments