-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: Revise Logger
portability through modules
#144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jafranc
wants to merge
6
commits into
main
Choose a base branch
from
jafranc/refact/decorators
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−40
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash -e | ||
|
||
function run_precommit_checks | ||
{ | ||
echo "running mypy" | ||
python -m mypy --config-file ./.mypy.ini --check-untyped-defs $1 | ||
echo "running ruff" | ||
python -m ruff check --unsafe-fixes --config .ruff.toml $1 | ||
echo "running yapf" | ||
python -m yapf -r -i --style .style.yapf $1 | ||
|
||
return 0 | ||
} | ||
|
||
|
||
source ${ENV_PYTHON}/bin/activate | ||
|
||
#sphinx-build -b html docs/ docs/_build -W | ||
for file in $(git diff --name-only --cached | grep -e "modified\|added" | grep py) | ||
do | ||
run_precommit_checks $file | ||
done | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import logging | ||
from geos.utils.Logger import Logger, getLogger | ||
from functools import wraps | ||
from typing import Type, TypeVar, Callable, Protocol, Any | ||
|
||
__doc__ = """ | ||
Group of decorators and Protocols that will be used in filters to wrap behaviors without code replication | ||
|
||
""" | ||
|
||
|
||
class HasLogger( Protocol ): | ||
"""Protocol for classes that have logging support.""" | ||
|
||
logger: Logger | ||
|
||
def setLoggerHandler( self, handler: logging.Handler ) -> None: | ||
"""Set a specific handler for the filter logger. | ||
|
||
In this filter 4 log levels are use, .info, .error, .warning and .critical, be sure to have at least the same 4 levels. | ||
|
||
Args: | ||
handler (logging.Handler): The handler to add. | ||
""" | ||
pass | ||
|
||
|
||
T = TypeVar( 'T', bound='HasLogger' ) | ||
|
||
|
||
def addLogSupport( loggerTitle: str ) -> Callable[ [ Type[ T ] ], Type[ T ] ]: | ||
"""Decorator to add logger support in the class following existing architecture. | ||
|
||
Args: | ||
loggerTitle (str): Title to display in the logger | ||
""" | ||
|
||
def decorator( cls: Type[ T ] ) -> Type[ T ]: | ||
original_init = cls.__init__ | ||
|
||
@wraps( original_init ) | ||
def new_init( self: T, *args: Any, **kwargs: Any ) -> None: | ||
spe_handler = kwargs.pop( 'speHandler', False ) | ||
if spe_handler: | ||
self.logger = logging.getLogger( loggerTitle ) | ||
self.logger.setLevel( logging.INFO ) | ||
else: | ||
self.logger = getLogger( loggerTitle, True ) | ||
|
||
original_init( self, *args, **kwargs ) | ||
|
||
def setLoggerHandler( self: T, handler: logging.Handler ) -> None: | ||
# No docstring needed - Protocol defines the contract | ||
if not self.logger.handlers: | ||
self.logger.addHandler( handler ) | ||
else: | ||
self.logger.warning( | ||
"The logger already has an handler, to use yours set the argument 'speHandler' to True during the filter initialization." | ||
) | ||
|
||
cls.__init__ = new_init # type: ignore[assignment] | ||
cls.setLoggerHandler = setLoggerHandler # type: ignore[assignment] | ||
|
||
return cls | ||
|
||
return decorator |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.