File size: 13,578 Bytes
6c50d1f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | import re
import uuid
from nltk.stem.snowball import SnowballStemmer
class TargetStatus:
EMPTY = 'EMPTY'
NON_EMPTY = 'NON_EMPTY'
TRUE = "TRUE"
FALSE = "FALSE"
MIN = "MIN"
MAX = "MAX"
MIDDLE = "MIDDLE"
EXAMPLE = 'EXAMPLE'
class TargetType:
ENUM = 'ENUM'
NUM = 'NUM'
ARRAY = "ARRAY"
BOOL = "BOOL"
STRING = "STRING"
EXAMPLE = 'EXAMPLE'
class Parameter:
def __init__(self, name, body={}, method_path=""):
self.name = name
self.raw_body = body
self.has_schema = False
self.schema = None
self.method_path = method_path
self.attribute_path_dict = {}
self.parameter_id = "h" + uuid.uuid4().__str__()
self.parameter_names = set()
self.parameter_names.add(name)
self.parameter_body_tuple = {}
self.parameter_body_tuple[name] = [(name, body)]
# record_all_attribute
self.attributes = set([name])
# target consists of (type, path, value)
self.cover_targets = set()
# if name == 'delete_lun_groups_request':
# print()
self.attribute_path_dict[name] = set([name])
self.parse(body)
# extend parameter name by path resource
if self.attribute_should_extend(name):
self.parameter_names.remove(name)
self.parameter_body_tuple[name].remove((name, body))
canonical_name = self.get_canonical_name(name)
tokens = self.tokenize_method_path(method_path)
for token in tokens:
attribute_path = token + '_' + canonical_name
self.attribute_path_dict[attribute_path] = set([name])
self.parameter_names.add(attribute_path)
self.parameter_body_tuple[attribute_path] = [(attribute_path, body)]
def parse(self, body={}):
if body.__contains__("schema"):
self.has_schema = True
self.parse_schema(body["schema"])
else:
if body.__contains__("name"):
name = body["name"]
self.parameter_names.add(name)
body_tuple_list = self.parameter_body_tuple.get(name, [])
body_tuple_list.append((name, body))
self.parameter_body_tuple[name] = body_tuple_list
self.parse_schema(body)
elif body.__contains__("properties"):
self.has_schema = True
self.schema = body
self.parse_schema(self.schema["properties"])
else:
print("Body without name")
def parse_schema(self, schema={}):
if isinstance(schema, dict):
if schema.__contains__("properties") or schema.__contains__("allOf") or schema.__contains__('name'):
queue = [("", schema)]
# handle allOf case
if schema.__contains__("allOf"):
for item in schema["allOf"]:
queue.append(("", item))
while len(queue) > 0:
elem = queue.pop()
prefix = elem[0]
elem = elem[1]
if elem.__contains__("properties"):
properties = elem["properties"]
# required_properties = []
# if elem.__contains__('required'):
# required_properties = elem['required']
for name in properties.keys():
# if name in required_properties:
# properties[name]['required'] = True
# check array type
is_array = False
if properties[name].__contains__("type") and properties[name]["type"] == "array":
is_array = True
# check prefix
if len(prefix) == 0:
child_prefix = prefix
else:
child_prefix = prefix
child_prefix += "."
self.parameter_names.add(name)
# add into attribute set
attribute_path = child_prefix + name
attribute_set = self.attribute_path_dict.get(name, set())
attribute_set.add(attribute_path)
body_tuple_list = self.parameter_body_tuple.get(name, [])
body_tuple_list.append((attribute_path, properties[name]))
self.parameter_body_tuple[name] = body_tuple_list
self.attributes.add(self.name + "." + attribute_path)
self.attribute_path_dict[name] = attribute_set
# if attribute_path == 'id':
# print()
# extend property name by attribute path
if self.attribute_should_extend(name):
self.parameter_names.remove(name)
self.parameter_body_tuple[name].remove((attribute_path, properties[name]))
is_first_level_attribute = False
if len(prefix) != 0:
prefix_tokens = [self.tokenize_attribute_path(child_prefix)]
else:
prefix_tokens = self.tokenize_method_path(self.method_path)
is_first_level_attribute = True
for prefix_token in prefix_tokens:
if is_first_level_attribute:
path = prefix_token + '_' + name
else:
path = prefix_token + '_' + name
attribute_path_set = self.attribute_path_dict.get(path, set())
attribute_path_set.add(attribute_path)
self.attribute_path_dict[path] = attribute_path_set
self.parameter_names.add(path)
body_tuple_list = self.parameter_body_tuple.get(path, [])
body_tuple_list.append((path, properties[name]))
self.parameter_body_tuple[path] = body_tuple_list
# traverse inner properties
if isinstance(properties[name], dict):
if is_array:
queue.append((child_prefix + name + "[0]", properties[name]))
else:
queue.append((child_prefix + name, properties[name]))
if elem.__contains__("allOf"):
for e in elem["allOf"]:
queue.append((prefix, e))
if elem.__contains__("items"):
if not prefix.endswith('[0]'):
prefix += '[0]'
self.parameter_body_tuple[prefix] = [
(prefix, elem["items"])]
self.attribute_path_dict[prefix] = [prefix]
self.parameter_names.add(prefix)
queue.append((prefix, elem["items"]))
targets = self.cover_targets
# try:
# int(self.name + 1)
# full_path = prefix
# except Exception as ex:
# if len(prefix) == 0:
# full_path = self.name
# else:
# full_path = self.name + "." + prefix
# self.attributes.add(full_path)
if len(prefix) == 0:
continue
full_path = prefix
# handle enum targets
if elem.__contains__('enum'):
for enum in elem["enum"]:
tar = (full_path, TargetType.ENUM, enum)
targets.add(tar)
elif elem.__contains__("type"):
element_type = elem['type']
# handle array type
if element_type == 'array':
targets.add((full_path.replace('[0]', ''), TargetType.ARRAY, TargetStatus.EMPTY))
targets.add((full_path.replace('[0]', ''), TargetType.ARRAY, TargetStatus.NON_EMPTY))
# handle string type
elif element_type == 'string':
if elem.__contains__('example'):
tar = (full_path, TargetType.EXAMPLE, TargetStatus.EXAMPLE)
targets.add(tar)
# if elem.__contains__("minLength"):
# targets.add((full_path, TargetType.STRING, TargetStatus.MIN))
# if elem.__contains__("maxLength"):
# targets.add((full_path, TargetType.STRING, TargetStatus.MAX))
# if elem.__contains__("minLength") and elem.__contains__("maxLength"):
# targets.add((full_path, TargetType.STRING, TargetStatus.MIDDLE))
elif element_type == 'boolean':
targets.add((full_path, TargetType.BOOL, TargetStatus.TRUE))
targets.add((full_path, TargetType.BOOL, TargetStatus.FALSE))
elif element_type == 'integer':
if elem.__contains__('example'):
tar = (full_path, TargetType.EXAMPLE, TargetStatus.EXAMPLE)
targets.add(tar)
# if elem.__contains__("minimum"):
# targets.add((full_path, TargetType.NUM, TargetStatus.MIN))
# if elem.__contains__("maximum"):
# targets.add((full_path, TargetType.NUM, TargetStatus.MAX))
# if elem.__contains__("minimum") and elem.__contains__("maximum"):
# targets.add((full_path, TargetType.NUM, TargetStatus.MIDDLE))
elif element_type == 'number':
if elem.__contains__('example'):
tar = (full_path, TargetType.EXAMPLE, TargetStatus.EXAMPLE)
targets.add(tar)
# if elem.__contains__("minimum"):
# targets.add((full_path, TargetType.NUM, TargetStatus.MIN))
# if elem.__contains__("maximum"):
# targets.add((full_path, TargetType.NUM, TargetStatus.MAX))
# if elem.__contains__("minimum") and elem.__contains__("maximum"):
# targets.add((full_path, TargetType.NUM, TargetStatus.MIDDLE))
# elif element_type == 'object':
# pass
# else:
# print()
# for property in elem.keys():
# item = elem[property]
# if isinstance(item, dict):
# queue.append((prefix, item))
else:
return
def get_canonical_name(self, name):
stemmer = SnowballStemmer("english")
stemmed_word = stemmer.stem(name)
return stemmed_word
def tokenize_method_path(self, method_path: str):
tokens = method_path.split('/')
result = []
version_result = []
has_version = False
pattern = re.compile('^v[0-9]+$')
for token in tokens:
# filter in path parameter
if '{' in token:
continue
# remove empty string
if len(token) == 0:
continue
# check whether has version
if pattern.match(token) is not None:
has_version = True
continue
token = self.get_canonical_name(token)
# ignore base path for service
if has_version:
version_result.append(token)
else:
result.append(token)
if has_version and len(version_result) > 0:
return version_result
return result
def tokenize_attribute_path(self, attribute_path: str):
if len(attribute_path) == 0:
return ''
tokens = attribute_path.replace('[0]', '').split('.')
result = []
for token in tokens:
if len(token) == 0:
continue
result.append(self.get_canonical_name(token))
return result[-1]
def attribute_should_extend(self, attribute_name):
targets = ['id', 'name']
name = self.get_canonical_name(attribute_name)
return name in targets
def __str__(self):
return f'({self.name}, {self.schema})'
|