Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions ipyfs/RPCs/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Add(IPFS):

def __call__(
self,
file,
file = None,
quiet: bool = None,
quieter: bool = None,
silent: bool = None,
Expand All @@ -15,18 +15,21 @@ def __call__(
wrap_with_directory: bool = None,
chunker: str = None,
pin: bool = None,
pin_name: str = None,
raw_leaves: bool = None,
nocopy: bool = None,
fscache: bool = None,
cid_version: int = None,
hash: str = None,
inline: bool = None,
inline_limit: int = None
inline_limit: int = None,
to_files: str = None,
files: dict = None,
) -> dict:
"""
Add a file or directory to IPFS.

:param file: File to add. Required: yes.
:param file: File to add. Required: yes, unless files is passed.
:param quiet: Write minimal output. Required: no.
:param quieter: Write only final hash. Required: no.
:param silent: Write no output. Required: no.
Expand All @@ -36,24 +39,32 @@ def __call__(
:param wrap_with_directory: Wrap files with a directory object. Required: no.
:param chunker: Chunking algorithm, size-[bytes], rabin-[min]-[avg]-[max] or buzhash. Default: size-262144. Required: no.
:param pin: Pin this object when adding. Default: true. Required: no.
:param pin_name: Name to use for the pin.Required: no.
:param raw_leaves: Use raw blocks for leaf nodes. Required: no.
:param nocopy: Add the file using filestore. Implies raw-leaves. (experimental). Required: no.
:param fscache: Check the filestore for pre-existing blocks. (experimental). Required: no.
:param cid_version: CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. Passing version 1 will cause the raw-leaves option to default to true. Required: no.
:param hash: Hash function to use. Implies CIDv1 if not sha2-256. (experimental). Default: sha2-256. Required: no.
:param inline: Inline small blocks into CIDs. (experimental). Required: no.
:param inline_limit: Maximum block size to inline. (experimental). Default: 32. Required: no.
:param to_files: Add reference to Files API (MFS) at the provided path. Required: no.
:param files: add multiple files - see requests.request => files parameter
:return response: Response from IPFS.
"""
if file is None and files is None:
raise(TypeError(" missing one of required : first positional argument: 'file' or keyword argument: 'files'"))
replace = {
"only_hash": "only-hash",
"wrap_with_directory": "wrap-with-directory",
"raw_leaves": "raw-leaves",
"cid_version": "cid-version",
"inline_limit": "inline-limit"
"inline_limit": "inline-limit",
"to_files": "to-files",
"pin_name": "pin-name"
}
return self._send(
params=locals(),
replace=replace,
file=file
file=file,
files=files
)
16 changes: 8 additions & 8 deletions ipyfs/RPCs/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

class BootStrap(IPFS):

def __init__(self):
super(BootStrap, self).__init__()
self.add = self.Add()
self.rm = self.Rm()
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.add = self.Add(**kwargs)
self.rm = self.Rm(**kwargs)

def __call__(self) -> dict:
"""
Expand All @@ -18,8 +18,8 @@ def __call__(self) -> dict:

class Add(IPFS):

def __init__(self):
super(BootStrap.Add, self).__init__()
def __init__(self,**kwargs):
super().__init__(**kwargs)

def __call__(
self,
Expand Down Expand Up @@ -49,8 +49,8 @@ def default(self) -> dict:

class Rm(IPFS):

def __init__(self):
super(BootStrap.Rm, self).__init__()
def __init__(self,**kwargs):
super().__init__(**kwargs)

def __call__(
self,
Expand Down
10 changes: 5 additions & 5 deletions ipyfs/RPCs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

class Commands(IPFS):

def __init__(self):
super(Commands, self).__init__()
self.completion = self.Completion()
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.completion = self.Completion(**kwargs)

def __call__(
self,
Expand All @@ -21,8 +21,8 @@ def __call__(

class Completion(IPFS):

def __init__(self):
super(Commands.Completion, self).__init__()
def __init__(self,**kwargs):
super().__init__(**kwargs)

def bash(self) -> dict:
"""
Expand Down
10 changes: 5 additions & 5 deletions ipyfs/RPCs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

class Config(IPFS):

def __init__(self):
super(Config, self).__init__()
self.profile = self.Profile()
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.profile = self.Profile(**kwargs)

def __call__(
self,
Expand Down Expand Up @@ -34,8 +34,8 @@ def __call__(

class Profile(IPFS):

def __init__(self):
super(Config.Profile, self).__init__()
def __init__(self,**kwargs):
super().__init__(**kwargs)

def apply(
self,
Expand Down
10 changes: 5 additions & 5 deletions ipyfs/RPCs/diag.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

class Diag(IPFS):

def __init__(self):
super(Diag, self).__init__()
self.cmds = self.Cmds()
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.cmds = self.Cmds(**kwargs)

class Cmds(IPFS):

def __init__(self):
super(Diag.Cmds, self).__init__()
def __init__(self,**kwargs):
super().__init__(**kwargs)

def __call__(
self,
Expand Down
26 changes: 16 additions & 10 deletions ipyfs/RPCs/pin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@

class Pin(IPFS):

def __init__(self):
super(Pin, self).__init__()
self.remote = self.Remote()
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.remote = self.Remote(**kwargs)

def add(
self,
path: str,
recursive: bool = None,
progress: bool = None
progress: bool = None,
name: str = None
) -> dict:
"""
Pin objects to local storage.

:param path: Path to object(s) to be pinned. Required: yes.
:param recursive: Recursively pin the object linked to by the specified object(s). Default: true. Required: no.
:param progress: Show progress. Required: no.
:param name: An optional name for created pin(s). Required: no.
:return response: Response from IPFS.
"""
replace = {"path": "arg"}
Expand All @@ -32,7 +34,9 @@ def ls(
path: str = None,
type: str = None,
quiet: bool = None,
stream: bool = None
stream: bool = None,
name: str = None,
names: bool = None
) -> dict:
"""
List objects pinned to local storage.
Expand All @@ -41,6 +45,8 @@ def ls(
:param type: The type of pinned keys to list. Can be "direct", "indirect", "recursive", or "all". Default: all. Required: no.
:param quiet: Write just hashes of objects. Required: no.
:param stream: Enable streaming of pins as they are discovered. Required: no.
:param name: Limit returned pins to ones with names that contain the value provided (case-sensitive, partial match). Implies --names=true. Required: no.
:param names: Include pin names in the output (slower, disabled by default). Required: no.
:return response: Response from IPFS.
"""
replace = {"path": "arg"}
Expand Down Expand Up @@ -106,9 +112,9 @@ def verify(

class Remote(IPFS):

def __init__(self):
super(Pin.Remote).__init__()
self.service = self.Service()
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.service = self.Service(**kwargs)

def add(
self,
Expand Down Expand Up @@ -172,8 +178,8 @@ def rm(

class Service(IPFS):

def __init__(self):
super(Pin.Remote.Service).__init__()
def __init__(self,**kwargs):
super().__init__(**kwargs)

def add(
self,
Expand Down
22 changes: 11 additions & 11 deletions ipyfs/RPCs/swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

class Swarm(IPFS):

def __init__(self):
super(Swarm, self).__init__()
self.addrs = self.Addrs()
self.filters = self.Filters()
self.peering = self.Peering()
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.addrs = self.Addrs(**kwargs)
self.filters = self.Filters(**kwargs)
self.peering = self.Peering(**kwargs)

def connect(
self,
Expand Down Expand Up @@ -61,8 +61,8 @@ def peers(

class Addrs(IPFS):

def __init__(self):
super(Swarm.Addrs, self).__init__()
def __init__(self,**kwargs):
super().__init__(**kwargs)

def __call__(self) -> dict:
"""
Expand Down Expand Up @@ -93,8 +93,8 @@ def local(

class Filters(IPFS):

def __init__(self):
super(Swarm.Filters, self).__init__()
def __init__(self,**kwargs):
super().__init__(**kwargs)

def add(
self,
Expand Down Expand Up @@ -130,8 +130,8 @@ def rm(

class Peering(IPFS):

def __init__(self):
super(Swarm.Peering, self).__init__()
def __init__(self,**kwargs):
super().__init__(**kwargs)

def add(
self,
Expand Down
42 changes: 33 additions & 9 deletions ipyfs/ipfs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import requests
import json
from collections import defaultdict


Expand All @@ -8,7 +9,9 @@ def __init__(
self,
host: str = "http://localhost",
port: int = 5001,
version: int = 0
version: int = 0,
response_return_status_code: bool = True,
init_self_attr_RPCs: bool or list = False,
):
self.host = host
self.port = port
Expand All @@ -19,29 +22,50 @@ def __init__(
f"/{self.__class__.__module__.split('.')[-1].lower()}"
)
class_name = self.__class__.__name__.lower()
self.response_return_status_code=response_return_status_code
if class_name not in self.uri:
self.uri += f"/{class_name}"
if self.__class__.__name__=='IPFS' and init_self_attr_RPCs:
from . import RPCs
if type(init_self_attr_RPCs) is list:
rpc_objects=[ obj.capitalize() for obj in init_self_attr_RPCs if obj.capitalize() in dir(RPCs) and type(getattr(RPCs,obj.capitalize())) is type ]
rpc_objects+=[ obj.upper() for obj in init_self_attr_RPCs if obj.upper() in dir(RPCs) and type(getattr(RPCs,obj.upper())) is type ]
else:
rpc_objects=[ obj for obj in dir(RPCs) if type(getattr(RPCs,obj)) is type ]
for rpc_object in rpc_objects:
setattr(self,rpc_object.lower(),getattr(RPCs,rpc_object)(host=host,port=port,version=version,response_return_status_code=response_return_status_code))



@staticmethod
def _response(response: requests.Response) -> dict:
def _response(response: requests.Response,response_return_status_code=True) -> dict:
try:
result = response.json()
except:
result = response.text
if result == '':
result = None
else:
try:
result=[ json.loads(r) for r in result.splitlines() ]
except:
pass
if response.status_code != 200:
raise Exception(result)
return {
"status_code": response.status_code,
"result": result
}
if response_return_status_code:
return {
"status_code": response.status_code,
"result": result
}
else:
return(result)

def _send(
self,
params: dict,
replace: dict = None,
file=None
file=None,
files: dict = None
) -> dict:
"""
Request to IPFS.
Expand Down Expand Up @@ -70,6 +94,6 @@ def _send(
requests.post(
url=uri,
params=data,
files={'file': file}
)
files={'file': file} if files is None else {'file': file , **files}
),self.response_return_status_code
)