Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed src/tasker/actions/__init__.py
Empty file.
1 change: 1 addition & 0 deletions src/tasker/profiles/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .time import Time
27 changes: 27 additions & 0 deletions src/tasker/profiles/time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from dataclasses import dataclass

from tasker.py import Event


@dataclass
class Time(Event):
start_hour: int
start_minute: int

end_hour: int
end_minute: int

_every_type = None
_every_value = None

def every_hour(self, value: int):
self._every_type = 1
self._every_value = value

return self

def every_minute(self, value: int):
self._every_type = 2
self._every_value = value

return self
1 change: 1 addition & 0 deletions src/tasker/py/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .action import Action
from .event import Event
from .main import TaskerPy
from .profile import Profile
from .profile_variable import ProfileVariable
Expand Down
7 changes: 7 additions & 0 deletions src/tasker/py/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from dataclasses import dataclass

@dataclass
class Event:
"""Basic building block for new profiles"""

_code_ = 0
33 changes: 30 additions & 3 deletions src/tasker/py/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from .scene import Scene
from .task import Task

from .event import Event


@dataclass
class TaskerPy:
Expand Down Expand Up @@ -61,11 +63,36 @@ def _generate_id(self, name: str) -> int:
hash_md5 = md5(name.encode()).hexdigest()
return int(hash_md5[:7], 16)

def add_profile(
self,
name: str,
event: Event,
profile_id: int = None
):
def decorator(task: Task):
profile = Profile(
profile_id,
task.id,
name,
event
)

if profile.id is None:
profile.id = self._generate_id(name)

if not (0 < profile.id < 1_000_000_000):
raise ValueError('Profile id invalid min: 0 max: 999_999_999')

self._profiles.append(profile)
return profile

return decorator

