Poodletooth-iLand/otp/otpbase/BackupManager.py

30 lines
942 B
Python
Raw Normal View History

2015-03-03 16:10:12 -06:00
import json
import os
class BackupManager:
2015-05-29 05:03:48 -05:00
def __init__(self, filepath='dependencies/backups/', extension='.json'):
2015-03-03 16:10:12 -06:00
self.filepath = filepath
self.extension = extension
def getFileName(self, category, info):
filename = os.path.join(self.filepath, category) + '/'
for i in info:
filename += str(i) + '_'
return filename[:-1] + self.extension
def load(self, category, info, default=None):
filename = self.getFileName(category, info)
if not os.path.exists(filename):
return default
with open(filename, 'r') as f:
return json.load(f)
def save(self, category, info, data):
filepath = os.path.join(self.filepath, category)
if not os.path.exists(filepath):
os.makedirs(filepath)
filename = self.getFileName(category, info)
with open(filename, 'w') as f:
json.dump(data, f)