From a6719abed78a221f6f263ea2b70d593603f1f109 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Thu, 23 Aug 2018 09:01:17 -0500 Subject: [PATCH] added storagecounter.py --- onionr/storagecounter.py | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 onionr/storagecounter.py diff --git a/onionr/storagecounter.py b/onionr/storagecounter.py new file mode 100644 index 00000000..50456a24 --- /dev/null +++ b/onionr/storagecounter.py @@ -0,0 +1,61 @@ +''' + Onionr - P2P Microblogging Platform & Social network. + + Keeps track of how much disk space we're using +''' +''' + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +''' +import config + +class StorageCounter: + def __init__(self, coreInst): + self._core = coreInst + self.dataFile = self._core.usageFile + return + + def isFull(self): + retData = False + if self._core.config.get('allocations.disk') >= self.getAmount(): + retData = True + return retData + + def _update(self, data): + with open(self.dataFile, 'w') as dataFile: + dataFile.write(str(data)) + def getAmount(self, data): + '''Return how much disk space we're using (according to record)''' + retData = 0 + try: + with open(self.dataFile, 'w') as dataFile: + retData = int(dataFile.read()) + except FileNotFoundError: + pass + return retData + + def addBytes(self, amount): + '''Record that we are now using more disk space, unless doing so would exceed configured max''' + newAmount = amount + self.getAmount() + retData = newAmount + if newAmount > self._core.config.get('allocations.disk'): + retData = False + else: + self._update(newAmount) + return retData + + def removeBytes(self, amount): + '''Record that we are now using less disk space''' + newAmount = self.getAmount() - amount + self._update(newAmount) + return newAmount \ No newline at end of file