From a30b8e8d69e7522a0347bc73b68c4e8e9be609ee Mon Sep 17 00:00:00 2001 From: Caroline Russell Date: Mon, 15 Jan 2024 11:51:46 -0500 Subject: [PATCH] Fix match statements, remove package name from endpoints. --- atom_tools/lib/slices.py | 51 +++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/atom_tools/lib/slices.py b/atom_tools/lib/slices.py index 3159747..aed7473 100644 --- a/atom_tools/lib/slices.py +++ b/atom_tools/lib/slices.py @@ -63,11 +63,11 @@ def import_slice(filename): return content.get('objectSlices', []), content.get( 'userDefinedTypes', [] ) - except [json.decoder.JSONDecodeError, UnicodeDecodeError]: - logging.warning( - f'Failed to load usages slice: {filename}\nPlease check ' - f'that you specified a valid json file.' - ) + # except [json.decoder.JSONDecodeError, UnicodeDecodeError]: + # logging.warning( + # f'Failed to load usages slice: {filename}\nPlease check ' + # f'that you specified a valid json file.' + # ) except FileNotFoundError: logging.warning( f'Failed to locate the usages slice file in the location ' @@ -91,20 +91,13 @@ def generate_endpoints(self): """ endpoints = [] for object_slice in self.object_slices: - parent = ( - '/' + object_slice.get('fullName', '') - .split(':')[0] - # .split('.')[-1] - .rstrip('/') - ) - endpoints.extend( - self.extract_endpoints_from_usages( - object_slice.get('usages', []), parent) + endpoints.extend(self.extract_endpoints_from_usages( + object_slice.get('usages', [])) ) endpoints.extend(self.extract_endpoints_from_udts()) return list(set(endpoints)) - def extract_endpoints(self, code, pkg): + def extract_endpoints(self, code): """ Extracts endpoints from the given code based on the specified language. @@ -120,22 +113,25 @@ def extract_endpoints(self, code, pkg): return endpoints matches = re.findall(UsageSlice.ENDPOINTS_REGEX, code) or [] match self.language: - case 'java', 'jar': + case 'java' | 'jar': if code.startswith('@') and ( 'Mapping' in code or 'Path' in code) and '(' in code: endpoints.extend( [ - pkg + v.replace('"', '').replace("'", "") + f'/{v.replace('"', '') + .replace("'", "") + .lstrip('/')}' for v in matches if v and not v.startswith(".") and "/" in v and not v.startswith("@") ] ) - case 'js', 'ts', 'javascript', 'typescript': + case 'js' | 'ts' | 'javascript' | 'typescript': if 'app.' in code or 'route' in code: endpoints.extend( [ - pkg + v.replace('"', '').replace("'", "") + f'/{v.replace('"', '') + .replace("'", "").lstrip('/')}' for v in matches if v and not v.startswith(".") and '/' in v and not v.startswith('@') @@ -145,12 +141,15 @@ def extract_endpoints(self, code, pkg): ) case _: endpoints.extend([ - pkg + v.replace('"', '').replace("'", "").replace('\n', '') + f'/{v.replace('"', '') + .replace("'", "") + .replace('\n', '') + .lstrip('/')}' for v in matches or [] if len(v) > 2 and '/' in v ]) return endpoints - def extract_endpoints_from_usages(self, usages, pkg): + def extract_endpoints_from_usages(self, usages): """ Extracts endpoints from the given list of usages. @@ -167,14 +166,14 @@ def extract_endpoints_from_usages(self, usages, pkg): defined_by = usage.get('definedBy', {}) invoked_calls = usage.get('invokedCalls', []) if resolved_method := target_obj.get('resolvedMethod'): - endpoints.extend(self.extract_endpoints(resolved_method, pkg)) + endpoints.extend(self.extract_endpoints(resolved_method)) elif resolved_method := defined_by.get('resolvedMethod'): - endpoints.extend(self.extract_endpoints(resolved_method, pkg)) + endpoints.extend(self.extract_endpoints(resolved_method)) if invoked_calls: for call in invoked_calls: if resolved_method := call.get('resolvedMethod'): endpoints.extend( - self.extract_endpoints(resolved_method, pkg)) + self.extract_endpoints(resolved_method)) return endpoints def extract_endpoints_from_udts(self): @@ -186,11 +185,9 @@ def extract_endpoints_from_udts(self): """ endpoints = [] for udt in self.user_defined_types: - pkg = udt.get('name') if fields := udt.get('fields'): for f in fields: - endpoints.extend( - self.extract_endpoints(f.get('name'), pkg)) + endpoints.extend(self.extract_endpoints(f.get('name'))) return endpoints