Skip to content

Commit 9c93ed3

Browse files
rename program to package (#60)
* rename program to package * test commit * Rename last instances of program to package. * Deprecate program properly. --------- Co-authored-by: JoeZiminski <joseph.j.ziminski@gmail.com>
1 parent d1df200 commit 9c93ed3

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

fancylog/fancylog.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import subprocess
88
import sys
9+
import warnings
910
from datetime import datetime
1011
from importlib.util import find_spec
1112

@@ -20,7 +21,7 @@
2021

2122
def start_logging(
2223
output_dir,
23-
package,
24+
package=None,
2425
variables=None,
2526
verbose=True,
2627
file_log_level="DEBUG",
@@ -37,6 +38,7 @@ def start_logging(
3738
log_to_console=True,
3839
timestamp=True,
3940
logger_name=None,
41+
program=None,
4042
):
4143
"""Prepare the log file, and then begin logging.
4244
@@ -81,6 +83,8 @@ def start_logging(
8183
logger_name
8284
If None, logger uses default logger; otherwise, logger
8385
name is set to `logger_name`.
86+
program
87+
Deprecated alias name for `package`.
8488
8589
Returns
8690
-------
@@ -91,6 +95,21 @@ def start_logging(
9195
output_dir = str(output_dir)
9296
print_log_level = "DEBUG" if verbose else "INFO"
9397

98+
if program:
99+
warnings.warn(
100+
"`program` is deprecated since 0.6.0 and will be "
101+
"removed in 0.7.0; use `package` instead.",
102+
DeprecationWarning,
103+
stacklevel=2,
104+
)
105+
package = program
106+
107+
if not package:
108+
raise ValueError(
109+
"`package` or `program` (deprecated)` must be "
110+
"passed to `start_logging()`."
111+
)
112+
94113
if log_to_file:
95114
if filename is None:
96115
filename = package.__name__
@@ -136,7 +155,7 @@ class LoggingHeader:
136155
def __init__(
137156
self,
138157
file,
139-
program,
158+
package,
140159
variable_objects,
141160
output_dir,
142161
write_header=True,
@@ -151,13 +170,13 @@ def __init__(
151170
152171
See start_logging() for parameters.
153172
"""
154-
self.program = program
173+
self.package = package
155174

156175
with open(file, "w", encoding="utf-8") as self.file:
157176
if write_header:
158177
self.write_log_header(output_dir, log_header)
159178
if write_git:
160-
self.write_git_info(self.program.__name__)
179+
self.write_git_info(self.package.__name__)
161180
if write_cli_args:
162181
self.write_command_line_arguments()
163182
if write_python:
@@ -167,12 +186,12 @@ def __init__(
167186
if write_variables and variable_objects:
168187
self.write_variables(variable_objects)
169188

170-
def write_git_info(self, program_name, header="GIT INFO"):
189+
def write_git_info(self, package_name, header="GIT INFO"):
171190
"""Write information about the git repository state.
172191
173192
Parameters
174193
----------
175-
program_name
194+
package_name
176195
The name of the installed package, to
177196
locate and inspect its Git repository.
178197
header
@@ -181,11 +200,11 @@ def write_git_info(self, program_name, header="GIT INFO"):
181200
"""
182201
self.write_separated_section_header(header)
183202
try:
184-
program_path = find_spec(program_name).submodule_search_locations[
203+
package_path = find_spec(package_name).submodule_search_locations[
185204
0
186205
]
187-
program_path = os.path.split(program_path)[0]
188-
git_info = get_git_info(program_path)
206+
package_path = os.path.split(package_path)[0]
207+
git_info = get_git_info(package_path)
189208

190209
self.file.write(f"Commit hash: {git_info.head.hash} \n")
191210
self.file.write(f"Commit message: {git_info.head.message} \n")
@@ -402,7 +421,7 @@ def write_log_header(self, output_dir, log_header):
402421
self.file.write("Output directory: " + output_dir + "\n")
403422
self.file.write("Current directory: " + os.getcwd() + "\n")
404423
with contextlib.suppress(AttributeError):
405-
self.file.write(f"Version: {self.program.__version__}")
424+
self.file.write(f"Version: {self.package.__version__}")
406425

407426
def write_separated_section_header(
408427
self,

tests/tests/test_deprecations.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
3+
import fancylog
4+
5+
6+
def test_no_package_passed(tmp_path):
7+
"""
8+
Test error is wrong if neither `package` or
9+
`program` (deprecated) is raised.
10+
"""
11+
with pytest.raises(ValueError) as e:
12+
fancylog.start_logging(tmp_path)
13+
14+
assert "`package` or `program`" in str(e.value)
15+
16+
17+
def test_program_warning_shown(tmp_path):
18+
"""
19+
Test depreciation warning is shown for `program`.
20+
"""
21+
with pytest.warns(DeprecationWarning) as w:
22+
fancylog.start_logging(tmp_path, program=fancylog)
23+
assert (
24+
"`program` is deprecated since 0.6.0 and will be removed in 0.7.0;"
25+
in str(w[0].message)
26+
)

0 commit comments

Comments
 (0)