Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] add register_extra_tentacle_data #175

Merged
merged 2 commits into from
Feb 13, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.9.10] - 2023-02-13
### Added
- API: register_extra_tentacle_data

## [2.9.9] - 2023-01-18
### Updated
- tentacles: log url on fetching error
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# OctoBot-Tentacles-Manager [2.9.9](https://github.com/Drakkar-Software/OctoBot-Tentacles-Manager/tree/master/CHANGELOG.md)
# OctoBot-Tentacles-Manager [2.9.10](https://github.com/Drakkar-Software/OctoBot-Tentacles-Manager/tree/master/CHANGELOG.md)
[![PyPI](https://img.shields.io/pypi/v/OctoBot-Tentacles-Manager.svg)](https://pypi.python.org/pypi/OctoBot-Tentacles-Manager/)
[![Downloads](https://pepy.tech/badge/OctoBot-Tentacles-Manager/month)](https://pepy.tech/project/OctoBot-Tentacles-Manager)
[![Github-Action-CI](https://github.com/Drakkar-Software/OctoBot-Tentacles-Manager/workflows/OctoBot-Tentacles-Manager-CI/badge.svg)](https://github.com/Drakkar-Software/OctoBot-Tentacles-Manager/actions)
Expand Down
2 changes: 1 addition & 1 deletion octobot_tentacles_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this library.

VERSION = "2.9.9"
VERSION = "2.9.10"
PROJECT_NAME = "OctoBot-Tentacles-Manager"
2 changes: 2 additions & 0 deletions octobot_tentacles_manager/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
load_tentacles,
reload_tentacle_info,
ensure_tentacle_info,
register_extra_tentacle_data,
are_tentacles_up_to_date,
is_tentacles_setup_config_successfully_loaded,
get_tentacles_installation_version,
Expand Down Expand Up @@ -140,6 +141,7 @@
"load_tentacles",
"reload_tentacle_info",
"ensure_tentacle_info",
"register_extra_tentacle_data",
"are_tentacles_up_to_date",
"is_tentacles_setup_config_successfully_loaded",
"get_tentacles_installation_version",
Expand Down
5 changes: 5 additions & 0 deletions octobot_tentacles_manager/api/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import octobot_tentacles_manager.constants as constants
import octobot_tentacles_manager.loaders as loaders
import octobot_tentacles_manager.util as util


def get_installed_tentacles_modules() -> set:
Expand Down Expand Up @@ -130,6 +131,10 @@ def _load_tentacle_class(tentacle_name):
return tentacle_class
except ImportError:
pass
try:
return util.get_tentacle_class_from_extra_tentacles(tentacle_name)
except KeyError:
pass
raise RuntimeError(f"Can't find tentacle: {tentacle_name}")
except ImportError as e:
raise ImportError(f"Can't import {e} module which is required to get associated "
Expand Down
9 changes: 9 additions & 0 deletions octobot_tentacles_manager/api/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import octobot_tentacles_manager.constants as constants
import octobot_tentacles_manager.loaders as loaders
import octobot_tentacles_manager.managers as managers
import octobot_tentacles_manager.util as util


def load_tentacles(verbose=True) -> bool:
Expand Down Expand Up @@ -47,3 +48,11 @@ def reload_tentacle_info() -> None:

def ensure_tentacle_info() -> None:
loaders.ensure_tentacles_metadata(constants.TENTACLES_PATH)


def register_extra_tentacle_data(tentacle_class, tentacle_type_path: str):
"""
:param tentacle_class: class of the tentacle
:param tentacle_type_path: path of the tentacle, ex "Trading/mode"
"""
return util.register_extra_tentacle_data(tentacle_class.get_name(), tentacle_type_path, tentacle_class)
4 changes: 2 additions & 2 deletions octobot_tentacles_manager/models/tentacle.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
class Tentacle(artifact.Artifact):
ARTIFACT_NAME = "tentacle"

def __init__(self, tentacle_root_path, name, tentacle_type):
def __init__(self, tentacle_root_path, name, tentacle_type, tentacle_class_names=None):
super().__init__(name)
self.tentacle_root_path = tentacle_root_path
self.tentacle_type = tentacle_type
self.tentacle_root_type = self.tentacle_type.get_root_type()
self.tentacle_path = path.join(self.tentacle_root_path, self.tentacle_type.to_path())
self.tentacle_module_path = None
self.tentacle_class_names = []
self.tentacle_class_names = tentacle_class_names or []
self.tentacles_requirements = None
self.tentacle_group = self.name
self.in_dev_mode = False
Expand Down
5 changes: 3 additions & 2 deletions octobot_tentacles_manager/models/tentacle_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ class TentacleFactory:
def __init__(self, tentacle_root_path):
self.tentacle_root_path = tentacle_root_path

def create_tentacle_from_type(self, name, tentacle_type) -> models.Tentacle:
def create_tentacle_from_type(self, name, tentacle_type, tentacle_class_names=None) -> models.Tentacle:
"""
Create a tentacle artifact from its name and type
:param name: the tentacle name
:param tentacle_type: the tentacle type
:param tentacle_class_names: list of tentacles classes names
:return: the created TentacleArtifact instance
"""
return models.Tentacle(self.tentacle_root_path, name, tentacle_type)
return models.Tentacle(self.tentacle_root_path, name, tentacle_type, tentacle_class_names=tentacle_class_names)

async def create_and_load_tentacle_from_module(self, tentacle_module) -> models.Tentacle:
"""
Expand Down
5 changes: 4 additions & 1 deletion octobot_tentacles_manager/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
from octobot_tentacles_manager.util.tentacle_explorer import (
load_tentacle_with_metadata,
get_tentacles_from_package,
register_extra_tentacle_data,
get_tentacle_class_from_extra_tentacles,
)
from octobot_tentacles_manager.util.file_util import (
get_file_creation_time,
Expand Down Expand Up @@ -66,10 +68,11 @@
"get_local_arch_download_path",
"load_tentacle_with_metadata",
"get_tentacles_from_package",
"register_extra_tentacle_data",
"get_tentacle_class_from_extra_tentacles",
"get_file_creation_time",
"log_tentacles_file_details",
"find_or_create",
"get_tentacles_from_package",
"replace_with_remove_or_rename",
"merge_folders",
"TentacleFilter",
Expand Down
20 changes: 20 additions & 0 deletions octobot_tentacles_manager/util/tentacle_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
# License along with this library.
import os
import os.path as path
import typing

import octobot_commons.logging as logging
import octobot_tentacles_manager.constants as constants
import octobot_tentacles_manager.models as models


_extra_tentacle_data_by_name: dict[str, tuple[models.TentacleType, typing.ClassVar]] = {}


def load_tentacle_with_metadata(tentacle_path: str):
loaded_tentacles = _parse_all_tentacles(tentacle_path)
_load_all_metadata(loaded_tentacles)
Expand All @@ -35,6 +39,19 @@ def get_tentacles_from_package(tentacles, package_name: str):
]


def register_extra_tentacle_data(tentacle_name: str, tentacle_type_path: str, tentacle_class):
"""
:param tentacle_name: name of the tentacle
:param tentacle_type_path: path of the tentacle, ex "Trading/mode"
:param tentacle_class: class of the tentacle
"""
_extra_tentacle_data_by_name[tentacle_name] = (models.TentacleType(tentacle_type_path), tentacle_class)


def get_tentacle_class_from_extra_tentacles(tentacle_name: str):
return _extra_tentacle_data_by_name[tentacle_name][1]


def _load_all_metadata(tentacles):
for tentacle in tentacles:
try:
Expand All @@ -52,6 +69,9 @@ def _parse_all_tentacles(root: str):
for tentacle_entry in os.scandir(path.join(root, tentacle_type.to_path()))
if not (tentacle_entry.name == constants.PYTHON_INIT_FILE or
tentacle_entry.name in constants.FOLDERS_BLACK_LIST)
] + [
factory.create_tentacle_from_type(tentacle_name, tentacle_type, [tentacle_name])
for tentacle_name, (tentacle_type, _) in _extra_tentacle_data_by_name.items()
]


Expand Down
Loading