Skip to content

Commit

Permalink
Applied ExtendedType as metaclass.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paebbels committed Jul 8, 2023
1 parent d9b1044 commit 7f59372
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions pyTooling/CLIAbstraction/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,21 @@
__version__ = "0.4.2"
__keywords__ = ["abstract", "executable", "cli", "cli arguments"]

from pathlib import Path
from platform import system
from shutil import which as shutil_which
from subprocess import (
from os import environ as os_environ
from pathlib import Path
from platform import system
from shutil import which as shutil_which
from subprocess import (
Popen as Subprocess_Popen,
PIPE as Subprocess_Pipe,
STDOUT as Subprocess_StdOut
)
from typing import Dict, Optional, ClassVar, Type, List, Tuple, Iterator, Generator
from typing import Dict, Optional as Nullable, ClassVar, Type, List, Tuple, Iterator, Generator

from pyTooling.Decorators import export
from pyTooling.Exceptions import ExceptionBase, PlatformNotSupportedException
from pyAttributes import Attribute
from pyTooling.Decorators import export
from pyTooling.Exceptions import ExceptionBase, PlatformNotSupportedException
from pyTooling.MetaClasses import ExtendedType
from pyAttributes import Attribute

from .Argument import (
CommandLineArgument, ExecutableArgument,
Expand All @@ -75,15 +77,15 @@ class CLIArgument(Attribute):


@export
class Program:
class Program(metaclass=ExtendedType, slots=True):
"""Represent a simple command line interface (CLI) executable (program or script)."""

_platform: str #: Current platform the executable runs on (Linux, Windows, ...)
_executableNames: ClassVar[Dict[str, str]] #: Dictionary of platform specific executable names.
_executablePath: Path #: The path to the executable (binary, script, ...).
_dryRun: bool #: True, if program shall run in *dry-run mode*.
__cliOptions__: ClassVar[Dict[Type[CommandLineArgument], int]] #: List of all possible CLI options.
__cliParameters__: Dict[Type[CommandLineArgument], Optional[CommandLineArgument]] #: List of all CLI parameters (used CLI options).
__cliParameters__: Dict[Type[CommandLineArgument], Nullable[CommandLineArgument]] #: List of all CLI parameters (used CLI options).

def __init_subclass__(cls, *args, **kwargs):
"""
Expand Down Expand Up @@ -230,23 +232,36 @@ def __str__(self):
class Executable(Program): # (ILogable):
"""Represent a CLI executable derived from :class:`Program`, that adds an abstraction of :class:`subprocess.Popen`."""

_BOUNDARY = "====== BOUNDARY pyTooling.CLIAbstraction BOUNDARY ======"

_environment: Dict[str, str] = None
_process: Subprocess_Popen = None
_iterator: Iterator = None

def __init__(self, executablePath: Path = None, binaryDirectoryPath: Path = None, dryRun: bool = False): #, environment: Environment = None):
_BOUNDARY: ClassVar[str] = "====== BOUNDARY pyTooling.CLIAbstraction BOUNDARY ======"

_workingDirectory: Nullable[Path]
_environment: Nullable[Dict[str, str]]
_process: Nullable[Subprocess_Popen]
_iterator: Nullable[Iterator]

def __init__(
self,
executablePath: Path = None,
binaryDirectoryPath: Path = None,
workingDirectory: Path = None,
# environment: Environment = None,
dryRun: bool = False
):
super().__init__(executablePath, binaryDirectoryPath, dryRun)

self._workingDirectory = None
self._environment = None
self._process = None
self._iterator = None

def StartProcess(self):
# start child process

if self._dryRun:
self.LogDryRun(f"Start process: {self!r}")
return

if (self._environment is not None):
if self._environment is not None:
envVariables = self._environment.Variables
else:
envVariables = None
Expand All @@ -259,6 +274,7 @@ def StartProcess(self):
stdin=Subprocess_Pipe,
stdout=Subprocess_Pipe,
stderr=Subprocess_StdOut,
cwd=self._workingDirectory,
env=envVariables,
universal_newlines=True,
bufsize=256
Expand Down

0 comments on commit 7f59372

Please sign in to comment.