-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_template.py
More file actions
80 lines (49 loc) · 1.48 KB
/
util_template.py
File metadata and controls
80 lines (49 loc) · 1.48 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
from __future__ import annotations
import os
from importlib import import_module, reload
from util_web import *
TemplateFolder = 'template'
TemplateKey = 'Template'
TemplateArgsKey = 'TemplateArgs'
ShowTemplate = 'ShowTemplate'
GenerateCode = 'GenerateCode'
ShowHomePage = 'ShowHomePage'
PageType = ShowTemplate
def currentSourceFile() -> str:
return getSessionState(TemplateKey)
def editCurrentTemplate():
cmd = f'code {TemplateFolder}{os.sep}{currentSourceFile()}'
print(cmd)
os.popen(cmd)
def currentSource() -> str:
return openTemplate(currentSourceFile())
def currentTemplate():
path = currentSourceFile()
if path is None:
return None
model = path.replace('.py', '')
t = import_module(model)
reload(t)
return t.template
def openTemplate(fName: str) -> str:
return openFile(f'{TemplateFolder}{os.sep}{fName}')
def openFile(path: str) -> str:
with open(path, 'r', encoding='utf-8') as f:
return f.read()
def writeFile(path: str, content: str):
try:
folder = os.path.dirname(path)
if not os.path.exists(folder):
os.makedirs(folder)
with open(path, 'w', encoding='utf-8') as f:
print(path)
print(content)
f.write(content)
except Exception as e:
print('writeFile', e)
def toShowTemplate():
setSessionState(PageType, ShowTemplate)
def toHomePage():
setSessionState(PageType, ShowHomePage)
if __name__ == '__main__':
pass