Skip to content

Commit e32b5f3

Browse files
committed
refactor!: Move kaptan to ConfigReader
1 parent e36f341 commit e32b5f3

17 files changed

+397
-335
lines changed

docs/about.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ from JSON and YAML.
9797
`$ tmuxp -f<config-file>` for config file.
9898

9999
[attempt at 1.7 test]: https://travis-ci.org/tmux-python/tmuxp/jobs/12348263
100-
[kaptan]: https://github.com/emre/kaptan
101100
[mit-licensed]: http://opensource.org/licenses/MIT
102101
[tmuxinator]: https://github.com/aziz/tmuxinator
103102
[teamocil]: https://github.com/remiprev/teamocil

docs/glossary.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ tmux(1)
1313
The tmux binary. Used internally to distinguish tmuxp is only a
1414
layer on top of tmux.
1515
16-
kaptan
17-
configuration management library, see [kaptan on github](https://github.com/emre/kaptan).
16+
ConfigReader
17+
configuration management class.
1818
1919
Server
2020
Tmux runs in the background of your system as a process.

src/tmuxp/cli/convert.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
2+
import pathlib
23

34
import click
4-
import kaptan
5+
6+
from tmuxp.config_reader import ConfigReader
57

68
from .utils import ConfigPath
79

@@ -25,20 +27,18 @@ def command_convert(confirmed, config):
2527
f"Unknown filetype: {ext} (valid: [.json, .yaml, .yml])"
2628
)
2729

28-
configparser = kaptan.Kaptan()
29-
configparser.import_config(config)
30-
newfile = config.replace(ext, ".%s" % to_filetype)
30+
configparser = ConfigReader.from_file(pathlib.Path(config))
31+
newfile = config.replace(ext, f".{to_filetype}")
3132

32-
export_kwargs = {"default_flow_style": False} if to_filetype == "yaml" else {}
33-
newconfig = configparser.export(to_filetype, indent=2, **export_kwargs)
33+
new_config = configparser.dump(format=to_filetype)
3434

3535
if not confirmed:
3636
if click.confirm(f"convert to <{config}> to {to_filetype}?"):
37-
if click.confirm("Save config to %s?" % newfile):
37+
if click.confirm(f"Save config to {newfile}?"):
3838
confirmed = True
3939

4040
if confirmed:
4141
buf = open(newfile, "w")
42-
buf.write(newconfig)
42+
buf.write(new_config)
4343
buf.close()
44-
print("New config saved to <%s>." % newfile)
44+
print(f"New config saved to <{newfile}>.")

src/tmuxp/cli/freeze.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import sys
33

44
import click
5-
import kaptan
65

76
from libtmux.server import Server
7+
from tmuxp.config_reader import ConfigReader
88
from tmuxp.exc import TmuxpException
99

