Skip to content

Commit

Permalink
Working on @LOAD_LAYOUT
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaid committed Dec 7, 2019
1 parent 49e52cf commit 504a3a0
Show file tree
Hide file tree
Showing 8 changed files with 1,442 additions and 35 deletions.
3 changes: 2 additions & 1 deletion LPHK.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def init():

else:
print("[LPHK] Invalid argument: " + sys.argv[1] + ". Ignoring...")


files.init(USER_PATH)
sound.init(USER_PATH)

def shutdown():
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ In order of priority:
* Default layout specification
* Auto connect overide
* Force launchpad model setting
* Temporary fix for LP classic, make use colors from function rows (orange, red)
* Add more sound commands
* Use different sound library that supports playing an arbitrary number of sounds, and controlling each sound individually while it plays
* Add optional label argument to `SOUND`
Expand Down
90 changes: 63 additions & 27 deletions files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from time import sleep
import os, json, platform, subprocess

LAYOUT_PATH = "/user_layouts/"
SCRIPT_PATH = "/user_scripts/"
LAYOUT_DIR = "user_layouts"
SCRIPT_DIR = "user_scripts"

FILE_VERSION = "0.1.1"

Expand All @@ -13,24 +13,37 @@
LEGACY_LAYOUT_EXT = ".LPHKlayout"
LEGACY_SCRIPT_EXT = ".LPHKscript"

USER_PATH = None
LAYOUT_PATH = None
SCRIPT_PATH = None

import window

curr_layout = None
in_error = False
layout_changed_since_load = False

def init(user_path_in):
global USER_PATH
global LAYOUT_PATH
global SCRIPT_PATH
USER_PATH = user_path_in
LAYOUT_PATH = os.path.join(USER_PATH, LAYOUT_DIR)
SCRIPT_PATH = os.path.join(USER_PATH, SCRIPT_DIR)

def save_layout(layout, name):
with open(name, "w") as f:
json.dump(layout, f, indent=2, sort_keys=True)
print("[files] Saved layout as " + name)

def load_layout(name):
def load_layout_json(name, printing=True):
with open(name, "r") as f:
layout = json.load(f)
print("[files] Loaded layout " + name)
if printing:
print("[files] Loaded layout " + name)
return layout

def load_legacy_layout(name):
def load_layout_legacy(name, printing=True):
layout = dict()
layout["version"] = "LEGACY"

Expand All @@ -53,9 +66,39 @@ def load_legacy_layout(name):
script_text = info[1].replace(":LPHK_NEWLINE_REP:", "\n")

layout["buttons"][-1].append({"color": color, "text": script_text})
print("[files] Loaded legacy layout " + name)
if printing:
print("[files] Loaded legacy layout " + name)
return layout


def load_layout(name, popups=True, save_converted=True, printing=True):
basename_list = os.path.basename(name).split(os.path.extsep)
ext = basename_list[-1]
title = os.path.extsep.join(basename_list[:-1])

if "." + ext == LEGACY_LAYOUT_EXT:
# TODO: Error checking on resultant JSON
layout = load_layout_legacy(name, printing=printing)


if save_converted:
name = os.path.dirname(name) + os.path.sep + title + LAYOUT_EXT
if popups:
window.app.popup(window.app, "Legacy layout loaded...", window.app.info_image, "The layout is in the legacy .LPHKlayout format. It will be\nconverted to the new .lpl format, and will be saved as such.", "OK")
else:
if printing:
print("[files] The layout is in the legacy .LPHKlayout format. It will be converted to the new .lpl format, and will be saved as such.")
save_layout(layout, name)
else:
# TODO: Error checking on loaded JSON
try:
layout = load_layout_json(name, printing=printing)
except json.decoder.JSONDecodeError:
if popups:
window.app.popup(window.app, "Error loading file!", window.app.info_image, "The layout is not in valid JSON format (the new .lpl extention).\n\nIf this was renamed from a .LPHKlayout file, please change\nthe extention back to .LPHKlayout and try loading again.", "OK")
raise

return layout

def save_lp_to_layout(name):
layout = dict()
layout["version"] = FILE_VERSION
Expand All @@ -71,7 +114,7 @@ def save_lp_to_layout(name):

save_layout(layout=layout, name=name)

def load_layout_to_lp(name):
def load_layout_to_lp(name, popups=True, save_converted=True, preload=None):
global curr_layout
global in_error
global layout_changed_since_load
Expand All @@ -81,24 +124,11 @@ def load_layout_to_lp(name):
scripts.unbind_all()
window.app.draw_canvas()

basename_list = os.path.basename(name).split(os.path.extsep)
ext = basename_list[-1]
title = os.path.extsep.join(basename_list[:-1])

if "." + ext == LEGACY_LAYOUT_EXT:
# TODO: Error checking on resultant JSON
layout = load_legacy_layout(name)

name = os.path.dirname(name) + os.path.sep + title + LAYOUT_EXT
window.app.popup(window.app, "Legacy layout loaded...", window.app.info_image, "The layout is in the legacy .LPHKlayout format. It will be\nconverted to the new .lpl format, and will be saved as such.", "OK")
save_layout(layout, name)
if preload == None:
print("FUK")
layout = load_layout(name, popups=popups, save_converted=save_converted)
else:
# TODO: Error checking on loaded JSON
try:
layout = load_layout(name)
except json.decoder.JSONDecodeError:
window.app.popup(window.app, "Error loading file!", window.app.info_image, "The layout is not in valid JSON format (the new .lpl extention).\n\nIf this was renamed from a .LPHKlayout file, please change\nthe extention back to .LPHKlayout and try loading again.", "OK")
return
layout = preload

