python-objectbroker/objectbroker/ObjectStore.py

33 lines
816 B
Python

import os
class DiscObjectStore:
"""DiscObjectStore ist ein Mapping Objekt, welches ein Verzeichnis im Dateisystem als Speicher nutzt und somit persistent zwischen Programmstarts arbeitet"""
def __init__(self,path = "./objects"):
self._path = path
self.check()
def check(self):
if not os.path.exists( self._path ):
os.makedirs( self._path )
def __dir__(self):
fl = os.listdir(self._path)
l = []
for f in fl:
if not f.startswith("."):
l.append(f)
return l
def __contains__(self, name):
return os.path.exists( "%s/%s" % (self._path, name))
def __getitem__(self, name):
f = open("%s/%s" % (self._path, name), "rb")
v = f.read()
f.close()
return v
def __setitem__(self, name, value):
f = open("%s/%s" % (self._path, name), "wb")
v = f.write(value)
f.close()