auto-editor 23.35.1__py3-none-any.whl → 23.40.1__py3-none-any.whl

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,7 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from collections.abc import Callable
4
+ from dataclasses import dataclass
4
5
  from fractions import Fraction
5
6
  from typing import Any
6
7
 
@@ -8,49 +9,27 @@ from .data_structs import Sym, print_str
8
9
  from .err import MyError
9
10
 
10
11
 
11
- class Proc:
12
- __slots__ = ("name", "proc", "arity", "contracts")
13
-
14
- def __init__(
15
- self,
16
- name: str,
17
- proc: Callable,
18
- arity: tuple[int, int | None] = (1, None),
19
- contracts: list[Any] | None = None,
20
- ):
21
- self.name = name
22
- self.proc = proc
23
- self.arity = arity
24
- self.contracts = contracts
25
-
26
- def __str__(self) -> str:
27
- return f"#<procedure:{self.name}>"
28
-
29
- __repr__ = __str__
30
-
31
- def __call__(self, *args: Any) -> Any:
32
- return self.proc(*args)
33
-
34
-
12
+ @dataclass(slots=True)
35
13
  class Contract:
36
14
  # Convenient flat contract class
37
- __slots__ = ("name", "c")
15
+ name: str
16
+ c: Callable[[object], bool]
38
17
 
39
- def __init__(self, name: str, c: Callable[[object], bool]):
40
- self.name = name
41
- self.c = c
18
+ def __call__(self, *v: object) -> bool:
19
+ if len(v) != 1:
20
+ o = self.name
21
+ raise MyError(f"`{o}` has an arity mismatch. Expected 1, got {len(v)}")
22
+ return self.c(v[0])
42
23
 
43
24
  def __str__(self) -> str:
44
- return f"<procedure:{self.name}>"
25
+ return self.name
45
26
 
46
- __repr__ = __str__
47
-
48
- def __call__(self, v: object) -> bool:
49
- return self.c(v)
27
+ def __repr__(self) -> str:
28
+ return f"#<proc:{self.name} (1 1)>"
50
29
 
51
30
 
52
31
  def check_contract(c: object, val: object) -> bool:
53
- if isinstance(c, Contract):
32
+ if type(c) is Contract:
54
33
  return c(val)
