Skip to content

Commit

Permalink
fix: Use __package__ to determine execution start (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
lfvjimisola authored Feb 19, 2024
1 parent e17538a commit 3402e4a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
12 changes: 1 addition & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,16 @@ readme = "README.md"
requires-python = ">=3.10"

classifiers = [
# How mature is this project? Common values are
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
"Development Status :: 4 - Beta",

# Indicate who your project is intended for
"Intended Audience :: Developers",
"Topic :: Software Development :: Build Tools",

# Pick your license as you wish (should match "license" above)
"License :: OSI Approved :: MIT License",

# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",

"Operating System :: OS Independent",
]
Expand All @@ -52,9 +45,6 @@ openapi-to-asciidoc = "openapi_to_asciidoc.convert:main"
[tool.hatch.version]
source = "vcs"

[tool.hatch.build.hooks.vcs]
version-file = "_version.py"

[tool.hatch.version.raw-options]
local_scheme = "no-local-version"

Expand Down
50 changes: 40 additions & 10 deletions src/openapi_to_asciidoc/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,50 @@

import argparse
import json
import os
import sys
from importlib.metadata import version
from typing import TextIO, Union

from openapi_to_asciidoc.objects import OpenApi, OpenApiSchema
if __package__ is None:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from openapi_to_asciidoc.objects import OpenApi, OpenApiSchema
else:
from openapi_to_asciidoc.objects import OpenApi, OpenApiSchema


@staticmethod
def create_directory_and_open(file_path: Union[TextIO, str]) -> TextIO:
"""
Create the directory if it doesn't exist and open the specified file.
Parameters:
- file_path (TextIO (sys.stdout) or str (path as argument on command line): The file path.
- mode (str): The mode in which the file should be opened.
Returns:
- TextIO: The opened file.
If the file path is sys.stdout, it is returned as is without attempting to create the directory.
"""
if file_path == sys.stdout:
return file_path

directory = os.path.dirname(os.path.abspath(file_path))

if not os.path.exists(directory):
os.makedirs(directory)

return open(file_path, "w")


def get_arguments():
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter)

parser.add_argument(
"-V",
"--version",
action="version",
version=version("maven-artifact"),
)
ver: str = "local dev" if __package__ is None else f"{version('openapi-to-asciidoc')}"

parser.add_argument("-V", "--version", action="version", version=ver)

parser.add_argument(
"-j",
"--json",
Expand All @@ -30,9 +60,9 @@ def get_arguments():
"-o",
"--output",
nargs="?",
help="Where to output result (default: openapi.adoc)",
type=argparse.FileType("w"),
default="openapi.adoc",
help="Where to output result (default: stdout)",
type=lambda file: create_directory_and_open(file),
default=sys.stdout,
)

return parser.parse_args()
Expand Down

0 comments on commit 3402e4a

Please sign in to comment.