-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
376 lines (312 loc) · 12.3 KB
/
run.py
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import os
from pydantic import validate_arguments
from enum import Enum
WORKFLOW_CLASS = "{{WORKFLOW_CLASS}}"
WORKFLOW_ID = "{{WORKFLOW_ID}}"
WORKFLOW_TASK_QUEUE = "{{WORKFLOW_TASK_QUEUE}}"
WORKFLOW_ACTIVITIES = "{{WORKFLOW_ACTIVITIES}}"
WORKFLOW_NAMESPACE = "{{WORKFLOW_NAME_SPACE}}"
SERVER_LOCATION_KEY = "{{SERVER_LOCATION}}"
SERVER_LOCATION = "localhost:7233"
WORKFLOW_NAMESPACE_KEY = "{{WORKFLOW_NAMESPACE}}"
WORKFLOW_NAMESPACE = "background-check"
WORKFLOW_TASK_QUEUE_KEY = "{{WORKFLOW_TASK_QUEUE}}"
WORKFLOW_TASK_QUEUE = "poker-game-task-queue"
def read_file(filepath):
f = open(filepath, "r")
output = f.read()
f.close()
return output
class FileActionEnum(str, Enum):
append = "append"
write = "write"
class Builder_Exception(Exception):
def __init__(self, message:str="Builder_Exception raised"):
self.message = message
super().__init__(self.message)
class Builder_File_Exception(Builder_Exception):
def __init__(self, message:str="Builder_Exception raised"):
self.message = message
super().__init__(self.message)
class File_Base():
@validate_arguments
def __init__(self, source_files:list=[]):
self.source_files:list = []
self.replace_keys:dict = {}
self.content:str = ""
if source_files != []:
for source_file in source_files:
self.add_source(source_file)
@validate_arguments
def add_source(self, filename):
if not self.file_exists(filename):
raise Builder_File_Exception(f"{filename} does not exist")
self.source_files.append(filename)
self.read_source_content()
@validate_arguments
def read_source_content(self):
output = ""
for source in self.source_files:
output += self.read_from_disk(source) + "\n\n"
self.content = output
@validate_arguments
def read_from_disk(self, filename):
f = open(filename, "r")
content = f.read()
f.close()
return content
@validate_arguments
def file_exists(self, file_path:str):
if not os.path.isfile(file_path):
return False
return True
@validate_arguments
def directory_exists(self, directory:str):
"""Check if the directory passed exists"""
#if file DNE then fail
if not os.path.exists(os.path.dirname(directory)):
return False
return True
@validate_arguments
def content(self, content:str, action:FileActionEnum = "write"):
if action == "append":
self.content += content
if action == "write":
self.content = content
@validate_arguments
def get_content(self):
return self.content
class Builder_File(File_Base):
@validate_arguments
def __init__(self, destination_file:str, source_files:list=[], imports:dict[str,list]=None):
self.imports:dict = {"python": []}
self.source_files:list = []
self.replace_keys:dict = {}
self.content:str = ""
if not self.directory_exists(os.path.dirname(destination_file)):
raise Builder_File_Exception(f"The destination dir '{destination_file}' does not exist")
self.destination = destination_file
if source_files != []:
for source_file in source_files:
self.add_source(source_file)
if None != imports:
for source, importItems in imports.items():
for item in importItems:
self.add_import(source=source, name=item)
@validate_arguments
def add_import(self, name:str, source:str="python"):
if not source in self.imports.keys():
self.imports[source] = []
self.imports[source].append(name)
@validate_arguments
def add_replace_key(self, find_key:str, replace_key:str, filename=None):
if find_key in self.replace_keys.keys():
raise Builder_File_Exception(f"{find_key} is already in replace_keys for {self.destination}")
self.replace_keys[find_key] = replace_key
def __find_replace(self) -> str:
for findstr, replacestr in self.replace_keys.items():
if findstr in self.content:
self.content = self.content.replace(findstr, replacestr)
return self.content
def __create_imports(self) -> str:
output = ""
for source, importItems in self.imports.items():
itemsString = ", ".join(importItems)
if len(importItems) > 0:
if source == "python":
output += f"import {itemsString}\n"
else:
output += f"from {source} import {itemsString}\n"
return output + "\n"
def __write(self):
if not os.path.exists(os.path.dirname(self.destination)):
os.mkdir(os.path.dirname(self.destination))
f = open(self.destination, "w")
imports = self.__create_imports()
info = imports + self.__find_replace()
f.write(f"{info}")
f.close()
def commit(self):
self.__write()
class Builder():
@validate_arguments
def __init__(self, program_name:str, destination_dir:str):
program_name = program_name.lower()
self.files = {}
self.replace_keys:dict = {'all':{}}
self.destination_dir = destination_dir
self.activities = {}
@validate_arguments
def add_replace_key(self, find_key:str, replace_key:str, filename=None):
if None == filename:
self.replace_keys['all'][find_key] = replace_key
else:
if len(self.replace_keys[filename]) > 0:
self.replace_keys[filename][find_key] = replace_key
else:
self.replace_keys[filename] = {find_key: replace_key}
@validate_arguments
def __process_replace_keys(self):
for file, replace_keys in self.replace_keys.items():
if "all" == file:
"""Run through all the files and do this find/replace"""
for findstr, replacestr in replace_keys.items():
for filename, fileobj in self.files.items():
self.files[filename].add_replace_key(findstr, replacestr)
else:
"""This is a find/replace for a specific file, do it there only"""
for findstr, replacestr in replace_keys.items():
self.files[file].add_replace_key(findstr, replacestr)
@validate_arguments
def workflow_class(self, class_name:str):
self.workflow_class = class_name
@validate_arguments
def workflow_id(self, workflow_id:str):
self.workflow_id = workflow_id
@validate_arguments
def add_activity(self, name:str, source_files:list=None):
self.activities[name] = source_files
def get_activities(self):
return self.activities
@validate_arguments
def add_file(self, filename:str, destination_file:str, source_files:list=None, imports:dict=None):
self.files[filename] = Builder_File(
source_files=source_files,
destination_file=destination_file,
imports=imports
)
def __make_activity_methods(self) -> str:
output:str = ""
if len(self.activities) > 0:
for activity, source_files in self.activities.items():
file_content = File_Base(source_files)
activity_replace = file_content.get_content()
output += f"""
@activity.defn
async def {activity}() -> None:
{activity_replace}
"""
output += "\n\n"
return output
def __make_execute_activities(self) -> str:
output:str = ""
if len(self.activities) > 0:
for activity, files in self.activities.items():
output += """return await workflow.execute_activity(
{{WORKFLOW_ACTIVITY}},
start_to_close_timeout=timedelta(seconds=10),
)
""".replace("{{WORKFLOW_ACTIVITY}}", activity)
output += "\n\n"
return output
@validate_arguments
def commit(self):
self.__process_replace_keys()
for filename, fileobj in self.files.items():
if filename == "program_file":
self.files[filename].add_replace_key("{{ACTIVITY_METHODS}}", self.__make_activity_methods())
self.files[filename].add_replace_key("{{EXECUTE_ACTIVITIES}}", self.__make_execute_activities())
fileobj.commit()
print(f"Application created in {self.destination_dir}")
from pathlib import Path
def main():
path_dir = Path(os.path.dirname(__file__))
current_dir = str(path_dir)
destination_dir = str(path_dir.parent) + "/poker-destination/"
# destination_dir = str(path_dir) + "/destination/"
program_name = "poker"
builder = Builder(program_name, destination_dir)
workflow_class_name = f"{program_name.capitalize()}Workflow"
activity_name = f"{program_name}_activity"
builder.add_activity(
activity_name,
source_files = [current_dir + "/sources/poker-activity-code.py.txt"]
)
builder.add_file(
filename="gitignore",
source_files = [
current_dir + "/templates/gitignore.txt"
],
destination_file=f"{destination_dir}.gitignore"
)
builder.add_file(
filename="dataclasses",
source_files = [
current_dir + "/sources/classes.py"
],
imports = {
"python": ["random"]
},
destination_file=f"{destination_dir}dataobjs.py"
)
program_file_name = "program_file"
builder.add_file(
filename=program_file_name,
source_files = [
current_dir + "/templates/workflow.py.txt"
],
imports = {
"dataobjs": ["StandardDeck", "Player", "PokerScorer"],
"datetime": ["timedelta"],
"temporalio": ["activity", "workflow"]
},
destination_file=f"{destination_dir}{program_name}.py"
)
workflow_class_imports_list = [workflow_class_name]
builder.add_file(
filename='app',
source_files = [
current_dir + "/templates/app.py.txt"
],
imports = {
"python": ["asyncio"],
"client": ["temporal_client"],
"datetime": ["timedelta"],
"temporalio": ["service"],
"temporalio.common": ["RetryPolicy"],
program_name: workflow_class_imports_list
},
destination_file = f"{destination_dir}/app.py"
)
builder.add_file(
filename='client',
source_files = [
current_dir + "/templates/client.py.txt"
],
destination_file = f"{destination_dir}/client.py"
)
builder.add_file(
filename='worker',
source_files = [
current_dir + "/templates/worker.py.txt"
],
imports = {
"python": ["asyncio"],
"config": ["config"],
"client": ["temporal_client"],
"datetime": ["timedelta"],
"temporalio.worker": ["Worker"],
program_name: workflow_class_imports_list + [activity_name]
},
destination_file = f"{destination_dir}/worker.py"
)
builder.add_file(
filename = 'config.ini',
source_files = [current_dir + "/templates/config.ini.txt"],
destination_file = destination_dir + "/config.ini"
)
builder.add_file(
filename = 'config.py',
source_files = [current_dir + "/templates/config.py.txt"],
destination_file = destination_dir + "/config.py"
)
builder.add_replace_key(WORKFLOW_TASK_QUEUE_KEY, WORKFLOW_TASK_QUEUE)
builder.add_replace_key(WORKFLOW_NAMESPACE_KEY, WORKFLOW_NAMESPACE)
builder.add_replace_key(SERVER_LOCATION_KEY, SERVER_LOCATION)
builder.add_replace_key(WORKFLOW_CLASS, workflow_class_name)
builder.add_replace_key(WORKFLOW_ID, f"{program_name}-workflow-id")
builder.add_replace_key(WORKFLOW_TASK_QUEUE, f"{program_name}-task-queue")
builder.add_replace_key(WORKFLOW_ACTIVITIES, ", ".join(builder.get_activities().keys()))
builder.commit()
if __name__ == "__main__":
main()