Skip to content

Commit bb739e6

Browse files
author
Madavaraj17
committed
Code Improvements for exporter.py
1 parent 53ec704 commit bb739e6

File tree

1 file changed

+51
-31
lines changed

1 file changed

+51
-31
lines changed

jina/exporter.py

+51-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from typing import List
23

34
from jina.orchestrate.flow.base import Flow
45
from jina.orchestrate.deployments import Deployment
@@ -7,55 +8,72 @@
78
from jina.schemas import get_full_schema
89
from jina_cli.export import api_to_dict
910

10-
1111
def export_kubernetes(args):
1212
"""Export to k8s yaml files
1313
1414
:param args: args from CLI
1515
"""
1616
from jina.jaml import JAMLCompatible
1717

18-
obj = JAMLCompatible.load_config(args.config_path)
18+
try:
19+
obj = JAMLCompatible.load_config(args.config_path)
20+
except Exception as e:
21+
default_logger.error(f"Failed to load config: {e}")
22+
raise
1923

2024
if isinstance(obj, (Flow, Deployment)):
21-
obj.to_kubernetes_yaml(
22-
output_base_path=args.outpath, k8s_namespace=args.k8s_namespace
23-
)
25+
try:
26+
obj.to_kubernetes_yaml(
27+
output_base_path=args.outpath, k8s_namespace=args.k8s_namespace
28+
)
29+
default_logger.info(f'Kubernetes YAML exported to {args.outpath}')
30+
except Exception as e:
31+
default_logger.error(f"Failed to export to Kubernetes YAML: {e}")
32+
raise
2433
else:
2534
raise NotImplementedError(
2635
f'Object of class {obj.__class__.__name__} cannot be exported to Kubernetes'
2736
)
2837

29-
3038
def export_docker_compose(args):
3139
"""Export to Docker compose yaml files
3240
3341
:param args: args from CLI
3442
"""
35-
3643
from jina.jaml import JAMLCompatible
3744

38-
obj = JAMLCompatible.load_config(args.config_path)
45+
try:
46+
obj = JAMLCompatible.load_config(args.config_path)
47+
except Exception as e:
48+
default_logger.error(f"Failed to load config: {e}")
49+
raise
3950

4051
if isinstance(obj, (Flow, Deployment)):
41-
obj.to_docker_compose_yaml(
42-
output_path=args.outpath, network_name=args.network_name
43-
)
52+
try:
53+
obj.to_docker_compose_yaml(
54+
output_path=args.outpath, network_name=args.network_name
55+
)
56+
default_logger.info(f'Docker Compose YAML exported to {args.outpath}')
57+
except Exception as e:
58+
default_logger.error(f"Failed to export to Docker Compose YAML: {e}")
59+
raise
4460
else:
4561
raise NotImplementedError(
4662
f'Object of class {obj.__class__.__name__} cannot be exported to Docker Compose'
4763
)
4864

49-
5065
def export_flowchart(args):
5166
"""Export to flowchart file
5267
5368
:param args: args from CLI
5469
"""
55-
Flow.load_config(args.config_path).plot(
56-
args.outpath, vertical_layout=args.vertical_layout
57-
)
58-
70+
try:
71+
flow = Flow.load_config(args.config_path)
72+
flow.plot(args.outpath, vertical_layout=args.vertical_layout)
73+
default_logger.info(f'Flowchart exported to {args.outpath}')
74+
except Exception as e:
75+
default_logger.error(f"Failed to export flowchart: {e}")
76+
raise
5977

6078
def export_schema(args):
6179
"""Export to JSON Schemas
@@ -64,26 +82,28 @@ def export_schema(args):
6482
"""
6583
from jina import __version__
6684

85+
def dump_data(dump_api: dict, paths: List[str], extension: str):
86+
for path in paths:
87+
f_name = (path % __version__) if '%s' in path else path
88+
try:
89+
with open(f_name, 'w', encoding='utf-8') as fp:
90+
if extension == 'yaml':
91+
JAML.dump(dump_api, fp)
92+
elif extension == 'json':
93+
json.dump(dump_api, fp, sort_keys=True)
94+
default_logger.info(f'API is exported to {f_name}')
95+
except Exception as e:
96+
default_logger.error(f"Failed to export schema to {f_name}: {e}")
97+
raise
98+
6799
if args.yaml_path:
68100
dump_api = api_to_dict()
69-
for yp in args.yaml_path:
70-
f_name = (yp % __version__) if '%s' in yp else yp
71-
with open(f_name, 'w', encoding='utf-8') as fp:
72-
JAML.dump(dump_api, fp)
73-
default_logger.info(f'API is exported to {f_name}')
101+
dump_data(dump_api, args.yaml_path, 'yaml')
74102

75103
if args.json_path:
76104
dump_api = api_to_dict()
77-
for jp in args.json_path:
78-
f_name = (jp % __version__) if '%s' in jp else jp
79-
with open(f_name, 'w', encoding='utf-8') as fp:
80-
json.dump(dump_api, fp, sort_keys=True)
81-
default_logger.info(f'API is exported to {f_name}')
105+
dump_data(dump_api, args.json_path, 'json')
82106

83107
if args.schema_path:
84108
dump_api = get_full_schema()
85-
for jp in args.schema_path:
86-
f_name = (jp % __version__) if '%s' in jp else jp
87-
with open(f_name, 'w', encoding='utf-8') as fp:
88-
json.dump(dump_api, fp, sort_keys=True)
89-
default_logger.info(f'API is exported to {f_name}')
109+
dump_data(dump_api, args.schema_path, 'json')

0 commit comments

Comments
 (0)