python-rockbox 0.1.0__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: python-rockbox
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: Create and manipulate WPS tags from Python
5
5
  Author: Stormy
6
6
  Author-email: Stormy <oohstormy@gmail.com>
@@ -14,9 +14,11 @@ Create and manipulate WPS tags from Python
14
14
  ### Currently supported:
15
15
  * All tags from Rockbox 4.0 (via `Tags` class)
16
16
  * Tags (`Tag` class)
17
+ * Rockbox formatted hex color (`Color` class)
17
18
  * Conditionals (`Match` class)
19
+ * If statements (`If` class)
18
20
 
19
- ## Example
21
+ ## Examples
20
22
 
21
23
  This conditional that displays an appropriate battery bitmap:
22
24
  ```py
@@ -43,6 +45,19 @@ Would `__repr__` to:
43
45
  %?bp<%xd(A)|%?bl<%xd(B)|%xd(C)|%xd(D)|%xd(E)|%xd(F)>>
44
46
  ```
45
47
 
48
+ This if statement:
49
+ ```py
50
+ wps = If(Tags.VOLUME, ">=", 0, [
51
+ "Clipping possible",
52
+ "Volume OK"
53
+ ])
54
+ ```
55
+
56
+ Would `__repr__` to:
57
+ ```c
58
+ %?if(%pv, >=, 0)<Clipping possible|Volume OK>
59
+ ```
60
+
46
61
  ## Install
47
62
 
48
63
  To install locally, make sure you have the following installed:
@@ -5,9 +5,11 @@ Create and manipulate WPS tags from Python
5
5
  ### Currently supported:
6
6
  * All tags from Rockbox 4.0 (via `Tags` class)
7
7
  * Tags (`Tag` class)
8
+ * Rockbox formatted hex color (`Color` class)
8
9
  * Conditionals (`Match` class)
10
+ * If statements (`If` class)
9
11
 
10
- ## Example
12
+ ## Examples
11
13
 
12
14
  This conditional that displays an appropriate battery bitmap:
13
15
  ```py
@@ -34,6 +36,19 @@ Would `__repr__` to:
34
36
  %?bp<%xd(A)|%?bl<%xd(B)|%xd(C)|%xd(D)|%xd(E)|%xd(F)>>
35
37
  ```
36
38
 
39
+ This if statement:
40
+ ```py
41
+ wps = If(Tags.VOLUME, ">=", 0, [
42
+ "Clipping possible",
43
+ "Volume OK"
44
+ ])
45
+ ```
46
+
47
+ Would `__repr__` to:
48
+ ```c
49
+ %?if(%pv, >=, 0)<Clipping possible|Volume OK>
50
+ ```
51
+
37
52
  ## Install
38
53
 
39
54
  To install locally, make sure you have the following installed:
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "python-rockbox"
3
- version = "0.1.0"
3
+ version = "0.2.0"
4
4
  description = "Create and manipulate WPS tags from Python"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -0,0 +1,4 @@
1
+ from .wps import Tag, Match, Color, If
2
+ from .tags import Tags
3
+
4
+ __all__: list[str] = ["Tag", "Tags", "Match", "Color", "If"]
@@ -1,5 +1,7 @@
1
1
  from .tags import Tags
2
2
 
3
+ VALID_OPERATORS: list[str] = ["=", "!=", ">", ">=", "<", "<="]
4
+
3
5
 
4
6
  class Color:
5
7
  def __init__(self, color: str) -> None:
@@ -40,13 +42,36 @@ class Tag:
40
42
  self.tag: str = tag
41
43
  self.arguments: tuple = args
42
44
 
45
+ @classmethod
46
+ def parse(cls, tag: str) -> None:
47
+ """
48
+ Parses WPS tag string to class representation.
49
+
50
+ Ex: `%xd(X)` -> `Tag(Tags.DISPLAY_IMAGE, "X")`
51
+ """
52
+ tag_name = tag.removeprefix("%").split("(")[0]
53
+ try:
54
+ arguments = tag.split("(")[1].removesuffix(")").split(",")
55
+ for index, arg in enumerate(arguments):
56
+ arguments[index] = arguments[index].strip()
57
+ if arg.isdigit():
58
+ arguments[index] = int(arg)
59
+ if arg == "-":
60
+ arguments[index] = None
61
+ arguments_tuple = tuple(arguments)
62
+ return cls(tag_name, *arguments_tuple)
63
+ except IndexError:
64
+ return cls(tag_name)
65
+
43
66
  def render_simple_tag(self) -> str:
44
67
  """Renders a tag with no arguments"""
45
68
  return f"%{self.tag}"
46
69
 
47
70
  def render_argument_tag(self) -> str:
48
71
  """Renders a tag that has arguments"""
49
- rendered_args = ", ".join(map(str, self.arguments))
72
+ rendered_args = ", ".join(
73
+ "-" if arg is None else str(arg) for arg in self.arguments
74
+ )
50
75
  return f"%{self.tag}({rendered_args})"
51
76
 
52
77
  def __repr__(self) -> str:
@@ -58,7 +83,7 @@ class Tag:
58
83
 
59
84
  class Match:
60
85
  def __init__(
61
- self, tag: Tag | Tags | str, cases: list[Tag | Match]
86
+ self, tag: Tag | Tags | str, cases: list[Tag | Match | tuple[Tag]]
62
87
  ) -> None:
63
88
  """Class representation of Rockbox WPS conditional"""
64
89
  if isinstance(tag, Tags):
@@ -75,3 +100,26 @@ class Match:
75
100
 
76
101
  def __repr__(self) -> str:
77
102
  return self._render_match()
103
+
104
+
105
+ class If:
106
+ def __init__(
107
+ self, tag: Tag | str, operator: str, operand: str | int, cases: list[str | int]
108
+ ):
109
+ self.tag = Tag(tag)
110
+ self.operator: str = self._verify_operator(operator)
111
+ self.operand: str = operand
112
+ self.cases: list[str | int] = cases
113
+
114
+ @staticmethod
115
+ def _verify_operator(operator: str) -> str:
116
+ if operator not in VALID_OPERATORS:
117
+ raise ValueError(f"Invalid operator: {operator}")
118
+ return operator
119
+
120
+ def _render_if(self) -> str:
121
+ statement: list = [self.tag, self.operator, self.operand]
122
+ return f"%?if({", ".join(map(str, statement))})<{"|".join(self.cases)}>"
123
+
124
+ def __repr__(self) -> str:
125
+ return self._render_if()
@@ -1,4 +0,0 @@
1
- from .wps import Tag, Match, Color
2
- from .tags import Tags
3
-
4
- __all__ = ["Tag", "Tags", "Match", "Color"]