forked from gali8/Tesseract-OCR-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparametersConstantsGenerator.py
executable file
·141 lines (112 loc) · 4.01 KB
/
parametersConstantsGenerator.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
#!/usr/bin/python
import subprocess
import os
import re
from time import gmtime, strftime
### Converter settings: ###
repo = "https://code.google.com/p/tesseract-ocr/"
repoTag = "3.03-rc1"
baseHeadersDir = "tesseract-ocr"
headers = [
"ccmain/tesseractclass.h",
"classify/classify.h",
"dict/dict.h",
"textord/textord.h",
"textord/makerow.cpp",
"wordrec/language_model.h",
"wordrec/wordrec.h",
]
resultClassPath = "./"
resultClassName = "G8TesseractParameters"
varPrefix = "kG8Param"
varPattern = re.compile('(\w*)(_VAR_H|_VAR)\(([^,]*), ([^,]*|"[^"]*"),([^;]*)\);')
### Constants: ###
copyrights = '''//
// %s
// Tesseract OCR iOS
// This code is auto-generated from Tesseract headers.
//
// Created by Nikolay Volosatov on %s.
// Copyright (c) %s Daniele Galiotto - www.g8production.com.
// All rights reserved.
//
''' % ("%s", strftime("%d/%m/%y", gmtime()), strftime("%Y", gmtime()))
headerTemplate = '''%s
#import <Foundation/Foundation.h>
#ifndef Tesseract_OCR_iOS_%s_h
#define Tesseract_OCR_iOS_%s_h
%s
#endif
''' % (copyrights % (resultClassName + '.h'), resultClassName, resultClassName, "%s")
codeTemplate = '''%s
#import "%s.h"
%s''' % (copyrights % (resultClassName + '.m'), resultClassName, "%s")
externVarTemplate = '''
///%s
///@param Type %s
///@param Default %s
extern NSString *const %s;
'''
depricatedExternVarTemplate = '''
///%s
///@param Type %s
///@param Default %s
extern NSString *const %s DEPRECATED_ATTRIBUTE;
'''
codeVarTemplate = 'NSString *const %s = @"%s";\n'
def bash(cmd, cwd=None):
print(cmd)
print(subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, cwd=cwd).stdout.read())
### Clone/pull repo ###
if not os.path.exists(baseHeadersDir):
bash('git clone ' + repo + ' ' + baseHeadersDir)
bash('cd ' + baseHeadersDir + ' && git checkout tags/' + repoTag + ' && cd ..')
### Read tesseractclass.h file ###
actualContent = ""
depricatedContent = ""
for header in headers:
f = open(baseHeadersDir + "/" + header, "r")
content = str(f.read())
f.close()
# Split actual and depricated parameters
splited = content.split('BEGIN DEPRECATED PARAMETERS')
if not len(splited) == 2:
splited = (splited[0], "")
(newActualContent, newDepricatedContent) = splited
actualContent = actualContent + '\n' + newActualContent
depricatedContent = depricatedContent + '\n' + newDepricatedContent
### Parse parameters from content ###
def parse(someContent):
result = varPattern.finditer(someContent)
variables = dict()
for match in result:
varType = match.group(1)
varName = match.group(3)
varDefault = match.group(4)
varDescription = match.group(5)
subnames = varName.split('_')
subnames = map(lambda s: s.title(), subnames)
objCVarName = varPrefix + ''.join(subnames);
varDefault = varDefault.strip()
varDescription = ''.join(varDescription.split('"')).strip()
varDescription = ' '.join(map(lambda s: s.strip(), varDescription.split('\n'))).strip()
variables[objCVarName] = (varType, varName, varDefault, varDescription)
return variables.items()
hContent = ''
mContent = ''
### Format result content ###
for (objCVarName,(varType, varName, varDefault, varDescription)) in parse(actualContent):
hContent = hContent + (externVarTemplate % (varDescription, varType, varDefault, objCVarName))
mContent = mContent + (codeVarTemplate % (objCVarName, varName))
print '%s %s = %s' % (varType, objCVarName, varName)
for (objCVarName,(varType, varName, varDefault, varDescription)) in parse(depricatedContent):
hContent = hContent + (depricatedExternVarTemplate % (varDescription, varType, varDefault, objCVarName))
mContent = mContent + (codeVarTemplate % (objCVarName, varName))
print '%s %s = %s' % (varType, objCVarName, varName)
### Write .h and .m files ###
f = open(resultClassName + ".h", "w")
f.write(headerTemplate % hContent)
f.close()
f = open(resultClassName + ".m", "w")
f.write(codeTemplate % mContent)
f.close()