Skip to content

Commit b399f8a

Browse files
committed
bin: enable specifying error policies
To be able to test parsing without the need to write custom code. Usage: pyodata URL --default-error-policy FATAL \ --custom-error-policy PARSER_INVALID_ANNOTATION=IGNORE \ --custom-error-policy PARSER_INVALID_ENUM_TYPE=IGNORE
1 parent 547fc1e commit b399f8a

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

bin/pyodata

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,29 @@ import sys
44
from argparse import ArgumentParser
55

66
import pyodata
7+
from pyodata.v2.model import PolicyFatal, PolicyWarning, PolicyIgnore, ParserError, Config
8+
79
import requests
810

911
from getpass import getpass
1012

1113

14+
ERROR_POLICIES={
15+
'FATAL': PolicyFatal,
16+
'WARNING': PolicyWarning,
17+
'IGNORE': PolicyIgnore
18+
}
19+
20+
POLICY_TARGETS={
21+
'PARSER_INVALID_PROPERTY': ParserError.PROPERTY,
22+
'PARSER_INVALID_ANNOTATION': ParserError.ANNOTATION,
23+
'PARSER_INVALID_ASSOCIATION': ParserError.ASSOCIATION,
24+
'PARSER_INVALID_ENUM_TYPE': ParserError.ENUM_TYPE,
25+
'PARSER_INVALID_ENTITY_TYPE': ParserError.ENTITY_TYPE,
26+
'PARSER_INVALID_COMPLEX_TYPE': ParserError.COMPLEX_TYPE
27+
}
28+
29+
1230
def print_out_metadata_info(args, client):
1331
print('[Printing out all Entity Sets ...]')
1432
for es in client.schema.entity_sets:
@@ -59,7 +77,6 @@ def print_out_function_import(args, client):
5977
response = function.execute()
6078
print(response)
6179

62-
6380
def _parse_args(argv):
6481
parser = ArgumentParser()
6582
parser.add_argument('SERVICE_ROOT_URL', type=str)
@@ -69,6 +86,11 @@ def _parse_args(argv):
6986
help='Path to the XML file with service $metadata')
7087
parser.add_argument('--no-session-init', default=False,
7188
action='store_true', help='Skip HTTP session initialization')
89+
parser.add_argument('--default-error-policy', default=None, choices=ERROR_POLICIES.keys(),
90+
help='Specify metadata parser default error handler')
91+
parser.add_argument('--custom-error-policy', action='append', type=str,
92+
help='Specify metadata parser custom error handlers in the form: TARGET=POLICY')
93+
7294
parser.set_defaults(func=print_out_metadata_info)
7395

7496
subparsers = parser.add_subparsers()
@@ -119,7 +141,38 @@ def _main(argv):
119141
else:
120142
print('[Fetching $metadata ...]')
121143

122-
client = pyodata.Client(args.SERVICE_ROOT_URL, session, metadata=static_metadata)
144+
config = None
145+
146+
def get_config():
147+
if config is None:
148+
return Config()
149+
150+
return config
151+
152+
if args.default_error_policy:
153+
config = get_config()
154+
config.set_default_error_policy(ERROR_POLICIES[args.default_error_policy]())
155+
156+
if args.custom_error_policy:
157+
custom_policies = dict()
158+
159+
try:
160+
for target, policy in (param.split('=') for param in args.custom_error_policy):
161+
try:
162+
custom_policies[POLICY_TARGETS[target]] = ERROR_POLICIES[policy]()
163+
except KeyError as ex:
164+
print(f'Invalid Error Target ({target}) or Error Policy ({policy}): {str(ex)}', file=sys.stderr)
165+
print('Allowed targets : {}'.format(';'.join(POLICY_TARGETS.keys())), file=sys.stderr)
166+
print('Allowed policies: {}'.format(';'.join(ERROR_POLICIES.keys())), file=sys.stderr)
167+
sys.exit(1)
168+
except ValueError as ex:
169+
print('Custom policy must have the format TARGET=POLICY: {}'.format(' '.join(args.custom_error_policy)), file=sys.stderr)
170+
sys.exit(1)
171+
172+
config = get_config()
173+
config.set_custom_error_policy(custom_policies)
174+
175+
client = pyodata.Client(args.SERVICE_ROOT_URL, session, metadata=static_metadata, config=config)
123176

124177
args.func(args, client)
125178

0 commit comments

Comments
 (0)