-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
common code extended with task decorator
require_exec
and generaliz…
…ation of default targets
- Loading branch information
Showing
7 changed files
with
81 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -6,3 +6,5 @@ | |
) | ||
|
||
from .dns import AddressLookups # noqa: F401 | ||
|
||
from .executable import require_exec # noqa: F401 |
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
PING_DESTINATIONS = ('google.com', | ||
'facebook.com', | ||
'nytimes.com') | ||
DESTINATIONS = ('google.com', | ||
'facebook.com', | ||
'nytimes.com') |
This file contains 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,43 @@ | ||
"""Common helpers requiring system executables""" | ||
import functools | ||
import shutil | ||
|
||
import netrics.task | ||
|
||
|
||
class ExecTask: | ||
"""Wrapped callable requiring a named system executable.""" | ||
|
||
def __init__(self, name, func): | ||
self._executable_name_ = name | ||
|
||
# assign func's __module__, __name__, etc. | ||
# (but DON'T update __dict__) | ||
# | ||
# (also assigns __wrapped__) | ||
functools.update_wrapper(self, func, updated=()) | ||
|
||
def __repr__(self): | ||
return repr(self.__wrapped__) | ||
|
||
def __call__(self, *args, **kwargs): | ||
# ensure executable on PATH | ||
executable_path = shutil.which(self._executable_name_) | ||
|
||
if executable_path is None: | ||
netrics.task.log.critical(f"{self._executable_name_} executable not found") | ||
return netrics.task.status.file_missing | ||
|
||
return self.__wrapped__(executable_path, *args, **kwargs) | ||
|
||
|
||
class require_exec: | ||
"""Decorator constructor to wrap a callable such that it first | ||
checks that the named system executable is accessible via `PATH`. | ||
""" | ||
def __init__(self, name): | ||
self.executable_name = name | ||
|
||
def __call__(self, func): | ||
return ExecTask(self.executable_name, func) |
This file contains 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 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 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