Skip to content

Commit

Permalink
Merge pull request strictdoc-project#1303 from strictdoc-project/stan…
Browse files Browse the repository at this point in the history
…islaw/error_handling

cli and server: validate --config parameter
  • Loading branch information
stanislaw authored Sep 14, 2023
2 parents cb500ef + 5c336ad commit a322fa7
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 36 deletions.
2 changes: 1 addition & 1 deletion strictdoc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from strictdoc.core.environment import SDocRuntimeEnvironment

__version__ = "0.0.44a8"
__version__ = "0.0.44a9"


environment = SDocRuntimeEnvironment(__file__)
82 changes: 76 additions & 6 deletions strictdoc/cli/cli_arg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from strictdoc.helpers.auto_described import auto_described


class CLIValidationError(Exception):
pass


class ImportReqIFCommandConfig:
def __init__(self, input_path: str, output_path: str, profile):
self.input_path: str = input_path
Expand All @@ -15,7 +19,27 @@ def __init__(self, input_path: str, output_path: str, profile):
class ManageAutoUIDCommandConfig:
def __init__(self, *, input_path: str, config_path: Optional[str]):
self.input_path: str = input_path
self.config_path: Optional[str] = config_path
self._config_path: Optional[str] = config_path

def get_path_to_config(self) -> Optional[str]:
path_to_input_dir = self.input_path
if os.path.isfile(path_to_input_dir):
path_to_input_dir = os.path.dirname(path_to_input_dir)
path_to_config = (
self._config_path
if self._config_path is not None
else path_to_input_dir
)
return path_to_config

def validate(self):
if self._config_path is not None and not os.path.exists(
self._config_path
):
raise CLIValidationError(
"Provided path to a configuration file does not exist: "
f"{self._config_path}"
)


class ImportExcelCommandConfig:
Expand All @@ -42,14 +66,36 @@ def __init__(
reload: bool,
port: Optional[int],
):
assert os.path.exists(input_path)
abs_input_path = os.path.abspath(input_path)
self.input_path: str = abs_input_path
self._input_path: str = input_path
self.output_path: Optional[str] = output_path
self.config_path: Optional[str] = config_path
self._config_path: Optional[str] = config_path
self.reload: bool = reload
self.port: Optional[int] = port

def get_full_input_path(self):
return os.path.abspath(self._input_path)

def get_path_to_config(self):
return (
self._config_path
if self._config_path is not None
else self._input_path
)

def validate(self):
if not os.path.exists(self._input_path):
raise CLIValidationError(
f"Provided input path does not exist: {self._input_path}"
)

if self._config_path is not None and not os.path.exists(
self._config_path
):
raise CLIValidationError(
"Provided path to a configuration file does not exist: "
f"{self._config_path}"
)


