-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
107 lines (74 loc) · 2.07 KB
/
utils.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
98
99
100
101
102
103
104
105
106
107
import os
import copy
import cPickle as pickle
# Constantes de posicionamiento de figuras
AL_FINAL = 1
DEBAJO = 2
ENCIMA = 3
# Constantes de mensajes
ALERTA_VERSION = """
La version del archivo guardado difiere de la version de Erdos.
Esto podria ocasionarle problemas
"""
class Singleton(type):
_inst = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._inst:
cls._inst[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._inst[cls]
class Config(object):
VERSION = 1.4
ANCHO = 350
ALTO = 350
class Archivo(object):
def __init__(self, path, arbol):
self.path = path
self.arbol = arbol
self.version = Config.VERSION
self.nombre = os.path.basename(path)
class IdHandler(object):
uid = 0
@classmethod
def get(cls):
cls.uid += 1
return cls.uid
class Historial(object):
def __init__(self):
self.historia = []
self._indice = -1
def guardar(self, arbol):
self._indice += 1
self.historia = self.historia[:self._indice]
self.historia.append(copy.deepcopy(arbol))
def deshacer(self):
if self._indice > 0:
self._indice -= 1
return self._get()
return False
def rehacer(self):
if self._indice != len(self.historia) - 1:
self._indice += 1
return self._get()
return False
def _get(self):
return self.historia[self._indice]
def unpack_list(lista):
return lista[0], lista[1::]
def path_ui(archivo_ui):
path = "gui" # pasar a un config!
return os.path.join(path, archivo_ui)
def guardar_objeto(obj, path):
archivo = Archivo(path, obj)
try:
with open(path, 'wb') as salida:
pickle.dump(archivo, salida, pickle.HIGHEST_PROTOCOL)
except Exception as err:
raise err
return archivo
def cargar_objeto(path):
try:
with open(path, 'rb') as entrada:
archivo = pickle.load(entrada)
except Exception as err:
raise err
return archivo