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

Config lefthook #25

Merged
merged 22 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from 20 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
67 changes: 34 additions & 33 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,44 @@ name: CI

on:
push:
branches: [ main ]
branches: '*'
pull_request:
branches: [ main ]
branches: '*'

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.11'

- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
poetry install

- name: Lint with Ruff
run: |
poetry run ruff check crategen/

- name: Type check with Mypy
run: |
poetry run mypy crategen/

- name: Run security checks with Bandit
run: |
poetry run bandit -r crategen/

- name: Install test dependencies
run: |
poetry add pytest pytest-cov pytest-mock

- name: Run tests
run: |
poetry run pytest --cov=crategen
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.11'

- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
poetry install

- name: Lint with Ruff
run: |
poetry run ruff check crategen/
if: ${{ success() }}

- name: Type check with Mypy
run: |
poetry run mypy crategen/

- name: Run security checks with Bandit
run: |
poetry run bandit -r crategen/

- name: Install test dependencies
run: |
poetry add pytest pytest-cov pytest-mock

- name: Run tests
run: |
poetry run pytest --cov=crategen
33 changes: 20 additions & 13 deletions crategen/cli.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import click
import json

import click

from crategen.converter_manager import ConverterManager


@click.command()
@click.option('--input', prompt='Input file', help='Path to the input JSON file.')
@click.option('--output', prompt='Output file', help='Path to the output JSON file.')
@click.option('--conversion-type', prompt='Conversion type', type=click.Choice(['tes-to-wrroc', 'wes-to-wrroc']), help='Type of conversion to perform.')
@click.option("--input", prompt="Input file", help="Path to the input JSON file.")
@click.option("--output", prompt="Output file", help="Path to the output JSON file.")
@click.option(
"--conversion-type",
prompt="Conversion type",
type=click.Choice(["tes-to-wrroc", "wes-to-wrroc"]),
help="Type of conversion to perform.",
)
def cli(input, output, conversion_type):
"""
Command Line Interface for converting TES/WES to WRROC.
"""
"""Command Line Interface for converting TES/WES to WRROC."""
manager = ConverterManager()

# Load input data from JSON file
with open(input, 'r') as input_file:
with open(input) as input_file:
data = json.load(input_file)

# Perform the conversion based on the specified type
if conversion_type == 'tes-to-wrroc':
if conversion_type == "tes-to-wrroc":
result = manager.convert_tes_to_wrroc(data)
elif conversion_type == 'wes-to-wrroc':
elif conversion_type == "wes-to-wrroc":
result = manager.convert_wes_to_wrroc(data)

# Save the result to the output JSON file
with open(output, 'w') as output_file:
with open(output, "w") as output_file:
json.dump(result, output_file, indent=4)

if __name__ == '__main__':

if __name__ == "__main__":
cli()
1 change: 1 addition & 0 deletions crategen/converter_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .converters.tes_converter import TESConverter
from .converters.wes_converter import WESConverter


class ConverterManager:
def __init__(self):
self.tes_converter = TESConverter()
Expand Down
3 changes: 2 additions & 1 deletion crategen/converters/abstract_converter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from abc import ABC, abstractmethod


class AbstractConverter(ABC):
@abstractmethod
def convert_to_wrroc(self, data):
"""Convert data to WRROC format"""

@abstractmethod
def convert_from_wrroc(self, wrroc_data):
"""Convert WRROC data to the original format"""
51 changes: 32 additions & 19 deletions crategen/converters/tes_converter.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
from .abstract_converter import AbstractConverter
from .utils import convert_to_iso8601
from ..models import TESData, WRROCDataTES
from pydantic import ValidationError

class TESConverter(AbstractConverter):
from ..models.tes_models import TESData
from ..models.wrroc_models import WRROCDataTES
from .abstract_converter import AbstractConverter


class TESConverter(AbstractConverter):
def convert_to_wrroc(self, tes_data):
# Validate TES data
try:
validated_tes_data = TESData(**tes_data)
except ValidationError as e:
raise ValueError(f"Invalid TES data: {e}")
raise ValueError(f"Invalid TES data: {e}") from e

