File: task.py

package info (click to toggle)
python-taskipy 1.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 436 kB
  • sloc: python: 1,262; makefile: 16; sh: 1
file content (80 lines) | stat: -rw-r--r-- 3,137 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from typing import Optional

from taskipy.exceptions import MalformedTaskError


class Task:
    def __init__(self, task_name: str, task_toml_contents: object):
        self.__task_name = task_name
        self.__task_command = self.__extract_task_command(task_toml_contents)
        self.__task_description = self.__extract_task_description(task_toml_contents)
        self.__task_use_vars = self.__extract_task_use_vars(task_toml_contents)
        self.__task_workdir = self.__extract_task_workdir(task_toml_contents)

    @property
    def name(self) -> str:
        return self.__task_name

    @property
    def command(self) -> str:
        return self.__task_command

    @property
    def workdir(self) -> Optional[str]:
        return self.__task_workdir

    @property
    def description(self) -> str:
        return self.__task_description

    @property
    def use_vars(self) -> Optional[bool]:
        return self.__task_use_vars

    def __extract_task_use_vars(self, task_toml_contents: object) -> Optional[bool]:
        if isinstance(task_toml_contents, str):
            return None

        if isinstance(task_toml_contents, dict):
            value = task_toml_contents.get('use_vars')
            if value is not None and not isinstance(value, bool):
                raise MalformedTaskError(self.__task_name, f'task\'s "use_vars" arg has to be bool type got {type(value)}')
            return value

        raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }')

    def __extract_task_command(self, task_toml_contents: object) -> str:
        if isinstance(task_toml_contents, str):
            return task_toml_contents

        if isinstance(task_toml_contents, dict):
            try:
                return task_toml_contents['cmd']
            except KeyError:
                raise MalformedTaskError(self.__task_name, 'the task item does not have the "cmd" property')

        raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }')

    def __extract_task_workdir(self, task_toml_contents: object) -> Optional[str]:
        if isinstance(task_toml_contents, str):
            return None

        if isinstance(task_toml_contents, dict):
            value = task_toml_contents.get('cwd')
            if value is not None and not isinstance(value, str):
                raise MalformedTaskError(self.__task_name, f'task\'s "cwd" arg has to be str type got {type(value)}')
            return value

        raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }')

    def __extract_task_description(self, task_toml_contents: object) -> str:
        if isinstance(task_toml_contents, str):
            return ''

        if isinstance(task_toml_contents, dict):
            try:
                return task_toml_contents['help']
            except KeyError:
                return ''

        raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, cwd, help, use_vars }')