13
13
# Add your new generator function here
14
14
# TODO: with some import syntax or clever moduling, it might be possible not
15
15
# to have to do anything here?
16
- from generators .ping_noauth import gen as ping_noauth
17
- from generators .list_opcodes_bad1 import gen as list_opcodes_bad1
18
- from generators .list_opcodes_directauth import gen as list_opcodes_directauth
16
+ # F401 ignored for flake8 as those methods are called through eval
17
+ from generators .ping_noauth import gen as ping_noauth # noqa: F401
18
+ from generators .list_opcodes_bad1 import gen as list_opcodes_bad1 # noqa: F401
19
+ from generators .list_opcodes_directauth import ( # noqa: F401
20
+ gen as list_opcodes_directauth ,
21
+ )
22
+
19
23
20
24
class TestSpec (object ):
21
25
"""Class to represent a test specification. Used to convert
@@ -32,15 +36,16 @@ def _traverse(key, element):
32
36
self .__dict__ .update (objd )
33
37
self .basedict = dictionary
34
38
39
+
35
40
def read_specs (folder ):
36
41
"""Read test specs from a folder"""
37
42
specfiles = [f for f in listdir (folder ) if isfile (join (folder , f ))]
38
43
specs = []
39
44
for file in specfiles :
40
45
print (f"Parsing spec file: { file } " )
41
46
# Only use the first part of the filename as spec name
42
- name = file .split ('.' )[0 ]
43
- with open (os .path .join (folder , file ), 'r' ) as f :
47
+ name = file .split ("." )[0 ]
48
+ with open (os .path .join (folder , file ), "r" ) as f :
44
49
spec = safe_load (f )
45
50
testspec = TestSpec (spec ["spec" ])
46
51
specs .append ((name , testspec ))
@@ -52,27 +57,32 @@ def generate_data(specs, output_folder):
52
57
for (name , spec ) in specs :
53
58
generate_spec_data (output_folder , spec , name )
54
59
60
+
55
61
def generate_spec_data (output_folder , spec , name ):
56
62
"""Generates data for a single spec and outputs it into the specified output folder."""
57
63
# The generator function has the same name as the test specification
58
64
(operation , result ) = eval (name )()
59
65
60
66
request_auth = create_auth (spec .request .auth )
61
67
request_content_len = spec .request .header .content_length
62
- if request_content_len == ' auto' :
68
+ if request_content_len == " auto" :
63
69
request_content_len = len (operation )
64
70
65
71
request_auth_len = spec .request .header .auth_length
66
- if request_auth_len == ' auto' :
72
+ if request_auth_len == " auto" :
67
73
request_auth_len = len (request_auth )
68
- request_header = pack_header (spec .request .header , request_auth_len , request_content_len )
74
+ request_header = pack_header (
75
+ spec .request .header , request_auth_len , request_content_len
76
+ )
69
77
70
78
response_content_len = spec .response .header .content_length
71
- if response_content_len == ' auto' :
79
+ if response_content_len == " auto" :
72
80
response_content_len = len (result )
73
81
74
82
response_auth_len = spec .response .header .auth_length
75
- response_header = pack_header (spec .response .header , response_auth_len , response_content_len )
83
+ response_header = pack_header (
84
+ spec .response .header , response_auth_len , response_content_len
85
+ )
76
86
77
87
request_buf = request_header + operation + request_auth
78
88
response_buf = response_header + result
@@ -81,13 +91,13 @@ def generate_spec_data(output_folder, spec, name):
81
91
out_data = {
82
92
"spec" : spec .basedict ,
83
93
"test_data" : {
84
- "request" : base64 .b64encode (request_buf ).decode (' ascii' ),
85
- "response" : base64 .b64encode (response_buf ).decode (' ascii' ),
86
- }
94
+ "request" : base64 .b64encode (request_buf ).decode (" ascii" ),
95
+ "response" : base64 .b64encode (response_buf ).decode (" ascii" ),
96
+ },
87
97
}
88
98
out_path = os .path .join (output_folder , name + ".test.yaml" )
89
99
print (f"Writing spec { name } test data to { out_path } " )
90
- with open (out_path , 'w' ) as f :
100
+ with open (out_path , "w" ) as f :
91
101
dump (out_data , f , sort_keys = False )
92
102
93
103
@@ -98,44 +108,48 @@ def pack_header(header, auth_len, body_len):
98
108
# packed field interpretation. See struct.pack docs for details.
99
109
# This should map to the Fixed Common Header defined here:
100
110
# https://parallaxsecond.github.io/parsec-book/parsec_client/wire_protocol.html#the-fixed-common-header
101
- return pack ('<IHBBHBQBBBIHIHH' ,
102
- header .magic_number ,
103
- header .header_size ,
104
- header .major_version_number ,
105
- header .minor_version_number ,
106
- header .flags ,
107
- header .provider ,
108
- header .session_handle ,
109
- header .content_type ,
110
- header .accept_type ,
111
- header .auth_type ,
112
- body_len ,
113
- auth_len ,
114
- header .opcode ,
115
- header .status ,
116
- 0
117
- )
111
+ return pack (
112
+ "<IHBBHBQBBBIHIHH" ,
113
+ header .magic_number ,
114
+ header .header_size ,
115
+ header .major_version_number ,
116
+ header .minor_version_number ,
117
+ header .flags ,
118
+ header .provider ,
119
+ header .session_handle ,
120
+ header .content_type ,
121
+ header .accept_type ,
122
+ header .auth_type ,
123
+ body_len ,
124
+ auth_len ,
125
+ header .opcode ,
126
+ header .status ,
127
+ 0 ,
128
+ )
118
129
119
130
120
131
def create_auth (auth_spec ):
121
132
"""Creates auth body of message"""
122
- if auth_spec .type == ' none' :
123
- return b''
124
- if auth_spec .type == ' direct' :
125
- return auth_spec .app_name .encode (' utf-8' )
126
- return b''
133
+ if auth_spec .type == " none" :
134
+ return b""
135
+ if auth_spec .type == " direct" :
136
+ return auth_spec .app_name .encode (" utf-8" )
137
+ return b""
127
138
128
139
129
140
def main ():
130
141
print ("Generating test data." )
131
142
132
- specdir = os .path .abspath (os .path .join (os .path .dirname (__file__ ), ' test_specs' ))
143
+ specdir = os .path .abspath (os .path .join (os .path .dirname (__file__ ), " test_specs" ))
133
144
print (f"Reading test specs from { specdir } " )
134
145
specs = read_specs (specdir )
135
146
136
- datadir = os .path .abspath (os .path .join (os .path .dirname (__file__ ), '../generator_output' ))
147
+ datadir = os .path .abspath (
148
+ os .path .join (os .path .dirname (__file__ ), "../generator_output" )
149
+ )
137
150
print (f"Generating test data to { datadir } " )
138
151
generate_data (specs , datadir )
139
152
153
+
140
154
if __name__ == "__main__" :
141
155
main ()
0 commit comments