| [133] | 1 | |
|---|
| 2 | __all__ = ['BaseDetector', 'Detector'] |
|---|
| 3 | |
|---|
| 4 | import re |
|---|
| 5 | |
|---|
| 6 | class BaseDetector: |
|---|
| 7 | |
|---|
| 8 | yaml_detectors = {} |
|---|
| 9 | |
|---|
| 10 | def add_detector(cls, tag, regexp, first): |
|---|
| 11 | if not 'yaml_detectors' in cls.__dict__: |
|---|
| 12 | cls.yaml_detectors = cls.yaml_detectors.copy() |
|---|
| 13 | for ch in first: |
|---|
| 14 | cls.yaml_detectors.setdefault(ch, []).append((tag, regexp)) |
|---|
| 15 | add_detector = classmethod(add_detector) |
|---|
| 16 | |
|---|
| 17 | def detect(self, value): |
|---|
| 18 | if value == u'': |
|---|
| 19 | detectors = self.yaml_detectors.get(u'', []) |
|---|
| 20 | else: |
|---|
| 21 | detectors = self.yaml_detectors.get(value[0], []) |
|---|
| 22 | detectors += self.yaml_detectors.get(None, []) |
|---|
| 23 | for tag, regexp in detectors: |
|---|
| 24 | if regexp.match(value): |
|---|
| 25 | return tag |
|---|
| 26 | |
|---|
| 27 | class Detector(BaseDetector): |
|---|
| 28 | pass |
|---|
| 29 | |
|---|
| 30 | Detector.add_detector( |
|---|
| 31 | u'tag:yaml.org,2002:bool', |
|---|
| 32 | re.compile(ur'''^(?:yes|Yes|YES|n|N|no|No|NO |
|---|
| 33 | |true|True|TRUE|false|False|FALSE |
|---|
| 34 | |on|On|ON|off|Off|OFF)$''', re.X), |
|---|
| 35 | list(u'yYnNtTfFoO')) |
|---|
| 36 | |
|---|
| 37 | Detector.add_detector( |
|---|
| 38 | u'tag:yaml.org,2002:float', |
|---|
| 39 | re.compile(ur'''^(?:[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*(?:[eE][-+][0-9]+)? |
|---|
| 40 | |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]* |
|---|
| 41 | |[-+]?\.(?:inf|Inf|INF) |
|---|
| 42 | |\.(?:nan|NaN|NAN))$''', re.X), |
|---|
| 43 | list(u'-+0123456789.')) |
|---|
| 44 | |
|---|
| 45 | Detector.add_detector( |
|---|
| 46 | u'tag:yaml.org,2002:int', |
|---|
| 47 | re.compile(ur'''^(?:[-+]?0b[0-1_]+ |
|---|
| 48 | |[-+]?0[0-7_]+ |
|---|
| 49 | |[-+]?(?:0|[1-9][0-9_]*) |
|---|
| 50 | |[-+]?0x[0-9a-fA-F_]+ |
|---|
| 51 | |[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re.X), |
|---|
| 52 | list(u'-+0123456789')) |
|---|
| 53 | |
|---|
| 54 | Detector.add_detector( |
|---|
| 55 | u'tag:yaml.org,2002:merge', |
|---|
| 56 | re.compile(ur'^(?:<<)$'), |
|---|
| 57 | ['<']) |
|---|
| 58 | |
|---|
| 59 | Detector.add_detector( |
|---|
| 60 | u'tag:yaml.org,2002:null', |
|---|
| 61 | re.compile(ur'''^(?: ~ |
|---|
| 62 | |null|Null|NULL |
|---|
| 63 | | )$''', re.X), |
|---|
| 64 | [u'~', u'n', u'N', u'']) |
|---|
| 65 | |
|---|
| 66 | Detector.add_detector( |
|---|
| 67 | u'tag:yaml.org,2002:timestamp', |
|---|
| 68 | re.compile(ur'''^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] |
|---|
| 69 | |[0-9][0-9][0-9][0-9] -[0-9][0-9]? -[0-9][0-9]? |
|---|
| 70 | (?:[Tt]|[ \t]+)[0-9][0-9]? |
|---|
| 71 | :[0-9][0-9] :[0-9][0-9] (?:\.[0-9]*)? |
|---|
| 72 | (?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$''', re.X), |
|---|
| 73 | list(u'0123456789')) |
|---|
| 74 | |
|---|
| 75 | Detector.add_detector( |
|---|
| 76 | u'tag:yaml.org,2002:value', |
|---|
| 77 | re.compile(ur'^(?:=)$'), |
|---|
| 78 | ['=']) |
|---|
| 79 | |
|---|
| 80 | # The following detector is only for documentation purposes. It cannot work |
|---|
| 81 | # because plain scalars cannot start with '!', '&', or '*'. |
|---|
| 82 | Detector.add_detector( |
|---|
| 83 | u'tag:yaml.org,2002:yaml', |
|---|
| 84 | re.compile(ur'^(?:!|&|\*)$'), |
|---|
| 85 | list(u'!&*')) |
|---|
| 86 | |
|---|