comparison mercurial/bundle2.py @ 21604:c399bf961cb9

bundle2: the ability to set ``data`` attribute of the part is now official We make it safe to set the data attribute after part creation. It is an allowed operation as long as the part has not started to be generated.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Thu, 22 May 2014 11:21:26 -0700
parents 31be5a6fa716
children f9dabfceb259
comparison
equal deleted inserted replaced
21603:31be5a6fa716 21604:c399bf961cb9
552 class bundlepart(object): 552 class bundlepart(object):
553 """A bundle2 part contains application level payload 553 """A bundle2 part contains application level payload
554 554
555 The part `type` is used to route the part to the application level 555 The part `type` is used to route the part to the application level
556 handler. 556 handler.
557
558 The part payload is contained in ``part.data``. It could be raw bytes or a
559 generator of byte chunks. The data attribute cannot be modified after the
560 generation has begun.
557 """ 561 """
558 562
559 def __init__(self, parttype, mandatoryparams=(), advisoryparams=(), 563 def __init__(self, parttype, mandatoryparams=(), advisoryparams=(),
560 data=''): 564 data=''):
561 self.id = None 565 self.id = None
562 self.type = parttype 566 self.type = parttype
563 self.data = data 567 self._data = data
564 self.mandatoryparams = mandatoryparams 568 self.mandatoryparams = mandatoryparams
565 self.advisoryparams = advisoryparams 569 self.advisoryparams = advisoryparams
566 # status of the part's generation: 570 # status of the part's generation:
567 # - None: not started, 571 # - None: not started,
568 # - False: currently generated, 572 # - False: currently generated,
569 # - True: generation done. 573 # - True: generation done.
570 self._generated = None 574 self._generated = None
575
576 # methods used to defines the part content
577 def __setdata(self, data):
578 if self._generated is not None:
579 raise ReadOnlyPartError('part is being generated')
580 self._data = data
581 def __getdata(self):
582 return self._data
583 data = property(__getdata, __setdata)
571 584
572 # methods used to generates the bundle2 stream 585 # methods used to generates the bundle2 stream
573 def getchunks(self): 586 def getchunks(self):
574 if self._generated is not None: 587 if self._generated is not None:
575 raise RuntimeError('part can only be consumed once') 588 raise RuntimeError('part can only be consumed once')