1from .draft06 import CodeGeneratorDraft06
2
3
4class CodeGeneratorDraft07(CodeGeneratorDraft06):
5 FORMAT_REGEXS = dict(CodeGeneratorDraft06.FORMAT_REGEXS, **{
6 'date': r'^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})\Z',
7 'iri': r'^\w+:(\/?\/?)[^\s]+\Z',
8 'iri-reference': r'^(\w+:(\/?\/?))?[^#\\\s]*(#[^\\\s]*)?\Z',
9 'idn-email': r'^[^@]+@[^@]+\.[^@]+\Z',
10 #'idn-hostname': r'',
11 'relative-json-pointer': r'^(?:0|[1-9][0-9]*)(?:#|(?:\/(?:[^~/]|~0|~1)*)*)\Z',
12 #'regex': r'',
13 'time': (
14 r'^(?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
15 r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6}))?'
16 r'([zZ]|[+-]\d\d:\d\d)?)?\Z'
17 ),
18 })
19
20 def __init__(self, definition, resolver=None, formats={}, use_default=True, use_formats=True):
21 super().__init__(definition, resolver, formats, use_default, use_formats)
22 # pylint: disable=duplicate-code
23 self._json_keywords_to_function.update((
24 ('if', self.generate_if_then_else),
25 ('contentEncoding', self.generate_content_encoding),
26 ('contentMediaType', self.generate_content_media_type),
27 ))
28
29 def generate_if_then_else(self):
30 """
31 Implementation of if-then-else.
32
33 .. code-block:: python
34
35 {
36 'if': {
37 'exclusiveMaximum': 0,
38 },
39 'then': {
40 'minimum': -10,
41 },
42 'else': {
43 'multipleOf': 2,
44 },
45 }
46
47 Valid values are any between -10 and 0 or any multiplication of two.
48 """
49 with self.l('try:', optimize=False):
50 self.generate_func_code_block(
51 self._definition['if'],
52 self._variable,
53 self._variable_name,
54 clear_variables=True
55 )
56 with self.l('except JsonSchemaValueException:'):
57 if 'else' in self._definition:
58 self.generate_func_code_block(
59 self._definition['else'],
60 self._variable,
61 self._variable_name,
62 clear_variables=True
63 )
64 else:
65 self.l('pass')
66 if 'then' in self._definition:
67 with self.l('else:'):
68 self.generate_func_code_block(
69 self._definition['then'],
70 self._variable,
71 self._variable_name,
72 clear_variables=True
73 )
74
75 def generate_content_encoding(self):
76 """
77 Means decoding value when it's encoded by base64.
78
79 .. code-block:: python
80
81 {
82 'contentEncoding': 'base64',
83 }
84 """
85 if self._definition['contentEncoding'] == 'base64':
86 with self.l('if isinstance({variable}, str):'):
87 with self.l('try:'):
88 self.l('import base64')
89 self.l('{variable} = base64.b64decode({variable})')
90 with self.l('except Exception:'):
91 self.exc('{name} must be encoded by base64')
92 with self.l('if {variable} == "":'):
93 self.exc('contentEncoding must be base64')
94
95 def generate_content_media_type(self):
96 """
97 Means loading value when it's specified as JSON.
98
99 .. code-block:: python
100
101 {
102 'contentMediaType': 'application/json',
103 }
104 """
105 if self._definition['contentMediaType'] == 'application/json':
106 with self.l('if isinstance({variable}, bytes):'):
107 with self.l('try:'):
108 self.l('{variable} = {variable}.decode("utf-8")')
109 with self.l('except Exception:'):
110 self.exc('{name} must encoded by utf8')
111 with self.l('if isinstance({variable}, str):'):
112 with self.l('try:'):
113 self.l('import json')
114 self.l('{variable} = json.loads({variable})')
115 with self.l('except Exception:'):
116 self.exc('{name} must be valid JSON')