Skip to content

Commit

Permalink
Update and modify files for the project
Browse files Browse the repository at this point in the history
- Added new files:
  - src/bee_py/utils/collection_node.py
  - tests/data/1.txt
  - tests/data/2.txt
  - tests/data/empty
  - tests/data/sub/3.txt
  - "tests/data/sub/\303\251"
  - "tests/data/sub/\360\237\230\216"
  - tests/test_files/bee_data.json
  - tests/test_files/test_account.json
- Updated files:
  - pyproject.toml
  - src/bee_py/modules/bzz.py
  - src/bee_py/modules/pss.py
  - src/bee_py/modules/tag.py
  - src/bee_py/types/type.py
  - src/bee_py/utils/bytes.py
  - src/bee_py/utils/collection.py
  - src/bee_py/utils/tar.py
  - tests/conftest.py
  - tests/data/bee_data.json
  - tests/data/test_account.json
  - tests/integration/chunk/test_soc.py
  - tests/integration/modules/conftest.py
  - tests/integration/modules/test_bzz.py
  • Loading branch information
Aviksaikat committed Nov 28, 2023
1 parent a9b5fe7 commit 6044a4c
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 95 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ number = true
[tool.flake8]
max-line-length= 120
ignore = ["E203", "W503"]
exclude = ["__pycache__", ".venv/*"]
exclude = ["__pycache__", ".venv/*", "funky_experiments/*"]
docstring-convention = "google"

# mypy
[tool.mypy]
exclude = ["build/", "dist/", "docs/"]
exclude = ["build/", "dist/", "docs/", "funky_experiments/*"]
check_untyped_defs = true

[tool.mypy-pytest]
Expand Down
36 changes: 14 additions & 22 deletions src/bee_py/modules/bzz.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from typing import Optional, Union

