-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatypes.py
64 lines (48 loc) · 1.56 KB
/
datatypes.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
from . import constants
import os
class DataType:
def __init__(self, _type, data):
self.__type = _type
self.__data = data
def get_type(self):
return self.__type
def get_data(self):
return self.__data
class Bytes(DataType):
def __init__(self, data):
DataType.__init__(self, constants.BYTES, data)
def length(self):
return len(self.get_data())
class Text(DataType):
def __init__(self, text):
DataType.__init__(self, constants.TEXT, text.decode("utf-8"))
def length(self):
return len(self.get_data())
class File(DataType):
def __init__(self, name, size, data, _type):
DataType.__init__(self, _type, data)
self.__name = name
self.__size = size
def save(self, path=None):
if not path:
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), self.__name)
with open(path, 'wb') as f:
f.write(self.get_data())
def get_name(self):
return self.__name
def get_size(self):
return self.__size
def get_extension(self):
tokens = self.__name.split('.')
return tokens[len(tokens)-1]
class SavedFile(File):
def __init__(self, name, size):
File.__init__(self, name, size, None, constants.SAVED_FILE)
class Object(DataType):
def __init__(self, data):
DataType.__init__(self, constants.OBJECT, data)
class List(DataType):
def __init__(self, data):
DataType.__init__(self, constants.LIST, data)
def length(self):
return len(self.get_data())