docopt-completion 0.1.1__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.1
3
+ Version: 0.1.2
4
4
  Summary: A completion for the docopt language
5
5
  License: MIT
6
6
  Author: Anders Ingemann
@@ -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()
@@ -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')
@@ -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.1"
4
+ version = "0.1.2"
5
5
  authors = ["Anders Ingemann <anders@ingemann.de>"]
6
6
  license = "MIT"
7
7
  readme = "docs/README.pypi.md"