-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocumasonry.py
276 lines (198 loc) · 7.25 KB
/
documasonry.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
from pylon import puts
# import re
import os
# import time
# from pylon import datalines
import pylon
from filler import Filler
from infotext import InfoText
class Documasonry:
"""Documasonry
yaml basic config
GUI
如果某个模板被打开且编辑了部分字段怎么办
- Word 和 Excel 会使用编辑中的模板, 输出包含编辑部分的成果
- AutoCAD 会开新的只读实例, 按照原模板输出成果
AutoCAD 特殊 field
- insert block {{地形}} to back layer
- 位置校正
原始模板在(0, 0)处 需要调整到界址线图形所在位置
polygon边框确定:
参数 padding比例, 是否方形
先定位中心 c, 边界框 w, h
padding = max(w, h) * padding_ratio
方形 ? 边界框长边 + padding : 边界框长边短边分别 + padding
yaml_text = '''
项目名称: test1
单位名称: test2
地形file: dx.dwg # relative path for output_folder
日期: # default today
'''
"""
def __init__(self, output_path='', template_paths=[]):
self.output_path = output_path
self.template_paths = template_paths
def set_output_path(self, path):
self.output_path = path
def set_template_paths(self, paths):
self.template_paths = paths
def read_templates_from_config(self, only_selected=True):
default_templates = self.read_config()['default_templates']
if only_selected:
return [item['file'] for item in default_templates if item['selected']]
else:
return [item['file'] for item in default_templates]
def generate(self, info, save=True, add_index=True):
for i, tmpl in enumerate(self.template_paths, 1):
filler = Filler(template_path=tmpl, output_folder=self.output_path)
filler.render(info=info)
if save:
prefix = '{:02d}-'.format(i) if add_index else ''
filler.save(info=info, close=True, prefix=prefix)
def detect_required_fields(self, quick=False):
'''quick=Ture -> preview required_fields from config.yaml
quick=False -> detect from open tmpl files and check by string "{{*}}
'''
field_names = []
if quick:
default_templates = self.read_config()['default_templates']
for tmpl in self.template_paths:
found = [item for item in default_templates if item['file'] == tmpl]
if found and found[-1].get('required_fields'):
field_names.extend(found[-1]['required_fields'])
else:
for tmpl in self.template_paths:
filler = Filler(template_path=tmpl, output_folder=self.output_path)
field_names.extend(filler.detect_required_fields(close=True, unique=True))
return list(pylon.dedupe(field_names))
def generate_required_fields_info_text(self, quick=False):
fields = self.detect_required_fields(quick=quick)
text = '\n'
for field in fields:
text += '{}: \n'.format(field)
if quick:
text += '\n'
else:
text += '\n# generated by reading template files\n'
return text
def combine_fields_info_text(self, info1_text, info2_text):
# result = ''
info1 = InfoText.from_string(info1_text)
info2 = InfoText.from_string(info2_text)
info1.merge(info2)
return info1.to_yaml_string()
def read_config(self):
'''config.yaml
default_templates:
- file: 'abspath/to/template1'
selected: true
required_fields: ['field1', 'field2', 'field3']
- file: 'abspath/to/template2'
selected: true
- file: 'abspath/to/template3'
selected: false
required_fields: ['field1', 'field2', 'field3']
required_fields 如不配置, 则需要用 filler.detect_required_fields() 检测出
'''
path = os.getcwd() + '/config.yaml'
config = InfoText.from_yaml(path).content
# config | puts(max_depth=10)
for i, item in enumerate(config['default_templates']):
file_path = item['file']
if not os.path.isfile(file_path):
file_path = os.path.join(os.getcwd(), file_path)
if os.path.isfile(file_path):
config['default_templates'][i]['file'] = file_path
return config
def test_documasonry_detect_fields():
template_paths = [os.getcwd() + '/test/test_templates/_test_{{项目名称}}-申请表.xls',
os.getcwd() + '/test/test_templates/test_{{name}}_面积计算表.doc',
os.getcwd() + '/test/test_templates/test_{{测试单位}}-宗地图.dwg',
os.getcwd() + '/test/test_templates/test_no_field_面积计算表.doc',
]
output_path = os.getcwd() + '/test/test_output'
masonry = Documasonry(output_path=output_path, template_paths=template_paths)
masonry.detect_required_fields() | puts()
# [项目名称, 单位名称, 地籍号, name, 面积90, 面积80, area, 测试单位, title, project, date, ratio, landcode, area80, area90] <-- list length 15
def test_documasonry_generate():
template_paths = [os.getcwd() + '/test/test_templates/_test_{{项目名称}}-申请表.xls',
os.getcwd() + '/test/test_templates/test_{{name}}_面积计算表.doc',
os.getcwd() + '/test/test_templates/test_{{测试单位}}-宗地图.dwg',
os.getcwd() + '/test/test_templates/test_no_field_面积计算表.doc',
]
output_path = os.getcwd() + '/test/test_output'
masonry = Documasonry(output_path=output_path, template_paths=template_paths)
text = '''
项目名称: test1
单位名称: test2
地籍号: 110123122
name: sjgisdgd
面积90: 124.1
面积80: 234.2
area: 124.2
测试单位: testconm
title: testtitle
project: pro.
date: 20124002
ratio: 2000
landcode: 235
area80: 94923
area90: 3257
'''
info = InfoText.from_string(text)
masonry.generate(info=info, save=True, add_index=True) | puts()
def test_load_config():
masonry = Documasonry(output_path=1, template_paths=[])
masonry.read_config()
masonry.read_templates_from_config()
# masonry.generate_required_fields_info_text(quick=True) | puts()
# masonry.generate_required_fields_info_text(quick=False) | puts()
def test_detect_fields():
masonry = Documasonry()
masonry.set_template_paths(paths=masonry.read_templates_from_config())
masonry.set_output_path(path='')
masonry.generate_required_fields_info_text(quick=True) | puts()
pass
def test_combine_fields_info_text():
info1_text = '''
项目名称: org
单位名称:
code: 110123122
'''
info2_text = '''
项目名称: new
单位名称: name
code:
area: 124.2
'''
d = Documasonry()
r = d.combine_fields_info_text(info1_text, info2_text)
puts('----')
puts(r)
info1_text = '''
'''
info2_text = '''
项目名称: test1
单位名称:
地籍号: 110123122
name: sjgisdgd
面积90: 124.1
面积80: 234.2
area: 124.2
'''
d = Documasonry()
r = d.combine_fields_info_text(info1_text, info2_text)
puts('----')
puts(r)
info1_text = '''
面积90: 20.1
项目名称:
单位名称:
面积80:
'''
info2_text = '''
'''
d = Documasonry()
r = d.combine_fields_info_text(info1_text, info2_text)
puts('----')
puts(r)