mirror of
https://github.com/Xevion/bulk-reminders.git
synced 2025-12-11 02:06:45 -06:00
Switch to JSONPickle, Implement stage additions and indexing & pair hashing
This commit is contained in:
1
Pipfile
1
Pipfile
@@ -14,6 +14,7 @@ dateutil = "*"
|
|||||||
python-dateutil = "*"
|
python-dateutil = "*"
|
||||||
pytz = "*"
|
pytz = "*"
|
||||||
tzlocal = "*"
|
tzlocal = "*"
|
||||||
|
jsonpickle = "*"
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.7"
|
python_version = "3.7"
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from typing import List
|
from typing import Iterator, List
|
||||||
|
|
||||||
|
import jsonpickle
|
||||||
|
|
||||||
|
|
||||||
class HistoryManager(object):
|
class HistoryManager(object):
|
||||||
@@ -18,13 +20,13 @@ class HistoryManager(object):
|
|||||||
|
|
||||||
def load(self) -> None:
|
def load(self) -> None:
|
||||||
"""Load data from the undo history file"""
|
"""Load data from the undo history file"""
|
||||||
with open(self.file, 'r') as history:
|
with open(self.file, 'r') as file:
|
||||||
self.stages = json.load(history)
|
self.stages = jsonpickle.decode(file.read())
|
||||||
|
|
||||||
def save(self) -> None:
|
def save(self) -> None:
|
||||||
"""Save data to the undo history file."""
|
"""Save data to the undo history file."""
|
||||||
with open(self.file, 'w') as history:
|
with open(self.file, 'w') as file:
|
||||||
json.dump(self.stages, history)
|
file.write(jsonpickle.encode(self.stages))
|
||||||
|
|
||||||
def getTotal(self) -> int:
|
def getTotal(self) -> int:
|
||||||
"""Returns the total number of undoable events known."""
|
"""Returns the total number of undoable events known."""
|
||||||
@@ -32,16 +34,39 @@ class HistoryManager(object):
|
|||||||
|
|
||||||
def exists(self, id: 'IDPair') -> int:
|
def exists(self, id: 'IDPair') -> int:
|
||||||
"""Check if a given ID exists anywhere in the undo history data. Returns the stage index or -1 if it wasn't found."""
|
"""Check if a given ID exists anywhere in the undo history data. Returns the stage index or -1 if it wasn't found."""
|
||||||
|
print(f'Checking for {id} in undo history')
|
||||||
for stage in self.stages:
|
for stage in self.stages:
|
||||||
for stageID in stage.events:
|
for undoable in stage.events:
|
||||||
if id == stageID:
|
if id == undoable.eventID:
|
||||||
return stage.index
|
return stage.index
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
def all_pairs(self) -> Iterator['IDPair']:
|
||||||
|
"""Generator for every IDPair object within the master HistoryManager"""
|
||||||
|
for stage in self.stages:
|
||||||
|
for event in stage.events:
|
||||||
|
yield event
|
||||||
|
|
||||||
|
def __len__(self) -> int:
|
||||||
|
"""Returns the number of stages"""
|
||||||
|
return len(self.stages)
|
||||||
|
|
||||||
|
def nextIndex(self):
|
||||||
|
"""Gets the next index (for a new stage)"""
|
||||||
|
if len(self.stages) == 0:
|
||||||
|
return 0
|
||||||
|
return self.stages[0].index + 1
|
||||||
|
|
||||||
|
def addStage(self, newStage: 'Stage'):
|
||||||
|
"""Adds and inserts a new Stage at the start of the history."""
|
||||||
|
self.stages.insert(0, newStage)
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
|
||||||
class Stage(object):
|
class Stage(object):
|
||||||
def __init__(self, index: int) -> None:
|
def __init__(self, index: int, commonCalendar: str) -> None:
|
||||||
self.index = index
|
self.index = index
|
||||||
|
self.commonCalendar = commonCalendar
|
||||||
self.events: List[IDPair] = []
|
self.events: List[IDPair] = []
|
||||||
|
|
||||||
def __contains__(self, item) -> bool:
|
def __contains__(self, item) -> bool:
|
||||||
@@ -65,3 +90,8 @@ class IDPair(object):
|
|||||||
elif type(other) is tuple:
|
elif type(other) is tuple:
|
||||||
return len(other) == 2 and other == (self.calendarID, self.eventID)
|
return len(other) == 2 and other == (self.calendarID, self.eventID)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
"""Returns a hash value for the IDPair"""
|
||||||
|
return hash((self.calendarID, self.eventID))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user