mirror of
https://github.com/openswarm-ai/openswarm.git
synced 2026-09-22 01:24:52 +02:00
[eric] scheduled tasks: agent can now manage workflows itself + tons of safety, cross-platform polish, and edge-case fixes
This commit is contained in:
@@ -13,9 +13,7 @@ class DebugFile(File):
|
||||
self.directory = directory # Reference to parent directory
|
||||
|
||||
def to_dict(self):
|
||||
"""
|
||||
Converts the DebugFile object to a dictionary format.
|
||||
"""
|
||||
"""Convert the DebugFile to a dict."""
|
||||
return {
|
||||
"name": os.path.basename(self.filename),
|
||||
"color": self.color,
|
||||
@@ -26,9 +24,7 @@ class DebugFile(File):
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, file_dict, directory):
|
||||
"""
|
||||
Creates a DebugFile object from a dictionary loaded from JSON.
|
||||
"""
|
||||
"""Build a DebugFile from a JSON-loaded dict."""
|
||||
filename = os.path.join(directory.path, file_dict["name"])
|
||||
return cls(
|
||||
filename=filename,
|
||||
|
||||
@@ -16,7 +16,6 @@ class Debugleton:
|
||||
sync_lock: threading.Lock
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
# Double-checked locking for thread-safe singleton creation
|
||||
if cls._instance is None:
|
||||
with cls._lock:
|
||||
if cls._instance is None:
|
||||
@@ -32,27 +31,18 @@ class Debugleton:
|
||||
print("\033[38;5;120m|\t...Project Scanned\t|\033[0m")
|
||||
print("\033[38;5;120m|\tDEBUGLETON INIT DONE\t|\033[0m")
|
||||
print("\033[38;5;120m---------------------------------\n\033[0m")
|
||||
# else: print("DEBUGLETON Already initialized INNER")
|
||||
# else: print("DEBUGLETON Already initialized OUTER")
|
||||
return cls._instance
|
||||
|
||||
|
||||
|
||||
def sync_to_saved(self, is_first_sync=False):
|
||||
# print(f"[sync_to_saved]: START")
|
||||
if not is_first_sync: self.sync_lock.acquire()
|
||||
# print(f"[sync_to_saved]: Acquired sync lock")
|
||||
self.dir = update_debug_toggles(save_to_file=False)
|
||||
# print(f"Synced to saved dir: {self.dir}")
|
||||
self.abspaths, self.instances = self.dir.get_ordered_abspaths_and_instances()
|
||||
# print(f"Synced to abspaths: {self.abspaths}")
|
||||
with open(NEEDS_RESYNC_FILE, 'w') as f:
|
||||
f.write('0')
|
||||
if not is_first_sync: self.sync_lock.release()
|
||||
# print(f"[sync_to_saved]: Released sync lock")
|
||||
# print(f"[sync_to_saved]: END")
|
||||
|
||||
def needs_resync(self):
|
||||
# print(f"[needs_resync]: START")
|
||||
num_tries = 0
|
||||
while self.is_syncing():
|
||||
print(f"Waiting for Debugleton to sync... ({num_tries})")
|
||||
@@ -67,8 +57,6 @@ class Debugleton:
|
||||
""")
|
||||
with open(NEEDS_RESYNC_FILE, 'r') as f:
|
||||
does_need_resync = True if f.read().strip() == '1' else False
|
||||
# if does_need_resync: print("Resyncing Debugleton...")
|
||||
# print(f"[needs_resync]: END")
|
||||
return does_need_resync
|
||||
|
||||
def is_syncing(self):
|
||||
@@ -76,7 +64,6 @@ class Debugleton:
|
||||
|
||||
def find_file_info(self, filepath: str):
|
||||
filepath = filepath.lower()
|
||||
# print(f"Finding file info for {filepath}")
|
||||
if self.needs_resync():
|
||||
self.sync_to_saved()
|
||||
try:
|
||||
|
||||
@@ -7,11 +7,10 @@ from debugger_backend.DEFAULTS import DEFAULT_COLOR, DEFAULT_TOGGLED, DEFAULT_SE
|
||||
from debugger_backend.path_mngr import get_abspath, get_root_rel_path
|
||||
|
||||
class Directory:
|
||||
def __init__(self, path, color=DEFAULT_COLOR, is_toggled=DEFAULT_TOGGLED,
|
||||
def __init__(self, path, color=DEFAULT_COLOR, is_toggled=DEFAULT_TOGGLED,
|
||||
set_manually=DEFAULT_SET_MANUALLY, emoji=DEFAULT_EMOJI):
|
||||
self.path = path
|
||||
# print(f"Directory init: {self.path}")
|
||||
self.children = [] # Can contain DebugFile or other Directory objects
|
||||
self.children = []
|
||||
self.color = color
|
||||
self.is_toggled = is_toggled
|
||||
self.set_manually = set_manually
|
||||
@@ -24,39 +23,26 @@ class Directory:
|
||||
return get_abspath(self.path)
|
||||
|
||||
def add_child(self, child):
|
||||
"""
|
||||
Adds a child to the directory (either a DebugFile or another Directory).
|
||||
"""
|
||||
"""Append a child DebugFile/Directory."""
|
||||
self.children.append(child)
|
||||
|
||||
def get_ordered_abspaths_and_instances(self):
|
||||
# print("[get_ordered_abspaths]: START")
|
||||
curr_file_path = os.path.abspath(__file__)
|
||||
root_dir = os.path.dirname(os.path.dirname(os.path.dirname(curr_file_path)))
|
||||
# print(f"[get_ordered_abspaths]: Curr path: {curr_file_path}")
|
||||
# print(f"[get_ordered_abspaths]: Dir path: {root_dir}")
|
||||
def construct_ordered_abspaths(dir: Directory, ordered_abspaths: list):
|
||||
dir_path = dir.path
|
||||
full_path = os.path.join(root_dir, dir_path)
|
||||
ordered_abspaths.append({"abspath": full_path, "instance": dir})
|
||||
# print(f"\t[construct_ordered_abspaths]: Full path: {full_path}")
|
||||
for child in dir.children:
|
||||
child_abspath = os.path.join(root_dir, child.path).lower()
|
||||
if os.path.isdir(child_abspath):
|
||||
construct_ordered_abspaths(child, ordered_abspaths)
|
||||
elif os.path.isfile(child_abspath):
|
||||
# print(f"\t[construct_ordered_abspaths]: Child is file: {child_abspath}")
|
||||
ordered_abspaths.append({"abspath": child_abspath, "instance": child})
|
||||
else:
|
||||
print(f"\033[38;5;120mEntry is non existent: {child_abspath}\033[0m")
|
||||
# print(f"\t[construct_ordered_abspaths]: Finished for dir: {full_path}")
|
||||
# print(f"\t[construct_ordered_abspaths]: RETURNING FROM DIR: {full_path}")
|
||||
return ordered_abspaths
|
||||
ordered_abspaths_and_instances = construct_ordered_abspaths(self, [])
|
||||
# print("[get_ordered_abspaths]: Finished getting ordered abspaths and instances")
|
||||
# for abspath_and_instance in ordered_abspaths_and_instances:
|
||||
# abspath = abspath_and_instance["abspath"]
|
||||
# print(f"\t[get_ordered_abspaths]: Abspath: {abspath}")
|
||||
ordered_abspaths = [abspath_and_instance["abspath"] for abspath_and_instance in ordered_abspaths_and_instances]
|
||||
ordered_instances = [abspath_and_instance["instance"] for abspath_and_instance in ordered_abspaths_and_instances]
|
||||
return ordered_abspaths, ordered_instances
|
||||
@@ -65,17 +51,12 @@ class Directory:
|
||||
def build_structure(self):
|
||||
print("[build_structure]: START")
|
||||
root_dir = self.get_abspath()
|
||||
# print(f"[build_structure]: Root dir: {root_dir}")
|
||||
excluded_dirs = [".venv", "debugger", "node_modules", ".git", "__pycache__"]
|
||||
project_structure = []
|
||||
|
||||
def construct_project_structure(dir_path: str, parent_dir: Directory):
|
||||
# print(f"[build_structure]: Scanning dir: {dir_path}")
|
||||
with os.scandir(dir_path) as it:
|
||||
for entry in it:
|
||||
# print(f"[build_structure]: Entry: {entry.path}")
|
||||
if any(excluded_dir in entry.path for excluded_dir in excluded_dirs):
|
||||
# print(f"[build_structure]: Excluding {entry.path}")
|
||||
continue
|
||||
root_rel_path = get_root_rel_path(entry.path)
|
||||
if entry.is_dir():
|
||||
@@ -88,16 +69,12 @@ class Directory:
|
||||
parent_dir.add_child(debug_file)
|
||||
else:
|
||||
continue
|
||||
|
||||
construct_project_structure(root_dir, self)
|
||||
# [print(f"[build_structure]: {file}") for file in project_structure]
|
||||
# print(f"[build_structure]: END")
|
||||
|
||||
construct_project_structure(root_dir, self)
|
||||
return
|
||||
|
||||
def to_dict(self):
|
||||
"""
|
||||
Converts the Directory object to a dictionary format, recursively.
|
||||
"""
|
||||
"""Recursively convert the Directory to a dict."""
|
||||
return {
|
||||
"name": os.path.basename(self.path),
|
||||
"color": self.color,
|
||||
@@ -108,22 +85,14 @@ class Directory:
|
||||
}
|
||||
|
||||
def prune_empty(self):
|
||||
# Recursively prune empty directories
|
||||
# Base case) if the current directory has no children, return
|
||||
# Recursive case) for each of the directories in the current directory, call prune_empty
|
||||
# then remove the directory from the children of the current directory if it has no children
|
||||
for child in self.children[:]:
|
||||
if isinstance(child, Directory):
|
||||
# Recursively prune empty subdirectories
|
||||
child.prune_empty()
|
||||
# If the subdirectory is empty after pruning, remove it
|
||||
if len(child.children) == 0:
|
||||
self.children.remove(child)
|
||||
|
||||
|
||||
def propagate_toggled_state(self):
|
||||
"""
|
||||
Propagates the toggled state down the hierarchy.
|
||||
"""
|
||||
"""Propagate the toggled state down the hierarchy."""
|
||||
for child in self.children:
|
||||
if isinstance(child, DebugFile) and not child.set_manually:
|
||||
child.is_toggled = self.is_toggled
|
||||
@@ -132,9 +101,7 @@ class Directory:
|
||||
child.propagate_toggled_state()
|
||||
|
||||
def propagate_color(self, parent_color=DEFAULT_COLOR):
|
||||
"""
|
||||
Propagates the color from parent to children.
|
||||
"""
|
||||
"""Propagate color from parent to children."""
|
||||
if self.color == DEFAULT_COLOR:
|
||||
self.color = lighten_color(parent_color)
|
||||
for child in self.children:
|
||||
@@ -144,9 +111,7 @@ class Directory:
|
||||
child.propagate_color(self.color)
|
||||
|
||||
def load_from_json(self, json_data):
|
||||
"""
|
||||
Loads a directory structure from a JSON file into this Directory instance.
|
||||
"""
|
||||
"""Load a directory structure from JSON into this Directory."""
|
||||
for item in json_data:
|
||||
if 'children' in item:
|
||||
subdir = Directory(
|
||||
@@ -160,7 +125,6 @@ class Directory:
|
||||
subdir.load_from_json(item['children'])
|
||||
self.add_child(subdir)
|
||||
else:
|
||||
# debug_file = DebugFile.from_dict(item, self)
|
||||
debug_file = DebugFile(
|
||||
filename=item['name'],
|
||||
path=os.path.join(self.path, item['name']),
|
||||
@@ -173,9 +137,7 @@ class Directory:
|
||||
self.add_child(debug_file)
|
||||
|
||||
def reset_colors(self):
|
||||
"""
|
||||
Resets the color of all DebugFile and Directory objects in this directory structure to the default color.
|
||||
"""
|
||||
"""Reset every nested color to the default."""
|
||||
self.color = DEFAULT_COLOR
|
||||
for child in self.children:
|
||||
if isinstance(child, DebugFile):
|
||||
@@ -185,9 +147,7 @@ class Directory:
|
||||
|
||||
|
||||
def lighten_color(color, amount=0.1):
|
||||
"""
|
||||
Lightens the given color by the specified amount.
|
||||
"""
|
||||
"""Lighten the given color by amount."""
|
||||
try:
|
||||
color = color.lstrip('#')
|
||||
r, g, b = int(color[:2], 16), int(color[2:4], 16), int(color[4:6], 16)
|
||||
|
||||
@@ -10,9 +10,7 @@ class File:
|
||||
return get_abspath(self.path)
|
||||
|
||||
def calls_debug_function(self):
|
||||
"""
|
||||
Checks if the file calls the debug function.
|
||||
"""
|
||||
"""True if the file contains a debug() call."""
|
||||
full_path = self.get_abspath()
|
||||
|
||||
if not full_path.endswith('.py') or full_path.endswith('.pyc'):
|
||||
@@ -25,5 +23,4 @@ class File:
|
||||
except (UnicodeDecodeError, FileNotFoundError) as e:
|
||||
print(f"Error reading file {full_path}")
|
||||
result = False
|
||||
# print(f"??calls_debug_function?? {result}")
|
||||
return result
|
||||
@@ -1,9 +1,9 @@
|
||||
import colorsys
|
||||
|
||||
def adjust_brightness(color, brightness_factor):
|
||||
hls = colorsys.rgb_to_hls(*[x/255.0 for x in color]) # Convert RGB to HLS
|
||||
hls = (hls[0], max(0, min(1, hls[1] + brightness_factor)), hls[2]) # Adjust lightness
|
||||
rgb = [int(x*255.0) for x in colorsys.hls_to_rgb(*hls)] # Convert back to RGB
|
||||
hls = colorsys.rgb_to_hls(*[x/255.0 for x in color])
|
||||
hls = (hls[0], max(0, min(1, hls[1] + brightness_factor)), hls[2])
|
||||
rgb = [int(x*255.0) for x in colorsys.hls_to_rgb(*hls)]
|
||||
return rgb
|
||||
|
||||
|
||||
@@ -14,8 +14,5 @@ def bold_and_italicize_text(text):
|
||||
return f"\033[1m\033[3m{text}\033[0m"
|
||||
|
||||
def hex_to_rgb(hex_code):
|
||||
# Remove the '#' symbol if it exists
|
||||
hex_code = hex_code.lstrip('#')
|
||||
|
||||
# Convert the hex code to RGB
|
||||
return tuple(int(hex_code[i:i+2], 16) for i in (0, 2, 4))
|
||||
@@ -2,7 +2,6 @@
|
||||
def is_fstring(arg_name):
|
||||
if not isinstance(arg_name, str):
|
||||
return False
|
||||
# print(f"arg_name: {arg_name}")
|
||||
fstring_start_values = ["f'", "f\""]
|
||||
num_start_matches = sum(arg_name.startswith(start_value) for start_value in fstring_start_values)
|
||||
conditions = [num_start_matches == 1]
|
||||
@@ -12,7 +11,6 @@ def is_text(arg_value, arg_name):
|
||||
arg_is_text = isinstance(arg_value, str) and len(arg_name) > 2 and arg_name[1:len(arg_name)-1] == arg_value and not arg_name.endswith(")")
|
||||
if not arg_is_text:
|
||||
arg_is_text = is_fstring(arg_name)
|
||||
# print(f"is_text: {arg_is_text}")
|
||||
return arg_is_text
|
||||
|
||||
def is_error(arg_value, arg_name):
|
||||
|
||||
@@ -12,10 +12,8 @@ CORS(app)
|
||||
def api_get_structure():
|
||||
print("GET /get_structure")
|
||||
scanned_dir=update_debug_toggles(save_to_file=True)
|
||||
# print("\n\nPS scanned_dir: ", scanned_dir)
|
||||
output = dir_to_output_format(scanned_dir)
|
||||
output = json.dumps(output, ensure_ascii=False, indent=4)
|
||||
# print("output: ", output)
|
||||
return Response(output, mimetype='application/json')
|
||||
|
||||
@app.route('/push_structure', methods=['POST'])
|
||||
@@ -23,7 +21,6 @@ def api_push_structure():
|
||||
print("POST /push_structure")
|
||||
data = request.get_json()
|
||||
data = data['projectStructure']
|
||||
# print(data)
|
||||
with open(DEBUG_TOGGLE_FILE, 'w', encoding='utf-8') as file:
|
||||
json.dump(data, file, indent=4)
|
||||
with open(NEEDS_RESYNC_FILE, 'w') as f:
|
||||
@@ -35,10 +32,8 @@ def api_reset_color():
|
||||
print("POST /reset_color")
|
||||
scanned_dir=update_debug_toggles(save_to_file=False)
|
||||
scanned_dir.reset_colors()
|
||||
# print("RS: scanned_dir: ", scanned_dir)
|
||||
output = dir_to_output_format(scanned_dir)
|
||||
output = json.dumps(output, ensure_ascii=False, indent=4)
|
||||
# print("RS: output: ", output)
|
||||
return Response(output, mimetype='application/json')
|
||||
|
||||
|
||||
|
||||
@@ -19,12 +19,11 @@ class LogConfig:
|
||||
for name, level in self.MODES.items():
|
||||
logging.addLevelName(level, name.upper())
|
||||
self.logger = logging.getLogger('custom_logger')
|
||||
self.logger.propagate = False # Prevent log propagation
|
||||
self.logger.propagate = False
|
||||
handler = logging.StreamHandler()
|
||||
formatter = logging.Formatter('%(message)s')
|
||||
handler.setFormatter(formatter)
|
||||
|
||||
# Remove existing handlers to prevent duplicate logging
|
||||
if self.logger.hasHandlers():
|
||||
self.logger.handlers.clear()
|
||||
|
||||
@@ -39,7 +38,6 @@ class LogConfig:
|
||||
|
||||
def set_debug_mode(self, mode):
|
||||
current_mode = get_log_mode()
|
||||
# print(f"Setting debug mode from {current_mode} -> to {mode}")
|
||||
if mode not in self.MODES: raise ValueError(f"Invalid mode: {mode}")
|
||||
set_log_mode(mode)
|
||||
self.logger.setLevel(self.MODES[mode])
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import os
|
||||
|
||||
# LOG_MODE_FILE = 'debugger/log_mode.txt'
|
||||
LOG_MODE_FILE = os.path.join(os.path.dirname(__file__), 'log_mode.txt')
|
||||
def set_log_mode(mode):
|
||||
with open(LOG_MODE_FILE, 'w') as f:
|
||||
@@ -10,4 +9,4 @@ def get_log_mode():
|
||||
if os.path.exists(LOG_MODE_FILE):
|
||||
with open(LOG_MODE_FILE, 'r') as f:
|
||||
return f.read().strip()
|
||||
return 'all' # Default to 'all' if the file doesn't exist
|
||||
return 'all'
|
||||
|
||||
@@ -8,16 +8,9 @@ from debugger_backend.DebugFile import DebugFile
|
||||
from collections import OrderedDict
|
||||
|
||||
def merge_directories(json_dir: Directory, scanned_dir: Directory):
|
||||
"""
|
||||
Merges two Directory instances: one loaded from JSON (json_dir) and one built from scanning (scanned_dir).
|
||||
The values from json_dir take precedence where attributes overlap.
|
||||
It matches based on full directory and file structure, not just file names.
|
||||
"""
|
||||
# print(f"Merging JSON_DIR: {json_dir.path}\n with SCAN_DIR: {scanned_dir.path}")
|
||||
"""Merge json_dir into scanned_dir; json values win on overlap, matched by full path."""
|
||||
json_abspaths, json_instances = json_dir.get_ordered_abspaths_and_instances()
|
||||
# print(f"json_abspaths: {json_abspaths}")
|
||||
scanned_abspaths, scanned_instances = scanned_dir.get_ordered_abspaths_and_instances()
|
||||
# print(f"scanned_abspaths: {scanned_abspaths}")
|
||||
|
||||
def find_matching_in_structure(scanned_child: Union[DebugFile, Directory], json_dir: Directory):
|
||||
assert json_dir in json_instances, f"JSON_DIR: {json_dir.path} not in json_instances"
|
||||
@@ -28,32 +21,26 @@ def merge_directories(json_dir: Directory, scanned_dir: Directory):
|
||||
try:
|
||||
json_id = json_abspaths.index(scanned_abspath)
|
||||
json_instance = json_instances[json_id]
|
||||
# print(f"Match found: {scanned_child.path} == {json_instance.path}")
|
||||
except ValueError:
|
||||
# print(f"SCANNED_ABSPATH: {scanned_abspath} not in JSON_ABSPATHS")
|
||||
pass
|
||||
return json_instance
|
||||
|
||||
def construct_merged_dir(json_dir: Directory, scanned_dir: Directory):
|
||||
for scanned_child in scanned_dir.children:
|
||||
# Use the new recursive function to find the corresponding child in the JSON directory structure
|
||||
matching_json_child = find_matching_in_structure(scanned_child, json_dir)
|
||||
|
||||
|
||||
if isinstance(scanned_child, DebugFile) and matching_json_child:
|
||||
# Merge attributes from the JSON-loaded structure
|
||||
scanned_child.color = matching_json_child.color
|
||||
scanned_child.is_toggled = matching_json_child.is_toggled
|
||||
scanned_child.set_manually = matching_json_child.set_manually
|
||||
scanned_child.emoji = matching_json_child.emoji
|
||||
|
||||
elif isinstance(scanned_child, Directory) and matching_json_child:
|
||||
# Merge directory attributes
|
||||
scanned_child.color = matching_json_child.color
|
||||
scanned_child.is_toggled = matching_json_child.is_toggled
|
||||
scanned_child.set_manually = matching_json_child.set_manually
|
||||
scanned_child.emoji = matching_json_child.emoji
|
||||
|
||||
# Recursively merge the subdirectories
|
||||
construct_merged_dir(matching_json_child, scanned_child)
|
||||
else:
|
||||
scanned_child.color = DEFAULT_COLOR
|
||||
@@ -65,7 +52,6 @@ def merge_directories(json_dir: Directory, scanned_dir: Directory):
|
||||
|
||||
|
||||
def update_debug_toggles(save_to_file=True) -> Directory:
|
||||
# print(f"[update_debug_toggles]: START")
|
||||
json_loaded_dir = None
|
||||
if os.path.exists(TOGGLE_FILE):
|
||||
with open(TOGGLE_FILE, 'r', encoding='utf-8') as file:
|
||||
@@ -78,65 +64,40 @@ def update_debug_toggles(save_to_file=True) -> Directory:
|
||||
set_manually=json_data[0].get('set_manually', DEFAULT_SET_MANUALLY),
|
||||
emoji=json_data[0].get('emoji', DEFAULT_EMOJI)
|
||||
)
|
||||
# print(f"Root: {json_loaded_dir}")
|
||||
# print("Json Children 1:")
|
||||
# [print(child.path) for child in json_loaded_dir.children]
|
||||
|
||||
json_loaded_dir.load_from_json(json_data[0]['children']) # Assuming the root is in json_data[0]
|
||||
# print("Json Children 2:")
|
||||
# [print(child.path) for child in json_loaded_dir.children]
|
||||
json_loaded_dir.load_from_json(json_data[0]['children'])
|
||||
|
||||
except json.JSONDecodeError:
|
||||
ValueError("Error: JSON file could not be decoded.")
|
||||
else:
|
||||
print("No JSON file found")
|
||||
# 1. Create a directory structure from the filesystem scan
|
||||
# print("Scanning directory...")
|
||||
scanned_dir = Directory(path="",
|
||||
color=json_loaded_dir.color if json_loaded_dir else DEFAULT_COLOR,
|
||||
is_toggled=json_loaded_dir.is_toggled if json_loaded_dir else DEFAULT_TOGGLED,
|
||||
scanned_dir = Directory(path="",
|
||||
color=json_loaded_dir.color if json_loaded_dir else DEFAULT_COLOR,
|
||||
is_toggled=json_loaded_dir.is_toggled if json_loaded_dir else DEFAULT_TOGGLED,
|
||||
set_manually=json_loaded_dir.set_manually if json_loaded_dir else DEFAULT_SET_MANUALLY,
|
||||
emoji=json_loaded_dir.emoji if json_loaded_dir else DEFAULT_EMOJI
|
||||
)
|
||||
# print(f"\n\nNum Children 1: {len(scanned_dir.children)}")
|
||||
# [print(child.path) for child in scanned_dir.children]
|
||||
scanned_dir.build_structure()
|
||||
# print(f"\n\nNum Children 2: {len(scanned_dir.children)}")
|
||||
# [print(child.path) for child in scanned_dir.children]
|
||||
scanned_dir.prune_empty()
|
||||
# print(f"\n\nNum Children 3: {len(scanned_dir.children)}")
|
||||
# [print(child.path) for child in scanned_dir.children]
|
||||
|
||||
# print("1.1 Merged Dir First Child: ", scanned_dir.children[0])
|
||||
# 4. Propagate the toggled state and color through the merged structure
|
||||
scanned_dir.propagate_toggled_state()
|
||||
# print(f"\n\nNum Children 4: {len(scanned_dir.children)}")
|
||||
# [print(child.path) for child in scanned_dir.children]
|
||||
|
||||
# 3. Merge the two directory structures
|
||||
if json_loaded_dir:
|
||||
merge_directories(json_loaded_dir, scanned_dir)
|
||||
|
||||
# print(f"\n\nNum Children 5: {len(scanned_dir.children)}")
|
||||
|
||||
scanned_dir.propagate_color()
|
||||
output = dir_to_output_format(scanned_dir)
|
||||
# print(f"\n\nNum Children 6: {len(scanned_dir.children)}")
|
||||
|
||||
|
||||
# 5. Write the updated structure back to the JSON file
|
||||
if save_to_file:
|
||||
with open(TOGGLE_FILE, 'w', encoding='utf-8') as file:
|
||||
json.dump(output, file, ensure_ascii=False, indent=4)
|
||||
# print(f"[update_debug_toggles]: END")
|
||||
return scanned_dir
|
||||
|
||||
def dir_to_output_format(input_dir):
|
||||
root_node = {
|
||||
"name": "root",
|
||||
"color": input_dir.color, # Use input_dir's color
|
||||
"is_toggled": input_dir.is_toggled, # Use input_dir's toggled state
|
||||
"set_manually": input_dir.set_manually, # Use input_dir's set_manually
|
||||
"emoji": input_dir.emoji, # Use input_dir's emoji
|
||||
"color": input_dir.color,
|
||||
"is_toggled": input_dir.is_toggled,
|
||||
"set_manually": input_dir.set_manually,
|
||||
"emoji": input_dir.emoji,
|
||||
"children": input_dir.to_dict()["children"]
|
||||
}
|
||||
return [ordered(root_node)]
|
||||
|
||||
Reference in New Issue
Block a user