Add user and host variables

This commit is contained in:
Joscha 2019-12-07 22:36:25 +00:00
parent 4ada2c37f5
commit 0a40757bb9
3 changed files with 43 additions and 2 deletions

View file

@ -152,6 +152,16 @@ DEFAULT_CONFIG.add(
"Location the file is currently being compiled for, as a Path. Set during compilation", "Location the file is currently being compiled for, as a Path. Set during compilation",
has_constant_value=False) has_constant_value=False)
DEFAULT_CONFIG.add(
"user",
"Current username. Set during compilation",
has_constant_value=False)
DEFAULT_CONFIG.add(
"host",
"Name of the current computer. Set during compilation",
has_constant_value=False)
class Config: class Config:
@staticmethod @staticmethod
def load_config_file(path: Optional[Path]) -> "Config": def load_config_file(path: Optional[Path]) -> "Config":
@ -361,3 +371,19 @@ class Config:
@target.setter @target.setter
def target(self, path: Path) -> None: def target(self, path: Path) -> None:
self._set("target", path) self._set("target", path)
@property
def user(self) -> str:
return self._get("user", str)
@target.setter
def user(self, user: str) -> None:
self._set("user", user)
@property
def host(self) -> str:
return self._get("host", str)
@target.setter
def host(self, host: str) -> None:
self._set("host", host)

View file

@ -82,8 +82,12 @@ class Processor:
def _process_binary(self, path: Path, config: Config) -> None: def _process_binary(self, path: Path, config: Config) -> None:
logger.debug(f"Processing as a binary file") logger.debug(f"Processing as a binary file")
if not config.targets:
logger.info(" (no targets)")
return
for target in config.targets: for target in config.targets:
logger.info(f" -> {style_path(str(target))}") logger.info(f" -> {style_path(target)}")
if not self._justify_target(target): if not self._justify_target(target):
logger.info("Skipping this target") logger.info("Skipping this target")
@ -114,7 +118,7 @@ class Processor:
return return
for target in config.targets: for target in config.targets:
logger.info(f" -> {style_path(str(target))}") logger.info(f" -> {style_path(target)}")
if not self._justify_target(target): if not self._justify_target(target):
logger.info("Skipping this target") logger.info("Skipping this target")
@ -122,6 +126,8 @@ class Processor:
config_copy = config.copy() config_copy = config.copy()
config_copy.target = target config_copy.target = target
config_copy.user = get_user()
config_copy.host = get_host()
try: try:
parser = Parser( parser = Parser(

View file

@ -1,10 +1,13 @@
import copy import copy
import getpass
import socket
import types import types
from pathlib import Path from pathlib import Path
from typing import Any, Dict from typing import Any, Dict
__all__ = [ __all__ = [
"copy_local_variables", "copy_local_variables",
"get_user", "get_host",
"ExecuteException", "safer_exec", "safer_eval", "ExecuteException", "safer_exec", "safer_eval",
"ReadFileException", "read_file", "ReadFileException", "read_file",
"WriteFileException", "write_file", "WriteFileException", "write_file",
@ -30,6 +33,12 @@ def copy_local_variables(local: Dict[str, Any]) -> Dict[str, Any]:
return local_copy return local_copy
def get_user() -> str:
return getpass.getuser()
def get_host() -> str:
return socket.gethostname()
class ExecuteException(Exception): class ExecuteException(Exception):
pass pass