Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, my_stars_home, mode):
self._db = TinyDB(os.path.join(my_stars_home, 'mystars.db'), storage=CachingMiddleware(JSONStorage))
if mode == 't':
self._db.purge_tables()
self._idx = self._db.table('index')
if not self._idx.contains(Query().name == 'language'):
self._idx.insert({
'name': 'language',
'docs': {}
})
if not self._idx.contains(Query().name == 'keyword'):
self._idx.insert({
'name': 'keyword',
'docs': {}
})
import os
import datetime
import tinydb
from tinydb import TinyDB, where
from wwb_scanner.utils import numpyjson
APP_PATH = os.path.expanduser('~/wwb_scanner_data')
class JSONStorage(tinydb.JSONStorage):
def read(self):
# Get the file size
self._handle.seek(0, os.SEEK_END)
size = self._handle.tell()
if not size:
# File is empty
return None
else:
self._handle.seek(0)
return numpyjson.load(self._handle)
def write(self, data):
self._handle.seek(0)
serialized = numpyjson.dumps(data)
self._handle.write(serialized)
self._handle.flush()
class _JsonRecord(StorageRecord):
eid_type = int
def __init__(self, database, element, eid=None):
super(_JsonRecord, self).__init__(database, eid or element.eid, element)
def __str__(self):
return json.dumps(self.element)
def __repr__(self):
return json.dumps(self.element)
class _JsonFileStorage(tinydb.JSONStorage):
"""Allow read-only as well as read-write access to the JSON file.
TinyDB's default storage (:any:`tinydb.JSONStorage`) assumes write access to the JSON file.
This isn't the case for system-level storage and possibly others.
"""
def __init__(self, path):
try:
super(_JsonFileStorage, self).__init__(path)
except IOError:
self.path = path
self._handle = open(path, 'r')
self.readonly = True
LOGGER.debug("'%s' opened read-only", path)
else:
self.readonly = False
LOGGER.debug("'%s' opened read-write", path)
def __init__(self, *args, **kwargs):
"""
Create a new instance of TinyDB.
All arguments and keyword arguments will be passed to the underlying
storage class (default: :class:`~tinydb.storages.JSONStorage`).
:param storage: The class of the storage to use. Will be initialized
with ``args`` and ``kwargs``.
"""
storage = kwargs.pop('storage', JSONStorage)
#: :type: Storage
self._storage = storage(*args, **kwargs)
self._table_cache = {}
self._table = self.table('_default')
class _JsonRecord(StorageRecord):
eid_type = int
def __init__(self, database, element, eid=None):
super(_JsonRecord, self).__init__(database, eid or element.eid, element)
def __str__(self):
return json.dumps(self)
def __repr__(self):
return json.dumps(self)
class _JsonFileStorage(tinydb.JSONStorage):
"""Allow read-only as well as read-write access to the JSON file.
TinyDB's default storage (:any:`tinydb.JSONStorage`) assumes write access to the JSON file.
This isn't the case for system-level storage and possibly others.
"""
def __init__(self, path):
try:
super(_JsonFileStorage, self).__init__(path)
except IOError:
self.path = path
self._handle = open(path, 'r')
self.readonly = True
LOGGER.debug("'%s' opened read-only", path)
else:
self.readonly = False
LOGGER.debug("'%s' opened read-write", path)
Args:
table_name (str): Name of the table to operate on.
"""
class JsonDatabase(AbstractDatabase):
"""A persistant, transactional record storage system.
Uses :py:class:`TinyDB` to store and retrieve data as JSON strings.
Attributes:
dbfile (str): Absolute path to database file.
database (TinyDB): Database handle.
"""
class JsonFileStorage(tinydb.JSONStorage):
"""Allow read-only as well as read-write access to the JSON file."""
def __init__(self, path):
try:
super(JsonDatabase.JsonFileStorage, self).__init__(path)
except IOError:
LOGGER.debug("Failed to open %s as read-write, attempting read-only", path)
self.path = path
self._handle = open(path, 'r')
self.readonly = True
else:
self.readonly = False
def write(self, *args, **kwargs):
if self.readonly:
raise ConfigurationError("Cannot write to '%s'" % self.path, "Check that you have `write` access.")
else: