Spaces:
Runtime error
Runtime error
File size: 5,920 Bytes
4d5bdd2 | 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | import time
import inspect
class Note:
def __init__(self, parentfad, id, typeid, value=None) -> None:
self.id = id
self.timestamp = time.time()
self.typeid = typeid
#print(f"constructor: {self.type()} id {id} timestamp {self.timestamp}")
self.parentfad = parentfad
self.properties = {}
if value is not None:
self.properties["value"] = value
def type(self):
return notetypes.inverse[self.typeid][0]
def get(self, name=None):
propertyname = name if name is not None else "value"
if propertyname in self.properties.keys():
value = self.properties[propertyname] # "value" is default property
else:
value = None
if isinstance(value, int): # any int value is supposed to be a ref to id; if you need to store numbers, use floats
return self.parentfad.notes[value].get() # so we pass it to the target mark, recursively, but without name: target note gives us just its value
elif isinstance(value, list):
return [(self.parentfad.notes[v].get() if isinstance(v, int) else v) for v in value if v] # do the same for each element in list
else:
return value
def get_string(self, name=None):
value = self.get(name)
if value == None:
return ''
elif isinstance(value, list):
return ", ".join(value)
else:
return value
def set(self, value):
if isinstance(value, int): # any int value is supposed to be a ref to id
assert(value in self.parentfad.notes.keys()) # so we ensure this id exists
self.properties["value"] = value
self.timestamp = time.time()
#print(f"set value: {self.type()} id {id} timestamp {self.timestamp}")
def set_property(self, name, value):
if isinstance(value, int): # any int value is supposed to be a ref to id
assert(value in self.parentfad.notes.keys()) # so we ensure this id exists
self.properties[name] = value
self.timestamp = time.time()
#print(f"set name value: {self.type()} id {id} timestamp {self.timestamp}")
def set_property_newnote(self, name, value, valuetype):
existing = self.parentfad.get_by_type_and_value(valuetype, value)
if existing:
note = existing[0] # we assume there's only one
else:
note = self.parentfad.newnote(valuetype, value)
self.properties[name] = note.id
self.timestamp = time.time()
#print(f"set name value: {self.type()} id {id} timestamp {self.timestamp}")
return note
def add_to_property(self, name, value):
if isinstance(value, int): # any int value is supposed to be a ref to id
if value not in self.parentfad.notes.keys(): # so we ensure this id exists
raise(Exception(f"Note: {inspect.stack()[0][3]}: trying to add an id ({value}) which does not point to a Note"))
elif value == None:
raise(Exception(f"Note: {inspect.stack()[0][3]}: trying to add a None value"))
if not name in self.properties.keys() or self.properties[name] is None: # no such property yet, or it's None: create list
self.properties[name] = [value]
elif isinstance(self.properties[name], list): # already a list
self.properties[name].append(value)
else: # not a list nor None, create list from the existing value and new one
self.properties[name] = [self.properties[name], value]
self.timestamp = time.time()
#print(f"add: {self.type()} id {id} timestamp {self.timestamp}")
def add_to_property_newnote(self, name, value, valuetype):
if value == None:
raise(Exception(f"Note: {inspect.stack()[0][3]}: trying to add a None value"))
existing = self.parentfad.get_by_type_and_value(valuetype, value)
if existing:
note = existing[0] # we assume there's only one
else:
note = self.parentfad.newnote(valuetype, value)
if not name in self.properties.keys() or self.properties[name] is None: # no such property yet, or it's None: create list
self.properties[name] = [note.id]
elif isinstance(self.properties[name], list): # already a list
self.properties[name].append(note.id)
else: # not a list nor None, create list from the existing value and new one
self.properties[name] = [self.properties[name], note.id]
self.timestamp = time.time()
return note
def get_refs(self):
refs = []
for key, note in self.parentfad.notes.items():
for pkey, pvalue in note.properties.items():
if isinstance(pvalue, int):
if pvalue == self.id:
refs.append(key)
return refs
def touch(self):
self.timestamp = time.time()
def is_newer(self, target: 'Note'): # type: ignore
return self.timestamp >= target.timestamp
def needs_updating_from(self):
refs_to_update = []
for key, value in self.properties.items():
if isinstance(value, int):
if not(self.is_newer(self.parentfad.notes[value])):
print(f"i refer to something newer than me! i need updating!\n me: {self.type()} at {self.timestamp}\n refer to: {self.parentfad.notes[value].type()} at {self.parentfad.notes[value].timestamp}")
refs_to_update.append(value)
return refs_to_update
def delete(self):
if self.get_refs():
raise(Exception(f"Note: {inspect.stack()[0][3]}: cannot delete a note that has references to it: from {self.get_refs()}"))
else:
self.parentfad.notes.pop(self.id)
|