Skip to content

Commit

Permalink
fix: Improvements in typing and return codes. (#224)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Butler <chris@thebutlers.me>
  • Loading branch information
butler54 authored Nov 13, 2020
1 parent 793ea7c commit c382cb5
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion tests/trestle/core/commands/init_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_init(tmpdir):
with pytest.raises(SystemExit) as pytest_wrapped_e:
cli.run()
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code is None
assert pytest_wrapped_e.value.code == 0
for directory in const.MODELTYPE_TO_MODELMODULE.keys():
assert os.path.isdir(directory)
assert os.path.isdir(os.path.join(const.TRESTLE_DIST_DIR, directory))
Expand Down
4 changes: 0 additions & 4 deletions trestle/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,3 @@ def _init_arguments(self) -> None:
def run() -> None:
"""Run the test cli."""
exit(Trestle().run())


if __name__ == '__main__':
run()
4 changes: 2 additions & 2 deletions trestle/core/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import argparse
import os
from shutil import copyfile
from typing import Union

from ilcli import Command # type: ignore

Expand All @@ -31,7 +30,7 @@ class InitCmd(Command):

name = 'init'

def _run(self, args: argparse.ArgumentParser) -> Union[int, None]:
def _run(self, args: argparse.ArgumentParser) -> int:
"""Create a trestle project in the current directory."""
dir_path = os.getcwd()

Expand All @@ -47,6 +46,7 @@ def _run(self, args: argparse.ArgumentParser) -> Union[int, None]:
except BaseException as err:
self.err(f'Initialization failed: {err}')
return 1
return 0

def _create_directories(self) -> None:
"""Create the directory tree if it does not exist."""
Expand Down
10 changes: 5 additions & 5 deletions trestle/core/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"""Dynamic Model Parser."""

import importlib
import pathlib
import warnings
from typing import Any, Dict, List, Type, cast
from typing import Any, Dict, List, Optional, Type, cast

from pydantic import Field, create_model

Expand Down Expand Up @@ -59,7 +60,6 @@ def parse_dict(data: Dict[str, Any], model_name: str) -> OscalBaseModel:

def root_key(data: Dict[str, Any]) -> str:
"""Find root model name in the data."""
warnings.warn('trestle.parser functions are deprecated', DeprecationWarning)
if len(data.items()) == 1:
return next(iter(data))

Expand All @@ -83,7 +83,7 @@ def to_class_name(name: str) -> str:
return ''.join(chars)


def to_full_model_name(root_key: str, name: str = None):
def to_full_model_name(root_key: str, name: str = None) -> Optional[str]:
"""Find model name from the root_key in the file."""
warnings.warn('trestle.parser functions are deprecated', DeprecationWarning)
try:
Expand Down Expand Up @@ -116,7 +116,7 @@ def to_full_model_name(root_key: str, name: str = None):
return None


def parse_file(file_name: str, model_name: str):
def parse_file(file_name: pathlib.Path, model_name: str) -> OscalBaseModel:
"""Load a model from the file.
Argument:
Expand Down Expand Up @@ -158,7 +158,7 @@ def wrap_for_output(model: OscalBaseModel) -> OscalBaseModel:
def wrap_for_input(raw_class: Type[OscalBaseModel]) -> Type[OscalBaseModel]:
"""In this instance we are wrapping an actual OSCAL class not an instance."""
warnings.warn(
'trestle.parser functions are deprecated. wrap_for_input built ito OSCALBaseModel', DeprecationWarning
'trestle.parser functions are deprecated. wrap_for_input built ito OscalBaseModel', DeprecationWarning
)
# TODO: Check behaviour is fine when no
class_name = raw_class.__name__
Expand Down
5 changes: 4 additions & 1 deletion trestle/oscal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.

#TODO: Ensure this is automatically updated succusfully.
OSCAL_VERSION = 'v1.0.0-milestone3'

0 comments on commit c382cb5

Please sign in to comment.