Skip to content

Commit

Permalink
🔨🚸(ozi-new interactive): added metadata viewer to the menu.
Browse files Browse the repository at this point in the history
Signed-off-by: rjdbcm <[email protected]>
  • Loading branch information
rjdbcm committed Jun 24, 2024
1 parent a6bca88 commit fd9e7b0
Showing 1 changed file with 47 additions and 59 deletions.
106 changes: 47 additions & 59 deletions ozi/new/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from prompt_toolkit.key_binding.bindings.focus import focus_next
from prompt_toolkit.key_binding.bindings.focus import focus_previous
from prompt_toolkit.key_binding.defaults import load_key_bindings
from prompt_toolkit.key_binding.key_processor import KeyPressEvent
from prompt_toolkit.layout import ConditionalMargin
from prompt_toolkit.layout import HSplit
from prompt_toolkit.layout import Layout
Expand Down Expand Up @@ -55,6 +54,8 @@
if TYPE_CHECKING:
from argparse import Namespace

from prompt_toolkit.key_binding.key_processor import KeyPressEvent


@lru_cache
def pypi_package_exists(package: str) -> bool: # pragma: no cover
Expand Down Expand Up @@ -137,12 +138,12 @@ class Admonition(RadioList[_T]):
checked_style = 'class:admonition-checked'
multiple_selection = False

def __init__(
def __init__( # noqa: C901
self, # noqa: ANN101
values: Sequence[tuple[_T, Any]],
default: _T | None = None,
) -> None:
super().__init__(values, default) # pragma: no cover
) -> None: # pragma: no cover
super().__init__(values, default)
# Key bindings.
kb = KeyBindings()

Expand All @@ -151,7 +152,8 @@ def _pageup(event: KeyPressEvent) -> None:
w = event.app.layout.current_window
if w.render_info:
self._selected_index = max(
0, self._selected_index - len(w.render_info.displayed_lines),
0,
self._selected_index - len(w.render_info.displayed_lines),
)