from bee_py.types.type import ( # Reference,; UploadHeaders,; Data,; CollectionEntry,
from bee_py.types.type import ( # Reference,; UploadHeaders,; Data,; CollectionEntry,; CollectionUploadHeaders,; FileUploadHeaders, # noqa: E501
BatchId,
BeeRequestOptions,
Collection,
CollectionUploadHeaders,
CollectionUploadOptions,
FileData,
FileHeaders,
FileUploadHeaders,
FileUploadOptions,
Reference,
ReferenceOrENS,
Expand All @@ -25,16 +23,16 @@
BZZ_ENDPOINT = "bzz"


def extract_file_upload_headers(
postage_batch_id: BatchId, options: Optional[FileUploadOptions] = None
) -> FileUploadHeaders:
def extract_file_upload_headers(postage_batch_id: BatchId, options: Optional[FileUploadOptions] = None) -> dict:
headers = extract_upload_headers(postage_batch_id, options)

options = FileUploadOptions.parse_obj(options)

if options and options.size:
headers.content_length = str(options.size)
headers["content-length"] = str(options.size)

if options and options.content_type:
headers.content_type = options.content_type
headers["content-type"] = options.content_type

return headers

Expand All @@ -45,7 +43,7 @@ def upload_file(
postage_batch_id: BatchId,
name: Optional[str] = None,
options: Optional[FileUploadOptions] = None,
):
) -> UploadResult:
"""
Uploads a single file to the Bee node.
Expand All @@ -60,8 +58,8 @@ def upload_file(
UploadResult: The result of the upload operation.
"""

if options.content_type:
options = options or {}
if not options or isinstance(options, dict):
options = FileUploadOptions.parse_obj(options)
options.content_type = "application/octet-stream"

headers = extract_file_upload_headers(postage_batch_id, options)
Expand Down Expand Up @@ -110,9 +108,9 @@ def download_file(request_options: BeeRequestOptions, _hash: ReferenceOrENS, pat
logger.error(response.raise_for_status())

file_headers = FileHeaders.parse_obj(read_file_headers(response.headers))
file_data = wrap_bytes_with_helpers(response.content).text()
file_data = wrap_bytes_with_helpers(response.content)

return FileData(headers=file_headers, data=file_data)
return FileData(headers=file_headers, data=file_data.data)


def download_file_readable(request_options: BeeRequestOptions, _hash: ReferenceOrENS, path: str = "") -> FileData:
Expand Down Expand Up @@ -162,10 +160,10 @@ def extract_collection_upload_headers(
options = CollectionUploadOptions.parse_obj(options)

if options and options.index_document:
headers["swarm_index_document"] = options.index_document
headers["swarm-index-document"] = options.index_document

if options and options.error_document:
headers["swarm_error_document"] = options.error_document
headers["swarm-error-document"] = options.error_document

return headers

Expand All @@ -174,7 +172,7 @@ def upload_collection(
request_options: BeeRequestOptions,
collection: Union[Collection, list],
postage_batch_id: BatchId,
options: Optional[CollectionUploadOptions] = None,
options: Optional[Union[CollectionUploadOptions, dict]] = None,
) -> UploadResult:
"""
Uploads a collection of data to the Bee node.
Expand All @@ -192,12 +190,6 @@ def upload_collection(

assert_collection(collection)

# if isinstance(collection, list):
# new_collection = []
# for item in collection:
# new_collection.append(CollectionEntry.parse_obj(item))
# collection = Collection(entries=new_collection)

tar_data = make_tar(collection)

headers = {
Expand Down
2 changes: 1 addition & 1 deletion src/bee_py/modules/pss.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import websockets

from bee_py.types.type import BatchId, BeeRequestOptions, Pin, Reference
from bee_py.types.type import BatchId, BeeRequestOptions
from bee_py.utils.headers import extract_upload_headers
from bee_py.utils.http import http
from bee_py.utils.logging import logger
Expand Down
29 changes: 27 additions & 2 deletions src/bee_py/modules/tag.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from typing import Optional

from bee_py.types.type import BeeRequestOptions, Reference, Tag
from bee_py.types.type import BeeRequestOptions, Tag
from bee_py.utils.http import http
from bee_py.utils.logging import logger

TAGS_ENDPOINT = "tags"


def create_tag(request_options: BeeRequestOptions, address: Optional[str]) -> Tag:
def create_tag(request_options: BeeRequestOptions, address: Optional[str] = None) -> Tag:
"""
Creates a new tag in the Bee node.
Expand All @@ -32,3 +32,28 @@ def create_tag(request_options: BeeRequestOptions, address: Optional[str]) -> Ta

tag_response = response.json()
return Tag.parse_obj(tag_response)


def retrieve_tag(request_options: BeeRequestOptions, uid: int) -> Tag:
"""
Retrieves tag information from Bee node
Args:
request_options (BeeRequestOptions): Bee Ky Options for making requests.
uid (int): UID of tag to be retrieved
Returns:
Tag: The retrieved tag.
"""
config = {
"url": f"{TAGS_ENDPOINT}/{uid}",
"method": "GET",
}
response = http(request_options, config)

if response.status_code != 200: # noqa: PLR2004
logger.info(response.json())
logger.error(response.raise_for_status())

tag_response = response.json()
return Tag.parse_obj(tag_response)
21 changes: 8 additions & 13 deletions src/bee_py/types/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
from requests import PreparedRequest, Response
from typing_extensions import TypeAlias

from bee_py.utils.hex import bytes_to_hex
# from bee_py.utils.hex import bytes_to_hex

# Befine all the types here
# Define all the types here

Type = TypeVar("Type")
Name = TypeVar("Name")
Length = TypeVar("Length", bound=int)
T: TypeAlias = str
T: TypeAlias = Union[str, bytes]

BeeRequestOptions = dict[str, Optional[Union[str, int, dict[str, str], PreparedRequest, Response]]]

Expand Down Expand Up @@ -198,19 +198,15 @@ def value(self) -> T:
return self.__value


class Data:
"""A class representing binary data with additional helper methods."""

def __init__(self, data):
self.data = data
class Data(BaseModel):
data: bytes

def text(self) -> str:
"""Converts the binary data using UTF-8 decoding into string.
Returns:
The decoded string.
"""

return self.data.decode("utf-8")

def hex(self) -> str: # noqa: A003
Expand All @@ -219,8 +215,7 @@ def hex(self) -> str: # noqa: A003
Returns:
The hexadecimal string representation of the data.
"""

return bytes_to_hex(self.data)
return self.data.hex()

def json(self) -> dict[str, Any]:
"""Converts the binary data into string which is then parsed into JSON.
Expand Down Expand Up @@ -730,7 +725,7 @@ class UploadOptions(BaseModel):

class FileUploadOptions(UploadOptions):
size: Optional[int] = None
content_type: Optional[str] = Field(None, alias="contentType")
content_type: Optional[str] = None


class CollectionEntry(BaseModel):
Expand All @@ -749,7 +744,7 @@ class CollectionUploadOptions(UploadOptions):

class FileData(FileHeaders, BaseModel):
headers: FileHeaders
data: T
data: bytes


class UploadHeaders(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion src/bee_py/utils/bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def wrap_bytes_with_helpers(data: bytes) -> Data:
msg = "Data must be a byte array."
raise TypeError(msg)

return Data(data)
return Data(data=data)


def bytes_at_offset(data: bytes, offset: int, length: Length) -> bytes:
Expand Down
12 changes: 5 additions & 7 deletions src/bee_py/utils/collection.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# from bee_py.types.type import Collection
# from bee_py.utils.error import BeeArgumentError
from typing import Any

from bee_py.types.type import Collection


def is_collection(data: Any):
if not isinstance(data, list):
return False

return all(
isinstance(entry, dict) and "data" in entry and "path" in entry and isinstance(entry["data"], bytes)
for entry in data
)
if not isinstance(data, Collection):
return False
return True


def assert_collection(data: Any):
Expand Down
18 changes: 12 additions & 6 deletions src/bee_py/utils/tar.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import io
import tarfile
from typing import Protocol
from typing import Protocol, Union

from bee_py.types.type import Collection
from bee_py.types.type import Collection, CollectionEntry


class StringLike(Protocol):
Expand All @@ -22,7 +22,7 @@ def fix_unicode_path(path: str) -> StringLike:
return StringLike(length=len(codes), char_code_at=lambda index: codes[index])


def make_tar(data: Collection) -> bytes:
def make_tar(data: Union[Collection, list[dict]]) -> bytes:
"""Creates a tar archive from the given data.
Args:
Expand All @@ -34,12 +34,18 @@ def make_tar(data: Collection) -> bytes:
A bytes object containing the tar archive.
"""

if isinstance(data, list):
# * Convert the list of dictionaries to a list of CollectionEntry objects
entries = [CollectionEntry(**entry) for entry in data]
# * Create a Collection object from the list of CollectionEntry objects
data = Collection(entries=entries)

tar_buffer = io.BytesIO()

with tarfile.open(mode="w", fileobj=tar_buffer) as tar:
for entry in data:
entry_path = entry["path"]
entry_data = entry["data"]
for entry in data.entries:
entry_path = entry.path
entry_data = entry.data.encode()

info = tarfile.TarInfo(name=entry_path)
info.size = len(entry_data)
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import random
from os import environ
from pathlib import Path

Expand Down Expand Up @@ -167,3 +168,10 @@ def peer_overlay(bee_peer_debug_ky_options) -> str:
node_addresses = get_node_addresses(bee_peer_debug_ky_options)

return node_addresses.overlay


@pytest.fixture
def random_byte_array(length=10, seed=500):
# * not completely random
random.seed(seed)
return bytearray(random.randint(0, 255) for _ in range(length)) # noqa: S311
4 changes: 0 additions & 4 deletions tests/data/bee_data.json

This file was deleted.

5 changes: 0 additions & 5 deletions tests/data/test_account.json

This file was deleted.

30 changes: 15 additions & 15 deletions tests/integration/chunk/test_soc.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
from bee_py.chunk.cac import make_content_addressed_chunk
from bee_py.chunk.soc import make_single_owner_chunk, upload_single_owner_chunk
from bee_py.utils.hex import bytes_to_hex
# from bee_py.chunk.cac import make_content_addressed_chunk
# from bee_py.chunk.soc import make_single_owner_chunk, upload_single_owner_chunk
# from bee_py.utils.hex import bytes_to_hex


# waiting for getPostageBatch
def test_upload_single_owner_chunk(
soc_signer, payload, bee_ky_options, get_postage_batch, try_delete_chunk_from_local_storage
):
identifier = bytes([0] * 32)
# # waiting for getPostageBatch
# def test_upload_single_owner_chunk(
# soc_signer, payload, bee_ky_options, get_postage_batch, try_delete_chunk_from_local_storage
# ):
# identifier = bytes([0] * 32)

cac = make_content_addressed_chunk(payload)
soc = make_single_owner_chunk(cac, identifier, soc_signer)
# soc_address = bytes_to_hex(soc.address)
# cac = make_content_addressed_chunk(payload)
# soc = make_single_owner_chunk(cac, identifier, soc_signer)
# # soc_address = bytes_to_hex(soc.address)

assert try_delete_chunk_from_local_storage
# assert try_delete_chunk_from_local_storage

# response = upload_single_owner_chunk(bee_ky_options, soc, get_postage_batch)
# # response = upload_single_owner_chunk(bee_ky_options, soc, get_postage_batch)

# # print(response)
# # # print(response)

# assert soc_address == response
# # assert soc_address == response
5 changes: 5 additions & 0 deletions tests/integration/modules/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,8 @@ def bee_debug_url_postage(get_postage_batch) -> BatchId:
@pytest.fixture
def bee_peer_debug_url_postage(get_postage_batch) -> BatchId:
return get_postage_batch("bee_peer_debug_url")


@pytest.fixture
def invalid_reference() -> str:
return "0000000000000000000000000000000000000000000000000000000000000000"
Loading

0 comments on commit 6044a4c

Please sign in to comment.