From 0aae7565381459139836cc8f740b257e75946556 Mon Sep 17 00:00:00 2001 From: Timothyxxx <384084775@qq.com> Date: Thu, 14 Mar 2024 12:54:10 +0800 Subject: [PATCH] Code clean --- desktop_env/evaluators/metrics/__init__.py | 4 +- desktop_env/evaluators/metrics/basic_os.py | 6 +- desktop_env/evaluators/metrics/calc.py | 41 ------ desktop_env/evaluators/metrics/demo.py | 28 ---- desktop_env/evaluators/metrics/gimp.py | 44 ------- desktop_env/evaluators/metrics/libreoffice.py | 10 -- desktop_env/evaluators/metrics/others.py | 72 +++-------- desktop_env/evaluators/metrics/pdf.py | 18 +++ desktop_env/evaluators/metrics/slides.py | 59 ++++----- desktop_env/evaluators/metrics/table.py | 121 ++++++------------ desktop_env/evaluators/metrics/thunderbird.py | 92 +++---------- desktop_env/evaluators/metrics/vscode.py | 9 -- 12 files changed, 120 insertions(+), 384 deletions(-) delete mode 100644 desktop_env/evaluators/metrics/calc.py delete mode 100644 desktop_env/evaluators/metrics/demo.py diff --git a/desktop_env/evaluators/metrics/__init__.py b/desktop_env/evaluators/metrics/__init__.py index c3f7f85..61bb025 100644 --- a/desktop_env/evaluators/metrics/__init__.py +++ b/desktop_env/evaluators/metrics/__init__.py @@ -114,7 +114,8 @@ from .slides import ( ) from .table import ( compare_table, - compare_csv + compare_csv, + compare_conference_city_in_order ) from .thunderbird import ( check_thunderbird_prefs, @@ -148,7 +149,6 @@ from .vscode import ( check_html_background_image, compare_zip_files ) -from .calc import compare_conference_city_in_order from .others import compare_epub, check_mp3_meta def infeasible(): diff --git a/desktop_env/evaluators/metrics/basic_os.py b/desktop_env/evaluators/metrics/basic_os.py index aac1f7c..05e51ff 100644 --- a/desktop_env/evaluators/metrics/basic_os.py +++ b/desktop_env/evaluators/metrics/basic_os.py @@ -1,6 +1,3 @@ -import subprocess - - def check_gnome_favorite_apps(apps_str: str, rule): # parse the string like "['thunderbird.desktop', 'vim.desktop', 'google-chrome.desktop']" # to a list of strings @@ -57,6 +54,7 @@ def check_moved_jpgs(directory_list, rule): else: return 0 + def is_in_vm_clickboard(config, terminal_output): print("terminal_output: ") print(terminal_output) @@ -67,4 +65,4 @@ def is_in_vm_clickboard(config, terminal_output): if not isinstance(expected_results, list): return 1 if expected_results in terminal_output else 0 else: - return 1 if all(result in terminal_output for result in expected_results) else 0 \ No newline at end of file + return 1 if all(result in terminal_output for result in expected_results) else 0 diff --git a/desktop_env/evaluators/metrics/calc.py b/desktop_env/evaluators/metrics/calc.py deleted file mode 100644 index 0ff0744..0000000 --- a/desktop_env/evaluators/metrics/calc.py +++ /dev/null @@ -1,41 +0,0 @@ -import logging -from typing import List - -import openpyxl - -logger = logging.getLogger("desktopenv.metrics.calc") - - -def compare_conference_city_in_order(actual_city_list_path, expected_city): - expected_city_list = expected_city["expected"] - wb = openpyxl.load_workbook(actual_city_list_path) - sheet = wb.active - actual_city_list = [] - for row in sheet["C2:C22"]: - for cell in row: - actual_city_list.append(cell.value) - # expected_city is the city that we want to compare with the actual city list - # must in order index - # debug - try: - for i in range(len(actual_city_list)): - if isinstance(expected_city_list[i], str): - if expected_city_list[i] not in actual_city_list[i]: - logger.debug(f"Expected city {expected_city_list[i]}; Actual city {actual_city_list[i]}") - print(f"Expected city {expected_city_list[i]}; Actual city {actual_city_list[i]}") - return 0. - - - elif isinstance(expected_city_list[i], List): - if not any(possible_str in actual_city_list[i] for possible_str in expected_city_list[i]): - logger.debug(f"Expected city {expected_city_list[i]}; Actual city {actual_city_list[i]}") - print(f"Expected city {expected_city_list[i]}; Actual city {actual_city_list[i]}") - return 0. - - else: - raise TypeError("Expected city should be a string or a list of strings") - - except: - return 0. - - return 1. diff --git a/desktop_env/evaluators/metrics/demo.py b/desktop_env/evaluators/metrics/demo.py deleted file mode 100644 index 4c5b55d..0000000 --- a/desktop_env/evaluators/metrics/demo.py +++ /dev/null @@ -1,28 +0,0 @@ -import fitz # PyMuPDF - -def extract_answers_from_pdf(pdf_file): - # 打开PDF文件 - doc = fitz.open(pdf_file) - answers = [] - - # 遍历每一页 - for page in doc: - # 提取当前页的文本 - text = page.get_text() - # 分割文本为行 - lines = text.split('\n') - for line in lines: - if line.strip(): # 排除空白行 - # 分割等号,提取答案 - parts = line.split('=') - if len(parts) > 1: - answer = parts[-1].strip() # 取等号后的部分为答案 - answers.append(answer) - - return answers - -# 假设你的文件名是'math_problems.pdf' -pdf_file = '/Users/lxc/Desktop/calculus.pdf' -answers = extract_answers_from_pdf(pdf_file) -for i, answer in enumerate(answers, 1): - print(f"题目{i}的答案是: {answer}") diff --git a/desktop_env/evaluators/metrics/gimp.py b/desktop_env/evaluators/metrics/gimp.py index 6b555ad..5a1175e 100644 --- a/desktop_env/evaluators/metrics/gimp.py +++ b/desktop_env/evaluators/metrics/gimp.py @@ -568,47 +568,3 @@ def check_image_file_size(src_path, rule): return 1.0 else: return 0.0 - - -if __name__ == "__main__": - # actual_config_path = "../../../cache/sessionrc_test" - # rule = { - # "key": "hide-docks", - # "value": "no" - # } - # print(check_config_status(actual_config_path, rule)) - # - # actual_config_path = "../../../cache/action-history_test" - # rule = { - # "key": ["history-item", "\"filters-vignette\""], - # "value": "1" - # } - # print(check_config_status(actual_config_path, rule)) - # - # actual_config_path = "../../../cache/gimprc_test" - # rule = { - # "key": "undo-levels", - # "value": "100" - # } - # print(check_config_status(actual_config_path, rule)) - # - # src_path = "../../../cache/734d6579-c07d-47a8-9ae2-13339795476b/green_background_with_object.png" - # tgt_path = "../../../cache/734d6579-c07d-47a8-9ae2-13339795476b/white_background_with_object.png" - # print(check_green_background(src_path, tgt_path)) - # - # tgt_path = "../../../cache/f4aec372-4fb0-4df5-a52b-79e0e2a5d6ce/Triangle_In_The_Middle.png" - # print(check_triangle_position(tgt_path)) - # - # src_path = "../../../cache/bb7db4c2-30b5-4be7-8dd7-b8c4ec7d3108/anmi_sharper.png" - # tgt_path = "../../../cache/bb7db4c2-30b5-4be7-8dd7-b8c4ec7d3108/anmi.png" - # print(check_sharper(src_path, tgt_path)) - # - # src_path = "../../../cache/3c8f201a-009d-4bbe-8b65-a6f8b35bb57f/compressed.jpeg" - # rule = { - # "max_size": 500000 - # } - # print(check_image_file_size(src_path, rule)) - - src_path = "../../../cache/d68204bf-11c1-4b13-b48b-d303c73d4bf6/12ad623d-7f35-550e-9c44-6830386b20a0_rearranged_gold.png" - tgt_path = "../../../cache/d68204bf-11c1-4b13-b48b-d303c73d4bf6/d1c7c561-6e76-5d7b-9c10-4af0332dfa50_tilearray.png" - print(check_structure_sim(src_path, tgt_path)) diff --git a/desktop_env/evaluators/metrics/libreoffice.py b/desktop_env/evaluators/metrics/libreoffice.py index 441d932..1870c34 100644 --- a/desktop_env/evaluators/metrics/libreoffice.py +++ b/desktop_env/evaluators/metrics/libreoffice.py @@ -26,13 +26,3 @@ def check_libre_locale(config_file: str, rules: Dict[str, List[str]]) -> float: for ptn in rules["locale_set"] ) ) - - -if __name__ == "__main__": - path1 = "../../任务数据/LibreOffice Calc/registrymodifications.ru.xcu" - print(check_libre_locale(path1, {"locale_set": ["ru-*", "de-*", "fr-*" - , "pt-*", "es-*", "it-*" - ] - } - ) - ) diff --git a/desktop_env/evaluators/metrics/others.py b/desktop_env/evaluators/metrics/others.py index 706bcf3..ebb5994 100644 --- a/desktop_env/evaluators/metrics/others.py +++ b/desktop_env/evaluators/metrics/others.py @@ -1,20 +1,20 @@ -import zipfile -import os.path +import logging import os +import os.path +import zipfile +from typing import List, Dict +from typing import Union, TypeVar import lxml.html from lxml.html import HtmlElement -from typing import List, Dict -from typing import Union, TypeVar from mutagen.easyid3 import EasyID3 from .general import diff_text_file from .utils import _match_value_to_rule -import logging - logger = logging.getLogger("desktopenv.metric.others") + def process_epub(filename: str) -> List[str]: file_list: List[str] = [] @@ -23,7 +23,7 @@ def process_epub(filename: str) -> List[str]: try: with zipfile.ZipFile(filename, "r") as z_f: - with z_f.open("toc.ncx") as in_f\ + with z_f.open("toc.ncx") as in_f \ , open(os.path.join(base_dir, "toc.ncx"), "w") as out_f: contents: str = in_f.read().decode() contents = contents.splitlines() @@ -31,7 +31,7 @@ def process_epub(filename: str) -> List[str]: if "navPoint" not in l: out_f.write(l + "\n") file_list.append(os.path.join(base_dir, "toc.ncx")) - with z_f.open("content.opf") as in_f\ + with z_f.open("content.opf") as in_f \ , open(os.path.join(base_dir, "content.opf"), "w") as out_f: contents: str = in_f.read().decode() contents = contents.splitlines() @@ -41,14 +41,14 @@ def process_epub(filename: str) -> List[str]: file_list.append(os.path.join(base_dir, "content.opf")) for f_n in z_f.namelist(): if f_n.endswith(".html"): - with z_f.open(f_n) as in_f\ + with z_f.open(f_n) as in_f \ , open(os.path.join(base_dir, f_n), "w") as out_f: html: HtmlElement = lxml.html.fromstring( - ''.join( filter( lambda ch: ch!="\n" and ch!="\r" - , in_f.read().decode() - ) - ).encode() - ) + ''.join(filter(lambda ch: ch != "\n" and ch != "\r" + , in_f.read().decode() + ) + ).encode() + ) out_f.write(lxml.html.tostring(html, pretty_print=True, encoding="unicode")) file_list.append(os.path.join(base_dir, f_n)) logger.debug("%s: %s", filename, file_list) @@ -56,6 +56,7 @@ def process_epub(filename: str) -> List[str]: except zipfile.BadZipFile: return [] + def compare_epub(result: str, expected: str) -> float: if result is None: return 0. @@ -69,8 +70,10 @@ def compare_epub(result: str, expected: str) -> float: metric *= current_metric return metric + V = TypeVar("Value") + def check_mp3_meta(result: str, meta: Dict[str, Dict[str, Union[str, V]]]) -> bool: # checks using _match_value_to_rule if result is None: @@ -85,44 +88,3 @@ def check_mp3_meta(result: str, meta: Dict[str, Dict[str, Union[str, V]]]) -> bo logger.debug("%s.%s: %s", result, k, value) metric = metric and _match_value_to_rule(value, r) return float(metric) - -if __name__ == "__main__": - import datetime - import sys - - logger = logging.getLogger() - logger.setLevel(logging.DEBUG) - - datetime_str: str = datetime.datetime.now().strftime("%Y%m%d@%H%M%S") - - file_handler = logging.FileHandler(os.path.join("logs", "normal-{:}.log".format(datetime_str))) - debug_handler = logging.FileHandler(os.path.join("logs", "debug-{:}.log".format(datetime_str))) - stdout_handler = logging.StreamHandler(sys.stdout) - sdebug_handler = logging.FileHandler(os.path.join("logs", "sdebug-{:}.log".format(datetime_str))) - - file_handler.setLevel(logging.INFO) - debug_handler.setLevel(logging.DEBUG) - stdout_handler.setLevel(logging.INFO) - sdebug_handler.setLevel(logging.DEBUG) - - formatter = logging.Formatter(fmt="\x1b[1;33m[%(asctime)s \x1b[31m%(levelname)s \x1b[32m%(module)s/%(lineno)d-%(processName)s\x1b[1;33m] \x1b[0m%(message)s") - file_handler.setFormatter(formatter) - debug_handler.setFormatter(formatter) - stdout_handler.setFormatter(formatter) - sdebug_handler.setFormatter(formatter) - - logger.addHandler(file_handler) - logger.addHandler(debug_handler) - logger.addHandler(stdout_handler) - logger.addHandler(sdebug_handler) - - metric = check_mp3_meta( "snapshots/test/cache/3f05f3b9-29ba-4b6b-95aa-2204697ffc06/Cheng Xiang - Missing You - gt.mp3" - , { "title": { "method": "eq" - , "ref": "Missing You" - } - , "artist": { "method": "eq" - , "ref": "Cheng Xiang" - } - } - ) - print(metric) diff --git a/desktop_env/evaluators/metrics/pdf.py b/desktop_env/evaluators/metrics/pdf.py index d6644d1..ef5b384 100644 --- a/desktop_env/evaluators/metrics/pdf.py +++ b/desktop_env/evaluators/metrics/pdf.py @@ -2,6 +2,7 @@ import operator from typing import Any from typing import Dict +import fitz # PyMuPDF from pypdf import PdfReader @@ -11,3 +12,20 @@ def check_pdf_pages(pdf_file: str, rules: Dict[str, Any]) -> float: reader = PdfReader(pdf_file) nb_pages: int = len(reader.pages) return float(getattr(operator, rules["relation"])(nb_pages, rules["ref_value"])) + + +def extract_answers_from_pdf(pdf_file): + doc = fitz.open(pdf_file) + answers = [] + + for page in doc: + text = page.get_text() + lines = text.split('\n') + for line in lines: + if line.strip(): + parts = line.split('=') + if len(parts) > 1: + answer = parts[-1].strip() + answers.append(answer) + + return answers diff --git a/desktop_env/evaluators/metrics/slides.py b/desktop_env/evaluators/metrics/slides.py index 76217e1..1c2e04f 100644 --- a/desktop_env/evaluators/metrics/slides.py +++ b/desktop_env/evaluators/metrics/slides.py @@ -165,23 +165,24 @@ def compare_pptx_files(file1_path, file2_path, **options): # compare the content of each slide for slide1, slide2 in zip(prs1.slides, prs2.slides): slide_idx += 1 + def get_slide_background_color(slide): background = slide.background if background.fill.background(): return background.fill.fore_color.rgb else: return None - + if get_slide_background_color(slide1) != get_slide_background_color(slide2) and examine_background_color: return 0 - + def get_slide_notes(slide): notes_slide = slide.notes_slide if notes_slide: return notes_slide.notes_text_frame.text else: return None - + if get_slide_notes(slide1).strip() != get_slide_notes(slide2).strip() and examine_note: return 0 # check if the shapes are the same @@ -192,14 +193,14 @@ def compare_pptx_files(file1_path, file2_path, **options): return 0 elif shape1.left != shape2.left or shape1.top != shape2.top or shape1.width != shape2.width or shape1.height != shape2.height: return 0 - + if examine_table_bottom_position: if slide_idx == 3 and shape1.shape_type == 19 and shape2.shape_type == 19: if shape1.top <= shape2.top or shape1.top < 3600000: return 0 elif shape1.left != shape2.left or shape1.top != shape2.top or shape1.width != shape2.width or shape1.height != shape2.height: return 0 - + if examine_right_position: if slide_idx == 2 and not hasattr(shape1, "text") and not hasattr(shape2, "text"): if shape1.left <= shape2.left or shape1.left < 4320000: @@ -207,28 +208,31 @@ def compare_pptx_files(file1_path, file2_path, **options): if examine_top_position: if slide_idx == 2 and shape1.shape_type == 13 and shape2.shape_type == 13: - if shape1.top >= shape2.top or shape1.top > 1980000: - return 0 + if shape1.top >= shape2.top or shape1.top > 1980000: + return 0 elif shape1.left != shape2.left or shape1.top != shape2.top or shape1.width != shape2.width or shape1.height != shape2.height: return 0 - + if examine_shape_for_shift_size: if shape1.left != shape2.left or shape1.top != shape2.top or shape1.width != shape2.width or shape1.height != shape2.height: - if not (hasattr(shape1, "text") and hasattr(shape2, "text") and shape1.text == shape2.text and shape1.text == "Elaborate on what you want to discuss."): + if not (hasattr(shape1, "text") and hasattr(shape2, + "text") and shape1.text == shape2.text and shape1.text == "Elaborate on what you want to discuss."): return 0 - - if (shape1.left != shape2.left or shape1.top != shape2.top or shape1.width != shape2.width or shape1.height != shape2.height) and examine_shape: + + if ( + shape1.left != shape2.left or shape1.top != shape2.top or shape1.width != shape2.width or shape1.height != shape2.height) and examine_shape: return 0 - + if examine_image_size: if shape1.shape_type == 13 and shape2.shape_type == 13: if shape1.width != shape2.width or shape1.height != shape2.height: return 0 elif shape1.left != shape2.left or shape1.top != shape2.top or shape1.width != shape2.width or shape1.height != shape2.height: return 0 - + if examine_modify_height: - if not hasattr(shape1, "text") and not hasattr(shape2, "text") or shape1.shape_type == 5 and shape2.shape_type == 5: + if not hasattr(shape1, "text") and not hasattr(shape2, + "text") or shape1.shape_type == 5 and shape2.shape_type == 5: if shape1.height != shape2.height: return 0 elif shape1.left != shape2.left or shape1.top != shape2.top or shape1.width != shape2.width or shape1.height != shape2.height: @@ -236,13 +240,13 @@ def compare_pptx_files(file1_path, file2_path, **options): if hasattr(shape1, "text") and hasattr(shape2, "text"): if shape1.text.strip() != shape2.text.strip() and examine_text: - return 0 - - # check if the paragraphs are the same + return 0 + + # check if the paragraphs are the same for para1, para2 in zip(shape1.text_frame.paragraphs, shape2.text_frame.paragraphs): if para1.alignment != para2.alignment and examine_alignment: return 0 - + # check if the runs are the same if para1.text != para2.text and examine_text: return 0 @@ -253,7 +257,7 @@ def compare_pptx_files(file1_path, file2_path, **options): for run1, run2 in zip(para1.runs, para2.runs): # check if the font properties are the same - if run1.font.name != run2.font.name and examine_font_name: + if run1.font.name != run2.font.name and examine_font_name: return 0 if run1.font.size != run2.font.size and examine_font_size: @@ -305,10 +309,9 @@ def compare_pptx_files(file1_path, file2_path, **options): return bullets - if examine_bullets and _extract_bullets(run1.part.blob.decode('utf-8')) != _extract_bullets(run2.part.blob.decode('utf-8')): + if examine_bullets and _extract_bullets(run1.part.blob.decode('utf-8')) != _extract_bullets( + run2.part.blob.decode('utf-8')): return 0 - - # fixme: Actually there are more properties to be compared, we can add them later via parsing the xml data @@ -524,15 +527,3 @@ def check_auto_saving_time(pptx_file, rules): logger.error(f"Error parsing XML: {e}") except FileNotFoundError: logger.error(f"File not found: {pptx_file}") - - -if __name__ == '__main__': - # print(compare_pptx_files( - # r"C:\Users\tianbaox\Desktop\DesktopEnv\cache\550ce7e7-747b-495f-b122-acdc4d0b8e54\New_Club_Spring_2018_Training_Gold.pptx", - # r"C:\Users\tianbaox\Desktop\DesktopEnv\cache\550ce7e7-747b-495f-b122-acdc4d0b8e54\New_Club_Spring_2018_Training_Gold.pptx")) - # print(evaluate_presentation_fill_to_rgb_distance(r"C:\Users\tianbaox\Desktop\DesktopEnv\cache\3b27600c-3668-4abd-8f84-7bcdebbccbdb\lec17-gui-events.pptx", {"rgb": (0, 0, 255)})) - # print(check_auto_saving_time(r"C:\Users\tianbaox\Desktop\DesktopEnv\cache\2cd43775-7085-45d8-89fa-9e35c0a915cf\registrymodifications.xcu", {"minutes": 3})) - print(compare_pptx_files( - r"D:\NJU\HKUNLP\Desktop-Env\DesktopEnv\cache\08aced46-45a2-48d7-993b-ed3fb5b32302\22_6_Gold.pptx", - r"D:\NJU\HKUNLP\Desktop-Env\DesktopEnv\cache\08aced46-45a2-48d7-993b-ed3fb5b32302\22_6.pptx", - examine_shape=False)) diff --git a/desktop_env/evaluators/metrics/table.py b/desktop_env/evaluators/metrics/table.py index dadd138..9e888c7 100644 --- a/desktop_env/evaluators/metrics/table.py +++ b/desktop_env/evaluators/metrics/table.py @@ -11,15 +11,15 @@ import openpyxl import pandas as pd from openpyxl import Workbook from openpyxl.cell.cell import Cell -from openpyxl.worksheet.cell_range import MultiCellRange from openpyxl.utils import get_column_letter +from openpyxl.worksheet.cell_range import MultiCellRange from openpyxl.worksheet.datavalidation import DataValidation from openpyxl.worksheet.worksheet import Worksheet +from rapidfuzz import fuzz from desktop_env.evaluators.metrics.utils import _match_value_to_rule, _read_cell_style, read_cell_value from desktop_env.evaluators.metrics.utils import load_charts, load_sparklines, load_rows_or_cols, load_xlsx_styles \ , load_filters, load_pivot_tables -from rapidfuzz import fuzz # from openpyxl.utils import coordinate_to_tuple @@ -165,7 +165,7 @@ def compare_table(result: str, expected: str = None, **options) -> float: logger.debug("Sheet1: \n%s", str(sheet1)) logger.debug("Sheet2: \n%s", str(sheet2)) try: - logger.debug("Sheet1 =v= Sheet2: \n%s", str(sheet1==sheet2)) + logger.debug("Sheet1 =v= Sheet2: \n%s", str(sheet1 == sheet2)) except: logger.debug("Sheet1 =/v= Sheet2") logger.debug("Assertion: %s =v= %s - %s", r["sheet_idx0"], r["sheet_idx1"], metric) @@ -231,14 +231,14 @@ def compare_table(result: str, expected: str = None, **options) -> float: value1 = value1.lower() value2 = value2.lower() - if rl["type"]=="includes": + if rl["type"] == "includes": metric: bool = value2 in value1 - elif rl["type"]=="included_by": + elif rl["type"] == "included_by": metric: bool = value1 in value2 - elif rl["type"]=="fuzzy_match": + elif rl["type"] == "fuzzy_match": metric: bool = fuzz.ratio(value1, value2) >= rl.get("threshold", 85.) - elif rl["type"]=="exact_match": - metric: bool = value1==value2 + elif rl["type"] == "exact_match": + metric: bool = value1 == value2 total_metric = total_metric and metric metric: bool = total_metric @@ -409,7 +409,7 @@ def compare_table(result: str, expected: str = None, **options) -> float: filters1: Dict[str, Any] = load_filters(*parse_idx(r["sheet_idx0"], xlworkbookr, xlworkbooke), **r) filters2: Dict[str, Any] = load_filters(*parse_idx(r["sheet_idx1"], xlworkbookr, xlworkbooke), **r) - metric: bool = filters1==filters2 + metric: bool = filters1 == filters2 logger.debug("Assertion: %s[filter] == %s[filter] - %s", r["sheet_idx0"], r["sheet_idx1"], metric) # }}} Compare Filters # @@ -421,7 +421,7 @@ def compare_table(result: str, expected: str = None, **options) -> float: pivots1: Dict[str, Any] = load_pivot_tables(*parse_idx(r["sheet_idx0"], xlworkbookr, xlworkbooke), **r) pivots2: Dict[str, Any] = load_pivot_tables(*parse_idx(r["sheet_idx1"], xlworkbookr, xlworkbooke), **r) - metric: bool = pivots1==pivots2 + metric: bool = pivots1 == pivots2 logger.debug("Assertion: %s[pivot]==%s[pivot] - %s", r["sheet_idx0"], r["sheet_idx1"], metric) # }}} Compare Pivot Tables # @@ -482,81 +482,36 @@ def compare_csv(result: str, expected: str, **options) -> float: return float(metric) -if __name__ == '__main__': - import datetime - import sys +def compare_conference_city_in_order(actual_city_list_path, expected_city): + expected_city_list = expected_city["expected"] + wb = openpyxl.load_workbook(actual_city_list_path) + sheet = wb.active + actual_city_list = [] + for row in sheet["C2:C22"]: + for cell in row: + actual_city_list.append(cell.value) + # expected_city is the city that we want to compare with the actual city list + # must in order index + # debug + try: + for i in range(len(actual_city_list)): + if isinstance(expected_city_list[i], str): + if expected_city_list[i] not in actual_city_list[i]: + logger.debug(f"Expected city {expected_city_list[i]}; Actual city {actual_city_list[i]}") + print(f"Expected city {expected_city_list[i]}; Actual city {actual_city_list[i]}") + return 0. - logger = logging.getLogger() - logger.setLevel(logging.DEBUG) - datetime_str: str = datetime.datetime.now().strftime("%Y%m%d@%H%M%S") + elif isinstance(expected_city_list[i], List): + if not any(possible_str in actual_city_list[i] for possible_str in expected_city_list[i]): + logger.debug(f"Expected city {expected_city_list[i]}; Actual city {actual_city_list[i]}") + print(f"Expected city {expected_city_list[i]}; Actual city {actual_city_list[i]}") + return 0. - file_handler = logging.FileHandler(os.path.join("logs", "normal-{:}.log".format(datetime_str))) - debug_handler = logging.FileHandler(os.path.join("logs", "debug-{:}.log".format(datetime_str))) - stdout_handler = logging.StreamHandler(sys.stdout) - sdebug_handler = logging.FileHandler(os.path.join("logs", "sdebug-{:}.log".format(datetime_str))) + else: + raise TypeError("Expected city should be a string or a list of strings") - file_handler.setLevel(logging.INFO) - debug_handler.setLevel(logging.DEBUG) - stdout_handler.setLevel(logging.INFO) - sdebug_handler.setLevel(logging.DEBUG) + except: + return 0. - formatter = logging.Formatter( - fmt="\x1b[1;33m[%(asctime)s \x1b[31m%(levelname)s \x1b[32m%(module)s/%(lineno)d-%(processName)s\x1b[1;33m] \x1b[0m%(message)s") - file_handler.setFormatter(formatter) - debug_handler.setFormatter(formatter) - stdout_handler.setFormatter(formatter) - sdebug_handler.setFormatter(formatter) - - stdout_handler.addFilter(logging.Filter("desktopenv")) - sdebug_handler.addFilter(logging.Filter("desktopenv")) - - logger.addHandler(file_handler) - logger.addHandler(debug_handler) - logger.addHandler(stdout_handler) - logger.addHandler(sdebug_handler) - - path1 = "snapshots/test/cache/4e6fcf72-daf3-439f-a232-c434ce416af6/Employee_Age_By_Birthday.xlsx" - path2 = "snapshots/test/cache/4e6fcf72-daf3-439f-a232-c434ce416af6/Employee_Age_By_Birthday_gold.xlsx" - rules = [ { "type": "sheet_data" - , "sheet_idx0": 0 - , "sheet_idx1": "EI0" - } - ] - print(compare_table(path1, path2 - , rules=rules - ) - ) - print(compare_table(path2, path2 - , rules=rules - ) - ) - - # Row Properties - # path1 = "../../任务数据/LibreOffice Calc/Date_Budget_Variance_HideNA.xlsx" - # path2 = "../../任务数据/LibreOffice Calc/Date_Budget_Variance_HideNA_gold.xlsx" - # workbook: Workbook = openpyxl.load_workbook(filename=path1) - # worksheet: Worksheet = workbook.active - # for r_no, dms in worksheet.column_dimensions.items(): - # print(r_no, type(r_no), type(dms), dms.hidden) - - # Conditional Formats - # import formulas - # path1 = "../../任务数据/LibreOffice Calc/Calendar_Highlight_Weekend_Days.xlsx" - # path2 = "../../任务数据/LibreOffice Calc/Calendar_Highlight_Weekend_Days_gold.xlsx" - # path3 = "../../任务数据/LibreOffice Calc/Calendar_Highlight_Weekend_Days_gold_test.xlsx" - # workbook: Workbook = openpyxl.load_workbook(filename=path2) - # worksheet: Worksheet = workbook.active - # print(worksheet.conditional_formatting) - # for itm in worksheet.conditional_formatting: - # print(itm.cells) - # for r in itm.rules: - # print( r.type, r.formula, r.dxf.font.color.rgb - # , r.dxf.fill.fgColor.rgb, r.dxf.fill.bgColor.rgb - # ) - # condition = formulas.Parser().ast("=" + r.formula[0])[1].compile() - ##print(r.type, r.operator, r.dxfId, r.dxf) - # for r in itm.cells: - # for c in r.cells: - # value = worksheet.cell(row=c[0], column=c[1]).value - # print(value, condition(str(value))) + return 1. diff --git a/desktop_env/evaluators/metrics/thunderbird.py b/desktop_env/evaluators/metrics/thunderbird.py index 90b8892..5b7aaa0 100644 --- a/desktop_env/evaluators/metrics/thunderbird.py +++ b/desktop_env/evaluators/metrics/thunderbird.py @@ -1,17 +1,19 @@ +import json +import logging +import re from typing import List, Pattern, Dict, Match from typing import Union, Any, TypeVar, Callable -import re -import json from .utils import _match_record from .utils import _match_value_to_rule as _match_pref -import logging logger = logging.getLogger("desktopenv.metric.thunderbird") V = TypeVar("Value") _pref_pattern: Pattern[str] = re.compile(r'^user_pref\("(?P(?:[^"]|\\")+)\", (?P.+)\);$'); + + def check_thunderbird_prefs(result: str, rule: Dict[str, Dict[str, Dict[str, Any]]]): """ Args: @@ -51,10 +53,10 @@ def check_thunderbird_prefs(result: str, rule: Dict[str, Dict[str, Dict[str, Any continue key: str = match_.group("key") - #value: str = match_.group("val") - #if value in {"true", "false"}: - #value = value.title() - #value: V = eval(value) + # value: str = match_.group("val") + # if value in {"true", "false"}: + # value = value.title() + # value: V = eval(value) value = json.loads(match_.group("val")) if key in expect_rules: logger.debug("K: %s, V: %s", key, repr(value)) @@ -64,9 +66,13 @@ def check_thunderbird_prefs(result: str, rule: Dict[str, Dict[str, Dict[str, Any return float(all(expect_metrics.values()) and unexpect_metric) + _value_processor: Callable[[str], str] = lambda val: val.replace("\\\"", "\"").replace("\\\\", "\\") -#_condition_pattern: Pattern[str] = re.compile(r'(?PAND|OR) \((?P[\w ]+),(?P[\w ' + '\'' + r']+),(?:"(?P(?:[^"]|\")+)"|(?P[^)]+))\)') -_condition_pattern: Pattern[str] = re.compile(r'\b(?:AND|OR) \((?:[\w ]+),(?:[\w ' + '\'' + r']+),(?:"(?:(?:[^"]|\")+)"|(?:[^)]+))\)|\bALL\b') +# _condition_pattern: Pattern[str] = re.compile(r'(?PAND|OR) \((?P[\w ]+),(?P[\w ' + '\'' + r']+),(?:"(?P(?:[^"]|\")+)"|(?P[^)]+))\)') +_condition_pattern: Pattern[str] = re.compile( + r'\b(?:AND|OR) \((?:[\w ]+),(?:[\w ' + '\'' + r']+),(?:"(?:(?:[^"]|\")+)"|(?:[^)]+))\)|\bALL\b') + + def check_thunderbird_filter(result: str, rules: Dict[str, List[Dict[str, str]]]) -> float: """ Args: @@ -112,8 +118,8 @@ def check_thunderbird_filter(result: str, rules: Dict[str, List[Dict[str, str]]] condition_str: str = _value_processor(l[11:-2]) logger.debug("FILTER CONDITION: %s", condition_str) - conditions: List[str] =\ - _condition_pattern.findall(condition_str) + conditions: List[str] = \ + _condition_pattern.findall(condition_str) logger.debug("FILTER CONDITIONS: %s", repr(conditions)) filter_["condition"] = conditions @@ -138,6 +144,7 @@ def check_thunderbird_folder(result: Union[str, List[str]], reference: Union[str remove_deleted (bool): ignore deleted messages which has status code 0008 or 0009. default: True remove_duplicate (bool): remove duplicate messages. default: True """ + def normalize_msg(msg, options): ignore_status = options.get('ignore_status', False) ignore_keys = options.get('ignore_keys', False) @@ -167,66 +174,3 @@ def check_thunderbird_folder(result: Union[str, List[str]], reference: Union[str mail2 = read_thunderbird_folder_file(gold) if mail1 != mail2: return .0 return 1.0 - - -if __name__ == "__main__": - #import lxml.etree - #from lxml.cssselect import CSSSelector - #from lxml.etree import _Element - - #xml = "../../任务数据/Thunderbird/vertical-card-view.xml" - #xml = "../../任务数据/Thunderbird/vertical-table-view.xml" - #at: _Element = lxml.etree.parse(xml) - - #elements: List[_Element] = CSSSelector('application[name=Thunderbird] page-tab-list')(at) # page tab tags - #elements: List[_Element] = CSSSelector('application[name=Thunderbird] panel>scroll-pane>internal-frame>panel[name$="anonym-x2024@outlook.com"]')(at) # email tag page - #elements: List[_Element] = CSSSelector('application[name=Thunderbird] panel>scroll-pane>internal-frame>panel[name$="anonym-x2024@outlook.com"]>section:nth-child(3)')(at) # email tag page - #elements: List[_Element] = CSSSelector('application[name=Thunderbird] panel>scroll-pane>internal-frame>panel[name$="anonym-x2024@outlook.com"]>section[attr|id=threadPane]>section[attr|id="threadTree"]>table[attr|class="tree-table"]>section[attr|class~="tree-table-header"]>table-row>column-header[name=Subject]>push-button', namespaces={"attr": "uri:deskat:attributes.at-spi.gnome.org"})(at) # table view, column header - #elements: List[_Element] = CSSSelector('application[name=Thunderbird] panel>scroll-pane>internal-frame>panel[name$="anonym-x2024@outlook.com"]>section[attr|id=threadPane]>section[attr|id="threadTree"]>table[attr|class="tree-table"]>tree>tree-item>section[name="Subject"]>section>section', namespaces={"attr": "uri:deskat:attributes.at-spi.gnome.org"})(at) # table view, column header - #print(len(elements)) - #for elm in elements: - #print(lxml.etree.tostring(elm, encoding="unicode", pretty_print=True)) - - import datetime - import os - import sys - - logger = logging.getLogger() - logger.setLevel(logging.DEBUG) - - datetime_str: str = datetime.datetime.now().strftime("%Y%m%d@%H%M%S") - - file_handler = logging.FileHandler(os.path.join("logs", "normal-{:}.log".format(datetime_str))) - debug_handler = logging.FileHandler(os.path.join("logs", "debug-{:}.log".format(datetime_str))) - stdout_handler = logging.StreamHandler(sys.stdout) - sdebug_handler = logging.FileHandler(os.path.join("logs", "sdebug-{:}.log".format(datetime_str))) - - file_handler.setLevel(logging.INFO) - debug_handler.setLevel(logging.DEBUG) - stdout_handler.setLevel(logging.INFO) - sdebug_handler.setLevel(logging.DEBUG) - - formatter = logging.Formatter(fmt="\x1b[1;33m[%(asctime)s \x1b[31m%(levelname)s \x1b[32m%(module)s/%(lineno)d-%(processName)s\x1b[1;33m] \x1b[0m%(message)s") - file_handler.setFormatter(formatter) - debug_handler.setFormatter(formatter) - stdout_handler.setFormatter(formatter) - sdebug_handler.setFormatter(formatter) - - stdout_handler.addFilter(logging.Filter("desktopenv")) - sdebug_handler.addFilter(logging.Filter("desktopenv")) - - logger.addHandler(file_handler) - logger.addHandler(debug_handler) - logger.addHandler(stdout_handler) - logger.addHandler(sdebug_handler) - - print( check_thunderbird_filter( "../../任务数据/Thunderbird/msgFilterRules.dat" - , { "expect": [ { "enabled": "yes" - , "action": "Move to folder" - , "actionValue": "mailbox://nobody@Local%20Folders/Promotions" - , "condition": ["AND (subject,contains,discount)"] - } - ] - } - ) - ) diff --git a/desktop_env/evaluators/metrics/vscode.py b/desktop_env/evaluators/metrics/vscode.py index f06623a..61976f1 100644 --- a/desktop_env/evaluators/metrics/vscode.py +++ b/desktop_env/evaluators/metrics/vscode.py @@ -271,12 +271,3 @@ def compare_result_files(src_path, tgt_path): if src_content == tgt_content: return 1.0 return 0.0 - - -if __name__ == "__main__": - src_path = "../../../cache/bb7db4c2-30b5-4be7-8dd7-b8c4ec7d3108/index.html" - rule = { - "type:": "value", - "value": "anmi_sharper.png" - } - print(check_html_background_image(src_path, rule))