A header_factory and header registry.
| 560 | } |
| 561 | |
| 562 | class HeaderRegistry: |
| 563 | |
| 564 | """A header_factory and header registry.""" |
| 565 | |
| 566 | def __init__(self, base_class=BaseHeader, default_class=UnstructuredHeader, |
| 567 | use_default_map=True): |
| 568 | """Create a header_factory that works with the Policy API. |
| 569 | |
| 570 | base_class is the class that will be the last class in the created |
| 571 | header class's __bases__ list. default_class is the class that will be |
| 572 | used if "name" (see __call__) does not appear in the registry. |
| 573 | use_default_map controls whether or not the default mapping of names to |
| 574 | specialized classes is copied in to the registry when the factory is |
| 575 | created. The default is True. |
| 576 | |
| 577 | """ |
| 578 | self.registry = {} |
| 579 | self.base_class = base_class |
| 580 | self.default_class = default_class |
| 581 | if use_default_map: |
| 582 | self.registry.update(_default_header_map) |
| 583 | |
| 584 | def map_to_type(self, name, cls): |
| 585 | """Register cls as the specialized class for handling "name" headers. |
| 586 | |
| 587 | """ |
| 588 | self.registry[name.lower()] = cls |
| 589 | |
| 590 | def __getitem__(self, name): |
| 591 | cls = self.registry.get(name.lower(), self.default_class) |
| 592 | return type('_'+cls.__name__, (cls, self.base_class), {}) |
| 593 | |
| 594 | def __call__(self, name, value): |
| 595 | """Create a header instance for header 'name' from 'value'. |
| 596 | |
| 597 | Creates a header instance by creating a specialized class for parsing |
| 598 | and representing the specified header by combining the factory |
| 599 | base_class with a specialized class from the registry or the |
| 600 | default_class, and passing the name and value to the constructed |
| 601 | class's constructor. |
| 602 | |
| 603 | """ |
| 604 | return self[name](name, value) |
no outgoing calls