55
34
  if (
56
35
  isinstance(c, Proc)
@@ -62,16 +41,70 @@ def check_contract(c: object, val: object) -> bool:
62
41
  return val is True
63
42
  if c is False:
64
43
  return val is False
65
-
66
- if type(c) is int:
67
- return val == c
68
44
  if type(c) in (int, float, Fraction, complex, str, Sym):
69
45
  return val == c
70
46
  raise MyError(f"Invalid contract, got: {print_str(c)}")
71
47
 
72
48
 
49
+ def check_args(
50
+ o: str,
51
+ values: list | tuple,
52
+ arity: tuple[int, int | None],
53
+ cont: tuple[Any, ...],
54
+ ) -> None:
55
+ lower, upper = arity
56
+ amount = len(values)
57
+
58
+ assert not (upper is not None and lower > upper)
59
+ base = f"`{o}` has an arity mismatch. Expected "
60
+
61
+ if lower == upper and len(values) != lower:
62
+ raise MyError(f"{base}{lower}, got {amount}")
63
+ if upper is None and amount < lower:
64
+ raise MyError(f"{base}at least {lower}, got {amount}")
65
+ if upper is not None and (amount > upper or amount < lower):
66
+ raise MyError(f"{base}between {lower} and {upper}, got {amount}")
67
+
68
+ if not cont:
69
+ return
70
+
71
+ for i, val in enumerate(values):
72
+ check = cont[-1] if i >= len(cont) else cont[i]
73
+ if not check_contract(check, val):
74
+ exp = f"{check}" if callable(check) else print_str(check)
75
+ raise MyError(f"`{o}` expected a {exp}, got {print_str(val)}")
76
+
77
+
78
+ class Proc:
79
+ __slots__ = ("name", "proc", "arity", "contracts")
80
+
81
+ def __init__(
82
+ self, n: str, p: Callable, a: tuple[int, int | None] = (1, None), *c: Any
83
+ ):
84
+ self.name = n
85
+ self.proc = p
86
+ self.arity = a
87
+ self.contracts: tuple[Any, ...] = c
88
+
89
+ def __call__(self, *args: Any) -> Any:
90
+ check_args(self.name, args, self.arity, self.contracts)
91
+ return self.proc(*args)
92
+
93
+ def __str__(self) -> str:
94
+ return self.name
95
+
96
+ def __repr__(self) -> str:
97
+ n = "inf" if self.arity[1] is None else f"{self.arity[1]}"
98
+
99
+ if self.contracts is None:
100
+ c = ""
101
+ else:
102
+ c = " (" + " ".join([f"{c}" for c in self.contracts]) + ")"
103
+ return f"#<proc:{self.name} ({self.arity[0]} {n}){c}>"
104
+
105
+
73
106
  def is_contract(c: object) -> bool:
74
- if isinstance(c, Contract):
107
+ if type(c) is Contract:
75
108
  return True
76
109
  if (
77
110
  isinstance(c, Proc)
@@ -86,8 +119,8 @@ def is_contract(c: object) -> bool:
86
119
 
87
120
  is_bool = Contract("bool?", lambda v: type(v) is bool)
88
121
  is_int = Contract("int?", lambda v: type(v) is int)
89
- is_uint = Contract("uint?", lambda v: type(v) is int and v > -1)
90
- is_nat = Contract("nat?", lambda v: type(v) is int and v > 0)
122
+ is_nat = Contract("nat?", lambda v: type(v) is int and v > -1)
123
+ is_nat1 = Contract("nat1?", lambda v: type(v) is int and v > 0)
91
124
  int_not_zero = Contract("(or/c (not/c 0) int?)", lambda v: v != 0 and is_int(v))
92
125
  is_num = Contract("number?", lambda v: type(v) in (int, float, Fraction, complex))
93
126
  is_real = Contract("real?", lambda v: type(v) in (int, float, Fraction))
@@ -105,13 +138,13 @@ is_proc = Contract("procedure?", lambda v: isinstance(v, (Proc, Contract)))
105
138
 
106
139
  def andc(*cs: object) -> Proc:
107
140
  return Proc(
108
- "flat-and/c", lambda v: all([check_contract(c, v) for c in cs]), (1, 1), [any_p]
141
+ "flat-and/c", lambda v: all([check_contract(c, v) for c in cs]), (1, 1), any_p
109
142
  )
110
143
 
111
144
 
112
145
  def orc(*cs: object) -> Proc:
113
146
  return Proc(
114
- "flat-or/c", lambda v: any([check_contract(c, v) for c in cs]), (1, 1), [any_p]
147
+ "flat-or/c", lambda v: any([check_contract(c, v) for c in cs]), (1, 1), any_p
115
148
  )
116
149
 
117
150
 
@@ -135,7 +168,11 @@ def lt_c(n: int | float | Fraction) -> Proc:
135
168
  return Proc(f"(</c {n})", lambda i: i < n, (1, 1), [is_real])
136
169
 
137
170
 
138
- def between_c(n: int | float | Fraction, m: int | float | Fraction) -> Proc:
171
+ def between_c(n: Any, m: Any) -> Proc:
139
172
  if m > n:
140
- return Proc(f"(between/c {n} {m})", lambda i: is_real(i) and i <= m and i >= n)
141
- return Proc(f"(between/c {n} {m})", lambda i: is_real(i) and i <= n and i >= m)
173
+ return Proc(
174
+ f"(between/c {n} {m})", lambda i: is_real(i) and i <= m and i >= n, (1, 1)
175
+ )
176
+ return Proc(
177
+ f"(between/c {n} {m})", lambda i: is_real(i) and i <= n and i >= m, (1, 1)
178
+ )
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
+ from collections.abc import Iterator
3
4
  from fractions import Fraction
4
5
  from io import StringIO
5
6
  from typing import Any
@@ -66,6 +67,61 @@ class Sym:
66
67
  return type(obj) is Sym and self.hash == obj.hash
67
68
 
68
69
 
70
+ class Keyword:
71
+ __slots__ = "val"
72
+
73
+ def __init__(self, val: str):
74
+ self.val = val
75
+
76
+ def __str__(self) -> str:
77
+ return f"#:{self.val}"
78
+
79
+ __repr__ = __str__
80
+
81
+ def __eq__(self, obj: object) -> bool:
82
+ return type(obj) is Keyword and self.val == obj.val
83
+
84
+
85
+ class QuotedKeyword:
86
+ __slots__ = "val"
87
+
88
+ def __init__(self, val: Keyword | str):
89
+ self.val = val if isinstance(val, Keyword) else Keyword(val)
90
+
91
+ def __str__(self) -> str:
92
+ return f"{self.val}"
93
+
94
+ __repr__ = __str__
95
+
96
+ def __eq__(self, obj: object) -> bool:
97
+ return type(obj) is QuotedKeyword and self.val == obj.val
98
+
99
+
100
+ class Quoted:
101
+ __slots__ = "val"
102
+
103
+ def __init__(self, val: list):
104
+ self.val = val
105
+
106
+ def __len__(self) -> int:
107
+ return len(self.val)
108
+
109
+ def __getitem__(self, key: int | slice) -> Any:
110
+ if isinstance(key, slice):
111
+ return Quoted(self.val[key])
112
+
113
+ return self.val[key]
114
+
115
+ def __iter__(self) -> Iterator:
116
+ return self.val.__iter__()
117
+
118
+ def __contains__(self, item: object) -> bool:
119
+ return item in self.val
120
+
121
+ def __eq__(self, obj: object) -> bool:
122
+ return type(obj) is Quoted and self.val == obj.val
123
+
124
+
69
125
  class Char:
70
126
  __slots__ = "val"
71
127
 
@@ -123,10 +179,20 @@ def display_str(val: object) -> str:
123
179
  return f"{val.real}{join}{val.imag}i"
124
180
  if type(val) is np.bool_:
125
181
  return "1" if val else "0"
126
-
127
- if isinstance(val, Fraction):
182
+ if type(val) is Fraction:
128
183
  return f"{val.numerator}/{val.denominator}"
129
- if isinstance(val, list):
184
+
185
+ if type(val) is Quoted:
186
+ if not val:
187
+ return "()"
188
+ result = StringIO()
189
+ result.write(f"({display_str(val[0])}")
190
+ for item in val[1:]:
191
+ result.write(f" {display_str(item)}")
192
+ result.write(")")
193
+ return result.getvalue()
194
+
195
+ if type(val) is list:
130
196
  if not val:
131
197
  return "#()"
132
198
  result = StringIO()
@@ -182,7 +248,9 @@ def print_str(val: object) -> str:
182
248
  return f'"{val}"'
183
249
  if type(val) is Char:
184
250
  return f"{val!r}"
185
- if type(val) is Sym:
251
+ if type(val) is Keyword:
252
+ return f"'{val}"
253
+ if type(val) in (Sym, Quoted, QuotedKeyword):
186
254
  return f"'{display_str(val)}"
187
255
 
188
256
  return display_str(val)
@@ -6,7 +6,7 @@ from typing import TYPE_CHECKING, Any, NamedTuple
6
6
 
7
7
  import numpy as np
8
8
 
9
- from auto_editor.analyze import FileSetup
9
+ from auto_editor.analyze import FileSetup, Levels
10
10
  from auto_editor.ffwrapper import FFmpeg, FileInfo
11
11
  from auto_editor.lang.palet import Lexer, Parser, env, interpret, is_boolarr
12
12
  from auto_editor.lib.data_structs import print_str
@@ -90,31 +90,40 @@ def make_av(
90
90
  return vtl, atl
91
91
 
92
92
 
93
- def run_interpreter(
94
- text: str,
95
- filesetup: FileSetup,
96
- log: Log,
93
+ def run_interpreter_for_edit_option(
94
+ text: str, filesetup: FileSetup
97
95
  ) -> NDArray[np.bool_]:
96
+ ensure = filesetup.ensure
97
+ src = filesetup.src
98
+ tb = filesetup.tb
99
+ bar = filesetup.bar
100
+ temp = filesetup.temp
101
+ log = filesetup.log
102
+
98
103
  try:
99
104
  parser = Parser(Lexer("`--edit`", text))
100
105
  if log.is_debug:
101
106
  log.debug(f"edit: {parser}")
102
107
 
103
108
  env["timebase"] = filesetup.tb
109
+ env["@levels"] = Levels(ensure, src, tb, bar, temp, log)
104
110
  env["@filesetup"] = filesetup
105
111
 
106
112
  results = interpret(env, parser)
107
- except (MyError, ZeroDivisionError) as e:
108
- log.error(e)
109
113
 
110
- if len(results) == 0:
111
- log.error("Expression in --edit must return a bool-array, got nothing")
114
+ if len(results) == 0:
115
+ raise MyError("Expression in --edit must return a bool-array, got nothing")
112
116
 
113
- result = results[-1]
114
- if not is_boolarr(result):
115
- log.error(
116
- f"Expression in --edit must return a bool-array, got {print_str(result)}"
117
- )
117
+ result = results[-1]
118
+ if callable(result):
119
+ result = result()
120
+
121
+ if not is_boolarr(result):
122
+ raise MyError(
123
+ f"Expression in --edit must return a bool-array, got {print_str(result)}"
124
+ )
125
+ except MyError as e:
126
+ log.error(e)
118
127
 
119
128
  assert isinstance(result, np.ndarray)
120
129
  return result
@@ -276,7 +285,7 @@ def make_layers(
276
285
 
277
286
  for i in map(str, inputs):
278
287
  filesetup = FileSetup(sources[i], ensure, len(inputs) < 2, tb, bar, temp, log)
279
- has_loud = run_interpreter(method, filesetup, log)
288
+ has_loud = run_interpreter_for_edit_option(method, filesetup)
280
289
 
281
290
  if len(mark_loud) > 0:
282
291
  mut_set_range(has_loud, mark_loud, loud_speed)
@@ -117,13 +117,10 @@ def render_av(
117
117
 
118
118
  apply_video_later = True
119
119
 
120
- if args.video_codec in encoders:
121
- apply_video_later = encoders[args.video_codec]["pix_fmt"].isdisjoint(
122
- allowed_pix_fmt
123
- )
124
-
125
120
  if args.scale != 1:
126
121
  apply_video_later = False
122
+ elif args.video_codec in encoders:
123
+ apply_video_later = set(encoders[args.video_codec]).isdisjoint(allowed_pix_fmt)
127
124
 
128
125
  log.debug(f"apply video quality settings now: {not apply_video_later}")
129
126
 
@@ -5,7 +5,7 @@ from dataclasses import dataclass, field
5
5
  from fractions import Fraction
6
6
 
7
7
  import auto_editor
8
- from auto_editor.analyze import FileSetup
8
+ from auto_editor.analyze import FileSetup, Levels
9
9
  from auto_editor.ffwrapper import FFmpeg, FileInfo
10
10
  from auto_editor.lang.palet import ClosingError, Lexer, Parser, env, interpret
11
11
  from auto_editor.lib.data_structs import print_str
@@ -77,9 +77,10 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None:
77
77
  src = sources["0"]
78
78
  tb = src.get_fps() if args.timebase is None else args.timebase
79
79
  ensure = Ensure(ffmpeg, src.get_sr(), temp, log)
80
- filesetup = FileSetup(src, ensure, strict, tb, Bar("none"), temp, log)
80
+ bar = Bar("none")
81
81
  env["timebase"] = tb
82
- env["@filesetup"] = filesetup
82
+ env["@levels"] = Levels(ensure, src, tb, bar, temp, log)
83
+ env["@filesetup"] = FileSetup(src, ensure, strict, tb, bar, temp, log)
83
84
 
84
85
  print(f"Auto-Editor {auto_editor.version} ({auto_editor.__version__})")
85
86
  text = None
@@ -649,8 +649,8 @@ def main(sys_args: list[str] | None = None):
649
649
  ("(- 10.5 3)", 7.5),
650
650
  ("(* 11.5 3)", 34.5),
651
651
  ("(/ 3/4 4)", Fraction(3, 16)),
652
- ("(/ 5)", Fraction(1, 5)),
653
- ("(/ 6 1)", 6),
652
+ ("(/ 5)", 0.2),
653
+ ("(/ 6 1)", 6.0),
654
654
  ("30/1", Fraction(30)),
655
655
  ("(sqrt -4)", 2j),
656
656
  ("(pow 2 3)", 8),
@@ -683,7 +683,7 @@ def main(sys_args: list[str] | None = None):
683
683
  ("(float? 21)", False),
684
684
  ("(frac? 4/5)", True),
685
685
  ("(frac? 3.4)", False),
686
- ('(string-append "Hello" " World")', "Hello World"),
686
+ ('(& "Hello" " World")', "Hello World"),
687
687
  ('(define apple "Red Wood") apple', "Red Wood"),
688
688
  ("(= 1 1.0)", True),
689
689
  ("(= 1 2)", False),
auto_editor/timeline.py CHANGED
@@ -100,59 +100,59 @@ class TlEllipse(_Visual):
100
100
 
101
101
  video_builder = pAttrs(
102
102
  "video",
103
- pAttr("start", Required, is_uint),
104
- pAttr("dur", Required, is_uint),
103
+ pAttr("start", Required, is_nat),
104
+ pAttr("dur", Required, is_nat),
105
105
  pAttr("src", Required, is_str),
106
106
  pAttr("offset", 0, is_int),
107
107
  pAttr("speed", 1, is_real),
108
- pAttr("stream", 0, is_uint),
108
+ pAttr("stream", 0, is_nat),
109
109
  )
110
110
  audio_builder = pAttrs(
111
111
  "audio",
112
- pAttr("start", Required, is_uint),
113
- pAttr("dur", Required, is_uint),
112
+ pAttr("start", Required, is_nat),
113
+ pAttr("dur", Required, is_nat),
114
114
  pAttr("src", Required, is_str),
115
115
  pAttr("offset", 0, is_int),
116
116
  pAttr("speed", 1, is_real),
117
117
  pAttr("volume", 1, is_real),
118
- pAttr("stream", 0, is_uint),
118
+ pAttr("stream", 0, is_nat),
119
119
  )
120
120
  text_builder = pAttrs(
121
121
  "text",
122
- pAttr("start", Required, is_uint),
123
- pAttr("dur", Required, is_uint),
122
+ pAttr("start", Required, is_nat),
123
+ pAttr("dur", Required, is_nat),
124
124
  pAttr("content", Required, is_str),
125
125
  pAttr("x", 0.5, is_real),
126
126
  pAttr("y", 0.5, is_real),
127
127
  pAttr("font", "Arial", is_str),
128
- pAttr("size", 55, is_uint),
128
+ pAttr("size", 55, is_nat),
129
129
  pAttr("align", "left", is_str),
130
130
  pAttr("opacity", 1, is_threshold),
131
131
  pAttr("anchor", "ce", is_str),
132
132
  pAttr("rotate", 0, is_real),
133
133
  pAttr("fill", "#FFF", is_str),
134
- pAttr("stroke", 0, is_uint),
134
+ pAttr("stroke", 0, is_nat),
135
135
  pAttr("strokecolor", "#000", is_str),
136
136
  )
137
137
 
138
138
  img_builder = pAttrs(
139
139
  "image",
140
- pAttr("start", Required, is_uint),
141
- pAttr("dur", Required, is_uint),
140
+ pAttr("start", Required, is_nat),
141
+ pAttr("dur", Required, is_nat),
142
142
  pAttr("src", Required, is_str),
143
143
  pAttr("x", 0.5, is_real),
144
144
  pAttr("y", 0.5, is_real),
145
145
  pAttr("opacity", 1, is_threshold),
146
146
  pAttr("anchor", "ce", is_str),
147
147
  pAttr("rotate", 0, is_real),
148
- pAttr("stroke", 0, is_uint),
148
+ pAttr("stroke", 0, is_nat),
149
149
  pAttr("strokecolor", "#000", is_str),
150
150
  )
151
151
 
152
152
  rect_builder = pAttrs(
153
153
  "rect",
154
- pAttr("start", Required, is_uint),
155
- pAttr("dur", Required, is_uint),
154
+ pAttr("start", Required, is_nat),
155
+ pAttr("dur", Required, is_nat),
156
156
  pAttr("x", Required, is_real),
157
157
  pAttr("y", Required, is_real),
158
158
  pAttr("width", Required, is_real),
@@ -161,7 +161,7 @@ rect_builder = pAttrs(
161
161
  pAttr("anchor", "ce", is_str),
162
162
  pAttr("rotate", 0, is_real),
163
163
  pAttr("fill", "#c4c4c4", is_str),
164
- pAttr("stroke", 0, is_uint),
164
+ pAttr("stroke", 0, is_nat),
165
165
  pAttr("strokecolor", "#000", is_str),
166
166
  )
167
167
  ellipse_builder = rect_builder