1010
from .. import config, util
@@ -71,9 +71,8 @@ def command_freeze(
7171
return
7272

7373
sconf = freeze(session)
74-
configparser = kaptan.Kaptan()
7574
newconfig = config.inline(sconf)
76-
configparser.import_config(newconfig)
75+
configparser = ConfigReader(newconfig)
7776

7877
if not quiet:
7978
print(
@@ -126,11 +125,11 @@ def command_freeze(
126125
)
127126

128127
if config_format == "yaml":
129-
newconfig = configparser.export(
130-
"yaml", indent=2, default_flow_style=False, safe=True
128+
newconfig = configparser.dump(
129+
format="yaml", indent=2, default_flow_style=False, safe=True
131130
)
132131
elif config_format == "json":
133-
newconfig = configparser.export("json", indent=2)
132+
newconfig = configparser.dump(format="json", indent=2)
134133

135134
if yes or click.confirm("Save to %s?" % dest):
136135
destdir = os.path.dirname(dest)

src/tmuxp/cli/import_config.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
2+
import pathlib
23
import sys
34

45
import click
5-
import kaptan
6+
7+
from tmuxp.config_reader import ConfigReader
68

79
from .. import config
810
from .utils import ConfigPath, _validate_choices, get_abs_path, tmuxp_echo
@@ -58,25 +60,22 @@ def command_import():
5860

5961

6062
def import_config(configfile, importfunc):
61-
configparser = kaptan.Kaptan(handler="yaml")
62-
63-
configparser.import_config(configfile)
64-
newconfig = importfunc(configparser.get())
65-
configparser.import_config(newconfig)
63+
existing_config = ConfigReader._from_file(pathlib.Path(configfile))
64+
new_config = ConfigReader(importfunc(existing_config))
6665

6766
config_format = click.prompt(
6867
"Convert to", value_proc=_validate_choices(["yaml", "json"]), default="yaml"
6968
)
7069

7170
if config_format == "yaml":
72-
newconfig = configparser.export("yaml", indent=2, default_flow_style=False)
71+
new_config = new_config.dump("yaml", indent=2, default_flow_style=False)
7372
elif config_format == "json":
74-
newconfig = configparser.export("json", indent=2)
73+
new_config = new_config.dump("json", indent=2)
7574
else:
7675
sys.exit("Unknown config format.")
7776

7877
tmuxp_echo(
79-
newconfig + "---------------------------------------------------------------"
78+
new_config + "---------------------------------------------------------------"
8079
"\n"
8180
"Configuration import does its best to convert files.\n"
8281
)
@@ -94,7 +93,7 @@ def import_config(configfile, importfunc):
9493
dest = dest_path
9594

9695
buf = open(dest, "w")
97-
buf.write(newconfig)
96+
buf.write(new_config)
9897
buf.close()
9998

10099
tmuxp_echo("Saved to %s." % dest)
@@ -138,12 +137,11 @@ def command_convert(confirmed, config):
138137
f"Unknown filetype: {ext} (valid: [.json, .yaml, .yml])"
139138
)
140139

141-
configparser = kaptan.Kaptan()
142-
configparser.import_config(config)
140+
configparser = ConfigReader.from_file(config)
143141
newfile = config.replace(ext, ".%s" % to_filetype)
144142

145143
export_kwargs = {"default_flow_style": False} if to_filetype == "yaml" else {}
146-
newconfig = configparser.export(to_filetype, indent=2, **export_kwargs)
144+
new_config = configparser.dump(format=to_filetype, indent=2, **export_kwargs)
147145

148146
if not confirmed:
149147
if click.confirm(f"convert to <{config}> to {to_filetype}?"):
@@ -152,7 +150,7 @@ def command_convert(confirmed, config):
152150

153151
if confirmed:
154152
buf = open(newfile, "w")
155-
buf.write(newconfig)
153+
buf.write(new_config)
156154
buf.close()
157155
print("New config saved to <%s>." % newfile)
158156

src/tmuxp/cli/load.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
from typing import List
1414

1515
import click
16-
import kaptan
1716

1817
from libtmux.common import has_gte_version
1918
from libtmux.server import Server
19+
from tmuxp import config_reader
2020

2121
from .. import config, exc, log, util
2222
from ..workspacebuilder import WorkspaceBuilder
@@ -266,7 +266,7 @@ def load_workspace(
266266
Notes
267267
-----
268268
269-
tmuxp will check and load a configuration file. The file will use kaptan
269+
tmuxp will check and load a configuration file. The file will use ConfigReader
270270
to load a JSON/YAML into a :py:obj:`dict`. Then :func:`config.expand` and
271271
:func:`config.trickle` will be used to expand any shorthands, template
272272
variables, or file paths relative to where the config/script is executed
@@ -333,18 +333,18 @@ def load_workspace(
333333
Accessed April 8th, 2018.
334334
"""
335335
# get the canonical path, eliminating any symlinks
336-
config_file = os.path.realpath(config_file)
336+
if isinstance(config_file, str):
337+
config_file = pathlib.Path(config_file)
337338

338339
tmuxp_echo(
339340
click.style("[Loading] ", fg="green")
340-
+ click.style(config_file, fg="blue", bold=True)
341+
+ click.style(str(config_file), fg="blue", bold=True)
341342
)
342343

343-
# kaptan allows us to open a yaml or json file as a dict
344-
sconfig = kaptan.Kaptan()
345-
sconfig = sconfig.import_config(config_file).get()
344+
# ConfigReader allows us to open a yaml or json file as a dict
345+
raw_config = config_reader.ConfigReader._from_file(config_file)
346346
# shapes configurations relative to config / profile file location
347-
sconfig = config.expand(sconfig, os.path.dirname(config_file))
347+
sconfig = config.expand(raw_config, cwd=os.path.dirname(config_file))
348348
# Overwrite session name
349349
if new_session_name:
350350
sconfig["session_name"] = new_session_name

src/tmuxp/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def expand(session_config, cwd=None, parent=None):
225225
226226
'shell_command': 'htop'
227227
228-
Kaptan will load JSON/YAML files into python dicts for you.
228+
ConfigReader will load JSON/YAML files into python dicts for you.
229229
230230
Parameters
231231
----------

0 commit comments

Comments
 (0)