-
Notifications
You must be signed in to change notification settings - Fork 6
/
gen_sync_code
executable file
·62 lines (48 loc) · 1.74 KB
/
gen_sync_code
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
#! /usr/bin/env python3
from typing import List, Tuple
import os
files = [('aio_odoorpc/aio_odoorpc.py', 'aio_odoorpc/odoorpc.py')]
delete_lines = ['aw = asyncio.create_task(aw)',
'await asyncio.sleep(0)']
repl = [('aio_odoorpc_base.aio', 'aio_odoorpc_base.sync'),
('T_AsyncHttpClient', 'T_HttpClient'),
('AsyncOdooRPC', 'OdooRPC'),
('async def', 'def'),
('aw = self.execute_kw', 'data = self.execute_kw'),
('(awaitable=aw', '(data=data'),
('await ', ''),
('_aio_', '_')]
def convert_async_to_sync(filename_async: str, filename_sync: str, repl: List[Tuple], delete_lines: List[str]):
dt_mod_async = os.path.getmtime(filename_async)
dt_mod_script = os.path.getmtime(__file__)
try:
dt_mod_sync = os.path.getmtime(filename_sync)
except:
dt_mod_sync = 0
if dt_mod_async < dt_mod_sync and dt_mod_script < dt_mod_sync:
return
lines = []
with open(filename_async, 'r') as file:
for line in file:
line_s = line.strip()
keep = True
for t in delete_lines:
if t == line_s:
print(f'Deleted line: {t}')
keep = False
break
if keep:
lines.append(line)
f_async = ''.join(lines)
f_sync = f_async
for t in repl:
index = f_sync.find(t[0])
if index >= 0:
f_sync = f_sync.replace(t[0], t[1])
else:
print(f'No match for rule: {t[0]} -> {t[1]}')
with open(filename_sync, 'w') as f:
f.write(f_sync)
os.system(f'autopep8 -i {filename_sync}')
for f in files:
convert_async_to_sync(f[0], f[1], repl, delete_lines)