Skip to content

Commit b645c89

Browse files
committed
Feature Added: JSON Export
1 parent 67dd93b commit b645c89

File tree

3 files changed

+111
-35
lines changed

3 files changed

+111
-35
lines changed

filexdb/database.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
from .collection import Collection
44
from .fileio import BinaryFileIO, JsonFileIO
5-
from .document import JsonArray
5+
from .document import JsonArray, Document
6+
67

78
class FileXdb:
89

@@ -18,7 +19,7 @@ def __init__(self, db_name: str, data_dir=None, mode="binary"):
1819
:param db_name: Name of Database without file extension.
1920
:param data_dir: Where the Database will be stored.
2021
"""
21-
self._database = {}
22+
self._database = Document({})
2223
self._db_name = db_name
2324
self._data_dir = data_dir
2425

@@ -54,4 +55,3 @@ def show_collections(self) -> JsonArray:
5455

5556
return _result
5657

57-

filexdb/document.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Mapping, List
22
import uuid
33
import json
4+
from .fileio import Export
45

56

67
def _get_id():
@@ -44,7 +45,7 @@ def prettify(self) -> str:
4445

4546

4647
class JsonArray(list):
47-
def __init__(self, _value:list) -> None:
48+
def __init__(self, _value: list) -> None:
4849
self.value = _value
4950
super().__init__(self.value)
5051

@@ -75,6 +76,16 @@ def count_item(self) -> int:
7576

7677

7778

79+
def export(self, _file_name, _file_dir=None, _mode="json"):
80+
"""
81+
82+
:param _file_name:
83+
:param _file_dir:
84+
:param _mode:
85+
:return:
86+
"""
87+
88+
e = Export(self.value, _file_name, _file_dir, _mode)
7889

7990

8091

filexdb/fileio.py

Lines changed: 96 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
__all__ = ("FileIO", "JsonFileIO", "BinaryFileIO")
88

9+
from typing import Tuple
910

10-
def create_db(db_name: str, data_dir: str = None):
11+
12+
def create_file(db_name: str, data_dir: str = None):
1113
"""
1214
Create a file if it doesn't exist yet.
1315
@@ -30,8 +32,32 @@ def create_db(db_name: str, data_dir: str = None):
3032

3133
# Create the file by opening it in "a" mode which creates the file if it
3234
# does not exist yet but does not modify its contents
33-
with open(_db_path, 'ab'):
34-
pass
35+
if not os.path.exists(_db_path):
36+
with open(_db_path, 'a'):
37+
pass
38+
39+
40+
def pre_process(ext: str, db_name: str, data_dir=None) -> tuple[str, str]:
41+
"""
42+
43+
:param ext:
44+
:param db_name:
45+
:param data_dir:
46+
:return:
47+
"""
48+
49+
# Adding ``FileXdb`` specific file extension to the Database-file.
50+
_file_name = f"{db_name}.{ext}"
51+
52+
# Setting default Database-file path.
53+
_file_full_path = _file_name
54+
55+
# Checking if Data Directory is on root or not.
56+
if data_dir is not None:
57+
# Creating Database-file full path by joining data_dir & db_name.
58+
_file_full_path = os.path.join(data_dir, _file_name)
59+
60+
return _file_name, _file_full_path
3561

3662

3763
class FileIO(ABC):
@@ -69,8 +95,20 @@ def write(self, data: dict):
6995

7096
raise NotImplementedError('To be overridden!')
7197

98+
@abstractmethod
99+
def get_export_path(self) -> str:
100+
"""
101+
Return Database path
102+
103+
:return: TDatabase path.
104+
"""
105+
106+
raise NotImplementedError('To be overridden!')
107+
108+
72109

73110
class BinaryFileIO(FileIO):
111+
74112
def __init__(self, db_name: str, data_dir=None):
75113
"""
76114
Create a new instance.
@@ -85,22 +123,11 @@ def __init__(self, db_name: str, data_dir=None):
85123

86124
super().__init__()
87125

88-
self._data_dir = data_dir
89-
90-
# Adding ``FileXdb`` specific file extension to the Database-file.
91-
self._db_name = f"{db_name}.fxdb"
92-
93-
# Setting default Database-file path.
94-
self._db_file_path = self._db_name
95-
96-
# Checking if Data Directory is on root or not.
97-
if self._data_dir is not None:
98-
99-
# Creating Database-file full path by joining data_dir & db_name.
100-
self._db_file_path = os.path.join(self._data_dir, self._db_name)
126+
self._db_name, self._db_file_path = pre_process("fxdb", db_name, data_dir)
101127

102128
# Create the Database/File if it doesn't exist
103-
create_db(self._db_name, self._data_dir)
129+
create_file(self._db_name, data_dir)
130+
104131

105132
def read(self) -> dict:
106133
"""
@@ -165,27 +192,19 @@ def write(self, data: dict) -> None:
165192
# Remove data that is behind the new cursor if the file has gotten shorter.
166193
file.truncate()
167194

195+
def get_export_path(self) -> str:
196+
return self._db_file_path
197+
168198

169199
class JsonFileIO(FileIO):
170-
def __init__(self, db_name:str, data_dir=None):
200+
def __init__(self, db_name: str, data_dir=None):
171201

172202
super().__init__()
173203

174-
self._data_dir = data_dir
175-
176-
# Adding ``JSON`` file extension to the Database-file.
177-
self._db_name = f"{db_name}.json"
178-
179-
# Setting default Database-file path.
180-
self._db_file_path = self._db_name
181-
182-
# Checking if Data Directory is on root or not.
183-
if self._data_dir is not None:
184-
# Creating Database-file full path by joining data_dir & db_name.
185-
self._db_file_path = os.path.join(self._data_dir, self._db_name)
204+
self._db_name, self._db_file_path = pre_process("json", db_name, data_dir)
186205

187206
# Create the Database/File if it doesn't exist
188-
create_db(self._db_name, self._data_dir)
207+
create_file(self._db_name, data_dir)
189208

190209
def read(self) -> dict:
191210
"""
@@ -249,3 +268,49 @@ def write(self, data: dict):
249268

250269
# Remove data that is behind the new cursor if the file has gotten shorter.
251270
file.truncate()
271+
272+
def get_export_path(self) -> str:
273+
return self._db_file_path
274+
275+
276+
class Export:
277+
def __init__(self, _data, _file_name=None, _file_dir=None, _mode="json") -> None:
278+
"""
279+
Exports data into different readable file.
280+
281+
:param _file_name: Where to export.
282+
:param _file_dir: Parent dir.
283+
:param _data: Data to export.
284+
:param _mode: Export to which mode.
285+
"""
286+
287+
# Caching arguments
288+
self.data = _data
289+
self.file_name = _file_name
290+
self.file_dir = _file_dir
291+
self.mode = _mode
292+
293+
self._db_name, self._db_file_path = pre_process(self.mode, self.file_name, self.file_dir)
294+
295+
# Create the Database/File if it doesn't exist
296+
create_file(self._db_name, self.file_dir)
297+
298+
# check mode
299+
if self.mode == "json":
300+
self.to_json(self.data, self._db_file_path)
301+
else:
302+
raise TypeError(f"`{self.mode}` is not a appropriate mode to export")
303+
304+
305+
306+
def to_json(self, _data, _file_path) -> None:
307+
"""
308+
Exports data into JSON file.
309+
310+
:param _data: Data to export.
311+
:param _file_path: Where to export.
312+
:return: None
313+
"""
314+
with open(_file_path, "w") as f:
315+
json.dump(_data, fp=f, indent=4)
316+

0 commit comments

Comments
 (0)