From 20fb08f202d0cd47232d3aef3e7dae095cc7322a Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Mon, 30 Dec 2019 22:13:10 +0530 Subject: [PATCH 1/8] Add gINIe parser. --- .gitignore | 3 ++ ginie.py | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 .gitignore create mode 100755 ginie.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..329efad --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +*.pyc +config.gie diff --git a/ginie.py b/ginie.py new file mode 100755 index 0000000..6cff476 --- /dev/null +++ b/ginie.py @@ -0,0 +1,135 @@ +#!/usr/bin/env python3 +"gINIe Parser for Wish" + +import re +import sys + + +class GinieParseError(ValueError): + "Exception Class for errors in" + + +def loads(string): + """ + Deserialize `string` (a `str` instance containing a gINIe document) + into a Python list. + + Return Value: List of `block` and `section` dicts + + `block` dicts are of form + { + 'type': 'block', + 'name': 'name_of_block_as_in_config', + 'config': {} + } + where `name` and `config` keys are based on the gINIe document. + + `section` dicts are of form + + { + 'type': 'block', + 'name': 'name_of_block_as_in_config', + 'blocks': [] + } + where `name` and `blocks` keys are based on the gINIe document and blocks + is a list of block dicts found in the section. + """ + config = [] + section_re = re.compile(r"^\|(?P
.+)\|$") + block_re = re.compile(r"^\[(?P[^\]]*)\]$") + line_re = re.compile(r"^\s*(?P[^\s=]+)(\s*)=\s*?(?P.*)$") + empty_re = re.compile(r"^\s*$") + comment_re = re.compile(r"^\s*(#|;)") + current_block = None + current_section = None + for idx, line in enumerate(string.splitlines()): + idx += 1 # Since line numbers begin with 1 + # Skip comments and empty lines + if empty_re.match(line) or comment_re.match(line): + continue + + # Section parsing + if line.startswith("|"): + match = section_re.match(line) + if match is None: + err = "Invalid line {}".format(idx) + raise GinieParseError(err) + current_section = { + "type": "section", + "name": match.group("section"), + "blocks": [] + } + config.append(current_section) + continue + + # Block Parsing + if line.startswith("["): + match = block_re.match(line) + if match is None: + err = "Invalid block name on line {}".format(idx) + raise GinieParseError(err) + current_block = { + "type": "block", + "name": match.group("block"), + "config": {} + } + if current_section is None: + config.append(current_block) + else: + current_section["blocks"].append(current_block) + continue + + # If it is neither a comment, nor a section, nor a block, it has to be + # a key, value pair. + if current_block is None: + raise GinieParseError("Found lines outside a block") + + match = line_re.match(line) + if match is None: + raise GinieParseError("Invalid line {}: {}".format(idx, line)) + + value = match.group('value').strip() + if value: + if value[0] == value[-1] == "'" or value[0] == value[-1] == '"': + value = value[1:-1] + current_block["config"][match.group('key').strip()] = value + + return config + + +def load(file): + """ + Deserialize `file` (a file-like object containing a gINIe document) + into a Python list. + + Return Value: List of `block` and `section` dicts + + `block` dicts are of form + { + 'type': 'block', + 'name': 'name_of_block_as_in_config', + 'config': {} + } + where `name` and `config` keys are based on the gINIe document. + + `section` dicts are of form + + { + 'type': 'block', + 'name': 'name_of_block_as_in_config', + 'blocks': [] + } + where `name` and `blocks` keys are based on the gINIe document and blocks + is a list of block dicts found in the section. + """ + return loads(file.read()) + + +if __name__ == "__main__": + if len(sys.argv) == 1: + print("Usage: {} file.gie|-".format(sys.argv[0])) + elif sys.argv[1] == '-': + print(load(sys.stdin)) + else: + with open(sys.argv[1]) as source: + print(load(source)) From 76b9bd3ad180593ee1596f0a218b3f588bfd1034 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 31 Dec 2019 10:29:55 +0530 Subject: [PATCH 2/8] Add default config for wish. --- config.default.gie | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 config.default.gie diff --git a/config.default.gie b/config.default.gie new file mode 100644 index 0000000..5bc65c0 --- /dev/null +++ b/config.default.gie @@ -0,0 +1,59 @@ +# vim: set ft=dosini: +# +# Comments start with a # or ; and always exist on a line of their own. +# +# Key value pairs are in the form of key = value. Keys cannot have whitespaces +# or = in them. Values can have any character as part of them. Surrounding +# spaces in values are stripped away. To keep surrounding spaces as a part of +# the value. Although, keys are case-sensitive in gINIe, wish treats them case- +# insensitively. +# +# Block names are enclosed in [] (e.g. [core]). Block names are case sensitive. +# All key value pairs after a block starts and before the next block begins are +# considered a part of that block. All key value pairs must be in a block. +# +# Available Blocks: +# +# core: Core block configures Wish itself. Available keys are: +# - auto_newline: Automatically add a newline if last line of output +# doesn't end in newline. (0 to disable, 1 to enable) +# - theme: Wish theme. +# - powerline: Enable / Disable powerline. (0 to disable, 1 to enable) +# +# plugin: Adds a plugin to the section the block is added to. All config for +# that plugin goes there. The key "name" defines the plugin to use. +# Plugin blocks outside a section are ignored. +# +# Section names are enclosed in || (e.g. |left|). All blocks after a section +# starts and before the next section begins are considered a part of that +# section. Blocks don't necessarily need to be in a section. +# +# Available sections are left, right for left prompt and right prompt +# respectively. + +[core] +auto_newline = 1 +powerline = 1 +theme = plain + +|left| +[plugin] +name = exit_code_smiley + +[plugin] +name = bg_jobs + +[plugin] +name = date +format = %d %b %H:%M + +[plugin] +name = path + +[plugin] +name = newline + +[plugin] +name = vcs + +|right| From aabc6ebfd67ba02b8e4196a4ae20dc10218803b3 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 31 Dec 2019 11:19:07 +0530 Subject: [PATCH 3/8] Add wish.py for generating wrappers around plugins. --- wish.py | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100755 wish.py diff --git a/wish.py b/wish.py new file mode 100755 index 0000000..03cec38 --- /dev/null +++ b/wish.py @@ -0,0 +1,140 @@ +#!/usr/bin/env python3 +""" +Generate function wrappers from config for wish. + +wish.py works by writing plugins which are isolated from the global plugins. +It assigns all the config to relevant variables inside a wrapper. e.g. + +If the config file is + +``` +|left| +[plugin] +name = date +format = %F +``` + +wish.py creates a wrapper around date as 0_date where 0 is the index of plugin. +Inside the wrapper, in each function, it sets WISH_DATE_FORMAT="%F" and +WISH_DATE_BG=$WISH_0_DATE_BG (same for FG), so the plugin sees its config as +defined in the config file. After calling the plugin's relevant function, it +sets WISH_0_DATE_BG=$WISH_DATE_BG (same for FG) in case, the plugin has changed +it). This allows multiple instances of the same plugin to coexist with +different config and different themes. +""" + +from os.path import isfile +import shlex +import sys + +import ginie + + +def parse_core(core): + "Parses a core block" + config = core["config"] + for key in core["config"]: + print("WISH_{}={}".format(key.upper(), shlex.quote(config[key]))) + + +def parse_plugin(plugin, plugin_idx): + "Parses a plugin and writes a wrapper around it" + config = plugin["config"] + try: + plugin_name = config["name"].strip().lower() + config["name"] = plugin_name + except KeyError: + return False + + keys_from_source = ("BG", "FG") + for j in ("start", "end", "set_colors", "main"): + print("function wish_{}_{}_{}()".format(plugin_idx, plugin_name, j)) + print("{") + for key in keys_from_source: + print("\tlocal WISH_{0}_{2}=$WISH_{1}_{0}_{2}".format( + plugin_name.upper(), + plugin_idx, + key.upper() + )) + for key in config: + if key == "name": + continue + print("\tlocal WISH_{}_{}={}".format(plugin_name.upper(), + key.upper(), + shlex.quote(config[key]))) + print("\twish_{}_{} $*".format(plugin_name, j)) + print("\tlocal err=$?") + for key in keys_from_source: + print("\tWISH_{1}_{0}_{2}=$WISH_{0}_{2}".format( + plugin_name.upper(), + plugin_idx, + key.upper() + )) + print("\treturn $err") + print("}") + return True + + +def print_plugin_list(name, array, always=False): + "Prints a list of plugins as a bash array" + if array or always: + print("{}=(".format(name)) + for plugin in array: + print("\t" + plugin) + print(")") + + +def main(): + "Parse a config file passed as first argument" + config_file = [] + for file_name in sys.argv[1:]: + if isfile(file_name): + with open(file_name) as file: + config_file += ginie.load(file) + else: + print("echo Invalid config file: {}".format( + shlex.quote(file_name))) + + plugin_idx = 0 + plugins_to_source = [] + left_plugins = [] + right_plugins = [] + for i in config_file: + kind = i["type"] + name = i["name"] + # Parse core blocks outside a section. + # No other blocks outside a section need to be parsed for now. + if kind == "block" and name == "core": + parse_core(i) + elif kind == "section": + # All sections other than left and right are skipped. + if name not in ("left", "right"): + continue + for block in i["blocks"]: + if block["name"] == "core": + parse_core(block) + elif block["name"] == "plugin" and parse_plugin(block, + plugin_idx): + plugin_name = "{}_{}".format(plugin_idx, + block["config"]["name"]) + if name == "left": + left_plugins.append(plugin_name) + else: + right_plugins.append(plugin_name) + plugins_to_source.append(block["config"]["name"]) + plugin_idx += 1 + + print_plugin_list("WISH_PLUGINS", left_plugins) + print_plugin_list("WISH_RIGHT_PLUGINS", right_plugins) + print_plugin_list("WISH_PLUGINS_SOURCE", plugins_to_source) + + +# Python 2 patch +# TODO: Write a better patch for Python 2 +try: + shlex.quote +except AttributeError: + shlex.quote = lambda x: x + +if __name__ == "__main__": + main() From 9a2d8314326bafad6aa6f25cbff2d98444f09fe4 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 31 Dec 2019 11:22:48 +0530 Subject: [PATCH 4/8] [Breaking] Update custom_text to better variable namespacing. --- plugins/custom_text.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/custom_text.sh b/plugins/custom_text.sh index eb0acbc..dc5ffaf 100644 --- a/plugins/custom_text.sh +++ b/plugins/custom_text.sh @@ -9,10 +9,10 @@ function wish_custom_text_end() { function wish_custom_text_set_colors() { WISH_CUSTOM_TEXT_FG=${WISH_CUSTOM_TEXT_FG:-$WISH_DEFAULT_FG} WISH_CUSTOM_TEXT_BG=${WISH_CUSTOM_TEXT_BG:-$WISH_DEFAULT_BG} - local default_text='To set custom text here, add WISH_CUSTOM_TEXT="your text" in ~/.bashrc' - WISH_CUSTOM_TEXT=${WISH_CUSTOM_TEXT:-$default_text} + local default_text='To set custom text here, add text="your text" in your config' + WISH_CUSTOM_TEXT_TEXT=${WISH_CUSTOM_TEXT:-$default_text} } function wish_custom_text_main() { - wish_append $WISH_CUSTOM_TEXT_BG $WISH_CUSTOM_TEXT_FG "$WISH_CUSTOM_TEXT" + wish_append $WISH_CUSTOM_TEXT_BG $WISH_CUSTOM_TEXT_FG "$WISH_CUSTOM_TEXT_TEXT" } From 186675ae4eefcb25139c599ff39e3b773aebdf6f Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 31 Dec 2019 11:55:23 +0530 Subject: [PATCH 5/8] Source config file. Add WISH_CONFIG_FILE array for paths of config files. --- wish.sh | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/wish.sh b/wish.sh index d4cc42c..a139ea4 100644 --- a/wish.sh +++ b/wish.sh @@ -10,10 +10,26 @@ function wish_print_right_prompt() { } function wish_init() { + # Find default config file if WISH_CONFIG_FILE is unset + if [[ ! -v WISH_CONFIG_FILE ]]; then + for path in "$XDG_CONFIG_HOME" "/usr/share" "$HOME/.config"; do + if [[ -f "$path/wish/config.gie" ]]; then + WISH_CONFIG_FILE="$path/wish/config.gie" + break + fi + done + fi + # Source config files + for path in "$XDG_CONFIG_HOME" "/usr/share" "$HOME/.config"; do + if [[ -f "$path/wish/wish.py" ]]; then + source <($path/wish/wish.py ${WISH_CONFIG_FILE[@]}) + break + fi + done # Source all plugins local plugin local path - for plugin in ${WISH_PLUGINS[@]} ${WISH_RIGHT_PLUGINS[@]}; do + for plugin in ${WISH_PLUGINS_SOURCE[@]}; do for path in "$XDG_CONFIG_HOME" "/usr/share" "$HOME/.config"; do source "$path/wish/plugins/$plugin.sh" &> /dev/null && break done From e01b29d4a0d3cdec6b8446be1d87749bdded5f51 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 31 Dec 2019 12:43:33 +0530 Subject: [PATCH 6/8] Update install.sh --- install.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/install.sh b/install.sh index c1eafd6..ae5b083 100755 --- a/install.sh +++ b/install.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash if hash git; then - git clone https://gitlab.com/ceda_ei/wish.git $HOME/.config/wish.git + git clone https://gitlab.com/ceda_ei/Wish.git $HOME/.config/wish else curl https://gitlab.com/ceda_ei/wish/-/archive/master/wish-master.tar -o /tmp/wish.tar mkdir $HOME/.config 2> /dev/null @@ -10,12 +10,11 @@ else mv wish-master/ wish/ mv wish/ $HOME/.config/ fi +cp $HOME/.config/wish/config.default.gie $HOME/.config/wish/config.gie cat >> ~/.bashrc < Date: Tue, 31 Dec 2019 13:32:02 +0530 Subject: [PATCH 7/8] Make WISH_PLUGINS_SOURCE backwards compatible --- wish.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wish.sh b/wish.sh index a139ea4..0f8fbdb 100644 --- a/wish.sh +++ b/wish.sh @@ -27,6 +27,11 @@ function wish_init() { fi done # Source all plugins + # If WISH_CONFIG_FILE is not set, then assume that the user hasn't updated + # to a config file yet. Set WISH_PLUGINS_SOURCE=WISH_PLUGINS. + if [[ ! -v WISH_CONFIG_FILE ]]; then + WISH_PLUGINS_SOURCE=("${WISH_PLUGINS[@]}" "${WISH_RIGHT_PLUGINS[@]}") + fi local plugin local path for plugin in ${WISH_PLUGINS_SOURCE[@]}; do From 9e55279faf8fa7406ee2f04a0818d40abb4b3ad9 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 31 Dec 2019 13:33:55 +0530 Subject: [PATCH 8/8] [Fix] Change wrong variable name in custom_text plugin --- plugins/custom_text.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/custom_text.sh b/plugins/custom_text.sh index dc5ffaf..700ee54 100644 --- a/plugins/custom_text.sh +++ b/plugins/custom_text.sh @@ -10,7 +10,7 @@ function wish_custom_text_set_colors() { WISH_CUSTOM_TEXT_FG=${WISH_CUSTOM_TEXT_FG:-$WISH_DEFAULT_FG} WISH_CUSTOM_TEXT_BG=${WISH_CUSTOM_TEXT_BG:-$WISH_DEFAULT_BG} local default_text='To set custom text here, add text="your text" in your config' - WISH_CUSTOM_TEXT_TEXT=${WISH_CUSTOM_TEXT:-$default_text} + WISH_CUSTOM_TEXT_TEXT=${WISH_CUSTOM_TEXT_TEXT:-$default_text} } function wish_custom_text_main() {