Skip to content

Commit 0617841

Browse files
committed
cli: Use logging library functions instead of print
This lets us remove a bunch of checks for verbose, which cleans up the API and it also lets us configure the logging at one location, such as by sending it to stderr.
1 parent 6655837 commit 0617841

File tree

4 files changed

+80
-87
lines changed

4 files changed

+80
-87
lines changed

cli/cloe_launch/__init__.py

+16-23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from typing import List, Optional
6+
import logging
67
import os
78
import shutil
89
import subprocess
@@ -22,35 +23,32 @@ class Configuration:
2223
profiles_dir = os.path.join(config_dir, "profiles")
2324
runtime_dir = os.path.expanduser("~/.cache/cloe/launcher")
2425

25-
verbose = 0
2626
conf_version = "1"
2727
_conf = {
2828
"version": conf_version,
2929
"shell_path": "/bin/bash",
3030
"conan_path": "conan",
3131
"relay_anonymous_files": True,
32-
"engine": {"pre_arguments": [], "post_arguments": [],},
32+
"engine": {
33+
"pre_arguments": [],
34+
"post_arguments": [],
35+
},
3336
}
3437

3538
all_profiles: List[str] = []
3639
default_profile: Optional[str] = None
3740
current_profile = None
3841

39-
def __init__(self, profile: str = None, verbose: int = 0):
40-
self.verbose = verbose
41-
42+
def __init__(self, profile: str = None):
4243
# Make configuration and runtime directories if needed:
4344
if not os.path.exists(self.config_dir):
44-
if self.verbose > 0:
45-
print("Create configuration directory:", self.config_dir)
45+
logging.info("Create configuration directory:", self.config_dir)
4646
os.makedirs(self.config_dir)
4747
if not os.path.exists(self.profiles_dir):
48-
if self.verbose > 0:
49-
print("Create profile directory:", self.profiles_dir)
48+
logging.info("Create profile directory:", self.profiles_dir)
5049
os.makedirs(self.profiles_dir)
5150
if not os.path.exists(self.runtime_dir):
52-
if self.verbose > 0:
53-
print("Create runtime directory:", self.runtime_dir)
51+
logging.info("Create runtime directory:", self.runtime_dir)
5452
os.makedirs(self.runtime_dir)
5553

5654
# Load configuration file:
@@ -95,17 +93,15 @@ def set_default(self, profile: str) -> None:
9593
raise ConfigurationError("profile {} does not exist".format(profile))
9694
self.default_profile = profile
9795
self._conf["default_profile"] = profile
98-
if self.verbose > 0:
99-
print(
100-
"Write configuration to {}:\n {}".format(self.config_file, self._conf)
101-
)
96+
logging.info(
97+
"Write configuration to {}:\n {}".format(self.config_file, self._conf)
98+
)
10299
with open(self.config_file, "w") as file:
103100
toml.dump(self._conf, file)
104101

105102
def read(self, profile: str) -> str:
106103
"""Read the specified profile."""
107-
if self.verbose > 0:
108-
print("Open:", self.profile_path(profile))
104+
logging.info("Open:", self.profile_path(profile))
109105
with open(self.profile_path(profile), "r") as file:
110106
return file.read()
111107

@@ -117,8 +113,7 @@ def edit(self, profile: str, create: bool = False) -> None:
117113
if not create and not os.path.exists(self.profile_path(profile)):
118114
raise ConfigurationError("profile {} does not exist".format(profile))
119115
cmd = [editor, self.profile_path(profile)]
120-
if self.verbose > 0:
121-
print("Exec:", " ".join(cmd))
116+
logging.info("Exec:", " ".join(cmd))
122117
subprocess.call(cmd)
123118

124119
def add(self, profile: str, file: str, force: bool = False) -> None:
@@ -127,8 +122,7 @@ def add(self, profile: str, file: str, force: bool = False) -> None:
127122
raise ConfigurationError(
128123
"cannot overwrite profile {} unless forced".format(profile)
129124
)
130-
if self.verbose > 1:
131-
print("Copy: {} -> {}".format(file, self.profile_path(profile)))
125+
logging.debug("Copy: {} -> {}".format(file, self.profile_path(profile)))
132126
shutil.copyfile(file, self.profile_path(profile))
133127
if profile not in self.all_profiles:
134128
self.all_profiles.append(profile)
@@ -137,8 +131,7 @@ def remove(self, profile: str) -> None:
137131
"""Remove the profile, if it exists."""
138132
file = os.path.join(self.profiles_dir, profile)
139133
if os.path.exists(file):
140-
if self.verbose > 0:
141-
print("Remove:", file)
134+
logging.info("Remove:", file)
142135
os.remove(file)
143136
if self.default_profile == profile:
144137
self.set_default(None)

cli/cloe_launch/__main__.py

+21-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import os
2222
import sys
2323
import click
24+
import logging
2425

