Download MobileViews_Apps_CompleteTraces/screenshot_state_mapping.py from zhangwenjia/MobileViews: direct link, hf CLI and curl.
- Browser
- Download file 5.22 kB
-
https://huggingface.co/datasets/zhangwenjia/MobileViews/resolve/main/MobileViews_Apps_CompleteTraces/screenshot_state_mapping.py
- Command line
-
hf download hf://datasets/zhangwenjia/MobileViews/MobileViews_Apps_CompleteTraces/screenshot_state_mapping.py
-
curl -L -o screenshot_state_mapping.py https://huggingface.co/datasets/zhangwenjia/MobileViews/resolve/main/MobileViews_Apps_CompleteTraces/screenshot_state_mapping.py
5.22 kB
| import os | |
| import json | |
| import csv | |
| def extract_node_data(utg_file_path, apk_folder_name): | |
| """ | |
| Extract 'screen_id', 'state_str', 'structure_str', 'vh_json_id', and 'vh_xml_id' from the 'utg.js' file, | |
| and prefix identifiers with the apk folder name for uniqueness. | |
| Parameters: | |
| - utg_file_path (str): The path to the utg.js file. | |
| - apk_folder_name (str): The name of the APK folder for prefixing identifiers. | |
| Returns: | |
| - List of dictionaries containing 'screen_id', 'state_str', 'structure_str', 'vh_json_id', and 'vh_xml_id' for each node. | |
| """ | |
| data = [] | |
| states_folder = os.path.join(os.path.dirname(utg_file_path), 'states') | |
| try: | |
| with open(utg_file_path, 'r') as file: | |
| content = file.read() | |
| content = content.replace("var utg =", "").strip() # Remove prefix for JSON parsing | |
| utg_data = json.loads(content) | |
| for node in utg_data.get("nodes", []): | |
| image_path = node.get("image") | |
| # Get the base name and suffix for mapping other files | |
| base_name = os.path.splitext(os.path.basename(image_path))[0].replace("screen_", "") | |
| # Map the corresponding state JSON and window dump files | |
| vh_json_id = f"{apk_folder_name}/states/state_{base_name}.json" | |
| vh_xml_id = f"{apk_folder_name}/states/window_dump_{base_name}.xml" | |
| # Construct full paths for each file and verify if they exist | |
| vh_json_path = os.path.join(states_folder, f"state_{base_name}.json") | |
| vh_xml_path = os.path.join(states_folder, f"window_dump_{base_name}.xml") | |
| # Only include the paths if the files exist | |
| data.append({ | |
| "screen_id": f"{apk_folder_name}/{image_path}", | |
| "state_str": node.get("state_str"), | |
| "structure_str": node.get("structure_str"), | |
| "vh_json_id": vh_json_id if os.path.isfile(vh_json_path) else "", | |
| "vh_xml_id": vh_xml_id if os.path.isfile(vh_xml_path) else "" | |
| }) | |
| except Exception as e: | |
| print(f"Error processing {utg_file_path}: {e}") | |
| return data | |
| def process_single_folder(apk_folder_path): | |
| """ | |
| Process a single APK folder containing a 'utg.js' file and save data to a CSV file. | |
| Parameters: | |
| - apk_folder_path (str): The path to the APK folder containing a utg.js file. | |
| """ | |
| output_csv_path = os.path.join(apk_folder_path, "screenshot_state_mapping.csv") | |
| utg_file_path = os.path.join(apk_folder_path, 'utg.js') | |
| apk_folder_name = os.path.basename(apk_folder_path.rstrip('/')) | |
| if os.path.isfile(utg_file_path): | |
| data = extract_node_data(utg_file_path, apk_folder_name) | |
| # Write data to CSV with specified column names | |
| with open(output_csv_path, 'w', newline='') as csvfile: | |
| fieldnames = ["screen_id", "state_str", "structure_str", "vh_json_id", "vh_xml_id"] | |
| writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
| writer.writeheader() | |
| writer.writerows(data) | |
| print(f"CSV file generated for {apk_folder_path} at {output_csv_path}") | |
| else: | |
| print(f"No utg.js file found in {apk_folder_path}") | |
| def process_multiple_folders(parent_folder_path): | |
| """ | |
| Process multiple APK folders within a parent folder, each containing a 'utg.js' file, and save all data to a single CSV file. | |
| Parameters: | |
| - parent_folder_path (str): The path to the parent folder containing multiple APK folders. | |
| """ | |
| output_csv_path = os.path.join(parent_folder_path, "screenshot_state_mapping.csv") | |
| all_data = [] | |
| # Iterate over subfolders (APK folders) | |
| for apk_folder_name in os.listdir(parent_folder_path): | |
| apk_folder_path = os.path.join(parent_folder_path, apk_folder_name) | |
| # Ensure it's a directory containing a utg.js file | |
| if os.path.isdir(apk_folder_path): | |
| utg_file_path = os.path.join(apk_folder_path, 'utg.js') | |
| if os.path.isfile(utg_file_path): | |
| folder_data = extract_node_data(utg_file_path, apk_folder_name) | |
| all_data.extend(folder_data) | |
| else: | |
| print(f"Skipping {apk_folder_path} - no utg.js file found") | |
| # Write all data to a single CSV file | |
| with open(output_csv_path, 'w', newline='') as csvfile: | |
| fieldnames = ["screen_id", "state_str", "structure_str", "vh_json_id", "vh_xml_id"] | |
| writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
| writer.writeheader() | |
| writer.writerows(all_data) | |
| print(f"Overall CSV file generated at {output_csv_path}") | |
| # Specify the folder path | |
| folder_path = '/path/to/folder' | |
| # Uncomment one of the following lines to run the desired function: | |
| # For processing a single APK folder, uncomment this line: | |
| # process_single_folder(folder_path) | |
| # For processing multiple APK folders in a parent folder, uncomment this line: | |
| # process_multiple_folders(folder_path) | |