~jelmer/dulwich/lp-pqm

« back to all changes in this revision

Viewing changes to dulwich/objects.py

  • Committer: Jelmer Vernooij
  • Date: 2012-02-01 22:13:51 UTC
  • mfrom: (413.11.554)
  • Revision ID: jelmer@samba.org-20120201221351-b3n2p9zttzh62dwu
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
"""Access to base git objects."""
21
21
 
22
 
 
23
22
import binascii
24
23
from cStringIO import (
25
24
    StringIO,
64
63
S_IFGITLINK = 0160000
65
64
 
66
65
def S_ISGITLINK(m):
 
66
    """Check if a mode indicates a submodule.
 
67
 
 
68
    :param m: Mode to check
 
69
    :return: a `boolean`
 
70
    """
67
71
    return (stat.S_IFMT(m) == S_IFGITLINK)
68
72
 
69
73
 
114
118
 
115
119
 
116
120
def serializable_property(name, docstring=None):
 
121
    """A property that helps tracking whether serialization is necessary.
 
122
    """
117
123
    def set(obj, value):
118
124
        obj._ensure_parsed()
119
125
        setattr(obj, "_"+name, value)
135
141
 
136
142
 
137
143
def check_hexsha(hex, error_msg):
 
144
    """Check if a string is a valid hex sha string.
 
145
 
 
146
    :param hex: Hex string to check
 
147
    :param error_msg: Error message to use in exception
 
148
    :raise ObjectFormatException: Raised when the string is not valid
 
149
    """
138
150
    try:
139
151
        hex_to_sha(hex)
140
152
    except (TypeError, AssertionError):
168
180
        self._sha = hex_to_sha(hexsha)
169
181
 
170
182
    def digest(self):
 
183
        """Return the raw SHA digest."""
171
184
        return self._sha
172
185
 
173
186
    def hexdigest(self):
 
187
        """Return the hex SHA digest."""
174
188
        return self._hexsha
175
189
 
176
190
 
213
227
        self.set_raw_string(text[header_end+1:])
214
228
 
215
229
    def as_legacy_object_chunks(self):
 
230
        """Return chunks representing the object in the experimental format.
 
231
 
 
232
        :return: List of strings
 
233
        """
216
234
        compobj = zlib.compressobj()
217
235
        yield compobj.compress(self._header())
218
236
        for chunk in self.as_raw_chunks():
220
238
        yield compobj.flush()
221
239
 
222
240
    def as_legacy_object(self):
 
241
        """Return string representing the object in the experimental format.
 
242
        """
223
243
        return "".join(self.as_legacy_object_chunks())
224
244
 
225
245
    def as_raw_chunks(self):
 
246
        """Return chunks with serialization of the object.
 
247
 
 
248
        :return: List of strings, not necessarily one per line
 
249
        """
226
250
        if self._needs_parsing:
227
251
            self._ensure_parsed()
228
252
        elif self._needs_serialization:
230
254
        return self._chunked_text
231
255
 
232
256
    def as_raw_string(self):
 
257
        """Return raw string with serialization of the object.
 
258
 
 
259
        :return: String object
 
260
        """
233
261
        return "".join(self.as_raw_chunks())
234
262
 
235
263
    def __str__(self):
 
264
        """Return raw string serialization of this object."""
236
265
        return self.as_raw_string()
237
266
 
238
267
    def __hash__(self):
 
268
        """Return unique hash for this object."""
239
269
        return hash(self.id)
240
270
 
241
271
    def as_pretty_string(self):
 
272
        """Return a string representing this object, fit for display."""
242
273
        return self.as_raw_string()
243
274
 
244
275
    def _ensure_parsed(self):
256
287
            self._needs_parsing = False
257
288
 
258
289
    def set_raw_string(self, text):
 
290
        """Set the contents of this object from a serialized string."""
259
291
        if type(text) != str:
260
292
            raise TypeError(text)
261
293
        self.set_raw_chunks([text])
262
294
 
263
295
    def set_raw_chunks(self, chunks):
 
296
        """Set the contents of this object from a list of chunks."""
264
297
        self._chunked_text = chunks
265
298
        self._deserialize(chunks)
266
299
        self._sha = None
339
372
 
340
373
    @classmethod
341
374
    def from_path(cls, path):
 
375
        """Open a SHA file from disk."""
342
376
        f = GitFile(path, 'rb')
343
377
        try:
344
378
            obj = cls.from_file(f)
454
488
 
455
489
    @property
456
490
    def id(self):
 
491
        """The hex SHA of this object."""
457
492
        return self.sha().hexdigest()
458
493
 
459
494
    def get_type(self):
 
495
        """Return the type number for this object class."""
460
496
        return self.type_num
461
497
 
462
498
    def set_type(self, type):
 
499
        """Set the type number for this object class."""
463
500
        self.type_num = type
464
501
 
465
502
    # DEPRECATED: use type_num or type_name as needed.
557
594
 
558
595
 
559
596
def parse_tag(text):
 
597
    """Parse a tag object."""
560
598
    return _parse_tag_or_commit(text)
561
599
 
562
600