Skip to content

Details

Creation and management of the database.

Examples:

>>> from scruby import Scruby
>>> db = Scruby()
>>> await db.set_key("key name", "Some text")
None
>>> await db.get_key("key name")
"Some text"
>>> await db.has_key("key name")
True
>>> await db.delete_key("key name")
None
>>> await db.napalm()
None

Scruby

Creation and management of the database.

Examples:

>>> from scruby import Scruby
>>> db = Scruby()
>>> await db.set_key("key name", "Some text")
None
>>> await db.get_key("key name")
"Some text"
>>> await db.has_key("key name")
True
>>> await db.delete_key("key name")
None
>>> await db.napalm()
None

Parameters:

Name Type Description Default
db_path str

Path to root directory of databases. Defaule by = "ScrubyDB" (in root of project)

'ScrubyDB'
Source code in src\scruby\db.py
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
class Scruby:
    """Creation and management of the database.

    Examples:
        >>> from scruby import Scruby
        >>> db = Scruby()
        >>> await db.set_key("key name", "Some text")
        None
        >>> await db.get_key("key name")
        "Some text"
        >>> await db.has_key("key name")
        True
        >>> await db.delete_key("key name")
        None
        >>> await db.napalm()
        None

    Args:
        db_path: Path to root directory of databases. Defaule by = "ScrubyDB" (in root of project)
    """

    def __init__(  # noqa: D107
        self,
        db_path: str = "ScrubyDB",
    ) -> None:
        super().__init__()
        self.__db_path = db_path

    @property
    def db_path(self) -> str:
        """Get database name."""
        return self.__db_path

    async def get_leaf_path(self, key: str) -> Path:
        """Get the path to the database cell by key.

        Args:
            key: Key name.
        """
        # Key to md5 sum.
        key_md5: str = hashlib.md5(key.encode("utf-8")).hexdigest()  # noqa: S324
        # Convert md5 sum in the segment of path.
        segment_path_md5: str = "/".join(list(key_md5))
        # The path of the branch to the database.
        branch_path: Path = Path(
            *(self.__db_path, segment_path_md5),
        )
        # If the branch does not exist, need to create it.
        if not await branch_path.exists():
            await branch_path.mkdir(parents=True)
        # The path to the database cell.
        leaf_path: Path = Path(*(branch_path, "leaf.json"))
        return leaf_path

    async def set_key(
        self,
        key: str,
        value: ValueOfKey,
    ) -> None:
        """Asynchronous method for adding and updating keys to database.

        Examples:
            >>> from scruby import Scruby
            >>> db = Scruby()
            >>> await db.set_key("key name", "Some text")
            None

        Args:
            key: Key name.
            value: Value of key.
        """
        # The path to the database cell.
        leaf_path: Path = await self.get_leaf_path(key)
        # Write key-value to the database.
        if await leaf_path.exists():
            # Add new key or update existing.
            data_json: bytes = await leaf_path.read_bytes()
            data: dict = orjson.loads(data_json) or {}
            data[key] = value
            await leaf_path.write_bytes(orjson.dumps(data))
        else:
            # Add new key to a blank leaf.
            await leaf_path.write_bytes(data=orjson.dumps({key: value}))

    async def get_key(self, key: str) -> ValueOfKey:
        """Asynchronous method for getting key from database.

        Examples:
            >>> from scruby import Scruby
            >>> db = Scruby()
            >>> await db.set_key("key name", "Some text")
            None
            >>> await db.get_key("key name")
            "Some text"
            >>> await db.get_key("key missing")
            KeyError

        Args:
            key: Key name.
        """
        # The path to the database cell.
        leaf_path: Path = await self.get_leaf_path(key)
        # Get value of key.
        if await leaf_path.exists():
            data_json: bytes = await leaf_path.read_bytes()
            data: dict = orjson.loads(data_json) or {}
            return data[key]
        raise KeyError()

    async def has_key(self, key: str) -> bool:
        """Asynchronous method for checking presence of  key in database.

        Examples:
            >>> from scruby import Scruby
            >>> db = Scruby()
            >>> await db.set_key("key name", "Some text")
            None
            >>> await db.has_key("key name")
            True
            >>> await db.has_key("key missing")
            False

        Args:
            key: Key name.
        """
        # The path to the database cell.
        leaf_path: Path = await self.get_leaf_path(key)
        # Checking whether there is a key.
        if await leaf_path.exists():
            data_json: bytes = await leaf_path.read_bytes()
            data: dict = orjson.loads(data_json) or {}
            try:
                data[key]
                return True
            except KeyError:
                return False
        return False

    async def delete_key(self, key: str) -> None:
        """Asynchronous method for deleting key from database.

        Examples:
            >>> from scruby import Scruby
            >>> db = Scruby()
            >>> await db.set_key("key name", "Some text")
            None
            >>> await db.delete_key("key name")
            None
            >>> await db.delete_key("key missing")
            KeyError

        Args:
            key: Key name.
        """
        # The path to the database cell.
        leaf_path: Path = await self.get_leaf_path(key)
        # Deleting key.
        if await leaf_path.exists():
            data_json: bytes = await leaf_path.read_bytes()
            data: dict = orjson.loads(data_json) or {}
            del data[key]
            await leaf_path.write_bytes(orjson.dumps(data))
            return
        raise KeyError()

    async def napalm(self) -> None:
        """Asynchronous method for full database deletion (Arg: db_path).

        Warning:
            - `Be careful, this will remove all keys.`

        Examples:
            >>> from scruby import Scruby
            >>> db = Scruby()
            >>> await db.set_key("key name", "Some text")
            None
            >>> await db.napalm()
            None
            >>> await db.napalm()
            FileNotFoundError
        """
        await to_thread.run_sync(rmtree, self.__db_path)
        return

