Enable support for types' properties

This commit is contained in:
Bartłomiej Pluta
2019-07-08 22:06:38 +02:00
parent 74eec7f997
commit b06a8533c0
22 changed files with 77 additions and 126 deletions

View File

@@ -2,6 +2,7 @@ from enum import Enum
from smnp.error.runtime import RuntimeException
from smnp.note.model import Note
from smnp.type.value import Value
class Type(Enum):
@@ -16,6 +17,37 @@ class Type(Enum):
def stringify(self, element):
return self.value[1](element)
@staticmethod
def integer(value):
return Value(Type.INTEGER, value, {})
@staticmethod
def string(value):
return Value(Type.STRING, value, {
"length": Type.integer(len(value))
})
@staticmethod
def list(value):
return Value(Type.LIST, value, {
"size": Type.integer(len(value))
})
@staticmethod
def note(value):
return Value(Type.NOTE, value, {
"octave": Type.integer(value.octave),
"duration": Type.integer(value.duration),
"dot": Type.string('.' if value.dot else '')
})
@staticmethod
def type(value):
return Value(Type.TYPE, value, {})
@staticmethod
def void():
return Value(Type.VOID, None)
def _failStringify(t):
raise RuntimeException(f"Not able to interpret {t.name}'", None)

View File

@@ -1,9 +1,10 @@
from smnp.type.model import Type
class Value:
def __init__(self, objectType, value):
def __init__(self, objectType, value, properties=None):
if properties is None:
properties = {}
self.value = value
self.properties = properties
if objectType.value[0] is None or type(value) == objectType.value[0]:
self.type = objectType
@@ -18,6 +19,7 @@ class Value:
return self.type.stringify(self.value)
def decompose(self):
from smnp.type.model import Type
if self.type != Type.LIST:
raise RuntimeError(f"Method 'decompose' can be applied only for lists")