python-base-command 0.1.4__py3-none-any.whl → 0.1.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,8 +15,6 @@ Public API
15
15
  - call_command — programmatic command invocation
16
16
  """
17
17
 
18
- from custom_python_logger import build_logger
19
-
20
18
  from .base import (
21
19
  BaseCommand,
22
20
  CommandError,
@@ -36,8 +34,3 @@ __all__ = [
36
34
  "Runner",
37
35
  "call_command",
38
36
  ]
39
-
40
- build_logger(
41
- project_name="python-base-command",
42
- log_format="%(asctime)s | %(levelname)s | %(message)s",
43
- )
@@ -10,9 +10,10 @@ import os
10
10
  import sys
11
11
  from argparse import Action, ArgumentParser, HelpFormatter
12
12
  from collections.abc import Sequence
13
+ from importlib.metadata import version
13
14
  from typing import Any, TextIO
14
15
 
15
- from custom_python_logger import CustomLoggerAdapter, get_logger
16
+ from custom_python_logger import CustomLoggerAdapter, build_logger, get_logger
16
17
 
17
18
  __all__ = [
18
19
  "BaseCommand",
@@ -21,6 +22,7 @@ __all__ = [
21
22
  "LabelCommand",
22
23
  ]
23
24
 
25
+ from python_base_toolkit.utils.path_utils import get_project_path_by_file
24
26
 
25
27
  # ---------------------------------------------------------------------------
26
28
  # Exceptions
@@ -171,9 +173,23 @@ class BaseCommand:
171
173
  ) -> None:
172
174
  _ = stdout, stderr # API compatibility with call_command(stdout=..., stderr=...)
173
175
  self.logger: CustomLoggerAdapter = get_logger(name=self.__class__.__module__.split(".", maxsplit=1)[0])
176
+ build_logger(
177
+ project_name=self.__class__.__name__,
178
+ log_format="%(asctime)s | %(levelname)s | %(message)s",
179
+ log_file=os.getenv("PYTHON_BASE_COMMAND_LOG_FILE", "true").lower() == "true",
180
+ )
174
181
 
175
182
  # ------------------------------------------------------------------ parser
176
183
 
184
+ def set_project_version(self, project_name: str | None = None) -> None:
185
+ if not project_name:
186
+ try:
187
+ project_path = get_project_path_by_file()
188
+ project_name = project_path.name
189
+ except Exception:
190
+ self.logger.warning("Project name not provided and could not be inferred from file markers.")
191
+ self.version = version(project_name) if project_name else self.version
192
+
177
193
  def create_parser(self, prog_name: str, subcommand: str, **kwargs: Any) -> CommandParser:
178
194
  """Create and return the CommandParser used to parse arguments."""
179
195
  kwargs.setdefault("formatter_class", CommandHelpFormatter)
@@ -262,7 +278,6 @@ class BaseCommand:
262
278
  If handle() returns a string and output_transaction is True,
263
279
  wraps it in BEGIN; / COMMIT;.
264
280
  """
265
- output: str | None = None
266
281
  if output := self.handle(**kwargs):
267
282
  if self.output_transaction:
268
283
  output = f"BEGIN;\n{output}\nCOMMIT;"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-base-command
3
- Version: 0.1.4
3
+ Version: 0.1.5
4
4
  Summary: Django-style BaseCommand framework for standalone Python CLI tools
5
5
  Project-URL: Homepage, https://github.com/aviz92/python-base-command
6
6
  Project-URL: Repository, https://github.com/aviz92/python-base-command
@@ -107,6 +107,25 @@ class Command(BaseCommand):
107
107
  self.logger.info(msg)
108
108
  ```
109
109
 
110
+ ```python
111
+ # commands/greet.py
112
+ from python_base_command import BaseCommand, CommandError
113
+
114
+
115
+ class Command(BaseCommand):
116
+ help = "Greet a user by name"
117
+
118
+ def __init__(self) -> None:
119
+ super().__init__()
120
+ self.set_project_version("python-base-command")
121
+
122
+ def add_arguments(self, parser):
123
+ pass
124
+
125
+ def handle(self, **kwargs):
126
+ pass
127
+ ```
128
+
110
129
  Run from anywhere inside the project:
111
130
 
112
131
  ```bash
@@ -0,0 +1,9 @@
1
+ python_base_command/__init__.py,sha256=sPoDneqhe6JjRMetB3N8Kq8ohsUilTUvzo1QrgmSyy0,967
2
+ python_base_command/base.py,sha256=YPN5DpM6qVfOqOVJX3JwyaEdx0gD6Q2C-lyBNpDhGCM,11450
3
+ python_base_command/registry.py,sha256=WJl5IgjqOWRZuN36od1K-gm_RzDYCwivMkFSHqLbhe0,3883
4
+ python_base_command/runner.py,sha256=rhMUhrJBJJ6XaxbuXuC5DcGqBHVqErqGm5BL9cR0YqQ,5906
5
+ python_base_command/utils.py,sha256=Q0U8uU1YgMJdeP4IpCktsI8dSjqgcF9nUnMTxOvqL0o,1320
6
+ python_base_command-0.1.5.dist-info/METADATA,sha256=k7Frnj-4M5IPdfaHylUduURaGUfC3Y_Tw9vHMMNR52Q,12289
7
+ python_base_command-0.1.5.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
8
+ python_base_command-0.1.5.dist-info/licenses/LICENSE,sha256=cSikHY6SZFsPZSBizCDAJ0-Bjjzxt-JtX6TVbKxwimo,1067
9
+ python_base_command-0.1.5.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.28.0
2
+ Generator: hatchling 1.29.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,9 +0,0 @@
1
- python_base_command/__init__.py,sha256=3szmLuo8nfQ3Mttzcx7-iRUi0U-2PEhQRg_v-daRo7Y,1131
2
- python_base_command/base.py,sha256=NoM-HbsGKMqPzR3mzcpiRvCVWDlgDwj5gqLD6UAHFaY,10668
3
- python_base_command/registry.py,sha256=WJl5IgjqOWRZuN36od1K-gm_RzDYCwivMkFSHqLbhe0,3883
4
- python_base_command/runner.py,sha256=rhMUhrJBJJ6XaxbuXuC5DcGqBHVqErqGm5BL9cR0YqQ,5906
5
- python_base_command/utils.py,sha256=Q0U8uU1YgMJdeP4IpCktsI8dSjqgcF9nUnMTxOvqL0o,1320
6
- python_base_command-0.1.4.dist-info/METADATA,sha256=8RmlhvCDeqEWjky6shVa3hxcbAlqxzXIQ5qkTBvKz6M,11919
7
- python_base_command-0.1.4.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
8
- python_base_command-0.1.4.dist-info/licenses/LICENSE,sha256=cSikHY6SZFsPZSBizCDAJ0-Bjjzxt-JtX6TVbKxwimo,1067
9
- python_base_command-0.1.4.dist-info/RECORD,,