10
10
import os
11
11
import struct
12
12
import json
13
+ from io import StringIO
13
14
from . import abstract , NURBS , multi , compatibility , operations , utilities , convert
14
- from . import _exchange
15
+ from . import _exchange as exch
15
16
16
17
17
18
def import_txt (file_name , two_dimensional = False , ** kwargs ):
@@ -69,18 +70,18 @@ def import_txt(file_name, two_dimensional=False, **kwargs):
69
70
:raises IOError: an error occurred reading the file
70
71
"""
71
72
# Read file
72
- content = _exchange .read_file (file_name )
73
+ content = exch .read_file (file_name )
73
74
74
75
# Are we using a Jinja2 template?
75
76
j2tmpl = kwargs .get ('jinja2' , False )
76
77
if j2tmpl :
77
- content = _exchange .process_template (content )
78
+ content = exch .process_template (content )
78
79
79
80
# File delimiters
80
81
col_sep = kwargs .get ('col_separator' , ";" )
81
82
sep = kwargs .get ('separator' , "," )
82
83
83
- return _exchange .import_text_data (content , sep , col_sep , two_dimensional )
84
+ return exch .import_text_data (content , sep , col_sep , two_dimensional )
84
85
85
86
86
87
def export_txt (obj , file_name , two_dimensional = False , ** kwargs ):
@@ -113,8 +114,8 @@ def export_txt(obj, file_name, two_dimensional=False, **kwargs):
113
114
col_sep = kwargs .get ('col_separator' , ";" )
114
115
sep = kwargs .get ('separator' , "," )
115
116
116
- content = _exchange .export_text_data (obj , sep , col_sep , two_dimensional )
117
- return _exchange .write_file (file_name , content )
117
+ content = exch .export_text_data (obj , sep , col_sep , two_dimensional )
118
+ return exch .write_file (file_name , content )
118
119
119
120
120
121
def import_csv (file_name , ** kwargs ):
@@ -143,8 +144,8 @@ def import_csv(file_name, **kwargs):
143
144
# File delimiters
144
145
sep = kwargs .get ('separator' , "," )
145
146
146
- content = _exchange .read_file (file_name , skip_lines = 1 )
147
- return _exchange .import_text_data (content , sep )
147
+ content = exch .read_file (file_name , skip_lines = 1 )
148
+ return exch .import_text_data (content , sep )
148
149
149
150
150
151
def export_csv (obj , file_name , point_type = 'evalpts' , ** kwargs ):
@@ -181,7 +182,7 @@ def export_csv(obj, file_name, point_type='evalpts', **kwargs):
181
182
line += "," .join ([str (p ) for p in pt ]) + "\n "
182
183
183
184
# Write to file
184
- return _exchange .write_file (file_name , line )
185
+ return exch .write_file (file_name , line )
185
186
186
187
187
188
def import_cfg (file_name , ** kwargs ):
@@ -191,14 +192,16 @@ def import_cfg(file_name, **kwargs):
191
192
192
193
Requires `libconf <https://pypi.org/project/libconf/>`_ package.
193
194
195
+ Use ``jinja2=True`` to activate Jinja2 template processing. Please refer to the documentation for details.
196
+
194
197
:param file_name: name of the input file
195
198
:type file_name: str
196
199
:return: a list of NURBS curve(s) or surface(s)
197
200
:rtype: list
198
201
:raises IOError: an error occurred writing the file
199
202
"""
200
- def callback (fp ):
201
- return libconf .load ( fp )
203
+ def callback (data ):
204
+ return libconf .loads ( data )
202
205
203
206
# Check if it is possible to import 'libconf'
204
207
try :
@@ -209,9 +212,13 @@ def callback(fp):
209
212
210
213
# Get keyword arguments
211
214
delta = kwargs .get ('delta' , - 1.0 )
215
+ use_template = kwargs .get ('jinja2' , False )
216
+
217
+ # Read file
218
+ file_src = exch .read_file (file_name )
212
219
213
220
# Import data
214
- return _exchange . import_dict ( file_name , delta , callback )
221
+ return exch . import_dict_str ( file_src = file_src , delta = delta , callback = callback , tmpl = use_template )
215
222
216
223
217
224
def export_cfg (obj , file_name ):
@@ -231,8 +238,8 @@ def export_cfg(obj, file_name):
231
238
:raises IOError: an error occurred writing the file
232
239
"""
233
240
234
- def callback (fp , data ):
235
- fp . write ( libconf .dumps (data ) )
241
+ def callback (data ):
242
+ return libconf .dumps (data )
236
243
237
244
# Check if it is possible to import 'libconf'
238
245
try :
@@ -241,8 +248,11 @@ def callback(fp, data):
241
248
print ("Please install 'libconf' package to use libconfig format: pip install libconf" )
242
249
return
243
250
244
- # Export data as a file
245
- _exchange .export_dict (obj , file_name , callback )
251
+ # Export data
252
+ exported_data = exch .export_dict_str (obj = obj , callback = callback )
253
+
254
+ # Write to file
255
+ return exch .write_file (file_name , exported_data )
246
256
247
257
248
258
def import_yaml (file_name , ** kwargs ):
@@ -252,15 +262,17 @@ def import_yaml(file_name, **kwargs):
252
262
253
263
Requires `ruamel.yaml <https://pypi.org/project/ruamel.yaml/>`_ package.
254
264
265
+ Use ``jinja2=True`` to activate Jinja2 template processing. Please refer to the documentation for details.
266
+
255
267
:param file_name: name of the input file
256
268
:type file_name: str
257
269
:return: a list of NURBS curve(s) or surface(s)
258
270
:rtype: list
259
271
:raises IOError: an error occurred reading the file
260
272
"""
261
- def callback (fp ):
273
+ def callback (data ):
262
274
yaml = YAML ()
263
- return yaml .load (fp )
275
+ return yaml .load (data )
264
276
265
277
# Check if it is possible to import 'ruamel.yaml'
266
278
try :
@@ -271,9 +283,13 @@ def callback(fp):
271
283
272
284
# Get keyword arguments
273
285
delta = kwargs .get ('delta' , - 1.0 )
286
+ use_template = kwargs .get ('jinja2' , False )
287
+
288
+ # Read file
289
+ file_src = exch .read_file (file_name )
274
290
275
291
# Import data
276
- return _exchange . import_dict ( file_name , delta , callback )
292
+ return exch . import_dict_str ( file_src = file_src , delta = delta , callback = callback , tmpl = use_template )
277
293
278
294
279
295
def export_yaml (obj , file_name ):
@@ -292,9 +308,12 @@ def export_yaml(obj, file_name):
292
308
:type file_name: str
293
309
:raises IOError: an error occurred writing the file
294
310
"""
295
- def callback (fp , data ):
311
+ def callback (data ):
312
+ # Ref: https://yaml.readthedocs.io/en/latest/example.html#output-of-dump-as-a-string
313
+ stream = StringIO ()
296
314
yaml = YAML ()
297
- yaml .dump (data , fp )
315
+ yaml .dump (data , stream )
316
+ return stream .getvalue ()
298
317
299
318
# Check if it is possible to import 'ruamel.yaml'
300
319
try :
@@ -303,27 +322,36 @@ def callback(fp, data):
303
322
print ("Please install 'ruamel.yaml' package to use YAML format: pip install ruamel.yaml" )
304
323
return
305
324
306
- # Export data as a file
307
- _exchange .export_dict (obj , file_name , callback )
325
+ # Export data
326
+ exported_data = exch .export_dict_str (obj = obj , callback = callback )
327
+
328
+ # Write to file
329
+ return exch .write_file (file_name , exported_data )
308
330
309
331
310
332
def import_json (file_name , ** kwargs ):
311
333
""" Imports curves and surfaces from files in JSON format.
312
334
335
+ Use ``jinja2=True`` to activate Jinja2 template processing. Please refer to the documentation for details.
336
+
313
337
:param file_name: name of the input file
314
338
:type file_name: str
315
339
:return: a list of NURBS curve(s) or surface(s)
316
340
:rtype: list
317
341
:raises IOError: an error occurred reading the file
318
342
"""
319
- def callback (fp ):
320
- return json .load ( fp )
343
+ def callback (data ):
344
+ return json .loads ( data )
321
345
322
346
# Get keyword arguments
323
347
delta = kwargs .get ('delta' , - 1.0 )
348
+ use_template = kwargs .get ('jinja2' , False )
349
+
350
+ # Read file
351
+ file_src = exch .read_file (file_name )
324
352
325
353
# Import data
326
- return _exchange . import_dict ( file_name , delta , callback )
354
+ return exch . import_dict_str ( file_src = file_src , delta = delta , callback = callback , tmpl = use_template )
327
355
328
356
329
357
def export_json (obj , file_name ):
@@ -338,11 +366,14 @@ def export_json(obj, file_name):
338
366
:type file_name: str
339
367
:raises IOError: an error occurred writing the file
340
368
"""
341
- def callback (fp , data ):
342
- fp .write (json .dumps (data , indent = 4 ))
369
+ def callback (data ):
370
+ return json .dumps (data , indent = 4 )
371
+
372
+ # Export data
373
+ exported_data = exch .export_dict_str (obj = obj , callback = callback )
343
374
344
- # Export data as a file
345
- _exchange . export_dict ( obj , file_name , callback )
375
+ # Write to file
376
+ return exch . write_file ( file_name , exported_data )
346
377
347
378
348
379
def export_obj (surface , file_name , ** kwargs ):
@@ -360,7 +391,7 @@ def export_obj(surface, file_name, **kwargs):
360
391
:raises IOError: an error occurred writing the file
361
392
"""
362
393
content = export_obj_str (surface , ** kwargs )
363
- return _exchange .write_file (file_name , content )
394
+ return exch .write_file (file_name , content )
364
395
365
396
366
397
def export_obj_str (surface , ** kwargs ):
@@ -465,7 +496,7 @@ def export_stl(surface, file_name, **kwargs):
465
496
if 'binary' in kwargs :
466
497
kwargs .pop ('binary' )
467
498
content = export_stl_str (surface , binary = binary , ** kwargs )
468
- return _exchange .write_file (file_name , content , binary = binary )
499
+ return exch .write_file (file_name , content , binary = binary )
469
500
470
501
471
502
def export_stl_str (surface , ** kwargs ):
@@ -546,7 +577,7 @@ def export_off(surface, file_name, **kwargs):
546
577
:raises IOError: an error occurred writing the file
547
578
"""
548
579
content = export_off_str (surface , ** kwargs )
549
- return _exchange .write_file (file_name , content )
580
+ return exch .write_file (file_name , content )
550
581
551
582
552
583
def export_off_str (surface , ** kwargs ):
@@ -643,12 +674,12 @@ def import_smesh(file):
643
674
:raises IOError: an error occurred reading the file
644
675
"""
645
676
if os .path .isfile (file ):
646
- return _exchange .import_smesh_single (file )
677
+ return exch .import_smesh_single (file )
647
678
elif os .path .isdir (file ):
648
679
files = sorted ([os .path .join (file , f ) for f in os .listdir (file )])
649
680
surf = multi .SurfaceContainer ()
650
681
for f in files :
651
- surf .add (_exchange .import_smesh_single (f ))
682
+ surf .add (exch .import_smesh_single (f ))
652
683
return surf
653
684
else :
654
685
raise IOError ("Input is not a file or a directory" )
@@ -695,7 +726,7 @@ def export_smesh(surface, file_name, **kwargs):
695
726
696
727
# Write to file
697
728
fname_curr = fname + "." + str (idx + 1 )
698
- _exchange .write_file (fname_curr + fext , line )
729
+ exch .write_file (fname_curr + fext , line )
699
730
700
731
701
732
def export_vmesh (volume , file_name , ** kwargs ):
@@ -742,7 +773,7 @@ def export_vmesh(volume, file_name, **kwargs):
742
773
743
774
# Write to file
744
775
fname_curr = fname + "." + str (idx + 1 )
745
- _exchange .write_file (fname_curr + fext , line )
776
+ exch .write_file (fname_curr + fext , line )
746
777
747
778
748
779
def import_3dm (file_name , ** kwargs ):
0 commit comments