Skip to content

Commit

Permalink
feat: initial project structure and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Karanjot786 committed Jul 14, 2024
1 parent 5cdc6c2 commit 113c445
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 331 deletions.
32 changes: 19 additions & 13 deletions crategen/cli.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import json
import click
import json
from crategen.converter_manager import ConverterManager

@click.command()
@click.option('--input', 'input_file', type=click.Path(exists=True), required=True, help='Path to the input file.')
@click.option('--output', 'output_file', type=click.Path(), required=True, help='Path to the output file.')
@click.option('--conversion-type', 'conversion_type', type=click.Choice(['tes_to_wrroc', 'wes_to_wrroc']), required=True, help='Type of conversion.')
def cli(input_file, output_file, conversion_type):
with open(input_file, 'r') as f:
data = json.load(f)

@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.
"""
manager = ConverterManager()

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

# Perform the conversion based on the specified type
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)

with open(output_file, 'w') as f:
json.dump(result, f, indent=2)

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

if __name__ == '__main__':
cli()
7 changes: 0 additions & 7 deletions crategen/converter_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from .converters.wes_converter import WESConverter

class ConverterManager:

def __init__(self):
self.tes_converter = TESConverter()
self.wes_converter = WESConverter()
Expand All @@ -12,9 +11,3 @@ def convert_tes_to_wrroc(self, tes_data):

def convert_wes_to_wrroc(self, wes_data):
return self.wes_converter.convert_to_wrroc(wes_data)

def convert_wrroc_to_tes(self, wrroc_data):
return self.tes_converter.convert_from_wrroc(wrroc_data)

def convert_wrroc_to_wes(self, wrroc_data):
return self.wes_converter.convert_from_wrroc(wrroc_data)
67 changes: 39 additions & 28 deletions crategen/converters/tes_converter.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
from .abstract_converter import AbstractConverter
import datetime
from ..utils.formatting import convert_to_iso8601

class TESConverter(AbstractConverter):

def convert_to_wrroc(self, tes_data):
# Implement the conversion logic from TES to WRROC
# Validate and extract data with defaults
id = tes_data.get("id", "")
name = tes_data.get("name", "")
description = tes_data.get("description", "")
executors = tes_data.get("executors", [{}])
inputs = tes_data.get("inputs", [])
outputs = tes_data.get("outputs", [])
creation_time = tes_data.get("creation_time", "")
end_time = tes_data.get("end_time", "")

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

def convert_from_wrroc(self, wrroc_data):
# Implement the conversion logic from WRROC to TES
# Validate and extract data with defaults
id = wrroc_data.get("@id", "")
name = wrroc_data.get("name", "")
description = wrroc_data.get("description", "")
instrument = wrroc_data.get("instrument", "")
object_data = wrroc_data.get("object", [])
result_data = wrroc_data.get("result", [])
start_time = wrroc_data.get("startTime", "")
end_time = wrroc_data.get("endTime", "")

# Convert from WRROC to TES
tes_data = {
"id": wrroc_data["@id"],
"name": wrroc_data.get("name", ""),
"description": wrroc_data.get("description", ""),
"executors": [{"image": wrroc_data.get("instrument")}],
"inputs": [{"url": obj["@id"], "path": obj["name"]} for obj in wrroc_data.get("object", [])],
"outputs": [{"url": res["@id"], "path": res["name"]} for res in wrroc_data.get("result", [])],
"creation_time": wrroc_data.get("startTime"),
"end_time": wrroc_data.get("endTime"),
"id": id,
"name": name,
"description": description,
"executors": [{"image": instrument}],
"inputs": [{"url": obj.get("@id", ""), "path": obj.get("name", "")} for obj in object_data],
"outputs": [{"url": res.get("@id", ""), "path": res.get("name", "")} for res in result_data],
"creation_time": start_time,
"end_time": end_time,
}
return tes_data

def convert_to_iso8601(self, timestamp):
if timestamp:
try:
return datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%SZ").isoformat() + "Z"
except ValueError:
# Handle incorrect format or other issues
return None
return None
55 changes: 31 additions & 24 deletions crategen/converters/wes_converter.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
from .abstract_converter import AbstractConverter
import datetime
from ..utils.formatting import convert_to_iso8601

class WESConverter(AbstractConverter):

def convert_to_wrroc(self, wes_data):
# Implement the conversion logic from WES to WRROC
# Validate and extract data with defaults
run_id = wes_data.get("run_id", "")
name = wes_data.get("run_log", {}).get("name", "")
state = wes_data.get("state", "")
start_time = wes_data.get("run_log", {}).get("start_time", "")
end_time = wes_data.get("run_log", {}).get("end_time", "")
outputs = wes_data.get("outputs", {})

# Convert to WRROC
wrroc_data = {
"@id": wes_data.get("run_id", ""),
"name": wes_data.get("run_log", {}).get("name", ""),
"status": wes_data.get("state", ""),
"startTime": self.convert_to_iso8601(wes_data.get("run_log", {}).get("start_time")),
"endTime": self.convert_to_iso8601(wes_data.get("run_log", {}).get("end_time")),
"result": [{"@id": output.get("location", ""), "name": output.get("name", "")} for output in wes_data.get("outputs", [])],
"@id": run_id,
"name": name,
"status": state,
"startTime": convert_to_iso8601(start_time),
"endTime": convert_to_iso8601(end_time),
"result": [{"@id": output.get("location", ""), "name": output.get("name", "")} for output in outputs],
}
return wrroc_data

def convert_from_wrroc(self, wrroc_data):
# Implement the conversion logic from WRROC to WES
# Validate and extract data with defaults
run_id = wrroc_data.get("@id", "")
name = wrroc_data.get("name", "")
start_time = wrroc_data.get("startTime", "")
end_time = wrroc_data.get("endTime", "")
state = wrroc_data.get("status", "")
result_data = wrroc_data.get("result", [])

# Convert from WRROC to WES
wes_data = {
"run_id": wrroc_data.get("@id", ""),
"run_id": run_id,
"run_log": {
"name": wrroc_data.get("name", ""),
"start_time": wrroc_data.get("startTime", ""),
"end_time": wrroc_data.get("endTime", ""),
"name": name,
"start_time": start_time,
"end_time": end_time,
},
"state": wrroc_data.get("status", ""),
"outputs": [{"location": res.get("@id", ""), "name": res.get("name", "")} for res in wrroc_data.get("result", [])],
"state": state,
"outputs": [{"location": res.get("@id", ""), "name": res.get("name", "")} for res in result_data],
}
return wes_data

def convert_to_iso8601(self, timestamp):
if timestamp:
try:
return datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%SZ").isoformat() + "Z"
except ValueError:
# Handle incorrect format or other issues
return None
return None
34 changes: 34 additions & 0 deletions crategen/utils/formatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import datetime

def convert_to_iso8601(timestamp):
"""
Convert a given timestamp to ISO 8601 format.
Handles multiple formats including RFC 3339, ISO 8601 with and without fractional seconds.
Args:
timestamp (str): The timestamp to be converted.
Returns:
str: The converted timestamp in ISO 8601 format, or None if the input format is incorrect.
"""
if timestamp:
# List of supported formats
formats = [
"%Y-%m-%dT%H:%M:%S.%fZ", # RFC 3339 with fractional seconds
"%Y-%m-%dT%H:%M:%SZ", # RFC 3339 without fractional seconds
"%Y-%m-%dT%H:%M:%S%z", # ISO 8601 with timezone
"%Y-%m-%dT%H:%M:%S.%f%z", # ISO 8601 with fractional seconds and timezone
]
for fmt in formats:
try:
dt = datetime.datetime.strptime(timestamp, fmt)
iso_format = dt.isoformat()
# Strip fractional seconds if not needed
if '.' in iso_format:
iso_format = iso_format.split('.')[0]
return iso_format + 'Z'
except ValueError:
continue
# Handle incorrect format or other issues
return None
return None
Loading

0 comments on commit 113c445

Please sign in to comment.