docopt-completion 0.1.0__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: docopt-completion
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Summary: A completion for the docopt language
5
5
  License: MIT
6
6
  Author: Anders Ingemann
@@ -15,12 +15,12 @@ log = logging.getLogger(root_name)
15
15
 
16
16
  __doc__: str = T.cast(str, pkg_doc) + """
17
17
  Usage:
18
- docopt-completion intermediate [FILE]
19
- docopt-completion bash [FILE]
20
- docopt-completion zsh [FILE]
21
- docopt-completion register [--dir=DIR] (bash|zsh) COMMAND
22
- docopt-completion -h
23
- docopt-completion --version
18
+ docopt.completion intermediate [FILE]
19
+ docopt.completion bash [FILE]
20
+ docopt.completion zsh [FILE]
21
+ docopt.completion register [--dir=DIR] (bash|zsh) COMMAND
22
+ docopt.completion -h
23
+ docopt.completion --version
24
24
 
25
25
  Reads a docopt help text on stdin, except for register. FILE is an intermediate
26
26
  with annotations.
@@ -33,7 +33,9 @@ Commands:
33
33
  in $fpath or to be sourced after compinit
34
34
  register Write the completion for COMMAND from its --help, which
35
35
  regenerates itself when COMMAND changes. Without --dir the
36
- script goes where bash-completion loads it from, or to
36
+ script goes to the shell's directory in
37
+ ${XDG_CONFIG_HOME:-~/.config}/docopt.completion/config.yaml,
38
+ else where bash-completion loads it from, or to
37
39
  ${XDG_DATA_HOME:-~/.local/share}/zsh/site-functions for zsh
38
40
 
39
41
  Options:
@@ -52,7 +54,7 @@ def docopt_completion(params: Params):
52
54
  directory = params['--dir'] or register.default_dir(shell, os.environ)
53
55
  path = register.register(shell, T.cast(str, params['COMMAND']), directory)
54
56
  log.info(f'Wrote {path}')
55
- if shell == 'zsh':
57
+ if shell == 'zsh' and params['--dir'] is None and register.configured_dir(shell, os.environ) is None:
56
58
  log.info(f'Add {directory} to $fpath before compinit if it isn\'t there already')
57
59
  return
58
60
  text = sys.stdin.read()
@@ -1,4 +1,4 @@
1
- # bash completion for "PROG", generated by docopt-completion
1
+ # bash completion for "PROG", generated by docopt.completion
2
2
 
3
3
  FN() {
4
4
  "REFRESH"
@@ -3,9 +3,11 @@ Completion for a docopt program that has none, generated from its `--help`. The
3
3
  itself when the program's resolved path or modification time changes.
4
4
  """
5
5
  import os
6
+ import re
6
7
  import shutil
7
8
  import subprocess
8
9
  import typing as T
10
+ import yaml
9
11
  import docopt_parser as P
10
12
  from docopt_completion import intermediate, tables, bash, zsh
11
13
 
@@ -14,8 +16,25 @@ class NotDocoptError(P.DocoptError):
14
16
  pass
15
17
 
16
18
 
19
+ def configured_dir(shell: str, env: T.Mapping[str, str]) -> "str | None":
20
+ """The shell's directory in $XDG_CONFIG_HOME/docopt.completion/config.yaml"""
21
+ config_home = env.get('XDG_CONFIG_HOME') or os.path.join(env['HOME'], '.config')
22
+ config_path = os.path.join(config_home, 'docopt.completion', 'config.yaml')
23
+ if os.path.exists(config_path):
24
+ with open(config_path) as handle:
25
+ config = T.cast(T.Dict[str, T.Dict[str, str]], yaml.safe_load(handle) or {})
26
+ if (configured := config.get(shell, {}).get('directory')) is not None:
27
+ # Expanded like the shell would: ~ and $VAR or ${VAR}
28
+ return re.sub(r'^~(?=/|$)', env['HOME'], re.sub(
29
+ r'\$(\w+)|\$\{(\w+)\}', lambda m: env.get(m.group(1) or m.group(2), ''), configured
30
+ ))
31
+ return None
32
+
33
+
17
34
  def default_dir(shell: str, env: T.Mapping[str, str]) -> str:
18
- """Where bash-completion lazy-loads user completions from; zsh has no such place"""
35
+ """The configured directory, else where bash-completion lazy-loads user completions from"""
36
+ if (configured := configured_dir(shell, env)) is not None:
37
+ return configured
19
38
  data = env.get('XDG_DATA_HOME') or os.path.join(env['HOME'], '.local', 'share')
20
39
  if shell == 'bash':
21
40
  user_dir = env.get('BASH_COMPLETION_USER_DIR', '').split(':')[0] or os.path.join(data, 'bash-completion')
@@ -38,9 +57,10 @@ def register(shell: str, command: str, directory: str) -> str:
38
57
  if node['type'] == 'argument' and intermediate.placeholder_word(key) in ('option', 'options'):
39
58
  raise NotDocoptError(f'The help of {command} looks GNU style ({key}), not docopt')
40
59
  compiled = tables.compile(result)
41
- name = os.path.basename(command)
60
+ # A path names a file, the usage names the command that runs it
61
+ name = result['prog'] if os.sep in command else command
42
62
  path = os.path.join(directory, f'{name}.bash' if shell == 'bash' else f'_{name}')
43
- refresh = (binary, path, ['docopt-completion', 'register', f'--dir={directory}', shell, command])
63
+ refresh = (binary, path, ['docopt.completion', 'register', f'--dir={directory}', shell, command])
44
64
  code = (bash if shell == 'bash' else zsh).generate(name, compiled, refresh)
45
65
  os.makedirs(directory, exist_ok=True)
46
66
  with open(path, 'w') as handle:
@@ -1,5 +1,5 @@
1
1
  #compdef "PROG"
2
- # zsh completion for "PROG", generated by docopt-completion
2
+ # zsh completion for "PROG", generated by docopt.completion
3
3
 
4
4
  FN() {
5
5
  "REFRESH"
@@ -1,7 +1,7 @@
1
1
  [tool.poetry]
2
2
  name = "docopt-completion"
3
3
  description = "A completion for the docopt language"
4
- version = "0.1.0"
4
+ version = "0.1.2"
5
5
  authors = ["Anders Ingemann <anders@ingemann.de>"]
6
6
  license = "MIT"
7
7
  readme = "docs/README.pypi.md"
@@ -37,7 +37,7 @@ pyright = "^1.1.238"
37
37
  types-docopt = "^0.6.11"
38
38
 
39
39
  [tool.poetry.scripts]
40
- docopt-completion = "docopt_completion.__main__:main"
40
+ "docopt.completion" = "docopt_completion.__main__:main"
41
41
 
42
42
  [tool.pyright]
43
43
  include = ["docopt_completion", "tests"]