From 842a6e1edb185eeb3d58df8a4fabac45c2bb16d2 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 17 Feb 2019 12:20:10 -0600 Subject: [PATCH] hopefully fixed ci, mostly completed contactmanager.py --- onionr/onionrexceptions.py | 5 +++ onionr/onionrusers/contactmanager.py | 49 ++++++++++++++++++++++------ run_tests.sh | 9 ++++- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/onionr/onionrexceptions.py b/onionr/onionrexceptions.py index b3710dbe..757091db 100755 --- a/onionr/onionrexceptions.py +++ b/onionr/onionrexceptions.py @@ -86,4 +86,9 @@ class DiskAllocationReached(Exception): # onionrsocket exceptions class MissingAddress(Exception): + pass + +# Contact exceptions + +class ContactDeleted(Exception): pass \ No newline at end of file diff --git a/onionr/onionrusers/contactmanager.py b/onionr/onionrusers/contactmanager.py index 958869f1..c47bbe45 100644 --- a/onionr/onionrusers/contactmanager.py +++ b/onionr/onionrusers/contactmanager.py @@ -17,16 +17,20 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import os, json +import os, json, onionrexceptions from onionrusers import onionrusers class ContactManager(onionrusers.OnionrUser): - def __init__(self, coreInst, publicKey, saveUser=False): + def __init__(self, coreInst, publicKey, saveUser=False, recordExpireSeconds=5): super(ContactManager, self).__init__(coreInst, publicKey, saveUser=saveUser) - self.data = {} self.dataDir = coreInst.dataDir + '/contacts/' - self.dataFile = coreInst.dataFile = publicKey + '.json' - if not os.path.exists(self.dataFile): + self.dataFile = '%s/contacts/%s.json' % (coreInst.dataDir, publicKey) + self.lastRead = 0 + self.recordExpire = recordExpireSeconds + self.data = self._loadData() + self.deleted = False + + if not os.path.exists(self.dataDir): os.mkdir(self.dataDir) def _writeData(self): @@ -34,10 +38,35 @@ class ContactManager(onionrusers.OnionrUser): with open(self.dataFile, 'w') as dataFile: dataFile.write(data) - def set_info(self, key, value): - return - def add_contact(self): + def _loadData(self): + self.lastRead = self._core._utils.getEpoch() + retData = {} + if os.path.exists(self.dataFile): + with open(self.dataFile, 'r') as dataFile: + retData = json.loads(dataFile.read()) + return retData + + def set_info(self, key, value, autoWrite=True): + if self.deleted: + raise onionrexceptions.ContactDeleted + + self.data[key] = value + if autoWrite: + self._writeData() return + + def get_info(self, key, forceReload=False): + if self.deleted: + raise onionrexceptions.ContactDeleted + + if (self._core._utils.getEpoch() - self.lastRead >= self.recordExpire) or forceReload: + self.data = self._loadData() + try: + return self.data[key] + except KeyError: + return None + def delete_contact(self): - return - \ No newline at end of file + self.deleted = True + if os.path.exists(self.dataFile): + os.remove(self.dataFile) \ No newline at end of file diff --git a/run_tests.sh b/run_tests.sh index f32868d3..d0a60b5b 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -2,8 +2,15 @@ cd onionr; mkdir testdata; ran=0 + + +close () { + rm -rf testdata; + exit 10; +} + for f in tests/*.py; do - python3 "$f" || break # if needed + python3 "$f" || close # if needed let "ran++" done rm -rf testdata;