2022-03-07 23:52:01 +01:00

45 lines
1.5 KiB
Python

# -*- coding: utf-8 -*-
# Copyright 2016-2017 Mads Michelsen (mail@brokkr.net)
# This file is part of Derailleur.
# Poca 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.
"""Jar exists only to save any object handed to it (although it defaults to
a dictionary). Likewise, once configured it will return said object when
called upon by the load method."""
import os
import pickle
class Jar:
'''Class for retrieving and saving feed/manager entry info'''
def __init__(self, config, db_filename):
self.db_filepath = os.path.join(config.dir, db_filename)
def load(self):
'''Retrieve contents of pickle'''
if not os.path.isfile(self.db_filepath):
pickle_contents = {}
success = self.save(pickle_contents)
if not success:
return False
try:
with open(self.db_filepath, mode='rb') as f:
pickle_contents = pickle.load(f)
except (PermissionError, pickle.UnpicklingError, EOFError):
return False
return pickle_contents
def save(self, pickle_contents):
'''Saves pickle_contents to file using pickle'''
try:
with open(self.db_filepath, 'wb') as f:
pickle.dump(pickle_contents, f)
return True
except (PermissionError, pickle.PicklingError, EOFError):
return False