Skip to content

Commit

Permalink
Merge pull request #6 from Gabisonfire/0.6
Browse files Browse the repository at this point in the history
0.6
  • Loading branch information
Gabisonfire committed Mar 9, 2023
2 parents f0ca805 + b620620 commit 4b1e2bb
Show file tree
Hide file tree
Showing 26 changed files with 1,093 additions and 109 deletions.
1 change: 0 additions & 1 deletion deckster/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
__version__ = "0.5.0"
108 changes: 92 additions & 16 deletions deckster/common/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,38 @@
import yaml
import logging
import sys
from deckster.common.keys import Key
from deckster.common.keys import Key, fake_key
from pathlib import Path
from filelock import FileLock
from jsonmerge import merge

global __version__
__version__ = "0.5.0"
__version__ = "0.6"

logger = logging.getLogger("deckster")

dir_path = f"{str(Path.home())}/.config/deckster/"

logger.debug(f"Config path set to: {dir_path}")

def read_config(cfg):
full_cfg = os.path.join(dir_path, "config.json")

class _counter:
count = 0

def read_config(cfg, custom_config = None):
"""Read the configuration file
Args:
cfg (string): the config to read (key)
custom_config (string, optional): Sepcify an alternate path. Defaults to None.
Returns:
string: value
"""
if custom_config == None:
full_cfg = os.path.join(dir_path, "config.json")
else:
full_cfg = os.path.join(dir_path, custom_config)
lock_path = f"{full_cfg}.lock"
cfg_file = None
with FileLock(lock_path):
Expand All @@ -31,9 +48,20 @@ def read_config(cfg):
for k in cfg_file:
if k == cfg:
return cfg_file[k]
return None

def write_config(cfg, value):
full_cfg = os.path.join(dir_path, "config.json")
def write_config(cfg, value, custom_config = None):
"""Write value for a config
Args:
cfg (string): config to update
value (string): value to write
custom_config (string, optional): Provide an alternate path. Defaults to None.
"""
if custom_config == None:
full_cfg = os.path.join(dir_path, "config.json")
else:
full_cfg = os.path.join(dir_path, custom_config)
lock_path = f"{full_cfg}.lock"
with FileLock(lock_path):
with open(full_cfg, "r") as cfgFile:
Expand All @@ -44,12 +72,30 @@ def write_config(cfg, value):
logger.debug(f"Writing '{value}' to '{cfg}'")
json.dump(data, cfgFile, indent=2)

def _fetch_templated_keys(json_key):
template_name = json_key["template"]
keysdir = os.path.expanduser(read_config("keys_dir"))
tmpl_files = [f for f in os.listdir(keysdir) if os.path.isfile(os.path.join(keysdir, f)) and (os.path.splitext(f)[1] in [".tmpl"])]
for tmpl_file in tmpl_files:
if os.path.splitext(tmpl_file)[0] == template_name:
template = json.load(open(os.path.join(keysdir, f"{template_name}.tmpl")))
if "key" in template or "page" in template:
raise Exception("'key' and 'template' cannot be in a template file.")
# The order here is important as changes in second param will overwrite the first one.
return merge(template, json_key)

# Add lock here too
def read_keys():
"""Read all keys
Returns:
list: A list containing all keys
"""
logger.debug(f"Reading keys...")
templist = []
keysdir = os.path.expanduser(read_config("keys_dir"))
keyfiles = [f for f in os.listdir(keysdir) if os.path.isfile(os.path.join(keysdir, f))]
keyfiles = [f for f in os.listdir(keysdir) if os.path.isfile(os.path.join(keysdir, f)) and (os.path.splitext(f)[1] in [".yaml", ".yml", ".json"])]

logger.debug(f"Found keys file: {keyfiles}")
json_keys = []
for files in keyfiles:
Expand All @@ -67,23 +113,51 @@ def read_keys():
except Exception as e:
logger.error(f"Error parsing {os.path.join(keysdir, files)}: {e}")
for x in json_keys:
if "template" in x:
x = _fetch_templated_keys(x)
templist.append(Key(x))
return templist

def find_key(key, page, key_list):
"""Find specific key on a page
Args:
key (integer): The key number
page (integer): The page to search the key
key_list (list): A list of keys
Returns:
Key: A found key
"""
logger.debug(f"Finding key {key} on page {page}.")
for x in key_list:
if x.key == key and x.page == page:
return x

def max_page(key_list):
"""Find the last page
Args:
key_list (list): List of keys
Returns:
integer: Page number
"""
high = 0
for x in key_list:
if x.page > high:
high = x.page
return high

def write_key_config(key, page, cfg, value):
"""Write value for a given config for a given key.
Args:
key (Key): A key
page (integer): Page number
cfg (string): config to write
value (string): value to write
"""
keysdir = os.path.expanduser(read_config("keys_dir"))
logger.debug(f"{keysdir} is the keys directory.")
keyfiles = [f for f in os.listdir(keysdir) if os.path.isfile(os.path.join(keysdir, f))]
Expand Down Expand Up @@ -127,14 +201,16 @@ def write_key_config(key, page, cfg, value):
logger.error(f"Could not find a match for key {key} on page {page}.")

def empty_set(key_count, page):
"""Creates empty keys
Args:
key_count (integer): Number of keys to generate
page (integer): The page to assign to generated keys
Returns:
list: A list of empty keys
"""
ks = []
for k in range(key_count):
x = {
"key": k,
"page": page,
"plugin": "empty",
"icon_default": "empty",
"button_type": "toggle"
}
ks.append(Key(x))
return ks
ks.append(fake_key(k, "empty", page))
return ks
Loading

0 comments on commit 4b1e2bb

Please sign in to comment.