refactor: move base classes to base.py in their directories, move data locations to miners.data, and rename types to models.
This commit is contained in:
@@ -13,86 +13,10 @@
|
||||
# See the License for the specific language governing permissions and -
|
||||
# limitations under the License. -
|
||||
# ------------------------------------------------------------------------------
|
||||
from __future__ import annotations
|
||||
|
||||
import warnings
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
from pyasic.errors import APIWarning
|
||||
|
||||
|
||||
class BaseWebAPI(ABC):
|
||||
def __init__(self, ip: str) -> None:
|
||||
# ip address of the miner
|
||||
self.ip = ip
|
||||
self.username = None
|
||||
self.pwd = None
|
||||
self.port = 80
|
||||
|
||||
self.token = None
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
if cls is BaseWebAPI:
|
||||
raise TypeError(f"Only children of '{cls.__name__}' may be instantiated")
|
||||
return object.__new__(cls)
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self.__class__.__name__}: {str(self.ip)}"
|
||||
|
||||
@abstractmethod
|
||||
async def send_command(
|
||||
self,
|
||||
command: str | bytes,
|
||||
ignore_errors: bool = False,
|
||||
allow_warning: bool = True,
|
||||
privileged: bool = False,
|
||||
**parameters: Any,
|
||||
) -> dict:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def multicommand(
|
||||
self, *commands: str, ignore_errors: bool = False, allow_warning: bool = True
|
||||
) -> dict:
|
||||
pass
|
||||
|
||||
def _check_commands(self, *commands):
|
||||
allowed_commands = self.get_commands()
|
||||
return_commands = []
|
||||
for command in [*commands]:
|
||||
if command in allowed_commands:
|
||||
return_commands.append(command)
|
||||
else:
|
||||
warnings.warn(
|
||||
f"""Removing incorrect command: {command}
|
||||
If you are sure you want to use this command please use WebAPI.send_command("{command}", ignore_errors=True) instead.""",
|
||||
APIWarning,
|
||||
)
|
||||
return return_commands
|
||||
|
||||
@property
|
||||
def commands(self) -> list:
|
||||
return self.get_commands()
|
||||
|
||||
def get_commands(self) -> list:
|
||||
"""Get a list of command accessible to a specific type of web API on the miner.
|
||||
|
||||
Returns:
|
||||
A list of all web commands that the miner supports.
|
||||
"""
|
||||
return [
|
||||
func
|
||||
for func in
|
||||
# each function in self
|
||||
dir(self)
|
||||
if not func == "commands"
|
||||
if callable(getattr(self, func)) and
|
||||
# no __ or _ methods
|
||||
not func.startswith("__") and not func.startswith("_") and
|
||||
# remove all functions that are in this base class
|
||||
func
|
||||
not in [
|
||||
func for func in dir(BaseWebAPI) if callable(getattr(BaseWebAPI, func))
|
||||
]
|
||||
]
|
||||
from .antminer import AntminerModernWebAPI, AntminerOldWebAPI
|
||||
from .auradine import AuradineWebAPI
|
||||
from .braiins_os import BOSerWebAPI, BOSMinerWebAPI
|
||||
from .epic import ePICWebAPI
|
||||
from .goldshell import GoldshellWebAPI
|
||||
from .innosilicon import InnosiliconWebAPI
|
||||
from .vnish import VNishWebAPI
|
||||
|
||||
@@ -22,7 +22,7 @@ from typing import Any
|
||||
import httpx
|
||||
|
||||
from pyasic import settings
|
||||
from pyasic.web import BaseWebAPI
|
||||
from pyasic.web.base import BaseWebAPI
|
||||
|
||||
|
||||
class AntminerModernWebAPI(BaseWebAPI):
|
||||
|
||||
@@ -24,10 +24,10 @@ import httpx
|
||||
|
||||
from pyasic import settings
|
||||
from pyasic.errors import APIError
|
||||
from pyasic.web import BaseWebAPI
|
||||
from pyasic.web.base import BaseWebAPI
|
||||
|
||||
|
||||
class FluxWebAPI(BaseWebAPI):
|
||||
class AuradineWebAPI(BaseWebAPI):
|
||||
def __init__(self, ip: str) -> None:
|
||||
super().__init__(ip)
|
||||
self.username = "admin"
|
||||
|
||||
98
pyasic/web/base.py
Normal file
98
pyasic/web/base.py
Normal file
@@ -0,0 +1,98 @@
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright 2022 Upstream Data Inc -
|
||||
# -
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); -
|
||||
# you may not use this file except in compliance with the License. -
|
||||
# You may obtain a copy of the License at -
|
||||
# -
|
||||
# http://www.apache.org/licenses/LICENSE-2.0 -
|
||||
# -
|
||||
# Unless required by applicable law or agreed to in writing, software -
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, -
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
|
||||
# See the License for the specific language governing permissions and -
|
||||
# limitations under the License. -
|
||||
# ------------------------------------------------------------------------------
|
||||
from __future__ import annotations
|
||||
|
||||
import warnings
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
from pyasic.errors import APIWarning
|
||||
|
||||
|
||||
class BaseWebAPI(ABC):
|
||||
def __init__(self, ip: str) -> None:
|
||||
# ip address of the miner
|
||||
self.ip = ip
|
||||
self.username = None
|
||||
self.pwd = None
|
||||
self.port = 80
|
||||
|
||||
self.token = None
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
if cls is BaseWebAPI:
|
||||
raise TypeError(f"Only children of '{cls.__name__}' may be instantiated")
|
||||
return object.__new__(cls)
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self.__class__.__name__}: {str(self.ip)}"
|
||||
|
||||
@abstractmethod
|
||||
async def send_command(
|
||||
self,
|
||||
command: str | bytes,
|
||||
ignore_errors: bool = False,
|
||||
allow_warning: bool = True,
|
||||
privileged: bool = False,
|
||||
**parameters: Any,
|
||||
) -> dict:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def multicommand(
|
||||
self, *commands: str, ignore_errors: bool = False, allow_warning: bool = True
|
||||
) -> dict:
|
||||
pass
|
||||
|
||||
def _check_commands(self, *commands):
|
||||
allowed_commands = self.get_commands()
|
||||
return_commands = []
|
||||
for command in [*commands]:
|
||||
if command in allowed_commands:
|
||||
return_commands.append(command)
|
||||
else:
|
||||
warnings.warn(
|
||||
f"""Removing incorrect command: {command}
|
||||
If you are sure you want to use this command please use WebAPI.send_command("{command}", ignore_errors=True) instead.""",
|
||||
APIWarning,
|
||||
)
|
||||
return return_commands
|
||||
|
||||
@property
|
||||
def commands(self) -> list:
|
||||
return self.get_commands()
|
||||
|
||||
def get_commands(self) -> list:
|
||||
"""Get a list of command accessible to a specific type of web API on the miner.
|
||||
|
||||
Returns:
|
||||
A list of all web commands that the miner supports.
|
||||
"""
|
||||
return [
|
||||
func
|
||||
for func in
|
||||
# each function in self
|
||||
dir(self)
|
||||
if not func == "commands"
|
||||
if callable(getattr(self, func)) and
|
||||
# no __ or _ methods
|
||||
not func.startswith("__") and not func.startswith("_") and
|
||||
# remove all functions that are in this base class
|
||||
func
|
||||
not in [
|
||||
func for func in dir(BaseWebAPI) if callable(getattr(BaseWebAPI, func))
|
||||
]
|
||||
]
|
||||
@@ -25,7 +25,7 @@ from grpclib.client import Channel
|
||||
|
||||
from pyasic import settings
|
||||
from pyasic.errors import APIError
|
||||
from pyasic.web import BaseWebAPI
|
||||
from pyasic.web.base import BaseWebAPI
|
||||
|
||||
from .proto.braiins.bos import *
|
||||
from .proto.braiins.bos.v1 import *
|
||||
|
||||
@@ -22,7 +22,7 @@ import httpx
|
||||
|
||||
from pyasic import settings
|
||||
from pyasic.errors import APIError
|
||||
from pyasic.web import BaseWebAPI
|
||||
from pyasic.web.base import BaseWebAPI
|
||||
|
||||
|
||||
class BOSMinerWebAPI(BaseWebAPI):
|
||||
|
||||
@@ -22,7 +22,7 @@ import httpx
|
||||
|
||||
from pyasic import settings
|
||||
from pyasic.errors import APIError
|
||||
from pyasic.web import BaseWebAPI
|
||||
from pyasic.web.base import BaseWebAPI
|
||||
|
||||
|
||||
class ePICWebAPI(BaseWebAPI):
|
||||
|
||||
@@ -22,7 +22,7 @@ from typing import Any
|
||||
import httpx
|
||||
|
||||
from pyasic import settings
|
||||
from pyasic.web import BaseWebAPI
|
||||
from pyasic.web.base import BaseWebAPI
|
||||
|
||||
|
||||
class GoldshellWebAPI(BaseWebAPI):
|
||||
|
||||
@@ -23,7 +23,7 @@ import httpx
|
||||
|
||||
from pyasic import settings
|
||||
from pyasic.errors import APIError
|
||||
from pyasic.web import BaseWebAPI
|
||||
from pyasic.web.base import BaseWebAPI
|
||||
|
||||
|
||||
class InnosiliconWebAPI(BaseWebAPI):
|
||||
|
||||
@@ -22,7 +22,7 @@ from typing import Any
|
||||
import httpx
|
||||
|
||||
from pyasic import settings
|
||||
from pyasic.web import BaseWebAPI
|
||||
from pyasic.web.base import BaseWebAPI
|
||||
|
||||
|
||||
class VNishWebAPI(BaseWebAPI):
|
||||
|
||||
Reference in New Issue
Block a user