for x in range(9):
for y in range(9):
Expand All @@ -117,7 +147,10 @@ def load_layout_to_lp(name):
script_validation = scripts.validate_script(script_text)
except:
new_layout_func = lambda: window.app.unbind_lp(prompt_save = False)
window.app.popup(window.app, "Script Validation Error", window.app.error_image, "Fatal error while attempting to validate script.\nPlease see LPHK.log for more information.", "OK", end_command = new_layout_func)
if popups:
window.app.popup(window.app, "Script Validation Error", window.app.error_image, "Fatal error while attempting to validate script.\nPlease see LPHK.log for more information.", "OK", end_command = new_layout_func)
else:
print("[files] Fatal error while attempting to validate script.\nPlease see LPHK.log for more information.")
raise
if script_validation != True:
lp_colors.update_all()
Expand All @@ -134,7 +167,10 @@ def load_layout_to_lp(name):

curr_layout = name
if converted_to_rg:
window.app.popup(window.app, "Layout converted to Classic/Mini/S...", window.app.info_image, "The colors in this layout have been converted to be\ncompatable with the Launchpad Classic/Mini/S.\n\nChanges have not yet been saved to the file.", "OK")
if popups:
window.app.popup(window.app, "Layout converted to Classic/Mini/S...", window.app.info_image, "The colors in this layout have been converted to be\ncompatable with the Launchpad Classic/Mini/S.\n\nChanges have not yet been saved to the file.", "OK")
else:
print("[files] The colors in this layout have been converted to be compatable with the Launchpad Classic/Mini/S. Changes have not yet been saved to the file.")
layout_changed_since_load = True
else:
layout_changed_since_load = False
Expand Down
1 change: 1 addition & 0 deletions parse.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# CURRENTLY UNUSED
import parser, types

variables = dict()
Expand Down
30 changes: 29 additions & 1 deletion scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import files

VALID_COMMANDS = ["@ASYNC", "@SIMPLE", "STRING", "DELAY", "TAP", "PRESS", "RELEASE", "WEB", "WEB_NEW", "SOUND", "WAIT_UNPRESSED", "M_MOVE", "M_SET", "M_SCROLL", "M_LINE", "M_LINE_MOVE", "M_LINE_SET", "LABEL", "IF_PRESSED_GOTO_LABEL", "IF_UNPRESSED_GOTO_LABEL", "GOTO_LABEL", "REPEAT_LABEL", "IF_PRESSED_REPEAT_LABEL", "IF_UNPRESSED_REPEAT_LABEL", "M_STORE", "M_RECALL", "M_RECALL_LINE", "OPEN", "RELEASE_ALL", "RESET_REPEATS"]
VALID_COMMANDS = ["@ASYNC", "@SIMPLE", "@LOAD_LAYOUT", "STRING", "DELAY", "TAP", "PRESS", "RELEASE", "WEB", "WEB_NEW", "SOUND", "WAIT_UNPRESSED", "M_MOVE", "M_SET", "M_SCROLL", "M_LINE", "M_LINE_MOVE", "M_LINE_SET", "LABEL", "IF_PRESSED_GOTO_LABEL", "IF_UNPRESSED_GOTO_LABEL", "GOTO_LABEL", "REPEAT_LABEL", "IF_PRESSED_REPEAT_LABEL", "IF_UNPRESSED_REPEAT_LABEL", "M_STORE", "M_RECALL", "M_RECALL_LINE", "OPEN", "RELEASE_ALL", "RESET_REPEATS"]
ASYNC_HEADERS = ["@ASYNC", "@SIMPLE"]

threads = [[None for y in range(9)] for x in range(9)]
Expand Down Expand Up @@ -435,6 +435,19 @@ def main_logic(idx):
return idx + 1
#RELEASE
kb.release(key)
elif split_line[0] == "@LOAD_LAYOUT":
layout_name = " ".join(split_line[1:])
print("[scripts] " + coords + " Load layout " + layout_name)
layout_path = os.path.join(files.LAYOUT_PATH, layout_name)
if not os.path.isfile(layout_path):
print("[scripts] " + coords + " ERROR: Layout file does not exist.")
return -1
try:
layout = files.load_layout(layout_path, popups=False, save_converted=False)
except files.json.decoder.JSONDecodeError:
print("[scripts] " + coords + " ERROR: Layout is malformated.")
return -1
files.load_layout_to_lp(layout_path, popups=False, save_converted=False, preload=layout)
elif split_line[0] == "OPEN":
path_name = " ".join(split_line[1:])
print("[scripts] " + coords + " Open file or folder " + path_name)
Expand Down Expand Up @@ -575,6 +588,7 @@ def validate_script(script_str):
if len(first_line_split) > 1:
return ("@ASYNC takes no arguments.", script_lines[0])
temp = script_lines.pop(0)

if first_line_split[0] == "@SIMPLE":
if len(first_line_split) < 2:
return ("@SIMPLE requires a key to bind.", first_line)
Expand All @@ -586,6 +600,20 @@ def validate_script(script_str):
if line != "" and line[0] != "-":
return ("When @SIMPLE is used, scripts can only contain comments.", line)

if first_line_split[0] == "@LOAD_LAYOUT":
for line in script_lines[1:]:
if line != "" and line[0] != "-":
return ("When @LOAD_LAYOUT is used, scripts can only contain comments.", line)

layout_path = os.path.join(files.LAYOUT_PATH, " ".join(first_line_split[1:]))
if not os.path.isfile(layout_path):
return ("'" + layout_path + "' does not exist!", first_line)

try:
layout = files.load_layout(layout_path, popups=False, save_converted=False, printing=False)
except:
return ("Layout '" + layout_path + "' is malformatted.", first_line)

#parse labels
labels = []
for line in script_lines:
Expand Down
Loading

0 comments on commit 504a3a0

Please sign in to comment.