-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathExporter.py
87 lines (67 loc) · 2.38 KB
/
Exporter.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
# -*- coding: utf-8 -*-
import os
import shutil
from Cheetah.Template import Template
import ppconfig
import generators
from cores import PCodes
from cores.PParser import PParser
def create_folder(folder, recreate = False):
if os.path.exists(folder):
if recreate:
shutil.rmtree(folder)
os.mkdir(folder)
else:
os.mkdir(folder)
class Exporter(object):
def run(self, option):
self.option = option
outputPath = ppconfig.OUTPUT_PATH
inputPath = ppconfig.INPUT_PATH
namespace = {
"NAME" : os.path.basename(inputPath),
"SOURCE_FILE" : inputPath,
"OUTPUT_PATH" : outputPath,
}
configCachePath = str(Template(ppconfig.MESSAGE_CONFIG_CACHE_FILE, searchList = [namespace, ppconfig]))
self.module = PCodes.Module()
self.module.loadConfigCache(configCachePath)
create_folder(outputPath, option.force)
self.parseInPath(inputPath)
self.generateCode(ppconfig.PROJECT_GENERATORS, self.module, namespace)
self.module.saveConfigCache(configCachePath)
def parseInPath(self, inputPath):
inputPath = os.path.normpath(inputPath)
self.module.searchPath.append(inputPath)
splitPos = len(inputPath) + 1
for root, dirs, files in os.walk(inputPath):
for fname in files:
name, ext = os.path.splitext(fname)
if ext != ".proto": continue
srcFullPath = os.path.join(root, fname)
relativePath = os.path.join(root[splitPos:], fname)
self.parseFile(relativePath, srcFullPath, ppconfig.OUTPUT_PATH)
return
def parseFile(self, fileName, fileFullPath, outputPath):
fileName = fileName.replace('\\', '/')
fileDescriptor = self.module.getFileDescriptor(fileName)
if fileDescriptor is None:
fileDescriptor = self.module.newFileDescriptor(fileName, fileFullPath)
pa = PParser(self.module, fileDescriptor)
pa.parse()
namespace = {
"NAME" : os.path.splitext(fileName)[0],
"SOURCE_FILE" : fileFullPath,
"OUTPUT_PATH" : outputPath,
}
self.generateCode(ppconfig.CODE_GENERATORS, fileDescriptor, namespace)
return True
def generateCode(self, generatorInfos, code, namespace):
for generatorInfo in generatorInfos:
className = generatorInfo["class"]
cls = getattr(generators, className) if isinstance(className, str) else className
generator = cls(generatorInfo, self)
dstPath = generatorInfo["output"]
dstPath = str(Template(dstPath, searchList = [namespace, ppconfig]))
generator.generate(namespace["NAME"], dstPath, code)
return