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

Fix and expand "open" command #143

Merged
merged 1 commit into from
Aug 29, 2024
Merged
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
40 changes: 24 additions & 16 deletions rosdoc2/verbs/open/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,33 @@ def prepare_arguments(parser):
'package_output_directory',
nargs='?',
default=DEFAULT_OUTPUT_DIR,
help='path where the built documentation for the package was output into',
help=f'(optional) path to the built documentation (default "{DEFAULT_OUTPUT_DIR}") '
'OR package name',
)
return parser


def main(options):
"""Execute the program."""
"""Open a web browser to display the built documentation."""
# Locate the entry point for the built documentation.
if not os.path.exists(options.package_output_directory):
sys.exit(f"given output directory '{options.package_output_directory}' does not exist")

if os.path.isfile(options.package_output_directory):
# Open directly.
webbrowser.open(f'file://{os.path.abspath(options.package_output_directory)}')
return

# TODO(wjwwood): fix this up, the expected path doesn't make sense now...
expected_path = os.path.join(options.package_output_directory, 'build', 'index.html')
if not os.path.exists(expected_path):
sys.exit(f"did not find package documentation at the expected path '{expected_path}'")

webbrowser.open(f'file://{os.path.abspath(expected_path)}')
path_to_open = None
if os.path.isdir(options.package_output_directory):
path_to_open = options.package_output_directory
# Maybe this is a package directory?
if os.path.isfile(os.path.join(path_to_open, 'index.html')):
# open it directly
path_to_open = os.path.join(path_to_open, 'index.html')
elif os.path.isfile(options.package_output_directory):
path_to_open = options.package_output_directory
else:
# Last chance: the default output_dir plus a package name
candidate = os.path.join(
DEFAULT_OUTPUT_DIR, options.package_output_directory, 'index.html')
if os.path.isfile(candidate):
path_to_open = candidate

if path_to_open:
webbrowser.open(f'file://{os.path.abspath(path_to_open)}')
else:
sys.exit('did not find package documentation at given package_output_directory '
f'"{options.package_output_directory}"')