hopefully fixed ci, mostly completed contactmanager.py

master
Kevin Froman 2019-02-17 12:20:10 -06:00
parent 47d5099e4d
commit 842a6e1edb
3 changed files with 52 additions and 11 deletions

View File

@ -86,4 +86,9 @@ class DiskAllocationReached(Exception):
# onionrsocket exceptions # onionrsocket exceptions
class MissingAddress(Exception): class MissingAddress(Exception):
pass
# Contact exceptions
class ContactDeleted(Exception):
pass pass

View File

@ -17,16 +17,20 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import os, json import os, json, onionrexceptions
from onionrusers import onionrusers from onionrusers import onionrusers
class ContactManager(onionrusers.OnionrUser): 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) super(ContactManager, self).__init__(coreInst, publicKey, saveUser=saveUser)
self.data = {}
self.dataDir = coreInst.dataDir + '/contacts/' self.dataDir = coreInst.dataDir + '/contacts/'
self.dataFile = coreInst.dataFile = publicKey + '.json' self.dataFile = '%s/contacts/%s.json' % (coreInst.dataDir, publicKey)
if not os.path.exists(self.dataFile): self.lastRead = 0
self.recordExpire = recordExpireSeconds
self.data = self._loadData()
self.deleted = False
if not os.path.exists(self.dataDir):
os.mkdir(self.dataDir) os.mkdir(self.dataDir)
def _writeData(self): def _writeData(self):
@ -34,10 +38,35 @@ class ContactManager(onionrusers.OnionrUser):
with open(self.dataFile, 'w') as dataFile: with open(self.dataFile, 'w') as dataFile:
dataFile.write(data) dataFile.write(data)
def set_info(self, key, value): def _loadData(self):
return self.lastRead = self._core._utils.getEpoch()
def add_contact(self): 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 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): def delete_contact(self):
return self.deleted = True
if os.path.exists(self.dataFile):
os.remove(self.dataFile)

View File

@ -2,8 +2,15 @@
cd onionr; cd onionr;
mkdir testdata; mkdir testdata;
ran=0 ran=0
close () {
rm -rf testdata;
exit 10;
}
for f in tests/*.py; do for f in tests/*.py; do
python3 "$f" || break # if needed python3 "$f" || close # if needed
let "ran++" let "ran++"
done done
rm -rf testdata; rm -rf testdata;