@kb.add('pagedown')
Expand Down Expand Up @@ -201,6 +203,7 @@ def _handle_enter(self) -> None: # noqa: ANN101
def admonition_dialog(
title: str = '',
text: str = '',
heading_label: str = '',
ok_text: str = '✔ Ok',
cancel_text: str = '✘ Exit',
style: BaseStyle | None = None,
Expand Down Expand Up @@ -231,19 +234,19 @@ def ok_handler() -> None:
lines = text.splitlines()

cb_list = Admonition(values=list(zip(lines, lines)), default=None)

longest_line = len(max(lines, key=len))
dialog = Dialog(
title=title,
body=HSplit(
[Label(text='Disclaimer', dont_extend_height=True), cb_list],
[Label(text=heading_label, dont_extend_height=True), cb_list],
padding=1,
),
buttons=[
Button(text=ok_text, handler=ok_handler),
Button(text=cancel_text, handler=_return_none),
],
with_background=True,
width=len(max(lines, key=len)) + 8,
width=longest_line + 8 if longest_line > 40 else 80,
)
bindings = KeyBindings()
bindings.add('tab')(focus_next)
Expand Down Expand Up @@ -306,7 +309,7 @@ def header_input(
) -> tuple[bool | None | list[str], list[str], dict[str, str]]: # pragma: no cover
header = input_dialog(
title='ozi-new interactive prompt',
text='\n'.join(('\n'.join(prefix.values()), '\n', *args)),
text='\n'.join(args),
validator=validator,
style=_style,
cancel_text='☰ Menu',
Expand Down Expand Up @@ -342,10 +345,9 @@ def menu_loop(
while True:
match button_dialog(
title='ozi-new interactive prompt',
text='\n'.join(
('\n'.join(prefix.values()), '\n', 'Main menu, select an action:'),
),
text='Main menu, select an action:',
buttons=[
('∋ Metadata', 1),
('⚙ Options', 0),
('↺ Reset', False),
('✘ Exit', None),
Expand Down Expand Up @@ -373,13 +375,7 @@ def menu_loop(
while True:
match button_dialog(
title='ozi-new interactive prompt',
text='\n'.join(
(
'\n'.join(prefix.values()),
'\n',
'Options menu, select an option:',
),
),
text='Options menu, select an option:',
buttons=[
('Audience', 'audience'),
('Environ.', 'environment'),
Expand All @@ -400,13 +396,7 @@ def menu_loop(
('txt', 'Plaintext'),
),
title='ozi-new interactive prompt',
text='\n'.join(
(
'\n'.join(prefix.values()),
'\n',
'Please select README type:',
),
),
text='Please select README type:',
style=_style,
ok_text='✔ Ok',
cancel_text='← Back',
Expand Down Expand Up @@ -435,6 +425,17 @@ def menu_loop(
else {}
),
)
case 1:
if admonition_dialog(
title='ozi-new interactive prompt',
heading_label='PKG-INFO Metadata:',
text='\n'.join(
prefix.values() if len(prefix) > 0 else {'Name:': 'Name:'},
),
ok_text='⌂ Prompt',
cancel_text='← Back',
).run():
break
return None, output, prefix


Expand All @@ -454,6 +455,7 @@ def check_package_exists() -> Validator:
if (
admonition_dialog(
title='ozi-new interactive prompt',
heading_label='Disclaimer',
text="""
The information provided on this prompt does not, and is not intended
to, constitute legal advice. All information, content, and materials
Expand Down Expand Up @@ -483,6 +485,7 @@ def check_package_exists() -> Validator:

prefix: dict[str, str] = {}
output = ['project']
project_name = ''
while True:
result, output, prefix = header_input(
'Name',
Expand All @@ -493,6 +496,7 @@ def check_package_exists() -> Validator:
validator=DynamicValidator(check_package_exists),
)
if result is True:
project_name = prefix.get('Name', '').replace('Name', '').strip(': ')
break
if isinstance(result, list):
return result
Expand All @@ -502,7 +506,7 @@ def check_package_exists() -> Validator:
'Summary',
output,
prefix,
'What does the project do?',
f'What does the project, {project_name}, do?',
'(a short summary 1-2 sentences)',
validator=LengthValidator(),
)
Expand All @@ -516,7 +520,7 @@ def check_package_exists() -> Validator:
'Keywords',
output,
prefix,
'What are some project keywords?\n(comma-separated list)',
f'What are some keywords used to describe {project_name}?\n(comma-separated list)',
validator=LengthValidator(),
)
if result is True:
Expand All @@ -529,7 +533,7 @@ def check_package_exists() -> Validator:
'Home-page',
output,
prefix,
"What is the project's home-page URL?",
f'What is the home-page URL for {project_name}?',
validator=LengthValidator(),
)
if result is True:
Expand All @@ -542,7 +546,7 @@ def check_package_exists() -> Validator:
'Author',
output,
prefix,
'What is the author or authors name?\n(comma-separated list)',
f'Who is the author or authors of {project_name}?\n(comma-separated list)',
validator=LengthValidator(),
split_on=',',
)
Expand All @@ -556,7 +560,7 @@ def check_package_exists() -> Validator:
'Author-email',
output,
prefix,
'What are the email addresses of the author or authors?\n(comma-separated list)',
f'What are the email addresses of the author or authors of {project_name}?\n(comma-separated list)', # noqa: B950, RUF100, E501
validator=LengthValidator(),
split_on=',',
)
Expand All @@ -571,9 +575,7 @@ def check_package_exists() -> Validator:
(zip(from_prefix(Prefix().license), from_prefix(Prefix().license))),
),
title='ozi-new interactive prompt',
text='\n'.join(
('\n'.join(prefix.values()), '\n', 'Please select a license classifier:'),
),
text=f'Please select a license classifier for {project_name}:',
style=_style,
cancel_text='☰ Menu',
ok_text='✔ Ok',
Expand Down Expand Up @@ -604,19 +606,15 @@ def check_package_exists() -> Validator:
if len(possible_spdx) < 1:
license_expression = input_dialog(
title='ozi-new interactive prompt',
text='\n'.join(
('\n'.join(prefix.values()), '\n', 'Edit SPDX license expression:'),
),
text=f'Edit SPDX license expression for {project_name}:',
default='',
style=_style,
cancel_text='Skip',
).run()
elif len(possible_spdx) == 1:
license_expression = input_dialog(
title='ozi-new interactive prompt',
text='\n'.join(
('\n'.join(prefix.values()), '\n', 'Edit SPDX license expression:'),
),
text=f'Edit SPDX license expression for {project_name}:',
default=possible_spdx[0],
style=_style,
cancel_text='Skip',
Expand All @@ -626,9 +624,7 @@ def check_package_exists() -> Validator:
license_id = radiolist_dialog(
values=sorted(zip(possible_spdx, possible_spdx)),
title='ozi-new interactive prompt',
text='\n'.join(
('\n'.join(prefix.values()), '\n', 'Please select a SPDX license-id:'),
),
text=f'Please select a SPDX license-id for {project_name}:',
style=_style,
cancel_text='☰ Menu',
ok_text='✔ Ok',
Expand All @@ -640,9 +636,7 @@ def check_package_exists() -> Validator:
else:
license_expression = input_dialog(
title='ozi-new interactive prompt',
text='\n'.join(
('\n'.join(prefix.values()), '\n', 'Edit SPDX license expression:'),
),
text=f'Edit SPDX license expression for {project_name}:',
default=license_id if license_id is not None else '',
style=_style,
cancel_text='Skip',
Expand Down Expand Up @@ -671,21 +665,15 @@ def check_package_exists() -> Validator:

if yes_no_dialog(
title='ozi-new interactive prompt',
text='\n'.join(
(
'\n'.join(prefix.values()),
'\n',
'Are there any maintainers of this project?\n(other than the author or authors)',
),
),
text=f'Are there any maintainers of {project_name}?\n(other than the author or authors)',
style=_style,
).run():
while True:
result, output, prefix = header_input(
'Maintainer',
output,
prefix,
'What is the maintainer or maintainers name?\n(comma-separated list)',
f'What is the maintainer or maintainers name of {project_name}?\n(comma-separated list)', # noqa: B950, RUF100, E501
validator=LengthValidator(),
split_on=',',
)
Expand All @@ -698,7 +686,7 @@ def check_package_exists() -> Validator:
'Maintainer-email',
output,
prefix,
'What are the email addresses of the maintainer or maintainers?\n(comma-separated list)', # noqa: B950, RUF100, E501
f'What are the email addresses of the maintainer or maintainers of {project_name}?\n(comma-separated list)', # noqa: B950, RUF100, E501
validator=LengthValidator(),
split_on=',',
)
Expand All @@ -707,14 +695,14 @@ def check_package_exists() -> Validator:
if isinstance(result, list):
return result

requires_dist = []
requires_dist: list[str] = []
while button_dialog(
title='ozi-new interactive prompt',
text='\n'.join(
(
'\n'.join(prefix.values()),
'\n'.join(requires_dist),
'\n',
'Do you want to add a dependency requirement?',
f'Do you want to add a dependency requirement to {project_name}?',
),
),
buttons=[
Expand All @@ -725,7 +713,7 @@ def check_package_exists() -> Validator:
).run():
requirement = input_dialog(
title='ozi-new interactive prompt',
text='\n'.join(('\n'.join(prefix.values()), '\n', 'Search PyPI packages:')),
text='Search PyPI packages:',
validator=PackageValidator(),
style=_style,
cancel_text='← Back',
Expand Down

0 comments on commit fd9e7b0

Please sign in to comment.