Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dirdbm.py 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. # -*- test-case-name: twisted.test.test_dirdbm -*-
  2. #
  3. # Copyright (c) Twisted Matrix Laboratories.
  4. # See LICENSE for details.
  5. """
  6. DBM-style interface to a directory.
  7. Each key is stored as a single file. This is not expected to be very fast or
  8. efficient, but it's good for easy debugging.
  9. DirDBMs are *not* thread-safe, they should only be accessed by one thread at
  10. a time.
  11. No files should be placed in the working directory of a DirDBM save those
  12. created by the DirDBM itself!
  13. Maintainer: Itamar Shtull-Trauring
  14. """
  15. import base64
  16. import glob
  17. import os
  18. import pickle
  19. from twisted.python.filepath import FilePath
  20. try:
  21. _open # type: ignore[has-type]
  22. except NameError:
  23. _open = open
  24. class DirDBM:
  25. """
  26. A directory with a DBM interface.
  27. This class presents a hash-like interface to a directory of small,
  28. flat files. It can only use strings as keys or values.
  29. """
  30. def __init__(self, name):
  31. """
  32. @type name: str
  33. @param name: Base path to use for the directory storage.
  34. """
  35. self.dname = os.path.abspath(name)
  36. self._dnamePath = FilePath(name)
  37. if not self._dnamePath.isdir():
  38. self._dnamePath.createDirectory()
  39. else:
  40. # Run recovery, in case we crashed. we delete all files ending
  41. # with ".new". Then we find all files who end with ".rpl". If a
  42. # corresponding file exists without ".rpl", we assume the write
  43. # failed and delete the ".rpl" file. If only a ".rpl" exist we
  44. # assume the program crashed right after deleting the old entry
  45. # but before renaming the replacement entry.
  46. #
  47. # NOTE: '.' is NOT in the base64 alphabet!
  48. for f in glob.glob(self._dnamePath.child("*.new").path):
  49. os.remove(f)
  50. replacements = glob.glob(self._dnamePath.child("*.rpl").path)
  51. for f in replacements:
  52. old = f[:-4]
  53. if os.path.exists(old):
  54. os.remove(f)
  55. else:
  56. os.rename(f, old)
  57. def _encode(self, k):
  58. """
  59. Encode a key so it can be used as a filename.
  60. """
  61. # NOTE: '_' is NOT in the base64 alphabet!
  62. return base64.encodebytes(k).replace(b"\n", b"_").replace(b"/", b"-")
  63. def _decode(self, k):
  64. """
  65. Decode a filename to get the key.
  66. """
  67. return base64.decodebytes(k.replace(b"_", b"\n").replace(b"-", b"/"))
  68. def _readFile(self, path):
  69. """
  70. Read in the contents of a file.
  71. Override in subclasses to e.g. provide transparently encrypted dirdbm.
  72. """
  73. with _open(path.path, "rb") as f:
  74. s = f.read()
  75. return s
  76. def _writeFile(self, path, data):
  77. """
  78. Write data to a file.
  79. Override in subclasses to e.g. provide transparently encrypted dirdbm.
  80. """
  81. with _open(path.path, "wb") as f:
  82. f.write(data)
  83. f.flush()
  84. def __len__(self):
  85. """
  86. @return: The number of key/value pairs in this Shelf
  87. """
  88. return len(self._dnamePath.listdir())
  89. def __setitem__(self, k, v):
  90. """
  91. C{dirdbm[k] = v}
  92. Create or modify a textfile in this directory
  93. @type k: bytes
  94. @param k: key to set
  95. @type v: bytes
  96. @param v: value to associate with C{k}
  97. """
  98. if not type(k) == bytes:
  99. raise TypeError("DirDBM key must be bytes")
  100. if not type(v) == bytes:
  101. raise TypeError("DirDBM value must be bytes")
  102. k = self._encode(k)
  103. # We create a new file with extension .new, write the data to it, and
  104. # if the write succeeds delete the old file and rename the new one.
  105. old = self._dnamePath.child(k)
  106. if old.exists():
  107. new = old.siblingExtension(".rpl") # Replacement entry
  108. else:
  109. new = old.siblingExtension(".new") # New entry
  110. try:
  111. self._writeFile(new, v)
  112. except BaseException:
  113. new.remove()
  114. raise
  115. else:
  116. if old.exists():
  117. old.remove()
  118. new.moveTo(old)
  119. def __getitem__(self, k):
  120. """
  121. C{dirdbm[k]}
  122. Get the contents of a file in this directory as a string.
  123. @type k: bytes
  124. @param k: key to lookup
  125. @return: The value associated with C{k}
  126. @raise KeyError: Raised when there is no such key
  127. """
  128. if not type(k) == bytes:
  129. raise TypeError("DirDBM key must be bytes")
  130. path = self._dnamePath.child(self._encode(k))
  131. try:
  132. return self._readFile(path)
  133. except (OSError):
  134. raise KeyError(k)
  135. def __delitem__(self, k):
  136. """
  137. C{del dirdbm[foo]}
  138. Delete a file in this directory.
  139. @type k: bytes
  140. @param k: key to delete
  141. @raise KeyError: Raised when there is no such key
  142. """
  143. if not type(k) == bytes:
  144. raise TypeError("DirDBM key must be bytes")
  145. k = self._encode(k)
  146. try:
  147. self._dnamePath.child(k).remove()
  148. except (OSError):
  149. raise KeyError(self._decode(k))
  150. def keys(self):
  151. """
  152. @return: a L{list} of filenames (keys).
  153. """
  154. return list(map(self._decode, self._dnamePath.asBytesMode().listdir()))
  155. def values(self):
  156. """
  157. @return: a L{list} of file-contents (values).
  158. """
  159. vals = []
  160. keys = self.keys()
  161. for key in keys:
  162. vals.append(self[key])
  163. return vals
  164. def items(self):
  165. """
  166. @return: a L{list} of 2-tuples containing key/value pairs.
  167. """
  168. items = []
  169. keys = self.keys()
  170. for key in keys:
  171. items.append((key, self[key]))
  172. return items
  173. def has_key(self, key):
  174. """
  175. @type key: bytes
  176. @param key: The key to test
  177. @return: A true value if this dirdbm has the specified key, a false
  178. value otherwise.
  179. """
  180. if not type(key) == bytes:
  181. raise TypeError("DirDBM key must be bytes")
  182. key = self._encode(key)
  183. return self._dnamePath.child(key).isfile()
  184. def setdefault(self, key, value):
  185. """
  186. @type key: bytes
  187. @param key: The key to lookup
  188. @param value: The value to associate with key if key is not already
  189. associated with a value.
  190. """
  191. if key not in self:
  192. self[key] = value
  193. return value
  194. return self[key]
  195. def get(self, key, default=None):
  196. """
  197. @type key: bytes
  198. @param key: The key to lookup
  199. @param default: The value to return if the given key does not exist
  200. @return: The value associated with C{key} or C{default} if not
  201. L{DirDBM.has_key(key)}
  202. """
  203. if key in self:
  204. return self[key]
  205. else:
  206. return default
  207. def __contains__(self, key):
  208. """
  209. @see: L{DirDBM.has_key}
  210. """
  211. return self.has_key(key)
  212. def update(self, dict):
  213. """
  214. Add all the key/value pairs in L{dict} to this dirdbm. Any conflicting
  215. keys will be overwritten with the values from L{dict}.
  216. @type dict: mapping
  217. @param dict: A mapping of key/value pairs to add to this dirdbm.
  218. """
  219. for key, val in dict.items():
  220. self[key] = val
  221. def copyTo(self, path):
  222. """
  223. Copy the contents of this dirdbm to the dirdbm at C{path}.
  224. @type path: L{str}
  225. @param path: The path of the dirdbm to copy to. If a dirdbm
  226. exists at the destination path, it is cleared first.
  227. @rtype: C{DirDBM}
  228. @return: The dirdbm this dirdbm was copied to.
  229. """
  230. path = FilePath(path)
  231. assert path != self._dnamePath
  232. d = self.__class__(path.path)
  233. d.clear()
  234. for k in self.keys():
  235. d[k] = self[k]
  236. return d
  237. def clear(self):
  238. """
  239. Delete all key/value pairs in this dirdbm.
  240. """
  241. for k in self.keys():
  242. del self[k]
  243. def close(self):
  244. """
  245. Close this dbm: no-op, for dbm-style interface compliance.
  246. """
  247. def getModificationTime(self, key):
  248. """
  249. Returns modification time of an entry.
  250. @return: Last modification date (seconds since epoch) of entry C{key}
  251. @raise KeyError: Raised when there is no such key
  252. """
  253. if not type(key) == bytes:
  254. raise TypeError("DirDBM key must be bytes")
  255. path = self._dnamePath.child(self._encode(key))
  256. if path.isfile():
  257. return path.getModificationTime()
  258. else:
  259. raise KeyError(key)
  260. class Shelf(DirDBM):
  261. """
  262. A directory with a DBM shelf interface.
  263. This class presents a hash-like interface to a directory of small,
  264. flat files. Keys must be strings, but values can be any given object.
  265. """
  266. def __setitem__(self, k, v):
  267. """
  268. C{shelf[foo] = bar}
  269. Create or modify a textfile in this directory.
  270. @type k: str
  271. @param k: The key to set
  272. @param v: The value to associate with C{key}
  273. """
  274. v = pickle.dumps(v)
  275. DirDBM.__setitem__(self, k, v)
  276. def __getitem__(self, k):
  277. """
  278. C{dirdbm[foo]}
  279. Get and unpickle the contents of a file in this directory.
  280. @type k: bytes
  281. @param k: The key to lookup
  282. @return: The value associated with the given key
  283. @raise KeyError: Raised if the given key does not exist
  284. """
  285. return pickle.loads(DirDBM.__getitem__(self, k))
  286. def open(file, flag=None, mode=None):
  287. """
  288. This is for 'anydbm' compatibility.
  289. @param file: The parameter to pass to the DirDBM constructor.
  290. @param flag: ignored
  291. @param mode: ignored
  292. """
  293. return DirDBM(file)
  294. __all__ = ["open", "DirDBM", "Shelf"]