2526
from cloe_launch.exec import Engine
2627
from cloe_launch import Configuration, ConfigurationError
@@ -38,11 +39,19 @@
3839
def main(ctx, verbose: int):
3940
"""Launch cloe-engine with profiles and manage launch profiles."""
4041

42+
if verbose == 0:
43+
level = logging.WARNING
44+
elif verbose == 1:
45+
level = logging.INFO
46+
else:
47+
level = logging.DEBUG
48+
logging.basicConfig(format="%(message)s", stream=sys.stderr, level=level)
49+
4150
class Options:
4251
def __init__(self, verbose):
4352
self.verbose = verbose
4453

45-
ctx.obj = Options(verbose=verbose)
54+
ctx.obj = Options(verbose > 0)
4655

4756

4857
# Common options among engine and profile commands:
@@ -130,7 +139,7 @@ def cli_exec(
130139
ENGINE_ARGS are passed on to cloe-engine.
131140
"""
132141
deny_profile_and_path(profile, profile_path)
133-
conf = Configuration(profile, opt.verbose)
142+
conf = Configuration(profile)
134143
engine = Engine(conf, conanfile=profile_path)
135144
engine.preserve_env = preserve_env
136145
engine.conan_options = conan_option
@@ -178,7 +187,7 @@ def cli_shell(
178187
) -> None:
179188
"""Launch shell with the correct environment from a profile."""
180189
deny_profile_and_path(profile, profile_path)
181-
conf = Configuration(profile, opt.verbose)
190+
conf = Configuration(profile)
182191
engine = Engine(conf, conanfile=profile_path)
183192
engine.preserve_env = preserve_env
184193
engine.conan_options = conan_option
@@ -196,7 +205,7 @@ def cli_shell(
196205
def cli_clean(opt, profile: str, profile_path: str) -> None:
197206
"""Clean launcher profile cache."""
198207
deny_profile_and_path(profile, profile_path)
199-
conf = Configuration(profile, opt.verbose)
208+
conf = Configuration(profile)
200209
engine = Engine(conf, conanfile=profile_path)
201210
engine.clean()
202211

@@ -215,7 +224,7 @@ def cli_profile():
215224
@click.pass_obj
216225
def cli_profile_show(opt, profile: str) -> None:
217226
"""Show a profile configuration."""
218-
conf = Configuration(profile, opt.verbose)
227+
conf = Configuration(profile)
219228
if conf.current_profile is None:
220229
raise ConfigurationError("no default profile is configured")
221230
data = conf.read(conf.current_profile)
@@ -228,9 +237,9 @@ def cli_profile_show(opt, profile: str) -> None:
228237
@click.pass_obj
229238
def cli_profile_list(opt) -> None:
230239
"""List all available profiles."""
231-
conf = Configuration(verbose=opt.verbose)
240+
conf = Configuration()
232241
for profile in conf.all_profiles:
233-
if opt.verbose > 0:
242+
if opt.verbose:
234243
if profile == conf.default_profile:
235244
print("*", profile)
236245
else:
@@ -253,7 +262,7 @@ def cli_profile_add(
253262
opt, profile: str, conanfile: str, force: bool = False, default: bool = False
254263
) -> None:
255264
"""Add a new profile."""
256-
conf = Configuration(verbose=opt.verbose)
265+
conf = Configuration()
257266
conf.add(profile, conanfile, force=force)
258267
if default:
259268
conf.set_default(profile)
@@ -279,7 +288,7 @@ def cli_profile_add(
279288
@click.pass_obj
280289
def cli_profile_edit(opt, profile: str, editor: str, create: bool) -> None:
281290
"""Edit a profile."""
282-
conf = Configuration(profile, verbose=opt.verbose)
291+
conf = Configuration(profile)
283292
if conf.current_profile is None:
284293
raise ConfigurationError("no default profile is configured")
285294
conf.edit(conf.current_profile, create)
@@ -292,7 +301,7 @@ def cli_profile_edit(opt, profile: str, editor: str, create: bool) -> None:
292301
@click.pass_obj
293302
def cli_profile_remove(opt, profile: str) -> None:
294303
"""Remove a profile."""
295-
conf = Configuration(verbose=opt.verbose)
304+
conf = Configuration()
296305
conf.remove(profile)
297306

298307

@@ -303,11 +312,11 @@ def cli_profile_remove(opt, profile: str) -> None:
303312
@click.pass_obj
304313
def cli_profile_default(opt, profile: str) -> None:
305314
"""Show or set the default profile."""
306-
conf = Configuration(profile, verbose=opt.verbose)
315+
conf = Configuration(profile)
307316
if profile is None:
308317
if conf.default_profile is not None:
309318
print(conf.default_profile)
310-
elif opt.verbose > 0:
319+
elif opt.verbose:
311320
print("Note: no default profile is configured")
312321
else:
313322
# Set default to provided value, which must exist already

0 commit comments

Comments
 (0)