Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Mar 1, 2024
2 parents 7093f2b + 5ca13db commit 54ef4d7
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions Manual/GenerateKeyboardShortcutTableFromJson.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,30 @@
### Provide the full directory path to the .json files as the command line argument.
### The output HTML file will also be placed there.
### For example: CMD > python GenerateKeyboardShortcutTableFromJson.py "C:/Users/Dev/Documents/Manual/" -name_as_desc
###
### You can provide an optional argument:
###
### -name_as_desc: Add this to write the hotkey's name as the description.
###
### Important: The JSON cannot contain trailing commas, this isn't supported
### using the built-in json module.
### Important: Technically, the JSON cannot contain trailing commas, this isn't supported
### using the built-in json module. Though it is supported through the yy_load function.
###

import sys
import json
import re
from collections import OrderedDict

def yy_load(file):
""" Load json from a file that possibly contains trailing commas """
# Do some tricky regex substitution
# so we can use the json module
data_string = ''.join(file.readlines())
data_string = re.sub("(,)(\s*[]}])","\g<2>", data_string)

# Now we can import using the json module
return json.loads(data_string)

# Utility functions
def get_combo_string(combo):
if not combo:
Expand Down Expand Up @@ -42,9 +57,10 @@ def get_combo_string(combo):
shortcuts_per_location = OrderedDict() # stores shortcuts under locations

# First read the Windows defaults file
with open(fdir + "/" + fname_win_hotkeys, 'r') as f:
with open(fdir + "/" + fname_win_hotkeys, 'r', encoding="utf-8") as f:
# Load all the data
input = json.load(f)
# input = json.load(f) # risk of errors if trailing commas are present
input = yy_load(f) # regex-replace variety that fixes things

# Add items under their respective locations (i.e. "group" per location)
for shortcut in input:
Expand Down Expand Up @@ -84,7 +100,7 @@ def get_combo_string(combo):
# Then add the combos in the macOS defaults file
with open(fdir + "/" + fname_mac_hotkeys, 'r') as f:
# Load all the data
input = json.load(f)
input = yy_load(f)

# Add items under their respective locations
for shortcut in input:
Expand Down

0 comments on commit 54ef4d7

Please # to comment.