-
Notifications
You must be signed in to change notification settings - Fork 0
/
traits.py
97 lines (77 loc) · 2.29 KB
/
traits.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
This module introduces the interfaces that, in turn, represent the concepts and abstractions
the application operates on.
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import List
from primitives import Command
class Canvas(ABC):
"""
The application state.
"""
@abstractmethod
def create(self, width: int, height: int):
"""
Create canvas or reset canvas dimensions and data
:param width: canvas width
:param height: canvas height
:return:
"""
pass
@abstractmethod
def clear(self):
"""
Clear canvas contents without resizing
:return:
"""
pass
@abstractmethod
def to_string(self) -> str:
"""
Serialize canvas contents into string
:return: serialized canvas contents
"""
pass
@abstractmethod
def draw(self, *argv):
"""
General drawing method; the first argument denotes the concrete shape to draw
:param argv: [shape, coordinate, color]
:return:
"""
pass
@abstractmethod
def fill(self, x: int, y: int, color: str):
"""
Bucket fill method
:param x: x coordinate of the base point
:param y: y coordinate of the base point
:param color: fill color - a 1-length str
:return:
"""
pass
@abstractmethod
def created(self) -> bool:
"""
Checks if the canvas has dimensions (the create() method has been called)
:return: True if the canvas has dimensions, False otherwise
"""
class Writer(ABC):
"""
Interface representing a console writer for the Canvas application to print out the app state.
Concrete implementations may include variants ranging from simple console printing to
more sophisticated version with ASCII animaion etc.
"""
@abstractmethod
def write(self, data: str):
pass
class CommandExecutor(ABC):
"""
Abstract class representing a concept of a command processor.
Takes a tuple (canvas, command), validates the command against the canvas and produces a
potentially new canvas.
"""
@abstractmethod
def execute(self, canvas: Canvas, command: Command) -> int:
pass