Show More
@@ -556,8 +556,13 b' class bundlepart(object):' | |||||
556 | handler. |
|
556 | handler. | |
557 |
|
557 | |||
558 | The part payload is contained in ``part.data``. It could be raw bytes or a |
|
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 |
|
559 | generator of byte chunks. | |
560 | generation has begun. |
|
560 | ||
|
561 | You can add parameters to the part using the ``addparam`` method. | |||
|
562 | Parameters can be either mandatory (default) or advisory. Remote side | |||
|
563 | should be able to safely ignore the advisory ones. | |||
|
564 | ||||
|
565 | Both data and parameters cannot be modified after the generation has begun. | |||
561 | """ |
|
566 | """ | |
562 |
|
567 | |||
563 | def __init__(self, parttype, mandatoryparams=(), advisoryparams=(), |
|
568 | def __init__(self, parttype, mandatoryparams=(), advisoryparams=(), | |
@@ -565,8 +570,8 b' class bundlepart(object):' | |||||
565 | self.id = None |
|
570 | self.id = None | |
566 | self.type = parttype |
|
571 | self.type = parttype | |
567 | self._data = data |
|
572 | self._data = data | |
568 | self.mandatoryparams = mandatoryparams |
|
573 | self._mandatoryparams = list(mandatoryparams) | |
569 | self.advisoryparams = advisoryparams |
|
574 | self._advisoryparams = list(advisoryparams) | |
570 | # status of the part's generation: |
|
575 | # status of the part's generation: | |
571 | # - None: not started, |
|
576 | # - None: not started, | |
572 | # - False: currently generated, |
|
577 | # - False: currently generated, | |
@@ -582,6 +587,24 b' class bundlepart(object):' | |||||
582 | return self._data |
|
587 | return self._data | |
583 | data = property(__getdata, __setdata) |
|
588 | data = property(__getdata, __setdata) | |
584 |
|
589 | |||
|
590 | @property | |||
|
591 | def mandatoryparams(self): | |||
|
592 | # make it an immutable tuple to force people through ``addparam`` | |||
|
593 | return tuple(self._mandatoryparams) | |||
|
594 | ||||
|
595 | @property | |||
|
596 | def advisoryparams(self): | |||
|
597 | # make it an immutable tuple to force people through ``addparam`` | |||
|
598 | return tuple(self._advisoryparams) | |||
|
599 | ||||
|
600 | def addparam(self, name, value='', mandatory=True): | |||
|
601 | if self._generated is not None: | |||
|
602 | raise ReadOnlyPartError('part is being generated') | |||
|
603 | params = self._advisoryparams | |||
|
604 | if mandatory: | |||
|
605 | params = self._mandatoryparams | |||
|
606 | params.append((name, value)) | |||
|
607 | ||||
585 | # methods used to generates the bundle2 stream |
|
608 | # methods used to generates the bundle2 stream | |
586 | def getchunks(self): |
|
609 | def getchunks(self): | |
587 | if self._generated is not None: |
|
610 | if self._generated is not None: |
@@ -106,10 +106,11 b' Create an extension to test bundle2 API' | |||||
106 | > bundler.newpart('test:empty') |
|
106 | > bundler.newpart('test:empty') | |
107 | > bundler.newpart('test:song', data=ELEPHANTSSONG) |
|
107 | > bundler.newpart('test:song', data=ELEPHANTSSONG) | |
108 | > bundler.newpart('test:debugreply') |
|
108 | > bundler.newpart('test:debugreply') | |
109 |
> bundler.newpart('test:math' |
|
109 | > mathpart = bundler.newpart('test:math') | |
110 | > [('pi', '3.14'), ('e', '2.72')], |
|
110 | > mathpart.addparam('pi', '3.14') | |
111 | > [('cooking', 'raw')], |
|
111 | > mathpart.addparam('e', '2.72') | |
112 | > '42') |
|
112 | > mathpart.addparam('cooking', 'raw', mandatory=False) | |
|
113 | > mathpart.data = '42' | |||
113 | > if opts['unknown']: |
|
114 | > if opts['unknown']: | |
114 | > bundler.newpart('test:UNKNOWN', data='some random content') |
|
115 | > bundler.newpart('test:UNKNOWN', data='some random content') | |
115 | > if opts['parts']: |
|
116 | > if opts['parts']: |
General Comments 0
You need to be logged in to leave comments.
Login now