This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv-reveiwer.py
162 lines (145 loc) · 3.79 KB
/
csv-reveiwer.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
#!/usr/bin/env python3
import csv
import sys
from os import path, system
from time import sleep
from termcolor import colored, cprint
from optparse import OptionParser
from pynput import keyboard
'''
0x0 Get CSV Parsed
0x1 Get Rows and Columns
0x2 First Row is title; Column Number selected for display
0x3 in-time parse the remaining list, and keep record
0x4 item view/list view
'''
global f, r, titles, num_arr, curr_pos
COLRS_NUM = 6
COLRS = ['red', 'yellow', 'green', 'cyan', 'blue', 'magenta']
def toBitArray(n):
return [1 if x=='1' else 0 for x in bin(n)[2:]]
def fromNumArray(num_arr):
n = 0
for x in num_arr:
n |= (1<<x)
return n
def m_cls(hlp=''):
t = system('clear')
print(hlp)
pass
def list_view(key):
global curr_pos
#less-like list view
pass
def item_view(key):
global prev_pos, curr_pos, f, r, num_arr, jumped_list
prev_pos = curr_pos
jumps_len = len(jumped_list)
if key==keyboard.Key.left:
curr_pos = (curr_pos-1) if curr_pos>0 else 0
pass
elif key==keyboard.Key.right:
curr_pos = (curr_pos+1) if curr_pos<jumps_len-2 else jumps_len-1
pass
elif key==keyboard.Key.page_up:
curr_pos = (curr_pos-10) if curr_pos>10 else 0
pass
elif key==keyboard.Key.page_down:
curr_pos = (curr_pos+10) if curr_pos<jumps_len-11 else jumps_len-1
pass
if prev_pos!=curr_pos:
m_cls('(LeftArrow and RightArrow for navigation; PageUp and PageDown for 10 items)')
f.seek(jumped_list[curr_pos])
t = next(r)
txt = ['\t\t\t\t[Item-%d/%d]'%(curr_pos, jumps_len)]
for i,x in enumerate(num_arr):
txt.append(colored(t[x], COLRS[i%COLRS_NUM]))
pass
txt.append('> ')
sys.stdout.write('\n'.join(txt))
sys.stdout.flush()
pass
pass
def main(options, args):
global f, r, prev_pos, curr_pos, num_arr
curr_pos = options.curr_pos + 1
## setup csv file
csvfile = './export.csv'
if len(args) and path.isfile(args[0]) and ('csv' in path.basename(args[0])):
csvfile = args[0]
pass
f = open(csvfile, 'rt', encoding='utf-8')
r = csv.reader(iter(f.readline, ''), dialect='excel')
titles = next(r)
## set display columns
if options.numbers>0:
num_arr = toBitArray(options.numbers)
num_arr = [i for (i,x) in enumerate(reversed(num_arr)) if x==1]
pass
else:
title = ['%d %s'%(i, x) for (i,x) in enumerate(titles)]
cprint('\t'.join(title), 'magenta')
num_arr = input('Please select columns (split by SPACE):').split(' ')
num_arr = [int(x) for x in num_arr]
print('Your long number: %r'%fromNumArray(num_arr))
input('press ENTER to continue...')
pass
## setup event monitor
if options.list_view:
list_view()
with keyboard.Listener(on_press=list_view) as listener:
listener.join()
pass
pass
else:
global jumped_list
jumped_list = []
try:
print('preloading...')
jumped_list.append(f.tell())
while next(r)[0]!='':
jumped_list.append(f.tell())
except Exception as e:
# cprint('Exception Caught! %d pcs obtained now.'%(len(jumped_list)), 'red')
# sleep(3)
pass
finally:
r = csv.reader(iter(f.readline, ''), dialect='excel')
f.seek(jumped_list[0])
item_view(keyboard.Key.left)
with keyboard.Listener(on_press=item_view) as listener:
listener.join()
pass
pass
pass
def m_exit():
global f
try:
f.close()
except Exception as e: pass
exit()
pass
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-l", "--list-view",
dest="list_view", action="store_true",
default=False,
help="change to list view, not item view")
parser.add_option("-b", "--numbers",
type="int",
dest="numbers",
default=-1,
help="the long number for column display")
parser.add_option("-p", "--position",
type="int",
dest="curr_pos",
default=0,
help="the long number for column display")
try:
(options, args) = parser.parse_args()
main(options, args)
except Exception as e:
cprint(e, 'red')
# raise e
finally:
m_exit()