# Extract validated data
id = validated_tes_data.id
name = validated_tes_data.name
description = validated_tes_data.description
executors = validated_tes_data.executors
inputs = validated_tes_data.inputs
outputs = validated_tes_data.outputs
creation_time = validated_tes_data.creation_time
end_time = validated_tes_data.logs[0].end_time if validated_tes_data.logs else ""
(
id,
name,
description,
creation_time,
state,
inputs,
outputs,
executors,
resources,
volumes,
logs,
tags,
) = validated_tes_data.dict().values()
end_time = validated_tes_data.logs[0].end_time
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Use a more robust method for extracting validated data

Using .dict().values() is potentially fragile if the order of fields in the Pydantic model changes. Consider using named attributes instead, e.g., validated_tes_data.id, validated_tes_data.name, etc. This would make the code more robust and easier to understand.

Suggested change
) = validated_tes_data.dict().values()
id = validated_tes_data.id
name = validated_tes_data.name
description = validated_tes_data.description
executors = validated_tes_data.executors
inputs = validated_tes_data.inputs
outputs = validated_tes_data.outputs
volumes = validated_tes_data.volumes
logs = validated_tes_data.logs
tags = validated_tes_data.tags


# Convert to WRROC
wrroc_data = {
"@id": id,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Inline variable that is immediately returned (inline-immediately-returned-variable)

"name": name,
"description": description,
"instrument": executors[0].image if executors else None,
"object": [{"@id": input.url, "name": input.path} for input in inputs],
"result": [{"@id": output.url, "name": output.path} for output in outputs],
"startTime": convert_to_iso8601(creation_time),
"endTime": convert_to_iso8601(end_time),
"instrument": executors[0]["image"] if executors else None,
"object": [
{"@id": input["url"], "name": input["path"], "type": input["type"]}
for input in inputs
],
"result": [
{"@id": output["url"], "name": output["path"]} for output in outputs
],
"startTime": creation_time,
"endTime": end_time,
}
return wrroc_data

Expand All @@ -40,7 +53,7 @@ def convert_from_wrroc(self, data):
try:
validated_data = WRROCDataTES(**data)
except ValidationError as e:
raise ValueError(f"Invalid WRROC data for TES conversion: {e}")
raise ValueError(f"Invalid WRROC data for TES conversion: {e}") from e

# Extract validated data
id = validated_data.id
Expand Down
24 changes: 12 additions & 12 deletions crategen/converters/wes_converter.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from .abstract_converter import AbstractConverter
from .utils import convert_to_iso8601
from ..models import WESData, WRROCDataWES
from pydantic import ValidationError

class WESConverter(AbstractConverter):
from ..models.wes_models import WESData
from ..models.wrroc_models import WRROCDataWES
from ..utils import convert_to_iso8601
from .abstract_converter import AbstractConverter


class WESConverter(AbstractConverter):
def convert_to_wrroc(self, wes_data):
# Validate WES data
try:
validated_wes_data = WESData(**wes_data)
except ValidationError as e:
raise ValueError(f"Invalid WES data: {e}")
raise ValueError(f"Invalid WES data: {e}") from e

# Extract validated data
run_id = validated_wes_data.run_id
Expand All @@ -27,7 +29,9 @@ def convert_to_wrroc(self, wes_data):
"status": state,
"startTime": convert_to_iso8601(start_time),
"endTime": convert_to_iso8601(end_time),
"result": [{"@id": output.location, "name": output.name} for output in outputs],
"result": [
{"@id": output.location, "name": output.name} for output in outputs
],
}
return wrroc_data

Expand All @@ -36,7 +40,7 @@ def convert_from_wrroc(self, data):
try:
validated_data = WRROCDataWES(**data)
except ValidationError as e:
raise ValueError(f"Invalid WRROC data for WES conversion: {e}")
raise ValueError(f"Invalid WRROC data for WES conversion: {e}") from e

# Extract validated data
run_id = validated_data.id
Expand All @@ -49,11 +53,7 @@ def convert_from_wrroc(self, data):
# Convert from WRROC to WES
wes_data = {
"run_id": run_id,
"run_log": {
"name": name,
"start_time": start_time,
"end_time": end_time,
},
"run_log": {"name": name, "start_time": start_time, "end_time": end_time},
"state": state,
"outputs": [{"location": res.id, "name": res.name} for res in result_data],
}
Expand Down
Loading
Loading