Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/gitdb/base.py: 71%

138 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors 

2# 

3# This module is part of GitDB and is released under 

4# the New BSD License: http://www.opensource.org/licenses/bsd-license.php 

5"""Module with basic data structures - they are designed to be lightweight and fast""" 

6from gitdb.util import bin_to_hex 

7 

8from gitdb.fun import ( 

9 type_id_to_type_map, 

10 type_to_type_id_map 

11) 

12 

13__all__ = ('OInfo', 'OPackInfo', 'ODeltaPackInfo', 

14 'OStream', 'OPackStream', 'ODeltaPackStream', 

15 'IStream', 'InvalidOInfo', 'InvalidOStream') 

16 

17#{ ODB Bases 

18 

19 

20class OInfo(tuple): 

21 

22 """Carries information about an object in an ODB, providing information 

23 about the binary sha of the object, the type_string as well as the uncompressed size 

24 in bytes. 

25 

26 It can be accessed using tuple notation and using attribute access notation:: 

27 

28 assert dbi[0] == dbi.binsha 

29 assert dbi[1] == dbi.type 

30 assert dbi[2] == dbi.size 

31 

32 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] 

46 

47 @property 

48 def hexsha(self): 

49 """: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] 

55 

56 @property 

57 def type_id(self): 

58 return type_to_type_id_map[self[1]] 

59 

60 @property 

61 def size(self): 

62 return self[2] 

63 #} END interface 

64 

65 

66class OPackInfo(tuple): 

67 

68 """As OInfo, but provides a type_id property to retrieve the numerical type id, and 

69 does not include a sha. 

70 

71 Additionally, the pack_offset is the absolute offset into the packfile at which 

72 all object information is located. The data_offset property points to the absolute 

73 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] 

87 

88 @property 

89 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 

101 

102 

103class ODeltaPackInfo(OPackInfo): 

104 

105 """Adds delta specific information, 

106 Either the 20 byte sha which points to some object in the database, 

107 or the negative offset from the pack_offset, so that pack_offset - delta_info yields 

108 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)) 

113 

114 #{ Interface 

115 @property 

116 def delta_info(self): 

117 return self[3] 

118 #} END interface 

119 

120 

121class OStream(OInfo): 

122 

123 """Base for object streams retrieved from the database, providing additional 

124 information about the stream. 

125 Generally, ODB streams are read-only as objects are immutable""" 

126 __slots__ = tuple() 

127 

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)) 

131 

132 def __init__(self, *args, **kwargs): 

133 tuple.__init__(self) 

134 

135 #{ Stream Reader Interface 

136 

137 def read(self, size=-1): 

138 return self[3].read(size) 

139 

140 @property 

141 def stream(self): 

142 return self[3] 

143 

144 #} END stream reader interface 

145 

146 

147class ODeltaStream(OStream): 

148 

149 """Uses size info of its stream, delaying reads""" 

150 

151 def __new__(cls, sha, type, size, stream, *args, **kwargs): 

152 """Helps with the initialization of subclasses""" 

153 return tuple.__new__(cls, (sha, type, size, stream)) 

154 

155 #{ Stream Reader Interface 

156 

157 @property 

158 def size(self): 

159 return self[3].size 

160 

161 #} END stream reader interface 

162 

163 

164class OPackStream(OPackInfo): 

165 

166 """Next to pack object information, a stream outputting an undeltified base object 

167 is provided""" 

168 __slots__ = tuple() 

169 

170 def __new__(cls, packoffset, type, size, stream, *args): 

171 """Helps with the initialization of subclasses""" 

172 return tuple.__new__(cls, (packoffset, type, size, stream)) 

173 

174 #{ Stream Reader Interface 

175 def read(self, size=-1): 

176 return self[3].read(size) 

177 

178 @property 

179 def stream(self): 

180 return self[3] 

181 #} END stream reader interface 

182 

183 

184class ODeltaPackStream(ODeltaPackInfo): 

185 

186 """Provides a stream outputting the uncompressed offset delta information""" 

187 __slots__ = tuple() 

188 

189 def __new__(cls, packoffset, type, size, delta_info, stream): 

190 return tuple.__new__(cls, (packoffset, type, size, delta_info, stream)) 

191 

192 #{ Stream Reader Interface 

193 def read(self, size=-1): 

194 return self[4].read(size) 

195 

196 @property 

197 def stream(self): 

198 return self[4] 

199 #} END stream reader interface 

200 

201 

202class IStream(list): 

203 

204 """Represents an input content stream to be fed into the ODB. It is mutable to allow 

205 the ODB to record information about the operations outcome right in this instance. 

206 

207 It provides interfaces for the OStream and a StreamReader to allow the instance 

208 to blend in without prior conversion. 

209 

210 The only method your content stream must support is 'read'""" 

211 __slots__ = tuple() 

212 

213 def __new__(cls, type, size, stream, sha=None): 

214 return list.__new__(cls, (sha, type, size, stream, None)) 

215 

216 def __init__(self, type, size, stream, sha=None): 

217 list.__init__(self, (sha, type, size, stream, None)) 

218 

219 #{ Interface 

220 @property 

221 def hexsha(self): 

222 """:return: our sha, hex encoded, 40 bytes""" 

223 return bin_to_hex(self[0]) 

224 

225 def _error(self): 

226 """:return: the error that occurred when processing the stream, or None""" 

227 return self[4] 

228 

229 def _set_error(self, exc): 

230 """Set this input stream to the given exc, may be None to reset the error""" 

231 self[4] = exc 

232 

233 error = property(_error, _set_error) 

234 

235 #} END interface 

236 

237 #{ Stream Reader Interface 

238 

239 def read(self, size=-1): 

240 """Implements a simple stream reader interface, passing the read call on 

241 to our internal stream""" 

242 return self[3].read(size) 

243 

244 #} END stream reader interface 

245 

246 #{ interface 

247 

248 def _set_binsha(self, binsha): 

249 self[0] = binsha 

250 

251 def _binsha(self): 

252 return self[0] 

253 

254 binsha = property(_binsha, _set_binsha) 

255 

256 def _type(self): 

257 return self[1] 

258 

259 def _set_type(self, type): 

260 self[1] = type 

261 

262 type = property(_type, _set_type) 

263 

264 def _size(self): 

265 return self[2] 

266 

267 def _set_size(self, size): 

268 self[2] = size 

269 

270 size = property(_size, _set_size) 

271 

272 def _stream(self): 

273 return self[3] 

274 

275 def _set_stream(self, stream): 

276 self[3] = stream 

277 

278 stream = property(_stream, _set_stream) 

279 

280 #} END odb info interface 

281 

282 

283class InvalidOInfo(tuple): 

284 

285 """Carries information about a sha identifying an object which is invalid in 

286 the queried database. The exception attribute provides more information about 

287 the cause of the issue""" 

288 __slots__ = tuple() 

289 

290 def __new__(cls, sha, exc): 

291 return tuple.__new__(cls, (sha, exc)) 

292 

293 def __init__(self, sha, exc): 

294 tuple.__init__(self, (sha, exc)) 

295 

296 @property 

297 def binsha(self): 

298 return self[0] 

299 

300 @property 

301 def hexsha(self): 

302 return bin_to_hex(self[0]) 

303 

304 @property 

305 def error(self): 

306 """:return: exception instance explaining the failure""" 

307 return self[1] 

308 

309 

310class InvalidOStream(InvalidOInfo): 

311 

312 """Carries information about an invalid ODB stream""" 

313 __slots__ = tuple() 

314 

315#} END ODB Bases