-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16e35cd
commit 7e7cab5
Showing
65 changed files
with
1,732 additions
and
440 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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import argparse | ||
import os | ||
import sys | ||
import time | ||
import typing | ||
from pathlib import Path | ||
|
||
# Global var if we want to run the benchmark | ||
DO_BENCH = False | ||
|
||
|
||
def parse_args(passed_args=None) -> Path: | ||
global DO_BENCH | ||
|
||
parser = argparse.ArgumentParser(description='Start HABApp') | ||
parser.add_argument( | ||
'-c', | ||
'--config', | ||
help='Path to configuration folder (where the config.yml is located)', | ||
default=None | ||
) | ||
parser.add_argument( | ||
'-s', | ||
'--sleep', | ||
help='Sleep time in seconds before starting HABApp', | ||
type=int, | ||
default=None | ||
) | ||
parser.add_argument( | ||
'-b', | ||
'--benchmark', | ||
help='Do a Benchmark based on the current config', | ||
action='store_true' | ||
) | ||
args = parser.parse_args(passed_args) | ||
|
||
DO_BENCH = args.benchmark | ||
|
||
if args.sleep: | ||
args.sleep = max(0, args.sleep) | ||
print(f'Waiting {args.sleep:d} seconds before starting HABApp ...', end='') | ||
time.sleep(args.sleep) | ||
print(' done!') | ||
|
||
path = args.config | ||
if path is not None: | ||
path = Path(path).resolve() | ||
return find_config_folder(path) | ||
|
||
|
||
def find_config_folder(arg_config_path: typing.Optional[Path]) -> Path: | ||
|
||
if arg_config_path is None: | ||
# Nothing is specified, we try to find the config automatically | ||
check_path = [] | ||
try: | ||
working_dir = Path(os.getcwd()) | ||
check_path.append( working_dir / 'HABApp') | ||
check_path.append( working_dir.with_name('HABApp')) | ||
check_path.append( working_dir.parent.with_name('HABApp')) | ||
except ValueError: | ||
# the ValueError gets raised if the working_dir or its parent is empty (e.g. c:\ or /) | ||
pass | ||
|
||
check_path.append(Path.home() / 'HABApp') # User home | ||
|
||
# if we run in a venv check the venv, too | ||
v_env = os.environ.get('VIRTUAL_ENV', '') | ||
if v_env: | ||
check_path.append(Path(v_env) / 'HABApp') # Virtual env dir | ||
else: | ||
# in case the user specifies the config.yml we automatically switch to the parent folder | ||
if arg_config_path.name.lower() == 'config.yml' and arg_config_path.is_file(): | ||
arg_config_path = arg_config_path.parent | ||
|
||
# Override automatic config detection if something is specified through command line | ||
check_path = [arg_config_path] | ||
|
||
for config_folder in check_path: | ||
config_folder = config_folder.resolve() | ||
if not config_folder.is_dir(): | ||
continue | ||
|
||
config_file = config_folder / 'config.yml' | ||
if config_file.is_file(): | ||
return config_folder | ||
|
||
# we have specified a folder, but the config does not exist so we will create it | ||
if arg_config_path is not None and arg_config_path.is_dir(): | ||
return arg_config_path | ||
|
||
# we have nothing found and nothing specified -> exit | ||
print('Config file "config.yml" not found!') | ||
print('Checked folders:\n - ' + '\n - '.join(str(k) for k in check_path)) | ||
print('Please create file or specify a folder with the "-c" arg switch.') | ||
sys.exit(1) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '0.19.1' | ||
__version__ = '0.20.0' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .events import ComplexEventValue, ValueUpdateEvent, ValueChangeEvent, \ | ||
ItemNoChangeEvent, ItemNoUpdateEvent, AllEvents | ||
from . import habapp_events | ||
from .event_filters import EventFilter, ValueChangeEventFilter, ValueUpdateEventFilter |
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,53 @@ | ||
from typing import Any | ||
|
||
import HABApp | ||
from HABApp.core.const import MISSING | ||
from . import ValueChangeEvent, ValueUpdateEvent | ||
|
||
|
||
class EventFilter: | ||
def __init__(self, event_type, **kwargs): | ||
assert len(kwargs) < 3, 'EventFilter only allows up to two args that will be used to filter' | ||
|
||
for arg in kwargs: | ||
if arg not in event_type.__annotations__: | ||
raise AttributeError(f'Filter attribute "{arg}" does not exist for "{event_type.__name__}"') | ||
|
||
self.__cls = event_type | ||
self.__filter = kwargs | ||
|
||
def create_event_listener(self, name, cb) -> 'HABApp.core.EventBusListener': | ||
kwargs = {'event_type': self.__cls} | ||
ct = 1 | ||
for k, v in self.__filter.items(): | ||
kwargs[f'attr_name{ct}'] = k | ||
kwargs[f'attr_value{ct}'] = v | ||
ct += 1 | ||
|
||
return HABApp.core.EventBusListener(name, cb, **kwargs) | ||
|
||
def __repr__(self): | ||
name = self.__class__.__name__ | ||
vals = [f'{k}={v}' for k, v in self.__filter.items()] | ||
if name == EventFilter.__name__: | ||
vals.insert(0, f'event_type={self.__cls.__name__}') | ||
return f'{name}({", ".join(vals)})' | ||
|
||
|
||
class ValueUpdateEventFilter(EventFilter): | ||
_EVENT_TYPE = ValueUpdateEvent | ||
|
||
def __init__(self, value): | ||
super().__init__(self._EVENT_TYPE, value=value) | ||
|
||
|
||
class ValueChangeEventFilter(EventFilter): | ||
_EVENT_TYPE = ValueChangeEvent | ||
|
||
def __init__(self, value: Any = MISSING, old_value: Any = MISSING): | ||
args = {} | ||
if value is not MISSING: | ||
args['value'] = value | ||
if old_value is not MISSING: | ||
args['old_value'] = old_value | ||
super().__init__(self._EVENT_TYPE, **args) |
Oops, something went wrong.