-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·165 lines (149 loc) · 4.22 KB
/
utils.py
File metadata and controls
executable file
·165 lines (149 loc) · 4.22 KB
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from sys import *
from os.path import *
from netifaces import *
from munch import *
from pprint import *
from sys import *
from inspect import *
import inspect
import types
import pyiface
from pyiface.ifreqioctls import IFF_UP, IFF_RUNNING
from collections import defaultdict
from bs4 import BeautifulSoup
import datetime
import signal
def net_info():
'''
Gives basic networking info like local IP
'''
net = Munch()
net.interfaces = dict()
net.def_if = interfaces()[1]
try:
net.def_if = gateways()['default'][AF_INET][1]
except KeyError:
pass
try:
net.ip = ifaddresses(net.def_if)[AF_INET][0]['addr']
net.def_mac = ifaddresses(net.def_if)[AF_PACKET][0]['addr']
except KeyError:
pass
#s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#n = s.fileno()
#ifr = ifreq()
#ifr.ifr_ifrn = "lo".encode('utf-8')
#fcntl.ioctl(n, SIOCGIFFLAGS, ifr);
#print("SIOCGIFFLAGS", ifr.ifr_flags);
net.ifs = " ".join(interfaces())
for i in interfaces():
ifd = Munch()
flags = pyiface.Interface(name=i).flags
#net.flags = defaultdict(lambda: Munch())
if flags & IFF_UP > 0:
ifd.flags = "U"
if flags & IFF_RUNNING > 0:
ifd.flags += "R"
try:
ifd.addr= ifaddresses(i)[AF_INET][0]['addr']
except KeyError:
pass
net.interfaces[i] = dict(ifd)
return dict(net)
def repeated_gray():
'''
Filters input text and prints in gray color lines which was already before
'''
passed = {}
for l in stdin:
l = l.rstrip()
if l in passed:
print('\033[37m%s\033[30m' % (l))
else:
print(l)
passed[l] = True
def info_gray():
'''
Filters input text and prints in gray color info lines
'''
passed = {}
for l in stdin:
l = l.rstrip()
e = l.find('=ERR=')
if e != -1:
print(l[:e] + '\033[1;38m=ERR=\033[0m' + l[e+5:])
elif 'INFO' in l:
print('\033[2;30m%s\033[0m' % (l))
else:
print(l)
import pandas as pd
def wikitable_transpose():
T = [[]]
i = j = 0;
for l in stdin:
l = l.rstrip()
if l == '|}':
for r in T:
for c in r:
print(c)
print('|-')
elif l[0] in ('|', '!') and (len(l) == 1 or l[1] not in ('+', '-')):
i += 1;
if i + 1 > len(T):
T.append([0])
if j + 1 > len(T[i]):
T[i].append(0)
T[i][j] = l
continue
elif l == '|-':
j += 1
i = 0;
continue
print(l)
def commonprefix_gray():
'''
Filters input text and prints in gray color start of current line which is same like previous
'''
p = ''
pcl = 0
for l in stdin:
l = l.rstrip()
c = commonprefix([p, l])
cl = len(c)
print('\033[2;36m' + l[:cl] + '\033[0;30m' + l[cl:])
if False:
if cl < pcl:
print(l)
else:
print('\033[37m' + '·' * cl + '\033[30m' + l[cl:])
p = l
pcl = cl
if __name__ == "__main__":
def handler(signum, frame):
exit(0)
signal.signal(signal.SIGHUP, handler)
try:
ret = 0
if len(argv) > 1:
a1 = argv[1]
argv = argv[1:]
if '(' in a1:
ret = eval(a1)
else:
ret = eval(a1 + '(' + ', '.join("'%s'" % (a)
for a in argv[1:]) + ')')
pprint(ret) if ret else 0
else:
for m in getmembers(modules[__name__]):
if isfunction(m[1]) and m[1].__module__ == __name__:
help(m[1])
if isinstance(ret, type(False)) and ret == False:
exit(os.EX_CONFIG)
#except KeyboardInterrupt:
except Exception as inst:
print(type(inst)) # the exception instance
print(inst.args) # arguments stored in .args
print(inst)
warning("\nInterrupted")