Skip to content

Commit

Permalink
(WIP) Add concurrent output option
Browse files Browse the repository at this point in the history
Building on slight refactoring of output, allow entries to appear as
soon as they are available rather than waiting until all complete.
  • Loading branch information
ingrinder committed Jan 10, 2024
1 parent b4d5b12 commit 05b94f8
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 69 deletions.
31 changes: 25 additions & 6 deletions archey/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging
import os
import sys
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor, as_completed
from contextlib import ExitStack
from enum import Enum
from typing import Callable, Optional
Expand Down Expand Up @@ -124,6 +124,7 @@ def args_parsing() -> argparse.Namespace:
return parser.parse_args()


# todo: refactor out into functions/methods (too many branches)
def main():
"""Simple entry point"""
args = args_parsing()
Expand Down Expand Up @@ -152,13 +153,17 @@ def main():
format_to_json=args.json,
)

# Begin with some output as soon as possible
output.begin_output()

# We will map this function onto our enabled entries to instantiate them.
def _entry_instantiator(entry: dict) -> Optional[Entry]:
def _entry_instantiator(entry: dict, index: int) -> Optional[Entry]:
# Based on **required** `type` field, instantiate the corresponding `Entry` object.
try:
return Entries[entry.pop("type")].value(
name=entry.pop("name", None), # `name` is fully-optional.
options=entry, # Remaining fields should be propagated as options.
index=index, # Provide entry with its position index.
)
except KeyError as key_error:
logging.warning("One entry (misses or) uses an invalid `type` field (%s).", key_error)
Expand All @@ -182,11 +187,25 @@ def _entry_instantiator(entry: dict) -> Optional[Entry]:
)
mapper = executor.map

for entry_instance in mapper(_entry_instantiator, available_entries):
if entry_instance:
output.add_entry(entry_instance)
# todo: solution fails if parallel_loading false because no executor
if configuration.get("output_concurrency"):
tasks = []
for index, entry in enumerate(available_entries):
future = executor.submit(_entry_instantiator, entry, index)
output.add_entry_concurrent(future)
tasks.append(future)

for future in as_completed(tasks):
output.entry_future_done_callback(future)

else:
for entry_instance in mapper(
_entry_instantiator, available_entries, range(len(available_entries))
):
if entry_instance:
output.add_entry(entry_instance)

output.output()
output.finish_output()

# Has the screenshot flag been specified ?
if args.screenshot is not None:
Expand Down
19 changes: 11 additions & 8 deletions archey/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@ class TerminalMovements(Enum):
TO_COLUMN = "G"
TO_POSITION = "H"

# override `Style`
def __str__(self):
return Style.escape_code_from_attrs(";".join(map(str, (1, self.value))))
ERASE_LINE = "K"

# Python 3.6 compatibility due to string format changes, see
# <https://docs.python.org/3/whatsnew/3.7.html#other-language-changes> (bpo-28794)
def __format__(self, _):
return super().__str__()
def move(self, *amounts):
"""
Returns an ANSI escape code corresponding to the movement given.
Use `TerminalMovements` for the direction, and specify amounts 1-indexed, row before column.
"""
display_attrs = ";".join(map(str, (*amounts, self.value)))
return f"\x1b[{display_attrs}"


class CursorPosition(Style):
Expand All @@ -141,4 +142,6 @@ def move(movement: TerminalMovements, *amounts):
Returns an ANSI escape code corresponding to the movement given.
Use `TerminalMovements` for the direction, and specify amounts 1-indexed, row before column.
"""
return Style.escape_code_from_attrs(";".join(map(str, (*amounts, movement))))
# return Style.escape_code_from_attrs(";".join(map(str, (*amounts, movement.value))))
display_attrs = ";".join(map(str, (*amounts, movement.value)))
return f"\x1b[{display_attrs}"
6 changes: 5 additions & 1 deletion archey/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ def __new__(cls, *_, **kwargs):
return super().__new__(cls)

@abstractmethod
def __init__(self, name: Optional[str] = None, value=None, options: Optional[dict] = None):
def __init__(
self, name: Optional[str] = None, value=None, options: Optional[dict] = None, index: int = 0
):
# Each entry will have always have the following attributes...
# `name`: key (defaults to the instantiated entry class name);
# `value`: value of entry as an appropriate object;
# `options`: configuration options *specific* to an entry instance;
# 'index': 0-based index (i.e. location in the list) of the entry;
self.name = name or self._PRETTY_NAME or self.__class__.__name__
self.value = value
self.options = options or {}
self.index = index

# Propagates a reference to default strings specified in `Configuration`.
self._default_strings = Configuration().get("default_strings")
Expand Down
Loading

0 comments on commit 05b94f8

Please sign in to comment.