Skip to content

Commit c13d39e

Browse files
authored
feat: add log_show_mode parameter to control log display (#3)
* feat: add `log_show_mode` parameter to control log display * refactor: refactor test cases * chore: update the ruff configuration file * style: fix lint and format
1 parent 9ffb584 commit c13d39e

File tree

4 files changed

+369
-276
lines changed

4 files changed

+369
-276
lines changed

py_node_manager/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from .logger import get_logger
22
from .manager import NodeManager
33

4-
54
__version__ = '0.1.1'
65

76

py_node_manager/manager.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import tarfile
66
import urllib.request
77
import zipfile
8-
from typing import Dict, Optional, Tuple
9-
from .logger import get_logger
8+
from typing import Dict, Literal, Optional, Tuple
109

10+
from .logger import get_logger
1111

1212
logger = get_logger(logging.getLogger(__name__))
1313

@@ -17,7 +17,13 @@ class NodeManager:
1717
Node.js manager class
1818
"""
1919

20-
def __init__(self, download_node: bool, node_version: str, is_cli: bool = False):
20+
def __init__(
21+
self,
22+
download_node: bool,
23+
node_version: str,
24+
is_cli: bool = False,
25+
log_show_mode: Literal['all', 'slim', 'hide'] = 'all',
26+
):
2127
"""
2228
Node.js manager class
2329
@@ -29,6 +35,7 @@ def __init__(self, download_node: bool, node_version: str, is_cli: bool = False)
2935
self.download_node = download_node
3036
self.node_version = node_version
3137
self.is_cli = is_cli
38+
self.log_show_mode = log_show_mode
3239
self.node_path = self._node_path()
3340
self.node_env = self._node_env()
3441
self.npm_path = self._npm_path()
@@ -86,7 +93,7 @@ def download_nodejs(self) -> str:
8693

8794
# Create directory for downloaded Node.js within the package directory
8895
# Use the package directory instead of current working directory
89-
# Get the directory of this utils.py file
96+
# Get the directory of this file
9097
package_dir = os.path.dirname(os.path.abspath(__file__))
9198
node_dir_path = os.path.join(package_dir, '.nodejs_cache')
9299
if not os.path.exists(node_dir_path):
@@ -100,18 +107,20 @@ def download_nodejs(self) -> str:
100107

101108
# If Node.js already exists, return the path without downloading
102109
if os.path.exists(node_executable):
103-
logger.info(f'📦 Using cached Node.js from {node_executable}')
110+
if self.log_show_mode in {'all', 'slim'}:
111+
logger.info(f'📦 Using cached Node.js from {node_executable}')
104112
return node_executable
105113

106114
# Download Node.js
107115
node_archive = os.path.join(node_dir_path, os.path.basename(node_url))
108-
logger.info('🌐 Node.js not found in PATH. Downloading Node.js...')
109-
if self.is_cli:
116+
if self.log_show_mode in {'all', 'slim'}:
117+
logger.info('🌐 Node.js not found in PATH. Downloading Node.js...')
118+
if self.log_show_mode == 'all' and self.is_cli:
110119
logger.info(f'📥 Downloading Node.js from {node_url}...')
111120
urllib.request.urlretrieve(node_url, node_archive)
112121

113122
# Extract Node.js
114-
if self.is_cli:
123+
if self.log_show_mode == 'all' and self.is_cli:
115124
logger.info('🔧 Extracting Node.js...')
116125
if node_archive.endswith('.tar.gz'):
117126
with tarfile.open(node_archive, 'r:gz') as tar:
@@ -130,7 +139,8 @@ def download_nodejs(self) -> str:
130139
if system != 'windows':
131140
os.chmod(node_executable, 0o755)
132141

133-
logger.info(f'✅ Node.js downloaded and extracted to {node_executable}')
142+
if self.log_show_mode in {'all', 'slim'}:
143+
logger.info(f'✅ Node.js downloaded and extracted to {node_executable}')
134144
return node_executable
135145

136146
def check_or_download_nodejs(self) -> Optional[str]:
@@ -143,7 +153,8 @@ def check_or_download_nodejs(self) -> Optional[str]:
143153
# First check if Node.js is available in PATH
144154
is_available, version = self.check_nodejs_available()
145155
if is_available:
146-
logger.info(f'💻 Using System Default Node.js {version}')
156+
if self.log_show_mode in {'all', 'slim'}:
157+
logger.info(f'💻 Using System Default Node.js {version}')
147158

148159
return None # Use system Node.js
149160

ruff.toml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1+
line-length = 120
2+
show-fixes = true
13
target-version = "py38"
24

3-
line-length = 120
5+
[lint]
6+
select = [
7+
"I", # isort
8+
"E", # pycodestyle errors
9+
"W", # pycodestyle warnings
10+
"F", # pyflakes
11+
]
12+
ignore = [
13+
"E501", # line too long
14+
"W191", # indentation contains tabs
15+
]
416

517
[format]
18+
docstring-code-format = true
619
quote-style = "single"

0 commit comments

Comments
 (0)