-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-info.py
executable file
·232 lines (205 loc) · 5.61 KB
/
format-info.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import codecs
import re
import argparse
### ArgParse configuration.
parser = argparse.ArgumentParser(description='Format a file into a table-like view.')
parser.add_argument("-i","--input", type=str, metavar="N", required=True, help="the file with the data to be formatted (required)")
parser.add_argument("-o","--output", type=str, metavar="N", default="output", help="the file with the formated data (default: output)")
parser.add_argument("-c","--columns", type=int, metavar="N", default=2, help="the amount of columns (default: 2)")
parser.add_argument("-w","--width", type=int, metavar="N", nargs='+', default=[], help="the width(s) between each column (default: 40)")
parser.add_argument("-n","--names", type=str, metavar="N", nargs='+', default=[], help="the name(s) for the columns")
parser.add_argument("-b","--borders", action="store_true", help=" draws the table borders.")
args = parser.parse_args()
### Variable Extraction
input_arg = args.input
output_arg = args.output
line_arg = args.columns
width_arg = args.width
table_arg = args.borders
name_arg = args.names
### Evaluate if header names will be drawed.
if (len(name_arg) > 0):
name_eval = True
else:
name_eval = False
### Evaluate if the width will be auto calculated.
if (len(width_arg) == 0):
width_eval = True
else:
width_eval = False
### Read the input.
with codecs.open(input_arg, "r", "utf-8") as text_file:
input = text_file.read()
result = u""
### Calculate widths if not specified.
if width_eval:
count = 0
for x in range(0, line_arg):
width_arg.append(0)
for line in input.split("\n"):
linetemp = re.sub("[\r\n\b]", "", line)
if len(linetemp) + 1 > width_arg[count]:
width_arg[count] = len(linetemp) + 1
if count < line_arg - 1:
count += 1
else:
count = 0
### Calculate the longitude.
longitude = 0
tempvalue = 0
for i in range(0, line_arg):
longitude += width_arg[tempvalue] + 1
if (tempvalue + 1) < len(width_arg):
tempvalue += 1
longitude += 1
### Draws the header names.
if name_eval and table_arg:
count = 0
### Header top
width_count = 0
breaks = 1 + width_arg[width_count]
if (width_count + 1) < len(width_arg):
width_count += 1
for x in range(0, longitude):
if (x == 0):
result += u"╔"
elif (x == longitude - 1):
result += u"╗"
elif (x == breaks):
result += u"╦"
breaks += 1 + width_arg[width_count]
if (width_count + 1) < len(width_arg):
width_count += 1
else:
result += u"═"
result += "\n"
### Header names
width_count = 0
line_result = ""
column = 1
linetemp = ""
count = 0
for name in name_arg:
count += 1
if (count > line_arg):
break
line_result += u"║"
line_result += name
for x in range(0, (width_arg[width_count] - len(name))):
line_result += u" "
if (width_count + 1) < len(width_arg):
width_count += 1
column += 1
if (len(line_result) != longitude):
breaks = len(line_result) + 1 + width_arg[width_count]
line_result += u"║"
if (width_count + 1) < len(width_arg):
width_count += 1
for x in range(len(line_result), longitude):
if (x == breaks):
line_result += u"║"
breaks += 1 + width_arg[width_count]
if (width_count + 1) < len(width_arg):
width_count += 1
else:
line_result += " "
line_result += "\n"
result += line_result
### Top border
count = 0
if table_arg:
width_count = 0
breaks = 1 + width_arg[width_count]
if (width_count + 1) < len(width_arg):
width_count += 1
for x in range(0, longitude):
if (x == 0):
if name_eval:
result += u"╠"
else:
result += u"╔"
elif (x == longitude - 1):
if name_eval:
result += u"╣"
else:
result += u"╗"
elif (x == breaks):
if name_eval:
result += u"╬"
else:
result += u"╦"
breaks += 1 + width_arg[width_count]
if (width_count + 1) < len(width_arg):
width_count += 1
else:
result += u"═"
result += u"\n"
### Data lines
column = 1
line_result = ""
width_count = 0
for line in input.split("\n"):
linetemp = re.sub("[\r\n\b]", "", line)
if (len(linetemp) != 0):
if (count == line_arg):
line_result += u"\n"
count = 0
width_count = 0
if (table_arg and count == 0):
line_result += u"║"
else:
line_result += u""
line_result += unicode(linetemp.replace("\n", "").replace("\r", ""))
for x in range(0, (width_arg[width_count] - len(linetemp))):
line_result += u" "
if (table_arg):
line_result += u"║"
if (width_count + 1) < len(width_arg):
width_count += 1
else:
if (width_count + 1) < len(width_arg):
width_count += 1
count += 1
column += 1
if (column > line_arg):
result += line_result
line_result = ""
column = 1
### Complete the line if it isn't.
if (len(line_result) != longitude and len(line_result) != 0):
breaks = len(line_result) - 1 + width_arg[width_count]
if (width_count + 1) < len(width_arg):
width_count += 1
for x in range(len(line_result) - 1, longitude):
if (x == breaks):
line_result += u"║"
breaks += 1 + width_arg[width_count]
if (width_count + 1) < len(width_arg):
width_count += 1
else:
line_result += " "
result += line_result
### Botton border.
width_count = 0
if table_arg:
result += "\n"
breaks = 1 + width_arg[width_count]
if (width_count + 1) < len(width_arg):
width_count += 1
for x in range(0, longitude):
if (x == 0):
result += u"╚"
elif (x == longitude - 1):
result += u"╝"
elif (x == breaks):
result += u"╩"
breaks += 1 + width_arg[width_count]
if (width_count + 1) < len(width_arg):
width_count += 1
else:
result += u"═"
### Write the output.
with codecs.open(output_arg, "w", "utf-8") as text_file:
text_file.write(result)