From bbfc77516ace4ba239c41e6e3120e1075f6e1975 Mon Sep 17 00:00:00 2001 From: Fawwaz Anugrah Wiradhika Dharmasatya <anugrahdwfawwaz@gmail.com> Date: Sun, 9 Jun 2024 16:34:54 +0700 Subject: [PATCH] refactor: move separator to const --- src/lib/ACLAnalyzer.py | 5 ++--- src/lib/ACReader.py | 4 +++- src/lib/CFGGenerator.py | 5 ++--- src/lib/Const.py | 2 ++ src/lib/DependencyManager.py | 5 ++--- src/lib/FileReader.py | 8 +++----- src/lib/RouteSanitizationAnalyzer.py | 5 ++--- tests/tc1/auth.py | 2 +- tests/tc1/class_views.py | 2 +- tests/tc1/requirements.txt | 2 ++ tests/tc1/views.py | 2 +- 11 files changed, 21 insertions(+), 21 deletions(-) create mode 100644 src/lib/Const.py create mode 100644 tests/tc1/requirements.txt diff --git a/src/lib/ACLAnalyzer.py b/src/lib/ACLAnalyzer.py index ef6d8d6..c6aa88c 100644 --- a/src/lib/ACLAnalyzer.py +++ b/src/lib/ACLAnalyzer.py @@ -3,7 +3,7 @@ from lib.ACReader import ACLData from lib.utils import format_log,flatten_node from tree_sitter import Node from .CFGGenerator import CFGGenerator,CFG -from sys import platform +from .Const import PATH_SEPARATOR class ACLAnalyzer(): def __init__(self,project:ProjectInfo,acl_info:ACLData) -> None: self.project_info:ProjectInfo = project @@ -481,8 +481,7 @@ class ACLAnalyzer(): if dependency[0 ]> route_position: break dependency_parts = dependency[1]['original'] # Module name - replacement = "\\" if platform.startswith("win") else "/" - if dependency_parts not in acl_class.location.replace(replacement,"."): + if dependency_parts not in acl_class.location.replace(PATH_SEPARATOR,"."): # Modul nya salah continue # Yang diimport class nya diff --git a/src/lib/ACReader.py b/src/lib/ACReader.py index 5cc45e7..fd27005 100644 --- a/src/lib/ACReader.py +++ b/src/lib/ACReader.py @@ -15,14 +15,16 @@ class ACLData(): class ACReader(): - def __init__(self,path:str) -> None: + def __init__(self,path:str,req_file:str|None=None) -> None: self.acl_path:str = path + self.req_file:str = req_file # @ACL def read(self) -> ACLData: format_log("Reading ACL File...") principal_list:list[str] = [] route_acl:dict[str,list[str]] = {} acl_context:dict[str,list|dict] = {} + # Baca File ACL with open(self.acl_path) as acl_file: line_type = None for line in acl_file: diff --git a/src/lib/CFGGenerator.py b/src/lib/CFGGenerator.py index 669cc4c..e2272cf 100644 --- a/src/lib/CFGGenerator.py +++ b/src/lib/CFGGenerator.py @@ -4,7 +4,7 @@ from lib.utils import format_log from tree_sitter import Node,TreeCursor from typing import Optional from .DependencyManager import FileInformation -from sys import platform +from .Const import PATH_SEPARATOR class CFGGenerator(): def __init__(self): @@ -17,8 +17,7 @@ class CFGGenerator(): ast,cursor,source_code = self.ast_parser.generate(ctx) method_list = self.ast_parser.get_all_function_info(source_code,ctx.location) deps_list = self.ast_parser.get_dependency_list(source_code,method_list) - replacement = "\\" if platform.startswith("win") else "/" - dependency = FileInformation(ctx.location.split(replacement)[-1],ctx.location,deps_list,method_list) + dependency = FileInformation(ctx.location.split(PATH_SEPARATOR)[-1],ctx.location,deps_list,method_list) return CFG(ast,ctx,cursor,source_code),dependency diff --git a/src/lib/Const.py b/src/lib/Const.py new file mode 100644 index 0000000..96a0e7e --- /dev/null +++ b/src/lib/Const.py @@ -0,0 +1,2 @@ +from sys import platform +PATH_SEPARATOR = "\\" if platform.startswith("win") else "/" \ No newline at end of file diff --git a/src/lib/DependencyManager.py b/src/lib/DependencyManager.py index cfc4b29..197ea14 100644 --- a/src/lib/DependencyManager.py +++ b/src/lib/DependencyManager.py @@ -1,4 +1,4 @@ -from sys import platform +from .Const import PATH_SEPARATOR class FileInformation(): def __init__(self,name,path,dependency_list,method_list): @@ -25,8 +25,7 @@ class DependencyManager(): self.informations.append(information) def get(self,path:str)->FileInformation|None: - replacement = "\\" if platform.startswith("win") else "/" - name = path.split(replacement)[-1] + name = path.split(PATH_SEPARATOR)[-1] for info in self.informations: if info.name == name and info.path == path: return info diff --git a/src/lib/FileReader.py b/src/lib/FileReader.py index 7bdaa78..b314569 100644 --- a/src/lib/FileReader.py +++ b/src/lib/FileReader.py @@ -4,7 +4,7 @@ from lib.utils import format_log from typing import Optional from lib.CFGGenerator import CFGGenerator,CFG from lib.custom_class import ElementContext -from sys import platform +from .Const import PATH_SEPARATOR from .DependencyManager import DependencyManager,FileInformation class FileReader: ANNOTATION_PATTERN = "\S*@.+" @@ -135,8 +135,7 @@ class FileReader: project_info.route_class.append(ElementContext(lines[i].strip().split(" ")[1],'function',file,additional_context)) else: # Dapatkan konteks berupa cfg setiap kelas - replacement = "\\" if platform.startswith("win") else "/" - project_info.route_class.append(ElementContext(".".join(file.split(replacement)[-1].split(".")[:-1]),'module',file,additional_context)) + project_info.route_class.append(ElementContext(".".join(file.split(PATH_SEPARATOR)[-1].split(".")[:-1]),'module',file,additional_context)) # Generate CFG jika ini rute cfg,deps = self.cfg_generator.generate(project_info.route_class[-1]) project_info.route_class[-1].set_cfg(cfg) @@ -162,8 +161,7 @@ class FileReader: for root, dirs, files in os.walk(self.project_path): for file in files: if(self.is_source_code_supported(file)): - separator = "\\" if platform.startswith("win") else "/" - file_list.append(f"{root}{separator}{file}") + file_list.append(f"{root}{PATH_SEPARATOR}{file}") return file_list def is_source_code_supported(self,filename:str)->bool: diff --git a/src/lib/RouteSanitizationAnalyzer.py b/src/lib/RouteSanitizationAnalyzer.py index d9b87f8..f80a354 100644 --- a/src/lib/RouteSanitizationAnalyzer.py +++ b/src/lib/RouteSanitizationAnalyzer.py @@ -3,7 +3,7 @@ from lib.custom_class import ElementContext from lib.utils import format_log,flatten_node from .CFGGenerator import CFG from tree_sitter import Node -from sys import platform +from .Const import PATH_SEPARATOR class RouteSanitizationAnalyzer(): def set_project(self,project_info:ProjectInfo): self.project_info = project_info @@ -365,8 +365,7 @@ class RouteSanitizationAnalyzer(): if dependency[0 ]> route_position: break dependency_parts = dependency[1]['original'] # Module name - replacement = "\\" if platform.startswith("win") else "/" - if dependency_parts not in acl_class.location.replace(replacement,"."): + if dependency_parts not in acl_class.location.replace(PATH_SEPARATOR,"."): # Modul nya salah continue # Yang diimport class nya diff --git a/tests/tc1/auth.py b/tests/tc1/auth.py index 2f81ba6..eb316f9 100644 --- a/tests/tc1/auth.py +++ b/tests/tc1/auth.py @@ -2,7 +2,7 @@ from flask import Blueprint, render_template, request, flash, redirect, url_for from .models import User from werkzeug.security import generate_password_hash, check_password_hash from . import db -#@ACL(login_required) +#@A\CL(login_required) from flask_login import login_user, login_required, logout_user, current_user diff --git a/tests/tc1/class_views.py b/tests/tc1/class_views.py index ff93e88..348327a 100644 --- a/tests/tc1/class_views.py +++ b/tests/tc1/class_views.py @@ -1,5 +1,5 @@ from flask import Blueprint, render_template, request, flash, jsonify,abort -# @ACL(login_required) +# @\ACL(login_required) from flask_login import login_required, current_user from .models import Note,Log from .db import db diff --git a/tests/tc1/requirements.txt b/tests/tc1/requirements.txt new file mode 100644 index 0000000..e35bdb3 --- /dev/null +++ b/tests/tc1/requirements.txt @@ -0,0 +1,2 @@ +# @ACL(login_required) +flask \ No newline at end of file diff --git a/tests/tc1/views.py b/tests/tc1/views.py index ac11e48..5a177fb 100644 --- a/tests/tc1/views.py +++ b/tests/tc1/views.py @@ -1,6 +1,6 @@ # @Routes from flask import Blueprint, render_template, request, flash, jsonify,abort -# @ACL(login_required) +# @A\CL(login_required) from flask_login import login_required, current_user from .models import Note,Log from .db import db -- GitLab