@auto_described
class ExportCommandConfig: # pylint: disable=too-many-instance-attributes
Expand All @@ -69,7 +115,7 @@ def __init__( # pylint: disable=too-many-arguments
assert isinstance(input_paths, list), f"{input_paths}"
self.input_paths: List[str] = input_paths
self.output_dir: str = output_dir
self.config_path: Optional[str] = config_path
self._config_path: Optional[str] = config_path
self.project_title: Optional[str] = project_title
self.formats = formats
self.fields = fields
Expand All @@ -81,6 +127,30 @@ def __init__( # pylint: disable=too-many-arguments
)
self.output_html_root: str = os.path.join(output_dir, "html")

def get_path_to_config(self) -> Optional[str]:
path_to_input_dir = self.input_paths[0]
if os.path.isfile(path_to_input_dir):
path_to_input_dir = os.path.dirname(path_to_input_dir)
path_to_config = (
self._config_path
if self._config_path is not None
else path_to_input_dir
)
return path_to_config

def validate(self):
for input_path_ in self.input_paths:
if not os.path.exists(input_path_):
raise CLIValidationError(
f"Provided input path does not exist: {input_path_}"
)
if self._config_path is not None:
if not os.path.exists(self._config_path):
raise CLIValidationError(
"Provided path to a configuration file does not exist: "
f"{self._config_path}"
)


class DumpGrammarCommandConfig:
def __init__(self, output_file):
Expand Down
45 changes: 19 additions & 26 deletions strictdoc/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from strictdoc import environment
from strictdoc.cli.cli_arg_parser import (
CLIValidationError,
DumpGrammarCommandConfig,
ExportCommandConfig,
ImportExcelCommandConfig,
Expand Down Expand Up @@ -61,17 +62,14 @@ def _main(parallelizer):

elif parser.is_export_command:
config: ExportCommandConfig = parser.get_export_config()
path_to_input_dir = config.input_paths[0]
if os.path.isfile(path_to_input_dir):
path_to_input_dir = os.path.dirname(path_to_input_dir)
path_to_config = (
config.config_path
if config.config_path is not None
else path_to_input_dir
)
try:
config.validate()
except CLIValidationError as exception_:
print(f"error: {exception_.args[0]}") # noqa: T201
sys.exit(1)
project_config: ProjectConfig = (
ProjectConfigLoader.load_from_path_or_get_default(
path_to_config=path_to_config,
path_to_config=config.get_path_to_config(),
environment=environment,
)
)
Expand All @@ -92,14 +90,14 @@ def _main(parallelizer):

elif parser.is_server_command:
server_config = parser.get_server_config()
path_to_config = (
server_config.config_path
if server_config.config_path is not None
else server_config.input_path
)
try:
server_config.validate()
except CLIValidationError as exception_:
print(f"error: {exception_.args[0]}") # noqa: T201
sys.exit(1)
project_config: ProjectConfig = (
ProjectConfigLoader.load_from_path_or_get_default(
path_to_config=path_to_config,
path_to_config=server_config.get_path_to_config(),
environment=environment,
)
)
Expand All @@ -123,19 +121,14 @@ def _main(parallelizer):

elif parser.is_manage_autouid_command:
config: ManageAutoUIDCommandConfig = parser.get_manage_autouid_config()
path_to_input_dir = (
config.input_path
if not os.path.isfile(config.input_path)
else os.path.dirname(config.input_path)
)
path_to_config = (
config.config_path
if config.config_path is not None
else path_to_input_dir
)
try:
config.validate()
except CLIValidationError as exception_:
print(f"error: {exception_.args[0]}") # noqa: T201
sys.exit(1)
project_config: ProjectConfig = (
ProjectConfigLoader.load_from_path_or_get_default(
path_to_config=path_to_config,
path_to_config=config.get_path_to_config(),
environment=environment,
)
)
Expand Down
2 changes: 1 addition & 1 deletion strictdoc/server/routers/main_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def create_main_router(

# FIXME: Remove this unused export config.
_export_config = ExportCommandConfig(
input_paths=[server_config.input_path],
input_paths=[server_config.get_full_input_path()],
output_dir=server_config.output_path,
config_path=None,
project_title=project_config.project_title,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]

features = [
"PROJECT_STATISTICS_SCREEN"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[DOCUMENT]
TITLE: Hello world doc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RUN: %expect_exit 1 %strictdoc export /path/does/not/exist | filecheck %s --dump-input=fail
RUN: %expect_exit 1 %strictdoc server /path/does/not/exist | filecheck %s --dump-input=fail

CHECK: error: Provided input path does not exist: {{.*}}/path/does/not/exist
6 changes: 4 additions & 2 deletions tests/unit/strictdoc/cli/test_cli_arg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def test_export_01_minimal():
assert export_config.fields == args.fields
assert export_config.formats == args.formats
assert export_config.input_paths == args.input_paths
assert export_config.config_path is None
# When no explicit --config path provided, the path to config defaults to
# the input path.
assert export_config.get_path_to_config() == "docs"
assert export_config.no_parallelization == args.no_parallelization
assert export_config.output_dir == os.path.join(os.getcwd(), "output")

Expand Down Expand Up @@ -195,7 +197,7 @@ def test_export_09_config():

config_parser = create_sdoc_args_parser(args)
export_config = config_parser.get_export_config()
assert export_config.config_path == "/path/to/strictdoc.toml"
assert export_config.get_path_to_config() == "/path/to/strictdoc.toml"


def test_passthrough_01_minimal():
Expand Down

0 comments on commit a322fa7

Please sign in to comment.