-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathpyodata
More file actions
executable file
·200 lines (148 loc) · 6.54 KB
/
pyodata
File metadata and controls
executable file
·200 lines (148 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python3
import sys
from argparse import ArgumentParser
import pyodata
from pyodata.policies import PolicyFatal, PolicyWarning, PolicyIgnore, ParserError
from pyodata.config import Config
from pyodata.v2 import ODataV2
from pyodata.v4 import ODataV4
import requests
from getpass import getpass
ERROR_POLICIES={
'FATAL': PolicyFatal,
'WARNING': PolicyWarning,
'IGNORE': PolicyIgnore
}
POLICY_TARGETS={
'PARSER_INVALID_PROPERTY': ParserError.PROPERTY,
'PARSER_INVALID_ANNOTATION': ParserError.ANNOTATION,
'PARSER_INVALID_ASSOCIATION': ParserError.ASSOCIATION,
'PARSER_INVALID_ENUM_TYPE': ParserError.ENUM_TYPE,
'PARSER_INVALID_ENTITY_TYPE': ParserError.ENTITY_TYPE,
'PARSER_INVALID_COMPLEX_TYPE': ParserError.COMPLEX_TYPE
}
def print_out_metadata_info(args, client):
print('[Printing out all Entity Sets ...]')
for es in client.schema.entity_sets:
print(es.name)
proprties = es.entity_type.proprties()
for prop in es.entity_type.key_proprties:
print(f' K {prop.name}({prop.typ.name})')
proprties.remove(prop)
for prop in proprties:
print(f' + {prop.name}({prop.typ.name})')
for prop in es.entity_type.nav_proprties:
if client.schema.config.odata_version == ODataV2:
print(f' + {prop.name}({prop.to_role.entity_type_name})')
else:
if prop.partner:
print(f' + {prop.name}({prop.partner.name})')
else:
print(f' + {prop.name}')
for fs in client.schema.function_imports:
print(f'{fs.http_method} {fs.name}')
for param in fs.parameters:
print(f' > {param.name}({param.typ.name})')
if fs.return_type is not None:
print(f' < {fs.return_type.name}')
def print_out_entity_set(args, client):
typ = client.schema.entity_set(args.name).entity_type
request = getattr(client.entity_sets, args.name).get_entities()
entities = request.execute()
keys = {kp.name for kp in typ.key_proprties}
members = {mp.name for mp in typ.proprties() if mp.name not in keys}
for entity in entities:
for prop in keys:
print(f'K {prop} = {getattr(entity, prop)}')
for prop in members:
print(f'+ {prop} = {getattr(entity, prop)}')
print('-' * 80)
def print_out_function_import(args, client):
function = getattr(client.functions, args.name)
response = function.execute()
print(response)
def _parse_args(argv):
parser = ArgumentParser()
parser.add_argument('SERVICE_ROOT_URL', type=str)
parser.add_argument('--user', default=None, type=str)
parser.add_argument('--password', default=None, type=str)
parser.add_argument('--metadata', default=None, type=str,
help='Path to the XML file with service $metadata')
parser.add_argument('--no-session-init', default=False,
action='store_true', help='Skip HTTP session initialization')
parser.add_argument('--default-error-policy', default=None, choices=ERROR_POLICIES.keys(),
help='Specify metadata parser default error handler')
parser.add_argument('--custom-error-policy', action='append', type=str,
help='Specify metadata parser custom error handlers in the form: TARGET=POLICY')
parser.add_argument('--version', default=2, choices=[2, 4], type=int)
parser.set_defaults(func=print_out_metadata_info)
subparsers = parser.add_subparsers()
entity_set_parser = subparsers.add_parser('ENTITY_SET')
entity_set_parser.add_argument('name', type=str)
entity_set_parser.set_defaults(func=print_out_entity_set)
func_import_parser = subparsers.add_parser('FUNCTION_IMPORT')
func_import_parser.add_argument('name', type=str)
func_import_parser.add_argument('PARAMETERS', nargs='*', type=str,
help='NAME=VALUE pairs')
func_import_parser.set_defaults(func=print_out_function_import)
args = parser.parse_args(argv[1:])
return args
def _main(argv):
args = _parse_args(argv)
session = requests.Session()
if args.user is not None:
if args.password is None:
args.password = getpass(f'Enter password for {args.user}: ')
session.auth = (args.user, args.password)
# Oh, I am sorry for the double negation,
# but I cannot find a better construction.
if not args.no_session_init:
print('[Initializing HTTP session ...]')
try:
session.head(args.SERVICE_ROOT_URL)
args.password = 'xxxxx'
except pyodata.exceptions.HttpError as err:
sys.stderr.write(str(err))
sys.stderr.write('\n')
sys.exit(1)
static_metadata = None
if args.metadata:
print(f'[Loading $metadata from: {args.metadata} ...]')
with open(args.metadata, 'rb') as mtd_fl:
static_metadata = mtd_fl.read()
else:
print('[Fetching $metadata ...]')
config = None
def get_config():
if config is None:
version = ODataV4
if args.version == 2:
version = ODataV2
return Config(odata_version=version)
return config
config = get_config()
if args.default_error_policy:
config = get_config()
config.set_default_error_policy(ERROR_POLICIES[args.default_error_policy]())
if args.custom_error_policy:
custom_policies = dict()
try:
for target, policy in (param.split('=') for param in args.custom_error_policy):
try:
custom_policies[POLICY_TARGETS[target]] = ERROR_POLICIES[policy]()
except KeyError as ex:
print(f'Invalid Error Target ({target}) or Error Policy ({policy}): {str(ex)}', file=sys.stderr)
print('Allowed targets : {}'.format(';'.join(POLICY_TARGETS.keys())), file=sys.stderr)
print('Allowed policies: {}'.format(';'.join(ERROR_POLICIES.keys())), file=sys.stderr)
sys.exit(1)
except ValueError as ex:
print('Custom policy must have the format TARGET=POLICY: {}'.format(' '.join(args.custom_error_policy)), file=sys.stderr)
sys.exit(1)
config = get_config()
config.set_custom_error_policy(custom_policies)
client = pyodata.Client(args.SERVICE_ROOT_URL, session, metadata=static_metadata, config=config)
args.func(args, client)
print('[Done!]')
return 0
if __name__ == '__main__':
sys.exit(_main(sys.argv))