|
1 | 1 | # -*- coding: utf-8 -*- |
| 2 | +# pylint: disable=too-many-lines |
2 | 3 |
|
3 | 4 | import abc |
4 | 5 | import collections |
5 | 6 | import datetime |
| 7 | +import enum |
6 | 8 | import json |
7 | 9 |
|
8 | 10 | import attr |
@@ -791,6 +793,63 @@ def _get_header_value_list_class(cls): |
791 | 793 | return NameValuePairListSemicolonSeparated |
792 | 794 |
|
793 | 795 |
|
| 796 | +class MimeTypeRegistry(enum.Enum): |
| 797 | + APPLICATION = 'application' |
| 798 | + AUDIO = 'audio' |
| 799 | + FONT = 'font' |
| 800 | + EXAMPLE = 'example' |
| 801 | + IMAGE = 'image' |
| 802 | + MESSAGE = 'message' |
| 803 | + MODEL = 'model' |
| 804 | + MULTIPART = 'multipart' |
| 805 | + TEXT = 'text' |
| 806 | + VIDEO = 'video' |
| 807 | + |
| 808 | + |
| 809 | +@attr.s |
| 810 | +class FieldValueMimeType(FieldValueComponentBase): |
| 811 | + type = attr.ib( |
| 812 | + validator=attr.validators.instance_of(six.string_types), |
| 813 | + default=None, |
| 814 | + ) |
| 815 | + registry = attr.ib( |
| 816 | + validator=attr.validators.optional(attr.validators.instance_of(MimeTypeRegistry)), |
| 817 | + default=None, |
| 818 | + ) |
| 819 | + |
| 820 | + def __str__(self): |
| 821 | + return '{}/{}'.format(self.registry.value, self.type) |
| 822 | + |
| 823 | + @property |
| 824 | + def value(self): |
| 825 | + return self |
| 826 | + |
| 827 | + @classmethod |
| 828 | + def get_canonical_name(cls): |
| 829 | + return '' |
| 830 | + |
| 831 | + @classmethod |
| 832 | + def _parse(cls, parsable): |
| 833 | + parser = ParserText(parsable) |
| 834 | + |
| 835 | + parser.parse_string_until_separator('registry', '/', item_class=MimeTypeRegistry) |
| 836 | + parser.parse_separator('/') |
| 837 | + parser.parse_string_by_length('type', parser.unparsed_length) |
| 838 | + |
| 839 | + return FieldValueMimeType(**parser), parser.parsed_length |
| 840 | + |
| 841 | + def compose(self): |
| 842 | + composer = ComposerText() |
| 843 | + |
| 844 | + composer.compose_string(str(self)) |
| 845 | + |
| 846 | + return composer.composed |
| 847 | + |
| 848 | + @classmethod |
| 849 | + def _check_name(cls, name): |
| 850 | + pass |
| 851 | + |
| 852 | + |
794 | 853 | @attr.s |
795 | 854 | class FieldValueSingleBase(FieldValueBase, Serializable): |
796 | 855 | value = attr.ib() |
@@ -916,6 +975,32 @@ def _as_markdown(self, level): |
916 | 975 | return self._markdown_result(self.value, level) |
917 | 976 |
|
918 | 977 |
|
| 978 | +class FieldValueStringBySeparatorBase(FieldValueSingleComplexBase): |
| 979 | + @classmethod |
| 980 | + @abc.abstractmethod |
| 981 | + def _get_separators(cls): |
| 982 | + raise NotImplementedError() |
| 983 | + |
| 984 | + @classmethod |
| 985 | + def _get_value_type(cls): |
| 986 | + return six.string_types |
| 987 | + |
| 988 | + @classmethod |
| 989 | + def _parse(cls, parsable): |
| 990 | + parser = ParserText(parsable) |
| 991 | + |
| 992 | + parser.parse_string_until_separator_or_end('value', cls._get_separators()) |
| 993 | + |
| 994 | + return cls(parser['value']), parser.parsed_length |
| 995 | + |
| 996 | + def compose(self): |
| 997 | + composer = ComposerText() |
| 998 | + |
| 999 | + composer.compose_string(self.value) |
| 1000 | + |
| 1001 | + return composer.composed |
| 1002 | + |
| 1003 | + |
919 | 1004 | class FieldValueStringEnum(FieldValueSingleComplexBase): |
920 | 1005 | @classmethod |
921 | 1006 | @abc.abstractmethod |
|
0 commit comments