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