Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formatted pt2 #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions chromium/utils/remove-json-keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,49 @@
json_folder = '_locales'

# UI initializations
os.system('color') ; print('\033[0;92m') # set font to bright green
os.system('color')
print('\033[0;92m') # set font to bright green
terminal_width = os.get_terminal_size()[0]
def print_trunc(msg) : print(msg if len(msg) < terminal_width else msg[0:terminal_width-4] + '...')

def print_trunc(msg):
print(msg if len(msg) < terminal_width else msg[0 : terminal_width - 4] + '...')

print('')

# Prompt user for keys to remove
keys_to_remove = []
while True:
key = input("Enter key to remove (or ENTER if done): ")
if not key : break
if not key:
break
keys_to_remove.append(key)

# Determine closest JSON dir
print_trunc(f'Searching for { json_folder }...')
script_dir = os.path.abspath(os.path.dirname(__file__))
for root, dirs, files in os.walk(script_dir): # search script dir recursively
for root, dirs, files in os.walk(script_dir): # search script dir recursively
if json_folder in dirs:
json_dir = os.path.join(root, json_folder) ; break
else: # search script parent dirs recursively
json_dir = os.path.join(root, json_folder)
break
else: # search script parent dirs recursively
parent_dir = os.path.dirname(script_dir)
while parent_dir and parent_dir != script_dir:
for root, dirs, files in os.walk(parent_dir):
if json_folder in dirs:
json_dir = os.path.join(root, json_folder) ; break
if json_dir : break
json_dir = os.path.join(root, json_folder)
break
if json_dir:
break
parent_dir = os.path.dirname(parent_dir)
else : json_dir = None
else:
json_dir = None

# Print result
if json_dir : print_trunc(f'JSON directory found!\n\n>> { json_dir }\n')
else : print_trunc(f'Unable to locate a { json_folder } directory.') ; exit()
if json_dir:
print_trunc(f'JSON directory found!\n\n>> { json_dir }\n')
else:
print_trunc(f'Unable to locate a { json_folder } directory.')
exit()

# Process JSON files and remove specified keys
keys_removed, keys_skipped, processed_count = [], [], 0
Expand All @@ -52,7 +63,8 @@ def print_trunc(msg) : print(msg if len(msg) < terminal_width else msg[0:termina

# Open found JSON file
file_path = os.path.join(root, filename)
with open(file_path, 'r', encoding='utf-8') as f : data = f.read()
with open(file_path, 'r', encoding='utf-8') as f:
data = f.read()

# Remove keys
modified = False
Expand All @@ -62,9 +74,11 @@ def print_trunc(msg) : print(msg if len(msg) < terminal_width else msg[0:termina
if count > 0:
keys_removed.append((key, os.path.relpath(file_path, json_dir)))
modified = True
else : keys_skipped.append((key, os.path.relpath(file_path, json_dir)))
else:
keys_skipped.append((key, os.path.relpath(file_path, json_dir)))
if modified:
with open(file_path, 'w', encoding='utf-8') as f : f.write(data)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(data)
processed_count += 1

# Print file summaries
Expand Down