Skip to content

Commit 9b6852d

Browse files
committed
Split config finding functions to workspace/finders.py
1 parent 5d8a9d9 commit 9b6852d

File tree

12 files changed

+468
-464
lines changed

12 files changed

+468
-464
lines changed

conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from libtmux.test import namer
1717
from tests.fixtures import utils as test_utils
18-
from tmuxp.cli.utils import get_workspace_dir
18+
from tmuxp.workspace.finders import get_workspace_dir
1919

2020
if t.TYPE_CHECKING:
2121
from libtmux.session import Session

docs/api.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ If you need an internal API stabilized please [file an issue](https://github.com
4545

4646
## CLI
4747

48-
```{eval-rst}
49-
.. automethod:: tmuxp.cli.utils.get_workspace_dir
50-
```
51-
5248
```{eval-rst}
5349
.. automethod:: tmuxp.cli.import_config.get_teamocil_dir
5450
```
@@ -70,15 +66,19 @@ If you need an internal API stabilized please [file an issue](https://github.com
7066
### Finding
7167

7268
```{eval-rst}
73-
.. automethod:: tmuxp.workspace.config.is_workspace_file
69+
.. automethod:: tmuxp.workspace.finders.is_workspace_file
70+
```
71+
72+
```{eval-rst}
73+
.. automethod:: tmuxp.workspace.finders.in_dir
7474
```
7575

7676
```{eval-rst}
77-
.. automethod:: tmuxp.workspace.config.in_dir
77+
.. automethod:: tmuxp.workspace.finders.in_cwd
7878
```
7979

8080
```{eval-rst}
81-
.. automethod:: tmuxp.workspace.config.in_cwd
81+
.. automethod:: tmuxp.workspace.finders.get_workspace_dir
8282
```
8383

8484
### Validation

src/tmuxp/cli/convert.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import typing as t
55

66
from tmuxp.config_reader import ConfigReader
7+
from tmuxp.workspace.finders import find_workspace_file, get_workspace_dir
78

8-
from .utils import find_workspace_file, get_workspace_dir, prompt_yes_no
9+
from .utils import prompt_yes_no
910

1011

1112
def create_convert_subparser(

src/tmuxp/cli/edit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import subprocess
55
import typing as t
66

7-
from .utils import find_workspace_file
7+
from tmuxp.workspace.finders import find_workspace_file
88

99

1010
def create_edit_subparser(

src/tmuxp/cli/freeze.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
from libtmux.server import Server
88
from tmuxp.config_reader import ConfigReader
99
from tmuxp.exc import TmuxpException
10+
from tmuxp.workspace.finders import get_workspace_dir
1011

1112
from .. import util
1213
from ..workspace import freezer
13-
from .utils import get_workspace_dir, prompt, prompt_choices, prompt_yes_no
14+
from .utils import prompt, prompt_choices, prompt_yes_no
1415

1516
if t.TYPE_CHECKING:
1617
from typing_extensions import Literal, TypeAlias, TypeGuard

src/tmuxp/cli/import_config.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55
import typing as t
66

77
from tmuxp.config_reader import ConfigReader
8+
from tmuxp.workspace.finders import find_workspace_file
89

910
from ..workspace import importers
10-
from .utils import (
11-
find_workspace_file,
12-
prompt,
13-
prompt_choices,
14-
prompt_yes_no,
15-
tmuxp_echo,
16-
)
11+
from .utils import prompt, prompt_choices, prompt_yes_no, tmuxp_echo
1712

1813

1914
def get_tmuxinator_dir() -> str:

src/tmuxp/cli/load.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,8 @@
2121
from .. import config_reader, exc, log, util
2222
from ..workspace import config
2323
from ..workspace.builder import WorkspaceBuilder
24-
from .utils import (
25-
find_workspace_file,
26-
get_workspace_dir,
27-
prompt_choices,
28-
prompt_yes_no,
29-
style,
30-
tmuxp_echo,
31-
)
24+
from ..workspace.finders import find_workspace_file, get_workspace_dir
25+
from .utils import prompt_choices, prompt_yes_no, style, tmuxp_echo
3226

3327
if t.TYPE_CHECKING:
3428
from typing_extensions import Literal, NotRequired, TypeAlias, TypedDict

src/tmuxp/cli/ls.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import typing as t
44

55
from tmuxp.workspace.constants import VALID_WORKSPACE_DIR_FILE_EXTENSIONS
6-
7-
from .utils import get_workspace_dir
6+
from tmuxp.workspace.finders import get_workspace_dir
87

98

109
def create_ls_subparser(

src/tmuxp/cli/utils.py

Lines changed: 0 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import logging
2-
import os
32
import re
43
import typing as t
54

6-
from colorama import Fore
7-
8-
from tmuxp.types import StrPath
9-
from tmuxp.workspace.constants import VALID_WORKSPACE_DIR_FILE_EXTENSIONS
10-
115
from .. import log
126

137
logger = logging.getLogger(__name__)
@@ -32,162 +26,6 @@ def tmuxp_echo(
3226
print(message)
3327

3428

35-
def get_workspace_dir() -> str:
36-
"""
37-
Return tmuxp configuration directory.
38-
39-
``TMUXP_CONFIGDIR`` environmental variable has precedence if set. We also
40-
evaluate XDG default directory from XDG_CONFIG_HOME environmental variable
41-
if set or its default. Then the old default ~/.tmuxp is returned for
42-
compatibility.
43-
44-
Returns
45-
-------
46-
str :
47-
absolute path to tmuxp config directory
48-
"""
49-
50-
paths = []
51-
if "TMUXP_CONFIGDIR" in os.environ:
52-
paths.append(os.environ["TMUXP_CONFIGDIR"])
53-
if "XDG_CONFIG_HOME" in os.environ:
54-
paths.append(os.path.join(os.environ["XDG_CONFIG_HOME"], "tmuxp"))
55-
else:
56-
paths.append("~/.config/tmuxp/")
57-
paths.append("~/.tmuxp")
58-
59-
for path in paths:
60-
path = os.path.expanduser(path)
61-
if os.path.isdir(path):
62-
return path
63-
# Return last path as default if none of the previous ones matched
64-
return path
65-
66-
67-
def find_workspace_file(
68-
workspace_file: StrPath,
69-
workspace_dir: t.Optional[StrPath] = None,
70-
) -> str:
71-
"""
72-
Return the real config path or raise an exception.
73-
74-
If config is directory, scan for .tmuxp.{yaml,yml,json} in directory. If
75-
one or more found, it will warn and pick the first.
76-
77-
If config is ".", "./" or None, it will scan current directory.
78-
79-
If config is has no path and only a filename, e.g. "myconfig.yaml" it will
80-
search config dir.
81-
82-
If config has no path and only a name with no extension, e.g. "myconfig",
83-
it will scan for file name with yaml, yml and json. If multiple exist, it
84-
will warn and pick the first.
85-
86-
Parameters
87-
----------
88-
workspace_file : str
89-
workspace file, valid examples:
90-
91-
- a file name, myconfig.yaml
92-
- relative path, ../config.yaml or ../project
93-
- a period, .
94-
"""
95-
if not workspace_dir:
96-
workspace_dir = get_workspace_dir()
97-
path = os.path
98-
exists, join, isabs = path.exists, path.join, path.isabs
99-
dirname, normpath, splitext = path.dirname, path.normpath, path.splitext
100-
cwd = os.getcwd()
101-
is_name = False
102-
file_error = None
103-
104-
workspace_file = os.path.expanduser(workspace_file)
105-
# if purename, resolve to confg dir
106-
if is_pure_name(workspace_file):
107-
is_name = True
108-
elif (
109-
not isabs(workspace_file)
110-
or len(dirname(workspace_file)) > 1
111-
or workspace_file == "."
112-
or workspace_file == ""
113-
or workspace_file == "./"
114-
): # if relative, fill in full path
115-
workspace_file = normpath(join(cwd, workspace_file))
116-
117-
# no extension, scan
118-
if path.isdir(workspace_file) or not splitext(workspace_file)[1]:
119-
if is_name:
120-
candidates = [
121-
x
122-
for x in [
123-
f"{join(workspace_dir, workspace_file)}{ext}"
124-
for ext in VALID_WORKSPACE_DIR_FILE_EXTENSIONS
125-
]
126-
if exists(x)
127-
]
128-
if not len(candidates):
129-
file_error = (
130-
"workspace-file not found in workspace dir (yaml/yml/json) %s "
131-
"for name" % (workspace_dir)
132-
)
133-
else:
134-
candidates = [
135-
x
136-
for x in [
137-
join(workspace_file, ext)
138-
for ext in [".tmuxp.yaml", ".tmuxp.yml", ".tmuxp.json"]
139-
]
140-
if exists(x)
141-
]
142-
143-
if len(candidates) > 1:
144-
tmuxp_echo(
145-
Fore.RED
146-
+ "Multiple .tmuxp.{yml,yaml,json} workspace_files in %s"
147-
% dirname(workspace_file)
148-
+ Fore.RESET
149-
)
150-
tmuxp_echo(
151-
"This is undefined behavior, use only one. "
152-
"Use file names e.g. myproject.json, coolproject.yaml. "
153-
"You can load them by filename."
154-
)
155-
elif not len(candidates):
156-
file_error = "No tmuxp files found in directory"
157-
if len(candidates):
158-
workspace_file = candidates[0]
159-
elif not exists(workspace_file):
160-
file_error = "file not found"
161-
162-
if file_error:
163-
raise FileNotFoundError(file_error, workspace_file)
164-
165-
return workspace_file
166-
167-
168-
def is_pure_name(path: str) -> bool:
169-
"""
170-
Return True if path is a name and not a file path.
171-
172-
Parameters
173-
----------
174-
path : str
175-
Path (can be absolute, relative, etc.)
176-
177-
Returns
178-
-------
179-
bool
180-
True if path is a name of config in config dir, not file path.
181-
"""
182-
return (
183-
not os.path.isabs(path)
184-
and len(os.path.dirname(path)) == 0
185-
and not os.path.splitext(path)[1]
186-
and path != "."
187-
and path != ""
188-
)
189-
190-
19129
def prompt(
19230
name: str,
19331
default: t.Any = None,

0 commit comments

Comments
 (0)