db_path property

Get database name.

delete_key(key) async

Asynchronous method for deleting key from database.

Examples:

>>> from scruby import Scruby
>>> db = Scruby()
>>> await db.set_key("key name", "Some text")
None
>>> await db.delete_key("key name")
None
>>> await db.delete_key("key missing")
KeyError

Parameters:

Name Type Description Default
key str

Key name.

required
Source code in src\scruby\db.py
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
async def delete_key(self, key: str) -> None:
    """Asynchronous method for deleting key from database.

    Examples:
        >>> from scruby import Scruby
        >>> db = Scruby()
        >>> await db.set_key("key name", "Some text")
        None
        >>> await db.delete_key("key name")
        None
        >>> await db.delete_key("key missing")
        KeyError

    Args:
        key: Key name.
    """
    # The path to the database cell.
    leaf_path: Path = await self.get_leaf_path(key)
    # Deleting key.
    if await leaf_path.exists():
        data_json: bytes = await leaf_path.read_bytes()
        data: dict = orjson.loads(data_json) or {}
        del data[key]
        await leaf_path.write_bytes(orjson.dumps(data))
        return
    raise KeyError()

get_key(key) async

Asynchronous method for getting key from database.

Examples:

>>> from scruby import Scruby
>>> db = Scruby()
>>> await db.set_key("key name", "Some text")
None
>>> await db.get_key("key name")
"Some text"
>>> await db.get_key("key missing")
KeyError

Parameters:

Name Type Description Default
key str

Key name.

required
Source code in src\scruby\db.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
async def get_key(self, key: str) -> ValueOfKey:
    """Asynchronous method for getting key from database.

    Examples:
        >>> from scruby import Scruby
        >>> db = Scruby()
        >>> await db.set_key("key name", "Some text")
        None
        >>> await db.get_key("key name")
        "Some text"
        >>> await db.get_key("key missing")
        KeyError

    Args:
        key: Key name.
    """
    # The path to the database cell.
    leaf_path: Path = await self.get_leaf_path(key)
    # Get value of key.
    if await leaf_path.exists():
        data_json: bytes = await leaf_path.read_bytes()
        data: dict = orjson.loads(data_json) or {}
        return data[key]
    raise KeyError()

