Skip to content

Commit 7e36f20

Browse files
committed
Plugin selection
1 parent 6c28c25 commit 7e36f20

3 files changed

Lines changed: 73 additions & 11 deletions

File tree

src/hermes/commands/init/base.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from requests import HTTPError
2020

2121
import hermes.commands.init.util.slim_click as sc
22+
from hermes.commands import marketplace
2223
from hermes.commands.base import HermesCommand, HermesPlugin
2324
from hermes.commands.init.util import (connect_github, connect_gitlab,
2425
connect_zenodo, git_info)
@@ -58,15 +59,10 @@ class HermesInitFolderInfo:
5859
def __init__(self):
5960
self.absolute_path: str = ""
6061
self.has_git_folder: bool = False
61-
# self.has_multiple_remotes: bool = False
62-
# self.git_remote_url: str = ""
63-
# self.git_base_url: str = ""
64-
# self.used_git_hoster: GitHoster = GitHoster.Empty
6562
self.has_hermes_toml: bool = False
6663
self.has_gitignore: bool = False
6764
self.has_citation_cff: bool = False
6865
self.has_readme: bool = False
69-
# self.current_branch: str = ""
7066
self.current_dir: str = ""
7167
self.dir_list: list[str] = []
7268
self.dir_folders: list[str] = []
@@ -180,6 +176,7 @@ def __init__(self, parser: argparse.ArgumentParser):
180176
}
181177
self.plugin_relevant_commands = ["harvest", "deposit"]
182178
self.builtin_plugins: dict[str: HermesPlugin] = get_builtin_plugins(self.plugin_relevant_commands)
179+
self.selected_plugins: list[marketplace.PluginInfo] = []
183180

