Skip to content
This repository
Browse code

Adds storage library, had only very basic testing

  • Loading branch information...
commit 389c6ac5b5d1f0a40ad5f0ad0cb9f7ec55729caa 1 parent 19ebba3
Jessica Tallon authored

Showing 2 changed files with 181 additions and 0 deletions. Show diff stats Hide diff stats

  1. 0  Libraries/__init__.py
  2. +181 0 Libraries/store.py
0  Libraries/__init__.py
No changes.
181 Libraries/store.py
... ... @@ -0,0 +1,181 @@
  1 +##
  2 +# Storage library
  3 +##
  4 +
  5 +import os, config, json
  6 +
  7 +class Store():
  8 + def __init__(self, name, data=None):
  9 + self.fpath = config.paths["databases"]
  10 + if self.fpath[-1] != "/":
  11 + self.fpath += "/" + name
  12 + else:
  13 + self.fpath += name
  14 + if data == None:
  15 + # Tries to load
  16 + if os.path.isfile(self.fpath):
  17 + self.__sfile = open(self.fpath)
  18 + data = self.__sfile.read()
  19 + data = json.loads(data)
  20 + self.__sfile.close()
  21 + else:
  22 + raise IOError
  23 + self.__type = type(data)
  24 + self.data = data
  25 + self.__sfile = None #holder
  26 + def __repr__(self):
  27 + return self.data.__str__()
  28 + def __str__(self):
  29 + return self.__repr__()
  30 + def __setitem__(self, key, value):
  31 + if type(self.data) == type({}) or type(self.data) == type([]):
  32 + self.data[key] = value
  33 + else:
  34 + raise AttributeError
  35 + def append(self, value):
  36 + if self.__type == type([]):
  37 + return self.data.append(value)
  38 + else:
  39 + raise AttributeError
  40 + def pop(self, index=-1):
  41 + if self.__type == type([]):
  42 + return self.data.pop(index)
  43 + else:
  44 + raise AttributeError
  45 + def sort(self, cmp=None, key=None, reverse=False):
  46 + if self.__type == type([]):
  47 + return self.data.sort(cmp, key, reverse)
  48 + else:
  49 + raise AttributeError
  50 + def index(self, value, start=0, stop=None):
  51 + if self.__type == type([]):
  52 + if stop == None:
  53 + return self.data[start:]
  54 + else:
  55 + return self.data[start:stop]
  56 + else:
  57 + raise AttributeError
  58 + def insert(self, index, object):
  59 + if self.__type == type([]):
  60 + return self.data.insert(index, object)
  61 + else:
  62 + raise AttributeError
  63 + def count(self, value):
  64 + if self.__type == type([]):
  65 + return self.data.count(value)
  66 + else:
  67 + raise AttributeError
  68 + def extend(self, iterable):
  69 + if self.__type == type([]):
  70 + return self.data.extend(iterable)
  71 + else:
  72 + raise AttributeError
  73 + def remove(self, value):
  74 + if self.__type == type([]):
  75 + return self.data.remove(value)
  76 + else:
  77 + raise AttributeError
  78 + def reverse(self):
  79 + if self.__type == type([]):
  80 + return self.data.reverse()
  81 + else:
  82 + raise AttributeError
  83 + def clear(self):
  84 + if self.__type == type({}):
  85 + return self.data.clear()
  86 + else:
  87 + raise AttributeError
  88 + def copy(self):
  89 + if self.__type == type({}):
  90 + return self.data.copy()
  91 + else:
  92 + raise AttributeError
  93 + def fromkeys(self, seq, value=None):
  94 + if self.__type == type({}):
  95 + return self.data.fromkeys(seq, value)
  96 + else:
  97 + raise AttributeError
  98 + def get(self, k, d=None):
  99 + if self.__type == type({}):
  100 + return self.data.get(k, d)
  101 + else:
  102 + raise AttributeError
  103 + def has_key(self, k):
  104 + if self.__type == type({}):
  105 + return self.data.has_key(k)
  106 + else:
  107 + raise AttributeError
  108 + def items(self):
  109 + if self.__type == type({}):
  110 + return self.data.items()
  111 + else:
  112 + raise AttributeError
  113 + def iteritems(self):
  114 + if self.__type == type({}):
  115 + return self.data.iteritems()
  116 + else:
  117 + raise AttributeError
  118 + def iterkeys(self):
  119 + if self.__type == type({}):
  120 + return self.data.iterkeys()
  121 + else:
  122 + raise AttributeError
  123 + def itervalues(self):
  124 + if self.__type == type({}):
  125 + return self.data.itervalues()
  126 + else:
  127 + raise AttributeError
  128 + def keys(self):
  129 + if self.__type == type({}):
  130 + return self.data.keys()
  131 + else:
  132 + raise AttributeError
  133 + def pop(self, k, *d):
  134 + if self.__type == type({}):
  135 + return self.data.pop(k, d)
  136 + else:
  137 + raise AttributeError
  138 + def popitem(self):
  139 + if self.__type == type({}):
  140 + return self.data.popitem()
  141 + else:
  142 + raise AttributeError
  143 + def setdefault(self, k, d=None):
  144 + if self.__type == type({}):
  145 + return self.data.setdefault(k, d)
  146 + else:
  147 + raise AttributeError
  148 + def update(self, e, **f):
  149 + if self.__type == type({}):
  150 + return self.data.update(e, F)
  151 + else:
  152 + raise AttributeError
  153 + def values(self):
  154 + if self.__type == type({}):
  155 + return self.data.values()
  156 + else:
  157 + raise AttributeError
  158 + def viewitems(self):
  159 + if self.__type == type({}):
  160 + return self.data.viewitems()
  161 + else:
  162 + raise AttributeError
  163 + def viewkeys(self):
  164 + if self.__type == type({}):
  165 + return self.data.viewkeys()
  166 + else:
  167 + raise AttributeError
  168 + def viewvalues(self):
  169 + if self.__type == type({}):
  170 + return self.data.viewvalues()
  171 + else:
  172 + raise AttributeError
  173 + def save(self):
  174 + foutd = json.dumps(self.data)
  175 + if os.path.isfile(self.fpath):
  176 + os.remove(self.fpath)
  177 + self.__sfile = open(self.fpath, "w")
  178 + self.__sfile.write(foutd)
  179 + self.__sfile.close()
  180 +
  181 +

0 comments on commit 389c6ac

Please sign in to comment.
Something went wrong with that request. Please try again.