Skip to content

Commit 85d5aeb

Browse files
authored
Merge pull request #32 from anpawo/rework-serialization
Rework serialization
2 parents 6e0990f + b27c25f commit 85d5aeb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+566
-445
lines changed

frontend/Constant.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# Types
5+
#
6+
7+
from typing import Annotated, Literal, Union
8+
9+
# unsigned int
10+
uint = Annotated[int, "unsigned"]
11+
12+
13+
# sides
14+
LEFT = "left"
15+
RIGHT = "right"
16+
UP = "up"
17+
DOWN = "down"
18+
19+
type side = Literal["left", "right", "up", "down"]
20+
21+
# index
22+
START = 0
23+
END = -1

frontend/Define.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

frontend/Global.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
3+
4+
type Arguments = list[int | float | str | Arguments]
5+
type Variable = list[tuple[str, Arguments]]
6+
type Stack = list[tuple[str, int, Arguments]]
7+
8+
9+
class Global:
10+
"""
11+
Globals used by the classes `Input` and `Transformation`.
12+
"""
13+
14+
# Input Variables declared in a scene.
15+
variable: Variable = []
16+
17+
# Represents the steps of the scene.
18+
stack: Stack = []
19+
20+
def __str__(self) -> str:
21+
return f"\nvariable={self.variable};\n\nstack={self.stack};\n"
22+
23+
def __repr__(self) -> str:
24+
return str(self)

frontend/Scene.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
3+
from abc import ABC, abstractmethod
4+
from frontend.Global import *
5+
6+
7+
class Scene(ABC):
8+
"""
9+
Parent class for scenes.
10+
11+
Needed to filter out classes made by the user for anything else other than a `Scene`.
12+
"""
13+
14+
@abstractmethod
15+
def scene(self) -> None: ...

frontend/Serialize.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
3+
4+
from typing import Type
5+
6+
7+
import json
8+
import inspect
9+
import sys
10+
11+
sys.path.append(".")
12+
13+
from VideoCode import *
14+
15+
16+
def commentsToLabel(s: str) -> str:
17+
if s == "":
18+
return ""
19+
if s[0:3] != "\n# ":
20+
return s[0] + commentsToLabel(s[1:])
21+
i = 3
22+
while s[i] != "\n":
23+
i += 1
24+
return f'label("{s[3:i]}")' + commentsToLabel(s[i:])
25+
26+
27+
def serializeScene(filepath: str) -> str:
28+
"""
29+
Serialiaze the `Scenes` of a file.
30+
"""
31+
32+
# Read the content of the file
33+
with open(filepath, "r") as file:
34+
content = file.read() # TODO: Replace comments with labels
35+
36+
# Exec the file to register the `Scene`
37+
global __name__
38+
__name__ = "Scene"
39+
exec(content, globals())
40+
41+
# Filter out anything other that the `Scenes`
42+
scenes: list[Type[Scene]] = [v for _, v in globals().items() if inspect.isclass(v) and Scene in v.__bases__]
43+
44+
# Temporary load the first `Scene` found. TODO: load the desired scene in the future
45+
scenes[0]().scene()
46+
47+
# Access Globals: `variable` and `stack`
48+
g = Global()
49+
50+
# Serialize the instructions to JSON
51+
return json.dumps(
52+
{
53+
"variable": g.variable,
54+
"stack": g.stack,
55+
}
56+
)
57+
58+
59+
if __name__ == "__main__":
60+
for k, v in json.loads(serializeScene("video.py")).items():
61+
print(f"{k:<12}{v}")

frontend/Types.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

frontend/VideoCode.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,17 @@
44
# VideoCode
55
#
66

7-
from Types import *
8-
from _ty import *
7+
# Inputs
8+
from frontend.input._AllInput import *
9+
10+
# Transformations
11+
from frontend.transformation._AllTransformation import *
12+
13+
# Scenes
14+
from frontend.Scene import *
15+
16+
# Global
17+
from frontend.Global import *
18+
19+
# Constant
20+
from frontend.Constant import *

frontend/__init__.py

Whitespace-only changes.

frontend/_ty.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

frontend/input/Image.pyi

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)