184181
def init_command_parser(self, command_parser: argparse.ArgumentParser) -> None:
185182
command_parser.add_argument('--template-branch', nargs=1, default="",
@@ -217,7 +214,10 @@ def __call__(self, args: argparse.Namespace) -> None:
217214
self.test_initialization()
218215

219216
sc.echo(f"Starting to initialize HERMES in {self.folder_info.absolute_path}")
220-
sc.max_steps = 7
217+
sc.max_steps = 8
218+
219+
sc.next_step("Configure HERMES plugins")
220+
self.choose_plugins()
221221

222222
sc.next_step("Configure deposition platform and setup method")
223223
self.choose_deposit_platform()
@@ -595,6 +595,38 @@ def connect_deposit_platform(self) -> None:
595595
connect_zenodo.setup(using_sandbox=True)
596596
self.create_zenodo_token()
597597

598+
def choose_plugins(self):
599+
"""User chooses the plugins he wants to use."""
600+
plugin_infos: list[marketplace.PluginInfo] = marketplace.get_plugin_infos()
601+
plugins_builtin: list[marketplace.PluginInfo] = list(filter(lambda p: p.builtin, plugin_infos))
602+
plugins_available: list[marketplace.PluginInfo] = list(filter(lambda p: not p.builtin, plugin_infos))
603+
plugins_selected: list[marketplace.PluginInfo] = []
604+
sc.echo("The following plugins are already builtin:")
605+
for info in plugins_builtin:
606+
sc.echo(str(info), formatting=sc.Formats.OKGREEN)
607+
sc.echo("")
608+
while True:
609+
if plugins_selected:
610+
sc.echo("The following plugins are going to be installed:")
611+
for info in plugins_selected:
612+
sc.echo(str(info), formatting=sc.Formats.OKCYAN)
613+
sc.echo("")
614+
if plugins_available:
615+
sc.echo("The following plugins are available for installation:")
616+
for info in plugins_available:
617+
sc.echo(str(info), formatting=sc.Formats.WARNING, no_log=True)
618+
sc.echo("")
619+
else:
620+
self.selected_plugins = plugins_selected
621+
break
622+
choice = sc.choose("Do you want to add a plugin?", ["No"] + [p.name for p in plugins_available])
623+
if choice == 0:
624+
self.selected_plugins = plugins_selected
625+
break
626+
else:
627+
chosen_plugin = plugins_available.pop(choice - 1)
628+
plugins_selected.append(chosen_plugin)
629+
598630
def no_git_setup(self, start_question: str = "") -> None:
599631
"""Makes the init for a gitless project (basically just creating hermes.toml)"""
600632
if start_question == "":

src/hermes/commands/init/util/slim_click.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,19 @@ def get_log_type(self, default: int = logging.INFO) -> int:
6262
return default
6363

6464

65-
def echo(text: str, formatting: Formats = Formats.EMPTY, log_as: int = logging.NOTSET):
65+
def echo(text: str, formatting: Formats = Formats.EMPTY, log_as: int = logging.NOTSET, no_log: bool = False):
6666
"""
6767
Prints the text with the given formatting. If log_as is set or AUTO_LOG_ON_ECHO is true it gets logged as well.
6868
:param text: The printed text.
6969
:param formatting: You can use the Formats Enum to give the text a special color or formatting.
7070
:param log_as: Creates a log entry with the given text if this is set.
71+
:param no_log: Never creates a log entry if True.
7172
"""
7273
# Get logging type from formatting if AUTO_LOG_ON_ECHO
7374
if AUTO_LOG_ON_ECHO and log_as == logging.NOTSET and text != "":
7475
log_as = formatting.get_log_type(logging.INFO)
7576
# Add text to log if there is a logger
76-
if log_as != logging.NOTSET and default_file_logger:
77+
if log_as != logging.NOTSET and default_file_logger and no_log == False:
7778
default_file_logger.log(log_as, text)
7879
# Format the text for the console
7980
if formatting != Formats.EMPTY:

src/hermes/commands/marketplace.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,38 @@ def _sort_plugins_by_step(plugins: list[SchemaOrgSoftwareApplication]) -> dict[s
9696
return sorted_plugins
9797

9898

99+
def _plugin_loc(_plugin: SchemaOrgSoftwareApplication) -> str:
100+
return "builtin" if _plugin.is_part_of == schema_org_hermes else (_plugin.url or "")
101+
102+
class PluginInfo:
103+
def __init__(self):
104+
self.name: str = ""
105+
self.location: str = ""
106+
self.step: str = ""
107+
self.builtin: bool = True
108+
def __str__(self):
109+
return f"[{self.step}] {self.name} ({self.location})"
110+
111+
112+
def get_plugin_infos() -> list[PluginInfo]:
113+
response = requests.get(MARKETPLACE_URL, headers={"User-Agent": hermes_user_agent})
114+
response.raise_for_status()
115+
parser = PluginMarketPlaceParser()
116+
parser.feed(response.text)
117+
infos: list[PluginInfo] = []
118+
if parser.plugins:
119+
plugins_sorted = _sort_plugins_by_step(parser.plugins)
120+
for step in plugins_sorted.keys():
121+
for plugin in plugins_sorted[step]:
122+
info = PluginInfo()
123+
info.name = plugin.name
124+
info.step = step
125+
info.location = _plugin_loc(plugin)
126+
info.builtin = plugin.is_part_of == schema_org_hermes
127+
infos.append(info)
128+
return infos
129+
130+
99131
def main():
100132
response = requests.get(MARKETPLACE_URL, headers={"User-Agent": hermes_user_agent})
101133
response.raise_for_status()
@@ -108,9 +140,6 @@ def main():
108140
MARKETPLACE_URL + "."
109141
)
110142

111-
def _plugin_loc(_plugin: SchemaOrgSoftwareApplication) -> str:
112-
return "builtin" if _plugin.is_part_of == schema_org_hermes else (_plugin.url or "")
113-
114143
if parser.plugins:
115144
print()
116145
max_name_len = max(map(lambda plugin: len(plugin.name), parser.plugins))

0 commit comments

Comments
 (0)