Add read-only dictdata

This commit is contained in:
minerobber 2019-05-27 04:37:24 +00:00
parent 96985ca2dd
commit ba4048d6d2
1 changed files with 15 additions and 0 deletions

View File

@ -1,4 +1,7 @@
import plugin
class ReadOnlyError(Exception):
pass
class DictData(plugin.JSONData):
def __init__(self,filename,**kwargs):
plugin.JSONData.__init__(self,kwargs)
@ -14,3 +17,15 @@ class DictData(plugin.JSONData):
return k in self.value
def get(self,k,default=None):
return self.value.get(k,default)
class ReadOnlyDictData(DictData):
def __setitem__(self,k,v):
raise ReadOnlyError("Cannot set item {} to {}".format(k,v))
class Numerical(ReadOnlyDictData):
def load(self,filename):
super(Numerical,self).load(filename)
n = {}
for k in self.value:
n[int(k)]=self.value[k]
self.value = n