Skip to content

Commit

Permalink
bugfix: constants
Browse files Browse the repository at this point in the history
bugfix: access and usage of contansts
feature: added samples usages
feature: update readme
  • Loading branch information
klich3 committed Jan 2, 2024
1 parent 1dec0a1 commit 08e5851
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 208 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__pycache__
src/Rocketstore/test
.venv
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ keywords:
- file
- rocket
license: MIT
version: 0.0.6
version: 0.0.7
4 changes: 3 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ include README.md
include *.toml
recursive-include src *.py
recursive-include src/Rocketstore *.py
recursive-include src/Rocketstore/utils *.py
recursive-include src/Rocketstore/utils *.py
prune */__pycache__
recursive-exclude Samples/*
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,31 @@ Compare Rocket-Store, SQL and file system terms:
To use Rocketstore, you must first import the library:

```python
from Rocketstore import Rocketstore, _FORMAT_JSON
from Rocketstore import Rocketstore

rs = Rocketstore()
```

usage of constants:
```python

#method 1:
rs = Rocketstore()
rs.post(..., rs._FORMAT_JSON)

#or

rs.post(..., Rocketstore._FORMAT_JSON)

```


### Post

```python
rs.post(collection="delete_fodders1", key="1", record={"some":"json input"}, flags=_FORMAT_JSON)
rs.post(collection="delete_fodders1", key="1", record={"some":"json input"}, flags=Rocketstore._FORMAT_JSON)
# or
rs.post("delete_fodders1", "1", {"some":"json input"}, _FORMAT_JSON)
rs.post("delete_fodders1", "1", {"some":"json input"}, Rocketstore._FORMAT_JSON)
```

Stores a record in a collection identified by a unique key
Expand Down Expand Up @@ -153,10 +166,10 @@ __Options__:
* data_format: Specify which format the records are stored in. Values are: _FORMAT_NATIVE - default. and RS_FORMAT_JSON - Use JSON data format.

```python
rs.options(data_format=_FORMAT_JSON)
rs.options(data_format=Rocketstore._FORMAT_JSON)
# or
rs.options(**{
"data_format": _FORMAT_JSON,
"data_format": Rocketstore._FORMAT_JSON,
...
})
```
Expand Down
6 changes: 0 additions & 6 deletions Samples/Post/README.md

This file was deleted.

22 changes: 0 additions & 22 deletions Samples/Post/test.py

This file was deleted.

Binary file removed dist/Rocket-Store-0.0.5.tar.gz
Binary file not shown.
Binary file removed dist/Rocket_Store-0.0.5-py3-none-any.whl
Binary file not shown.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "Rocket-Store"
version = "0.0.6"
version = "0.0.7"
authors = [
{ name="Anton Sychev", email="[email protected]" },
]
Expand Down
29 changes: 23 additions & 6 deletions src/Rocket_Store.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: Rocket-Store
Version: 0.0.5
Version: 0.0.7
Summary: Using the filesystem as a searchable database.
Author-email: Anton Sychev <[email protected]>
License: Rocket Store
Expand Down Expand Up @@ -90,18 +90,31 @@ Compare Rocket-Store, SQL and file system terms:
To use Rocketstore, you must first import the library:

```python
from Rocketstore import Rocketstore, _FORMAT_JSON
from Rocketstore import Rocketstore

rs = Rocketstore()
```

usage of constants:
```python

#method 1:
rs = Rocketstore()
rs.post(..., rs._FORMAT_JSON)

#or

rs.post(..., Rocketstore._FORMAT_JSON)

```


### Post

```python
rs.post(collection="delete_fodders1", key="1", record={"some":"json input"}, flags=_FORMAT_JSON)
rs.post(collection="delete_fodders1", key="1", record={"some":"json input"}, flags=Rocketstore._FORMAT_JSON)
# or
rs.post("delete_fodders1", "1", {"some":"json input"}, _FORMAT_JSON)
rs.post("delete_fodders1", "1", {"some":"json input"}, Rocketstore._FORMAT_JSON)
```

Stores a record in a collection identified by a unique key
Expand Down Expand Up @@ -195,10 +208,10 @@ __Options__:
* data_format: Specify which format the records are stored in. Values are: _FORMAT_NATIVE - default. and RS_FORMAT_JSON - Use JSON data format.

```python
rs.options(data_format=_FORMAT_JSON)
rs.options(data_format=Rocketstore._FORMAT_JSON)
# or
rs.options(**{
"data_format": _FORMAT_JSON,
"data_format": Rocketstore._FORMAT_JSON,
...
})
```
Expand All @@ -224,9 +237,13 @@ Contributions are welcome. Please open an issue to discuss what you would like t

### Publish to Pypi

***Local:***
```shell
python -m pip install build twine
python3 -m build
twine check dist/*
twine upload dist/*
```

***Live:***
No need do nothing GitHub have Workflow action its publish auto
8 changes: 0 additions & 8 deletions src/Rocket_Store.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ LICENSE
MANIFEST.in
README.md
pyproject.toml
src/RocketStore/RocketStore.py
src/RocketStore/Rocketstore.py
src/RocketStore/__init__.py
src/RocketStore/__version__.py
src/RocketStore/example.py
src/RocketStore/utils/__init__.py
src/RocketStore/utils/files.py
src/Rocket_Store.egg-info/PKG-INFO
src/Rocket_Store.egg-info/SOURCES.txt
src/Rocket_Store.egg-info/dependency_links.txt
Expand All @@ -18,7 +11,6 @@ src/Rocket_Store.egg-info/top_level.txt
src/Rocketstore/Rocketstore.py
src/Rocketstore/__init__.py
src/Rocketstore/__version__.py
src/Rocketstore/example.py
src/Rocketstore/utils/__init__.py
src/Rocketstore/utils/files.py
tests/test.py
63 changes: 32 additions & 31 deletions src/Rocketstore/Rocketstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@

from .utils.files import file_lock, file_unlock, identifier_name_test, file_name_wash

# Constants
_ORDER = 0x01 # Sort ASC
_ORDER_DESC = 0x02 # Sort DESC
_ORDERBY_TIME = 0x04 # Sort by time not implemented
_LOCK = 0x08 # Lock file
_DELETE = 0x10 # Delete file / collection / database
_KEYS = 0x20 # Return keys only
_COUNT = 0x40 # Return count only
_ADD_AUTO_INC = 0x01 # Add auto incrementing sequence to key
_ADD_GUID = 0x02 # Add Globally Unique IDentifier to key (RFC 4122)
_FORMAT_JSON = 0x01 # Store data in JSON format
_FORMAT_NATIVE = 0x02 # Store data in native format (JSON)
_FORMAT_XML = 0x04 # Store data in XML format
_FORMAT_PHP = 0x08 # Store data in PHP format

# TODO: new binary json format
# _FORMAT_BSON = 0x09 #https://en.wikipedia.org/wiki/BSON
# _FORMAT_PROTOBUF = 0x10 #https://protobuf.dev/
Expand All @@ -59,12 +44,27 @@

class Rocketstore:

# Constants
_ORDER = 0x01 # Sort ASC
_ORDER_DESC = 0x02 # Sort DESC
_ORDERBY_TIME = 0x04 # Sort by time not implemented
_LOCK = 0x08 # Lock file
_DELETE = 0x10 # Delete file / collection / database
_KEYS = 0x20 # Return keys only
_COUNT = 0x40 # Return count only
_ADD_AUTO_INC = 0x01 # Add auto incrementing sequence to key
_ADD_GUID = 0x02 # Add Globally Unique IDentifier to key (RFC 4122)
_FORMAT_JSON = 0x01 # Store data in JSON format
_FORMAT_NATIVE = 0x02 # Store data in native format (JSON)
_FORMAT_XML = 0x04 # Store data in XML format
_FORMAT_PHP = 0x08 # Store data in PHP format

data_storage_area: str = os.path.join(os.path.sep, "tmp", "rsdb")

def __init__(self, **set_option) -> None:
# https://docs.python.org/es/dev/library/tempfile.html
# TODO: use tempdir
self.data_format = _FORMAT_JSON
self.data_format = self._FORMAT_JSON
self.lock_retry_interval = 13
self.lock_files = True
self.key_cache = {}
Expand All @@ -76,16 +76,17 @@ def options(self, **options) -> None:
'''
inital setup of Rocketstore
@Sample:
from Rocketstore import Rocketstore, _FORMAT_JSON, _FORMAT_NATIVE
from Rocketstore import Rocketstore
rs.options(**{
"data_storage_area": "./",
"data_format": _FORMAT_NATIVE
"data_format": Rocketstore._FORMAT_NATIVE
})
'''

if "data_format" in options:
if options["data_format"] in [_FORMAT_JSON, _FORMAT_XML, _FORMAT_NATIVE]:
self.data_format = options.get("data_format", _FORMAT_JSON)
if options["data_format"] in [self._FORMAT_JSON, self._FORMAT_XML, self._FORMAT_NATIVE]:
self.data_format = options.get(
"data_format", self._FORMAT_JSON)
else:
raise ValueError(
f"Unknown data format: '{options['data_format']}'")
Expand Down Expand Up @@ -143,12 +144,12 @@ def post(self, collection=None, key=None, record=None, flags=0) -> any:
key = ""

# Insert a sequence
if len(key) < 1 or flags & _ADD_AUTO_INC:
if len(key) < 1 or flags & self._ADD_AUTO_INC:
_sequence = self.sequence(collection)
key = f"{_sequence}-{key}" if key else str(_sequence)

# Insert a Globally Unique IDentifier
if flags & _ADD_GUID:
if flags & self._ADD_GUID:
uid = hex(int(os.urandom(8).hex(), 16))[2:]
guid = f"{uid[:8]}-{uid[8:12]}-4000-8{uid[12:15]}-{uid[15:]}"
key = f"{guid}-{key}" if len(key) > 0 else guid
Expand All @@ -158,7 +159,7 @@ def post(self, collection=None, key=None, record=None, flags=0) -> any:
os.path.join(self.data_storage_area, collection))
file_name = os.path.join(dir_to_write, key)

if self.data_format & _FORMAT_JSON:
if self.data_format & self._FORMAT_JSON:
os.makedirs(dir_to_write, mode=0o775, exist_ok=True)

with open(file_name, "w") as file:
Expand Down Expand Up @@ -209,7 +210,7 @@ def get(self, collection=None, key=None, flags=0, min_time=None, max_time=None)

wildcard = not "*" in key or not "?" in key or key == "" or not key

if wildcard and not (flags & _DELETE and (not key or key == "")):
if wildcard and not (flags & self._DELETE and (not key or key == "")):
_list = []

# Read directory into cache
Expand Down Expand Up @@ -238,9 +239,9 @@ def get(self, collection=None, key=None, flags=0, min_time=None, max_time=None)
keys = _list

# Order by key value
if flags & (_ORDER | _ORDER_DESC) and keys and len(keys) > 1 and not (flags & (_DELETE | (flags & _COUNT))):
if flags & (self._ORDER | self._ORDER_DESC) and keys and len(keys) > 1 and not (flags & (self._DELETE | (flags & self._COUNT))):
keys.sort()
if flags & _ORDER_DESC:
if flags & self._ORDER_DESC:
keys.reverse()
else:
if collection and isinstance(self.key_cache.get(collection), list) and key not in self.key_cache[collection]:
Expand All @@ -250,14 +251,14 @@ def get(self, collection=None, key=None, flags=0, min_time=None, max_time=None)

count = len(keys)

if len(keys) > 0 and collection and not (flags & (_KEYS | _COUNT | _DELETE)):
if len(keys) > 0 and collection and not (flags & (self._KEYS | self._COUNT | self._DELETE)):
records = [None] * len(keys)

for i in range(len(keys)):
file_name = os.path.join(scan_dir, keys[i])

# Read JSON record file
if self.data_format & _FORMAT_JSON:
if self.data_format & self._FORMAT_JSON:
try:
with open(file_name, 'r') as file:
records[i] = json.load(file)
Expand All @@ -271,7 +272,7 @@ def get(self, collection=None, key=None, flags=0, min_time=None, max_time=None)
raise ValueError(
"Sorry, that data format is not supported")

elif flags & _DELETE:
elif flags & self._DELETE:
# DELETE RECORDS
print(f"276 DELETE: c({collection}) k({key})")

Expand Down Expand Up @@ -351,7 +352,7 @@ def get(self, collection=None, key=None, flags=0, min_time=None, max_time=None)
records = [e for e in records if e != "*deleted*"]

result = {'count': count}
if result['count'] and keys and not (flags & (_COUNT | _DELETE)):
if result['count'] and keys and not (flags & (self._COUNT | self._DELETE)):
result['key'] = keys
if records:
result['result'] = records
Expand All @@ -362,7 +363,7 @@ def delete(self, collection=None, key=None):
'''
Delete one or more records or collections
'''
return self.get(collection=collection, key=key, flags=_DELETE)
return self.get(collection=collection, key=key, flags=self._DELETE)

def sequence(self, seq_name: str) -> int:
'''
Expand Down
2 changes: 1 addition & 1 deletion src/Rocketstore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
__version__,
)

from .Rocketstore import Rocketstore, _ORDER, _ORDER_DESC, _ORDERBY_TIME, _LOCK, _DELETE, _KEYS, _COUNT, _ADD_AUTO_INC, _ADD_GUID, _FORMAT_JSON, _FORMAT_NATIVE, _FORMAT_XML, _FORMAT_PHP
from .Rocketstore import Rocketstore
6 changes: 3 additions & 3 deletions src/Rocketstore/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
Desc: Versions
"""

__title__ = "RocketStore"
__title__ = "Rocketstore"
__description__ = "Rocket Store (Python) - Fast and Simple Database"
__url__ = "https://github.com/klich3/rocket-store-python"
__version__ = "0.0.6"
__build__ = 0x000022
__version__ = "0.0.7"
__build__ = 0x000029
__author__ = "Anton Sychev"
__author_email__ = "[email protected]"
__copyright__ = "Copyright Simon Riget"
Loading

0 comments on commit 08e5851

Please sign in to comment.