From 7377cb0d265c79c23d472d1394589bdaa19eebfc Mon Sep 17 00:00:00 2001 From: UpstreamData Date: Mon, 12 Sep 2022 15:15:13 -0600 Subject: [PATCH] refactor some classes into their own files and fill base __init__.py with imports --- pyasic/API/__init__.py | 28 +---------- pyasic/API/btminer.py | 3 +- pyasic/__init__.py | 48 +++++++++++++++++++ pyasic/config/__init__.py | 2 +- pyasic/errors/__init__.py | 41 ++++++++++++++++ pyasic/miners/_backends/bosminer.py | 2 +- pyasic/miners/_backends/btminer.py | 2 +- pyasic/miners/_backends/cgminer.py | 2 +- pyasic/miners/antminer/cgminer/X9/__init__.py | 1 + pyasic/miners/base.py | 8 ---- .../innosilicon/cgminer/T3X/T3H_Plus.py | 2 +- pyasic/miners/miner_factory.py | 18 ++----- pyasic/miners/miner_listener.py | 9 +--- pyasic/misc/__init__.py | 22 +++++++++ pyasic/network/__init__.py | 16 +++---- pyasic/settings/__init__.py | 9 +--- 16 files changed, 134 insertions(+), 79 deletions(-) create mode 100644 pyasic/errors/__init__.py create mode 100644 pyasic/misc/__init__.py diff --git a/pyasic/API/__init__.py b/pyasic/API/__init__.py index c85af84b..c53547c3 100644 --- a/pyasic/API/__init__.py +++ b/pyasic/API/__init__.py @@ -19,33 +19,7 @@ import warnings import logging from typing import Union - -class APIError(Exception): - def __init__(self, *args): - if args: - self.message = args[0] - else: - self.message = None - - def __str__(self): - if self.message: - return f"{self.message}" - else: - return "Incorrect API parameters." - - -class APIWarning(Warning): - def __init__(self, *args): - if args: - self.message = args[0] - else: - self.message = None - - def __str__(self): - if self.message: - return f"{self.message}" - else: - return "Incorrect API parameters." +from pyasic.errors import APIError, APIWarning class BaseMinerAPI: diff --git a/pyasic/API/btminer.py b/pyasic/API/btminer.py index 43f1f3fb..24bb3fa6 100644 --- a/pyasic/API/btminer.py +++ b/pyasic/API/btminer.py @@ -24,7 +24,8 @@ from typing import Union from passlib.handlers.md5_crypt import md5_crypt from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes -from pyasic.API import BaseMinerAPI, APIError +from pyasic.errors import APIError +from pyasic.API import BaseMinerAPI from pyasic.settings import PyasicSettings diff --git a/pyasic/__init__.py b/pyasic/__init__.py index ff3b45f9..1b4aea78 100644 --- a/pyasic/__init__.py +++ b/pyasic/__init__.py @@ -11,3 +11,51 @@ # 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 pyasic.API.bmminer import BMMinerAPI +from pyasic.API.bosminer import BOSMinerAPI +from pyasic.API.btminer import BTMinerAPI +from pyasic.API.cgminer import CGMinerAPI +from pyasic.API.unknown import UnknownAPI + +from pyasic.config import MinerConfig + +from pyasic.data import ( + MinerData, + BraiinsOSError, + InnosiliconError, + WhatsminerError, + X19Error, +) + +from pyasic.errors import APIError, APIWarning + +from pyasic.miners import get_miner +from pyasic.miners.base import AnyMiner +from pyasic.miners.miner_factory import MinerFactory +from pyasic.miners.miner_listener import MinerListener + +from pyasic.network import MinerNetwork + +from pyasic.settings import PyasicSettings + +__all__ = [ + "BMMinerAPI", + "BOSMinerAPI", + "BTMinerAPI", + "CGMinerAPI", + "UnknownAPI", + "MinerConfig", + "MinerData", + "BraiinsOSError", + "InnosiliconError", + "WhatsminerError", + "X19Error", + "APIError", + "APIWarning", + "get_miner", + "AnyMiner", + "MinerFactory", + "MinerListener", + "MinerNetwork", + "PyasicSettings", +] diff --git a/pyasic/config/__init__.py b/pyasic/config/__init__.py index 76b30679..5e2272a0 100644 --- a/pyasic/config/__init__.py +++ b/pyasic/config/__init__.py @@ -247,7 +247,7 @@ class MinerConfig: temp_mode: Literal["auto", "manual", "disabled"] = "auto" temp_target: float = 70.0 temp_hot: float = 80.0 - temp_dangerous: float = 10.0 + temp_dangerous: float = 100.0 minimum_fans: int = None fan_speed: Literal[tuple(range(101))] = None # noqa - Ignore weird Literal usage diff --git a/pyasic/errors/__init__.py b/pyasic/errors/__init__.py new file mode 100644 index 00000000..368ee443 --- /dev/null +++ b/pyasic/errors/__init__.py @@ -0,0 +1,41 @@ +# 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. + + +class APIError(Exception): + def __init__(self, *args): + if args: + self.message = args[0] + else: + self.message = None + + def __str__(self): + if self.message: + return f"{self.message}" + else: + return "Incorrect API parameters." + + +class APIWarning(Warning): + def __init__(self, *args): + if args: + self.message = args[0] + else: + self.message = None + + def __str__(self): + if self.message: + return f"{self.message}" + else: + return "Incorrect API parameters." diff --git a/pyasic/miners/_backends/bosminer.py b/pyasic/miners/_backends/bosminer.py index 582ce6ca..80a2e797 100644 --- a/pyasic/miners/_backends/bosminer.py +++ b/pyasic/miners/_backends/bosminer.py @@ -22,7 +22,7 @@ import toml from pyasic.miners.base import BaseMiner from pyasic.API.bosminer import BOSMinerAPI -from pyasic.API import APIError +from pyasic.errors import APIError from pyasic.data.error_codes import BraiinsOSError from pyasic.data import MinerData diff --git a/pyasic/miners/_backends/btminer.py b/pyasic/miners/_backends/btminer.py index 909dfaa5..4df0c608 100644 --- a/pyasic/miners/_backends/btminer.py +++ b/pyasic/miners/_backends/btminer.py @@ -19,7 +19,7 @@ from typing import Union from pyasic.API.btminer import BTMinerAPI from pyasic.miners.base import BaseMiner -from pyasic.API import APIError +from pyasic.errors import APIError from pyasic.data import MinerData from pyasic.data.error_codes import WhatsminerError diff --git a/pyasic/miners/_backends/cgminer.py b/pyasic/miners/_backends/cgminer.py index 3839c7f1..89426b05 100644 --- a/pyasic/miners/_backends/cgminer.py +++ b/pyasic/miners/_backends/cgminer.py @@ -19,7 +19,7 @@ from typing import Union from pyasic.API.cgminer import CGMinerAPI from pyasic.miners.base import BaseMiner -from pyasic.API import APIError +from pyasic.errors import APIError from pyasic.config import MinerConfig from pyasic.data import MinerData diff --git a/pyasic/miners/antminer/cgminer/X9/__init__.py b/pyasic/miners/antminer/cgminer/X9/__init__.py index dd09e694..237591e0 100644 --- a/pyasic/miners/antminer/cgminer/X9/__init__.py +++ b/pyasic/miners/antminer/cgminer/X9/__init__.py @@ -13,3 +13,4 @@ # limitations under the License. from .S9 import CGMinerS9 +from .T9 import CGMinerT9 diff --git a/pyasic/miners/base.py b/pyasic/miners/base.py index 69a52adb..5706565b 100644 --- a/pyasic/miners/base.py +++ b/pyasic/miners/base.py @@ -91,18 +91,10 @@ class BaseMiner(ABC): async def fault_light_off(self) -> bool: pass - # async def send_file(self, src, dest): - # async with (await self._get_ssh_connection()) as conn: - # await asyncssh.scp(src, (conn, dest)) - @abstractmethod async def check_light(self) -> bool: pass - # @abstractmethod - async def get_board_info(self): - return None - @abstractmethod async def get_config(self) -> MinerConfig: pass diff --git a/pyasic/miners/innosilicon/cgminer/T3X/T3H_Plus.py b/pyasic/miners/innosilicon/cgminer/T3X/T3H_Plus.py index 1d59f0a5..f78d3cff 100644 --- a/pyasic/miners/innosilicon/cgminer/T3X/T3H_Plus.py +++ b/pyasic/miners/innosilicon/cgminer/T3X/T3H_Plus.py @@ -18,7 +18,7 @@ from pyasic.data import MinerData from pyasic.data.error_codes import InnosiliconError from pyasic.settings import PyasicSettings from pyasic.config import MinerConfig -from pyasic.API import APIError +from pyasic.errors import APIError import httpx import warnings diff --git a/pyasic/miners/miner_factory.py b/pyasic/miners/miner_factory.py index 2364ecf2..43125b67 100644 --- a/pyasic/miners/miner_factory.py +++ b/pyasic/miners/miner_factory.py @@ -32,7 +32,9 @@ from pyasic.miners._backends.bosminer_old import ( # noqa - Ignore _module impo from pyasic.miners.unknown import UnknownMiner -from pyasic.API import APIError +from pyasic.errors import APIError + +from pyasic.misc import Singleton import asyncio import ipaddress @@ -59,6 +61,7 @@ MINER_CLASSES = { "Default": BMMinerT9, "BMMiner": BMMinerT9, "Hiveon": HiveonT9, + "CGMiner": CGMinerT9, }, "ANTMINER S17": { "Default": BMMinerS17, @@ -252,15 +255,6 @@ MINER_CLASSES = { } -class Singleton(type): - _instances = {} - - def __call__(cls, *args, **kwargs): - if cls not in cls._instances: - cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) - return cls._instances[cls] - - class MinerFactory(metaclass=Singleton): """A factory to handle identification and selection of the proper class of miner""" @@ -637,9 +631,7 @@ class MinerFactory(metaclass=Singleton): else: # make sure the command succeeded if data["STATUS"][0]["STATUS"] not in ("S", "I"): - # this is an error - if data["STATUS"][0]["STATUS"] not in ("S", "I"): - return False, data["STATUS"][0]["Msg"] + return False, data["STATUS"][0]["Msg"] return True, None @staticmethod diff --git a/pyasic/miners/miner_listener.py b/pyasic/miners/miner_listener.py index 7d4a7488..3a414b39 100644 --- a/pyasic/miners/miner_listener.py +++ b/pyasic/miners/miner_listener.py @@ -14,14 +14,7 @@ import asyncio - -class Singleton(type): - _instances = {} - - def __call__(cls, *args, **kwargs): - if cls not in cls._instances: - cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) - return cls._instances[cls] +from pyasic.misc import Singleton class _MinerListener: diff --git a/pyasic/misc/__init__.py b/pyasic/misc/__init__.py new file mode 100644 index 00000000..a0bd8525 --- /dev/null +++ b/pyasic/misc/__init__.py @@ -0,0 +1,22 @@ +# 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. + + +class Singleton(type): + _instances = {} + + def __call__(cls, *args, **kwargs): + if cls not in cls._instances: + cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) + return cls._instances[cls] diff --git a/pyasic/network/__init__.py b/pyasic/network/__init__.py index 1e5c19f3..d8781d13 100644 --- a/pyasic/network/__init__.py +++ b/pyasic/network/__init__.py @@ -37,7 +37,9 @@ class MinerNetwork: """ def __init__( - self, ip_addr: Union[str, None] = None, mask: Union[str, int, None] = None + self, + ip_addr: Union[str, List[str], None] = None, + mask: Union[str, int, None] = None, ) -> None: self.network = None self.ip_addr = ip_addr @@ -63,18 +65,14 @@ class MinerNetwork: if self.network: return self.network + # if there is no IP address passed, default to 192.168.1.0 + if not self.ip_addr: + self.ip_addr = "192.168.1.0" if "-" in self.ip_addr: self.network = MinerNetworkRange(self.ip_addr) elif isinstance(self.ip_addr, list): self.network = MinerNetworkRange(self.ip_addr) else: - # if there is no IP address passed, default to 192.168.1.0 - if not self.ip_addr: - default_gateway = "192.168.1.0" - # if we do have an IP address passed, use that - else: - default_gateway = self.ip_addr - # if there is no subnet mask passed, default to /24 if not self.mask: subnet_mask = "24" @@ -84,7 +82,7 @@ class MinerNetwork: # save the network and return it self.network = ipaddress.ip_network( - f"{default_gateway}/{subnet_mask}", strict=False + f"{self.ip_addr}/{subnet_mask}", strict=False ) logging.debug(f"Setting MinerNetwork: {self.network}") diff --git a/pyasic/settings/__init__.py b/pyasic/settings/__init__.py index 0410aea8..2df13ea9 100644 --- a/pyasic/settings/__init__.py +++ b/pyasic/settings/__init__.py @@ -14,14 +14,7 @@ from dataclasses import dataclass - -class Singleton(type): - _instances = {} - - def __call__(cls, *args, **kwargs): - if cls not in cls._instances: - cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) - return cls._instances[cls] +from pyasic.misc import Singleton @dataclass