src/pls.nim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
from sequtils import all from pegs import peg, match, replace from os import getenv, fileExists, execShellCmd, `/` from parseopt import getopt, cmdLongOption, cmdShortOption, cmdArgument from strutils import `%`, split, strip, join, startsWith, endsWith, replace, contains from tables import newTable, TableRef, `[]=`, `[]`, hasKey, pairs, len, keys import plspkg/config type ConfigParseError = ref object of ValueError type RuntimeError = ref object of ValueError let USAGE* = """$1 v$2 - $3 (c) 2021 $4 Usage: pls <action> [<thing>] Executes <action> (on <thing>). <thing> can contain a start and/or leading * to perform simple searches. Options: --help, -h Displays this message. --actions, -a[:<query>] Displays all known actions, optionally matching <query>. <query> can contain a start and/or leading * for simple searches. --things, -t[:<query>] Displays all known things, optionally matching <query> <query> can contain a start and/or leading * for simple searches. --full, -f If -a or -t are specified, display all properties for each action or thing. --version, -v Displays the version of the application. """ % [pkgTitle, pkgVersion, pkgDescription, pkgAuthor] # PEG strings used when parsing the configuration file. let PEG_PLACEHOLDER* = peg"'{{' {[a-zA-Z0-9._-]+} '}}'" let PEG_ID* = peg"^[a-z0-9][a-zA-Z0-9._-]+$" let PEG_DEF* = peg"^[a-z0-9][a-zA-Z0-9._-]+ ('+' [a-z0-9][a-zA-Z0-9._-]+)*$" # Hash containing all the actions and things saved in the configuration file. var DATA* = newTable[string, TableRef[string, TableRef[string, string]]]() DATA["actions"] = newTable[string, TableRef[string, string]]() DATA["things"] = newTable[string, TableRef[string, string]]() # Default configuration file, saved if no pls.yml is present on the system. const DEFAULT_CFG = """ actions: # Define actions here things: # Define things here """ # The path to the pls.yml configuration file. var CONFIG_FILE: string if defined(windows): CONFIG_FILE = getenv("USERPROFILE") / "pls.yml" else: CONFIG_FILE = getenv("HOME") / "pls.yml" # Argument/option management var ARGS = newSeq[string]() var OPT_FULL = false var OPT_SHOW = "" var OPT_SHOW_QUERY = "" ### Helper Methods ## proc parseProperty(line: string, index: int): tuple[name: string, value: string] = let parts = line.split(":") if parts.len < 2: raise ConfigParseError(msg: "Line $1 - Invalid property." % $index) result.name = parts[0].strip result.value = parts[1..parts.len-1].join(":").strip proc parseConfig(cfg: string): void = var section = "" var itemId = "" var indent = 0 var count = 0 for l in cfg.lines: count += 1 if l.startsWith(" "): var line = l.strip var obj = "" if line.len == 0: raise ConfigParseError(msg: "Line $1 - Invalid empty line within item." % $count) if line[0] == '#': # comment continue if section == "actions": obj = "action ID" if section == "things": obj = "property name" if l.strip(true, false).len < l.strip(false, true).len-4: raise ConfigParseError(msg: "Line $1 - Invalid $2 indentation, expected 4 spaces." % [$count, obj]) if section == "" or indent == 0: raise ConfigParseError(msg: "Line $1 - Invalid $2 indentation." % [$count, obj]) if itemId == "": raise ConfigParseError(msg: "Line $1 - Invalid $2 indentation (not within an item)." % [$count, obj]) let p = parseProperty(line, count) if (section == "actions" and not p.name.match(PEG_DEF)) or (section == "things" and not p.name.match(PEG_ID)): raise ConfigParseError(msg: "Line $1 - Invalid $2 '$3'" % [$count, obj, p.name]) if DATA[section][itemId].hasKey(p.name): raise ConfigParseError(msg: "Line $1 - Duplicate property '$2'" % [$count, p.name]) DATA[section][itemId][p.name] = p.value indent = 4 continue if l.startsWith(" "): var line = l.strip var obj = "" if line.len == 0: raise ConfigParseError(msg: "Line $1 - Invalid empty line within section." % $count) if line[0] == '#': # comment continue if section == "actions": obj = "action" if section == "things": obj = "thing" if l.strip(true, false).len < l.strip(false, true).len-2: raise ConfigParseError(msg: "Line $1 - Invalid $2 indentationn, expected 2 spaces." % [$count, obj]) if section == "": raise ConfigParseError(msg: "Line $1 - Invalid $2 indentation." % [$count, obj]) if line[line.len-1] != ':' or line == ":": raise ConfigParseError(msg: "Line $1 - Invalid $2 identifier." % [$count, obj]) itemId = line[0..line.len-2] if not itemId.match(PEG_ID): raise ConfigParseError(msg: "Line $1 - Invalid $2 identifier '$3'." % [$count, obj, itemId]) if DATA[section].hasKey(itemId): raise ConfigParseError(msg: "Line $1 - Duplicate item '$2'" % [$count, itemId]) # Start new item DATA[section][itemId] = newTable[string, string]() indent = 2 continue if l == "": itemId = "" continue if l == "actions:": if section == "actions": raise ConfigParseError(msg: "Line $1 - Duplicated 'actions' section." % $count) section = "actions" continue if l == "things:": if section == "things": raise ConfigParseError(msg: "Line $1 - Duplicated 'things' section." % $count) section = "things" continue if l.strip.startsWith("#"): # comment continue else: raise ConfigParseError(msg: "Line $1 - Invalid line." % $count) proc lookupActionDef(action: string, props: seq[string]): string = result = "" if not DATA["actions"].hasKey(action): raise RuntimeError(msg: "Action '$1' not found" % action) var defs = DATA["actions"][action] var score = 0 # Cycle through action definitions for key, val in defs.pairs: var params = key.split("+") # Check if all params are available var match = params.all do (x: string) -> bool: props.contains(x) if match and params.len > score: score = params.len result = val proc resolvePlaceholder(ident, initialThing: string): string = var id = ident var thing = initialThing let parts = id.split(".") if parts.len > 2: raise RuntimeError(msg: "Invalid placeholder '$1'." % id) elif parts.len == 2: thing = parts[0] id = parts[1] if not DATA["things"].hasKey(thing): raise RuntimeError(msg: "Unable to access thing '$1' in placeholder '$2'." % [thing, ident]) if not DATA["things"][thing].hasKey(id): raise RuntimeError(msg: "Unable to access property '$1' in thing '$2' within placeholder '$3'." % [id, thing, ident]) result = DATA["things"][thing][id] result = result.replace(PEG_PLACEHOLDER) do (m: int, n: int, c: openArray[string]) -> string: return resolvePlaceholder(c[0], thing) proc execute*(action, thing: string): int {.discardable.} = if not DATA["things"].hasKey(thing): raise RuntimeError(msg: "Thing '$1' not found. Nothing to do." % thing) let props = DATA["things"][thing] var keys = newSeq[string](0) for key, val in props.pairs: keys.add key var cmd = lookupActionDef(action, keys) if cmd != "": cmd = cmd.replace(PEG_PLACEHOLDER) do (m: int, n: int, c: openArray[string]) -> string: return resolvePlaceholder(c[0], thing) result = execShellCmd cmd proc filterItems*(t: string, query=""): seq[string] = result = newSeq[string]() var endsWith = false var startsWith = false var contains = false if query.endsWith("*"): startsWith = true if query.startsWith("*"): endsWith = true if startsWith and endsWith: contains = true let q = query.replace("*", "") let filter = proc (str: string): bool = if contains: return str.contains(q) elif endsWith: return str.endsWith(q) elif startsWith: return str.startsWith(q) elif q.len == 0: return true else: return str == q for s, props in DATA[t].pairs: if s.filter(): result.add(s) proc show*(t: string, query="", full = false): void = let keys = filterItems(t, query) for s in keys: if OPT_FULL: echo "\n$1:" % s for key, val in DATA[t][s].pairs: echo " $1: $2" % [key, val] else: echo "- $1" % s ### MAIN ### if not CONFIG_FILE.fileExists(): CONFIG_FILE.writeFile(DEFAULT_CFG) try: CONFIG_FILE.parseConfig() except: echo "(!) Unable to parse pls.yml file: $1" % getCurrentExceptionMsg() quit(1) for kind, key, val in getopt(): case kind: of cmdArgument: ARGS.add key of cmdLongOption, cmdShortOption: case key: of "help", "h": echo USAGE quit(0) of "version", "v": echo pkgVersion quit(0) of "full", "f": OPT_FULL = true of "actions", "a": OPT_SHOW = "actions" OPT_SHOW_QUERY = val of "things", "t": OPT_SHOW = "things" OPT_SHOW_QUERY = val else: discard else: discard if ARGS.len < 1: if OPT_SHOW != "": show(OPT_SHOW, OPT_SHOW_QUERY, OPT_FULL) quit(0) else: echo USAGE quit(0) elif ARGS.len < 2: for key in DATA["things"].keys: execute(ARGS[0], key) else: for thing in filterItems("things", ARGS[1]): try: execute(ARGS[0], thing) except: echo "(!) " & getCurrentExceptionMsg() |