Guest User

Untitled

a guest
Apr 30th, 2023
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. ```py
  2. import os
  3. import ujson as json
  4. import hashlib
  5. import zlib
  6.  
  7. class JTSON(object):
  8. def __init__(self, location):
  9. self.location = os.path.expanduser(location) # This is the path and filename of the database file
  10. self.load(self.location)
  11.  
  12. def __repr__(self):
  13. return "Your JTSON database at {}".format(str(self.location))
  14.  
  15. def load(self, location):
  16. """ Loads the database from file into memory if file exists by calling _load(self) else, ccreates a new one"""
  17. if os.path.exists(location):
  18. self._load()
  19. else:
  20. self.db = {}
  21. return True
  22.  
  23. def _load(self):
  24. """ Loads the database file into memory using Json format"""
  25. with open(self.location, 'r') as f:
  26. data = f.read()
  27. if data:
  28. compressed_data = bytes.fromhex(data) #Create a bytes object from a string of hexadecimal numbers.
  29. decompressed_data = zlib.decompress(compressed_data) #The functions in this module allow compression and decompression using the zlib library, which is based on GNU zip.
  30. self.db = json.loads(decompressed_data.decode())
  31. else:
  32. self.db = {}
  33.  
  34. def dumpdb(self):
  35. """ Writes the memory database content into a file """
  36. data = json.dumps(self.db).encode()
  37. compressed_data = zlib.compress(data)
  38. with open(self.location, "w+b") as f:
  39. f.write(compressed_data.hex().encode())
  40. return True
  41. def set(self, key, value):
  42. """ Adds the key value pair to database """
  43. try:
  44. key_hash = hashlib.sha256(key.encode()).hexdigest()
  45. if not isinstance(value, str):
  46. value = str(value)
  47. self.db[key_hash] = value
  48. self.dumpdb()
  49. return True
  50. except Exception as e:
  51. print("[X] Error Saving Values to Database : "+str(e))
  52. return False
  53.  
  54. def get(self, key):
  55. """ Gets the value corresponding to the key """
  56. try:
  57. key_hash = hashlib.sha256(key.encode()).hexdigest() #Hashlib secure hashes and message digests. In this we use sha256() to create a SHA-256 hash object.
  58. return self.db[key_hash]
  59. except KeyError:
  60. print("No Value Can Be Found for "+ str(key))
  61. return False
  62.  
  63. def get_all(self):
  64. """ Returns all key value pairs in database """
  65. return self.db
  66.  
  67. def delete(self, key):
  68. """ removes the key and related value pair from database"""
  69. key_hash = hashlib.sha256(key.encode()).hexdigest()
  70. if not key_hash in self.db:
  71. return False
  72. else:
  73. del self.db[key_hash]
  74. self.dumpdb()
  75. return True
  76. def flush(self):
  77. """ Clears entire database """
  78. a = input("Do you really want to do that? \n"
  79. "Enter Y for yes or any other input for No")
  80. if not a=="Y" or a == "y":
  81. self.db = {}
  82. self.dumpdb()
  83. return True
  84. return "Flush Aborted"
  85. ```
Advertisement
Add Comment
Please, Sign In to add comment