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