get_leaf_path(key) async

Get the path to the database cell by key.

Parameters:

Name Type Description Default
key str

Key name.

required
Source code in src\scruby\db.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
async def get_leaf_path(self, key: str) -> Path:
    """Get the path to the database cell by key.

    Args:
        key: Key name.
    """
    # Key to md5 sum.
    key_md5: str = hashlib.md5(key.encode("utf-8")).hexdigest()  # noqa: S324
    # Convert md5 sum in the segment of path.
    segment_path_md5: str = "/".join(list(key_md5))
    # The path of the branch to the database.
    branch_path: Path = Path(
        *(self.__db_path, segment_path_md5),
    )
    # If the branch does not exist, need to create it.
    if not await branch_path.exists():
        await branch_path.mkdir(parents=True)
    # The path to the database cell.
    leaf_path: Path = Path(*(branch_path, "leaf.json"))
    return leaf_path

has_key(key) async

Asynchronous method for checking presence of key in database.

Examples:

>>> from scruby import Scruby
>>> db = Scruby()
>>> await db.set_key("key name", "Some text")
None
>>> await db.has_key("key name")
True
>>> await db.has_key("key missing")
False

Parameters:

Name Type Description Default
key str

Key name.

required
Source code in src\scruby\db.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
async def has_key(self, key: str) -> bool:
    """Asynchronous method for checking presence of  key in database.

    Examples:
        >>> from scruby import Scruby
        >>> db = Scruby()
        >>> await db.set_key("key name", "Some text")
        None
        >>> await db.has_key("key name")
        True
        >>> await db.has_key("key missing")
        False

    Args:
        key: Key name.
    """
    # The path to the database cell.
    leaf_path: Path = await self.get_leaf_path(key)
    # Checking whether there is a key.
    if await leaf_path.exists():
        data_json: bytes = await leaf_path.read_bytes()
        data: dict = orjson.loads(data_json) or {}
        try:
            data[key]
            return True
        except KeyError:
            return False
    return False

napalm() async

Asynchronous method for full database deletion (Arg: db_path).

Warning
  • Be careful, this will remove all keys.

Examples:

>>> from scruby import Scruby
>>> db = Scruby()
>>> await db.set_key("key name", "Some text")
None
>>> await db.napalm()
None
>>> await db.napalm()
FileNotFoundError
Source code in src\scruby\db.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
async def napalm(self) -> None:
    """Asynchronous method for full database deletion (Arg: db_path).

    Warning:
        - `Be careful, this will remove all keys.`

    Examples:
        >>> from scruby import Scruby
        >>> db = Scruby()
        >>> await db.set_key("key name", "Some text")
        None
        >>> await db.napalm()
        None
        >>> await db.napalm()
        FileNotFoundError
    """
    await to_thread.run_sync(rmtree, self.__db_path)
    return

set_key(key, value) async

Asynchronous method for adding and updating keys to database.

Examples:

>>> from scruby import Scruby
>>> db = Scruby()
>>> await db.set_key("key name", "Some text")
None

Parameters:

Name Type Description Default
key str

Key name.

required
value ValueOfKey

Value of key.

required
Source code in src\scruby\db.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
async def set_key(
    self,
    key: str,
    value: ValueOfKey,
) -> None:
    """Asynchronous method for adding and updating keys to database.

    Examples:
        >>> from scruby import Scruby
        >>> db = Scruby()
        >>> await db.set_key("key name", "Some text")
        None

    Args:
        key: Key name.
        value: Value of key.
    """
    # The path to the database cell.
    leaf_path: Path = await self.get_leaf_path(key)
    # Write key-value to the database.
    if await leaf_path.exists():
        # Add new key or update existing.
        data_json: bytes = await leaf_path.read_bytes()
        data: dict = orjson.loads(data_json) or {}
        data[key] = value
        await leaf_path.write_bytes(orjson.dumps(data))
    else:
        # Add new key to a blank leaf.
        await leaf_path.write_bytes(data=orjson.dumps({key: value}))