def add_task(
self,
name: str | None = None,
task_id: int | None = None,
output_variables: dict[str, str] | None = None,
name: str = None,
task_id: int = None,
output_variables: dict[str, str] = None,
):
"""
Decorator to add a task to the TaskerPy application.
Expand Down
4 changes: 3 additions & 1 deletion src/tasker/py/profile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from dataclasses import dataclass

from .event import Event

@dataclass
class Profile:
id: int
task_id: int
name: str
event: Event
60 changes: 59 additions & 1 deletion src/tasker/xml/profile_xml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,64 @@
from .xml_builder import XmlBuilder

from tasker.py.profile import Profile
from tasker.profiles import Time


class ProfileXml(XmlBuilder):
def __init__(self, profile):
def __init__(self, profile: Profile):
self.profile = profile

def _create_event(self):
E = self.E

profile_event = self.profile.event

match profile_event:
case Time():
start_hour = str(profile_event.start_hour)
start_minute = str(profile_event.start_minute)

end_hour = str(profile_event.end_hour)
end_minute = str(profile_event.end_minute)

every_type = profile_event._every_type

every_list = []

if (every_type is not None):
every_type = str(every_type)
every_value = str(
profile_event._every_value
)

every_list = [
E.rep(every_type),
E.repval(every_value)
]

yield E.Time(
E.fh(start_hour),
E.fm(start_minute),
*every_list,
E.th(end_hour),
E.tm(end_minute),
sr='con0'
)

def to_xml(self):
E = self.E

profile_id = str(self.profile.id)
task_id = str(self.profile.task_id)
profile_name = self.profile.name

return E.Profile(
E.cdate('0'),
E.edate('1'),
E.flags('8'),
E.id(profile_id),
E.mid0(task_id),
E.nme(profile_name),
*self._create_event(),
sr='prof'
)
14 changes: 9 additions & 5 deletions src/tasker/xml/task_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ def _profile_variables_to_xml(self, *variables: ProfileVariable):
def to_xml(self):
E = self.E

task_collision = int(self.task.collision)
task_id = str(self.task.id)
task_name = self.task.name
task_priority = str(self.task.priority)
task_collision = str(self.task.collision)

notify = str(self.task.notify).lower()
awake = str(self.task.awake).lower()
profile_variables = self.task.profile_variables
Expand All @@ -98,10 +102,10 @@ def to_xml(self):
return E.Task(
E.cdate('0'),
E.edate('1'),
E.id(f'{self.task.id}'),
E.nme(self.task.name),
E.pri(f'{self.task.priority}'),
E.rty(f'{task_collision}'),
E.id(task_id),
E.nme(task_name),
E.pri(task_priority),
E.rty(task_collision),
E.showinnot(notify),
E.stayawake(awake),
*self._actions_to_xml(*self.task()),
Expand Down
36 changes: 36 additions & 0 deletions test/__mocks__/profiles/time.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<TaskerData sr="" dvi="1">
<Profile sr="prof">
<cdate>0</cdate>
<edate>1</edate>
<flags>8</flags>
<id>1</id>
<mid0>172601898</mid0>
<nme>Profile Time</nme>
<Time sr="con0">
<fh>12</fh>
<fm>0</fm>
<rep>1</rep>
<repval>2</repval>
<th>20</th>
<tm>0</tm>
</Time>
</Profile>
<Task sr="task">
<cdate>0</cdate>
<edate>1</edate>
<id>172601898</id>
<nme>task_beep</nme>
<pri>100</pri>
<rty>0</rty>
<showinnot>false</showinnot>
<stayawake>false</stayawake>
<Action sr="act0" ve="7">
<code>171</code>
<Int sr="arg0" val="8000"/>
<Int sr="arg1" val="1000"/>
<Int sr="arg2" val="50"/>
<Int sr="arg3" val="3"/>
<Str sr="arg4"/>
</Action>
</Task>
</TaskerData>
2 changes: 1 addition & 1 deletion test/__mocks__/beep.xml → test/__mocks__/tasks/beep.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Int sr="arg1" val="1000"/>
<Int sr="arg2" val="50"/>
<Int sr="arg3" val="3"/>
<Str sr="arg4" ve="3"/>
<Str sr="arg4"/>
</Action>
</Task>
</TaskerData>
18 changes: 9 additions & 9 deletions test/__mocks__/flash.xml → test/__mocks__/tasks/flash.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
<Str sr="arg0" val="Hello, World" ve="3"/>
<Int sr="arg1" val="0"/>
<Int sr="arg2" val="0"/>
<Str sr="arg3" ve="3"/>
<Str sr="arg4" ve="3"/>
<Str sr="arg5" ve="3"/>
<Str sr="arg6" ve="3"/>
<Str sr="arg7" ve="3"/>
<Str sr="arg8" ve="3"/>
<Str sr="arg3"/>
<Str sr="arg4"/>
<Str sr="arg5"/>
<Str sr="arg6"/>
<Str sr="arg7"/>
<Str sr="arg8"/>
<Int sr="arg9" val="1"/>
<Str sr="arg10" ve="3"/>
<Str sr="arg10"/>
<Int sr="arg11" val="1"/>
<Int sr="arg12" val="0"/>
<Str sr="arg13" ve="3"/>
<Str sr="arg13"/>
<Int sr="arg14" val="0"/>
<Str sr="arg15" ve="3"/>
<Str sr="arg15"/>
</Action>
</Task>
</TaskerData>
17 changes: 15 additions & 2 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from pytest import Function, fixture, mark

from tasker.actions.alert import Beep, Flash
from tasker.py import TaskerPy
from tasker.actions.alert import Beep, Flash, Notify
from tasker.profiles.time import Time
from tasker.py import TaskerPy, Profile


@fixture
Expand Down Expand Up @@ -40,3 +41,15 @@ def task_flash():
yield Flash('Hello, World')

return task_flash

@fixture
def time_profile(beep_task):
app = TaskerPy()

profile_creator = app.add_profile(
'Profile Time',
Time(12, 0, 20, 0).every_hour(2),
profile_id=1,
)

return profile_creator(beep_task)
11 changes: 11 additions & 0 deletions test/helpers/str_exporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pathlib import Path


PYCACHE_DIR = 'test/__pycache__/'

path_default = Path(PYCACHE_DIR)


def tmp_export_str(filename: str, data: str):
path = path_default / filename
path.write_text(data, encoding='utf8')
36 changes: 36 additions & 0 deletions test/helpers/xml_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from lxml import etree
from pathlib import Path


MOCK_DIR = 'test/__mocks__'

path_default = Path(MOCK_DIR)


def xml_to_string(element_xml):
return etree.tostring(element_xml, pretty_print=True, encoding='utf-8').decode()


def read_file(path: Path):
return path.read_text(encoding='utf8')


def read_xml_task(filename):
path = path_default / 'tasks'
path /= filename

return read_file(path)


def read_xml_profile(filename):
path = path_default / 'profiles'
path /= filename

return read_file(path)


def read_xml_scene(filename):
path = path_default / 'scenes'
path /= filename

return read_file(path)
23 changes: 23 additions & 0 deletions test/test_profile_xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from tasker.xml import TaskerXml

from test.helpers.xml_reader import (
read_xml_profile,
xml_to_string
)

from test.helpers.str_exporter import tmp_export_str


def test_time_xml(time_profile, beep_task):
expected_xml = read_xml_profile('time.xml')

xml = TaskerXml(
profiles=[time_profile],
tasks=[beep_task],
).to_xml()

xml_string = xml_to_string(xml)

tmp_export_str('time_exported.xml', xml_string)

assert xml_string == expected_xml
Loading
Loading