Skip to content

Commit e1d8323

Browse files
committed
hxl2pandas (#4), hxl2tab (#2): save hxl output to temporary file
1 parent 3608e08 commit e1d8323

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

bin/hxl2pandas

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
#===============================================================================
2+
# ===============================================================================
33
#
44
# FILE: hxl2pandas
55
#
@@ -10,7 +10,7 @@
1010
# table are mostly as reference of how pandas (more specifically
1111
# DataFrame) could be used as an intermediate format to export
1212
# HXL to other formats already supported by Pandas.
13-
#
13+
#
1414
# While the reference table may still be useful for those who
1515
# are doing manual conversionor to help understand how different
1616
# tools used for data mining / machine learningwould use HXL
@@ -31,7 +31,7 @@
3131
# VERSION: v1.0-draft
3232
# CREATED: 2021-01-36 03:43 UTC v1.0 imported from hxl2example
3333
# REVISION: ---
34-
#===============================================================================
34+
# ===============================================================================
3535

3636
import sys
3737
import os
@@ -43,6 +43,9 @@ import hxl.converters
4343
import hxl.filters
4444
import hxl.io
4545

46+
import tempfile
47+
import time
48+
4649
# In Python2, sys.stdin is a byte stream; in Python3, it's a text stream
4750
STDIN = sys.stdin.buffer
4851

@@ -65,6 +68,8 @@ class HXL2Pandas:
6568
self.EXIT_ERROR = 1
6669
self.EXIT_SYNTAX = 2
6770

71+
self.original_outfile = None
72+
6873
def make_args_HXL2Pandas(self):
6974

7075
self.hxlhelper = HXLUtils()
@@ -84,12 +89,29 @@ class HXL2Pandas:
8489
called will convert the HXL source to example format.
8590
"""
8691

87-
# NOTE: the next lines, in fact, only generate an csv outut. So you
88-
# can use as starting point.
89-
with self.hxlhelper.make_source(args, stdin) as source, \
90-
self.hxlhelper.make_output(args, stdout) as output:
91-
hxl.io.write_hxl(output.output, source,
92-
show_tags=not args.strip_tags)
92+
# If the user specified an output file, we will save on
93+
# self.original_outfile. The args.outfile will be used for temporary
94+
# output
95+
if args.outfile:
96+
self.original_outfile = args.outfile
97+
98+
try:
99+
temp = tempfile.NamedTemporaryFile()
100+
args.outfile = temp.name
101+
102+
# NOTE: the next lines, in fact, only generate an csv outut. So you
103+
# can use as starting point.
104+
with self.hxlhelper.make_source(args, stdin) as source, \
105+
self.hxlhelper.make_output(args, stdout) as output:
106+
hxl.io.write_hxl(output.output, source,
107+
show_tags=not args.strip_tags)
108+
109+
print('Temporary HXLated csv at ', args.outfile)
110+
print('TODO: implement the conversion')
111+
time.sleep(20)
112+
113+
finally:
114+
temp.close()
93115

94116
return self.EXIT_OK
95117

bin/hxl2tab

+28-7
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ import hxl.converters
3535
import hxl.filters
3636
import hxl.io
3737

38+
import tempfile
39+
import time
40+
3841
# In Python2, sys.stdin is a byte stream; in Python3, it's a text stream
3942
STDIN = sys.stdin.buffer
4043

41-
4244
class HXL2Tab:
4345
"""
4446
HXL2Tab is a classe to export already HXLated data in the format
@@ -57,6 +59,8 @@ class HXL2Tab:
5759
self.EXIT_ERROR = 1
5860
self.EXIT_SYNTAX = 2
5961

62+
self.original_outfile = None
63+
6064
def make_args_HXL2Tab(self):
6165

6266
self.hxlhelper = HXLUtils()
@@ -76,12 +80,29 @@ class HXL2Tab:
7680
called will convert the HXL source to example format.
7781
"""
7882

79-
# NOTE: the next lines, in fact, only generate an csv outut. So you
80-
# can use as starting point.
81-
with self.hxlhelper.make_source(args, stdin) as source, \
82-
self.hxlhelper.make_output(args, stdout) as output:
83-
hxl.io.write_hxl(output.output, source,
84-
show_tags=not args.strip_tags)
83+
# If the user specified an output file, we will save on
84+
# self.original_outfile. The args.outfile will be used for temporary
85+
# output
86+
if args.outfile:
87+
self.original_outfile = args.outfile
88+
89+
try:
90+
temp = tempfile.NamedTemporaryFile()
91+
args.outfile = temp.name
92+
93+
# NOTE: the next lines, in fact, only generate an csv outut. So you
94+
# can use as starting point.
95+
with self.hxlhelper.make_source(args, stdin) as source, \
96+
self.hxlhelper.make_output(args, stdout) as output:
97+
hxl.io.write_hxl(output.output, source,
98+
show_tags=not args.strip_tags)
99+
100+
print('Temporary HXLated csv at ', args.outfile)
101+
print('TODO: implement the conversion')
102+
time.sleep(20)
103+
104+
finally:
105+
temp.close()
85106

86107
return self.EXIT_OK
87108

0 commit comments

Comments
 (0)