-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProcBaseRoutines.py
1176 lines (923 loc) · 33.3 KB
/
ProcBaseRoutines.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
Base classes and general purpose utility methods
"""
import os
import SeqFileIO
import ProcFieldConstants
import ProcDataConstants
#import numpy
PFC = ProcFieldConstants
F_NULL_HANDLER = PFC.F_NULL_HANDLER
F_PROTOCOL = PFC.F_PROTOCOL
F_TERM_LIST = PFC.F_TERM_LIST
PDC = ProcDataConstants
# --
CONVERSION = "convtype"
FIELD_NAME = "fieldname"
FIELD_NUMBER = "fieldnumber"
ERROR_VAL = "errval"
NUM_BASE = "base"
PREFIX_VAL = "prefix"
SUFFIX_VAL = "suffix"
BEFORE_VAL = "before"
AFTER_VAL = "after"
HAS_VAL = "has"
WORDS_VAL = "words"
SUBWORD = "subword"
# --
PROC_PATH_PREFIX_LIST = ( "/proc", "/proc/", "/proc/net/", "/proc/self/net/",
"/proc/self/", "/proc/sysvipc/" )
FILE_HANDLER_REGISTRY = dict()
PARTIAL_HANDLER_REGISTRY = dict()
HANDLER_TO_PATH = dict()
# ---
def hilo_pair_from_str(raw):
"""
Convert '%d.%6d' float to long by shifting decimal places
"""
__parts = raw.partition(".")
if len(__parts) == 3:
try:
__res = long(__parts[0]) * 1000000 + long(__parts[2])
except ValueError:
__res = 0
else:
__res = 0
return __res
# ---
def error_by_rule(rule):
"""Return an appropriate error value based on the ruleset supplied."""
try:
__conv = rule[CONVERSION]
except KeyError:
__conv = str
try:
__err = rule[ERROR_VAL]
except KeyError:
if __conv == int:
__err = 0
elif __conv == long:
__err = 0L
elif __conv == float:
__err = 0.0
else:
__err = ""
return __err
def apply_string_check(rawdata, check, val):
"""
Apply the selection criteria and return hit/miss and transformed string
"""
__hit = True
__trans = rawdata
if check is WORDS_VAL:
__hit = val == len(__trans.split())
elif check is HAS_VAL:
__hit = __trans.find(val) != -1
elif check is SUBWORD:
__split = __trans.split()
__hit = val < len(__split)
if __hit:
__trans = __split[val]
elif check is PREFIX_VAL:
__hit = __trans.startswith(val)
if __hit:
__trans = __trans[len(val):]
elif check is SUFFIX_VAL:
__hit = __trans.endswith(val)
if __hit:
__trans = __trans[:-len(val)]
elif check is BEFORE_VAL:
__split = __trans.partition(val)
if len(__split) == 3:
if len(__split[1]) > 0:
__trans = __split[0]
else:
__hit = False
else:
__hit = False
elif check is AFTER_VAL:
__split = __trans.partition(val)
if len(__split) != 3:
__hit = False
elif len(__split[1]) == 0:
__hit = False
else:
__trans = __split[2]
return(__hit, __trans)
def matches_all_crit(rawdata, rule, order = ""):
"""
Test to see if 'rawdata' passes all selection tests in the given
parse rule.
"""
__result = True
__raw = rawdata
__crit = dict()
__def_order = ( WORDS_VAL, HAS_VAL, PREFIX_VAL, SUFFIX_VAL, BEFORE_VAL,
AFTER_VAL )
if order == "":
order = __def_order
# print "dbg:: MAC: order({olist})".format(olist=str(order))
for __check in order:
__crit[__check] = True
if __result and rule.has_key(__check):
__val = rule[__check]
__result, __raw = apply_string_check(__raw, __check, __val)
# print "dbg:: MAC: L1: rc({rc}) ch({ch}) v({val}) \
#buf({raw})".format(ch=__check, rc=__result, raw=__raw, val=__val)
# print "dbg:: MAC: break: crit({crit})".format(crit=str(__crit))
for __check in rule:
# print "dbg:: MAC: L2.1: rc({rc}) ch({ch}) has({has})".\
#format(ch=__check, rc=__result, has=__crit.has_key(__check))
if not __result:
__need = False
elif __crit.has_key(__check):
__need = not __crit[__check]
else:
__need = True
if __need:
__val = rule[__check]
__result, __raw = apply_string_check(__raw, __check, __val)
# print "dbg:: MAC: L2.2: rc({rc}) ch({ch}) v({val}) \
#buf({raw})".format(ch=__check, rc=__result, raw=__raw, val=__val)
return __result
def conv_by_rules(rawdata, rule, order=""):
"""Apply ruleset to string, by order if given, return parsed result"""
# see if we can use a different default without breaking too many things...
# __def_order = ( SUBWORD, BEFORE_VAL, AFTER_VAL, PREFIX_VAL, SUFFIX_VAL )
__def_order = ( PREFIX_VAL, SUFFIX_VAL, SUBWORD, AFTER_VAL, BEFORE_VAL )
if order == "":
order = __def_order
# print "dbg:: CBR: order({order})".format(order=str(order))
try:
__conv = rule[CONVERSION]
except KeyError:
__conv = str
try:
__base = rule[NUM_BASE]
except KeyError:
__base = 10
__hit = True
__val = rawdata
__used = dict()
for __cr in order:
__used[__cr] = True
if __hit and rule.has_key(__cr):
__hit, __val = apply_string_check(__val, __cr, rule[__cr])
for __cr in rule:
# print "dbg:: CBR: L2.1: rc({rc}) ch({ch}) has({has})".format(
#ch=__cr, rc=__hit, has=__used.has_key(__cr))
if not __hit:
__need = False
elif __used.has_key(__cr):
__need = not __used[__cr]
else:
__need = True
if __need:
__hit, __val = apply_string_check(__val, __cr, rule[__cr])
# print "dbg:: CBR: L2.2: rc({rc}) ch({ch}) v({val}) buf({raw})".\
#format(ch=__cr, rc=__hit, raw=__val, val=rule[__cr])
try:
if not __hit:
__val = error_by_rule(rule)
elif __conv == int:
__val = int(__val, __base)
elif __conv == long:
__val = long(__val, __base)
elif __conv == float:
__val = float(__val)
except ValueError:
__val = error_by_rule(rule)
return __val
# ---
def number_or_unlimited(buff):
"""Convert a string to a long, or set to an error value"""
if buff.strip() == "unlimited":
result = PDC.INF
else:
try:
result = long(buff)
except ValueError:
result = PDC.NAN
return result
def array_of_longs(wordlist):
"""Convert a list of strings to long's"""
__nums = dict()
for __off in range(0, len(wordlist)):
__nums[__off] = long(wordlist[__off])
return __nums
def breakout_option_list(combined, delim = ",", assign = "="):
"""Convert a string of name=value pairs to a dictionary"""
__optlist = dict()
__entries = combined.split(delim)
for __off in range(0, len(__entries)):
__part = __entries[__off].partition(assign)
if len(__part) == 3:
__optlist[__part[0]] = __part[2]
else:
__optlist[__entries[__off]] = ""
return __optlist
# ---
def get_file_registry():
"""Return the file handler registry"""
return FILE_HANDLER_REGISTRY
def get_partial_file_registry():
"""Return the file handler registry for partial file path matches"""
return PARTIAL_HANDLER_REGISTRY
def proc_file_to_path(proc_file):
"""
If the arg passed in doesn't exist, try prepending well known directories
to find the file.
"""
__path = ""
if os.path.isfile(proc_file):
__path = proc_file
else:
for __pref in PROC_PATH_PREFIX_LIST:
__trial = "{prefix}{file}".format(prefix=__pref, file=proc_file)
if __path == "" and os.path.isfile(__trial):
__path = __trial
return __path
def proc_file_to_symlink(proc_file):
"""
If the arg passed in doesn't exist, try prepending well known directories
to find the file.
"""
__path = ""
if os.path.islink(proc_file):
__path = proc_file
else:
for __pref in PROC_PATH_PREFIX_LIST:
__trial = "{prefix}{file}".format(prefix=__pref, file=proc_file)
if __path == "" and os.path.islink(__trial):
__path = __trial
return __path
def get_handler(proc_file):
"""
Lookup routine to find the code that knows how to parse the requested
/proc/net/ datafile
"""
__handler = 0
__append_list = PROC_PATH_PREFIX_LIST
if proc_file in FILE_HANDLER_REGISTRY:
__handler = FILE_HANDLER_REGISTRY[proc_file]
else:
for __prefix in __append_list:
__exp_file = "{prefix}{procfile}".format(prefix=__prefix,
procfile=proc_file)
if __exp_file in FILE_HANDLER_REGISTRY:
__handler = FILE_HANDLER_REGISTRY[__exp_file]
if __handler == 0:
__matchlen = 0
for __patt in PARTIAL_HANDLER_REGISTRY:
if len(__patt) <= __matchlen:
pass
elif __patt.endswith("/"):
__parts = proc_file.split(__patt)
if len(__parts) > 1:
if __parts[-1:][0].find("/") == -1:
__matchlen = len(__patt)
__handler = PARTIAL_HANDLER_REGISTRY[__patt]
elif proc_file.endswith(__patt):
__matchlen = len(__patt)
__handler = PARTIAL_HANDLER_REGISTRY[__patt]
if __handler == 0:
__handler = FILE_HANDLER_REGISTRY[F_NULL_HANDLER]
return __handler
def register_file(proc_file, handler):
"""Associate the given code object with a specific /proc/net datafile"""
FILE_HANDLER_REGISTRY[proc_file] = handler
HANDLER_TO_PATH[str(handler)] = proc_file
def register_partial_file(end_of_path, handler):
"""
Associate the given code object with a filename pattern to allow partial
matches
"""
PARTIAL_HANDLER_REGISTRY[end_of_path] = handler
def show_proc_file_handlers():
"""Print a list of all the known file to handler mappings"""
for __file in FILE_HANDLER_REGISTRY:
print "For {file} use {handler}".format(file=__file,
handler=str(FILE_HANDLER_REGISTRY[__file].__name__))
def show_partial_proc_file_handlers():
"""
Print a list of all the known 'end of path' file patterns and their handler
mappings
"""
for __patt in PARTIAL_HANDLER_REGISTRY:
print "Path {patt} matches {handler}".format(patt=__patt,
handler=str(PARTIAL_HANDLER_REGISTRY[__patt]))
def show_handler_file_path(cl_instance):
"""
Return the fullpath of the /proc file associated with the base class of the
instance provided
"""
__key = "<class '{tmod}.{tcl}'>".format(
tmod=cl_instance.__class__.__module__,
tcl=cl_instance.__class__.__name__)
return HANDLER_TO_PATH[__key]
def add_parse_rule(handler, rule, order=""):
"""
Append the supplied ruleset to the list of parsing rules defined
for the instance of the file parser.
"""
__rn = len(handler.parse_rule)
# print "dbg:: apr: n:{rn:d} r({rule})".format(rn=__rn, rule=str(rule))
if rule.has_key(FIELD_NUMBER) or not rule.has_key(FIELD_NAME):
handler.floating_rule[__rn] = False
elif rule.has_key(PREFIX_VAL) or rule.has_key(SUFFIX_VAL):
# -- rules without a field number, but that have a prefix
# -- and/or suffix check are parsed separately
handler.floating_rule[__rn] = True
else:
handler.floating_rule[__rn] = False
handler.parse_rule[__rn] = (rule, order)
# ---
class ProcNetNULL(object):
"""
Dummy class that just acts like reading from an empty file, returned as the
handler for unrecognized files.
"""
# pylint: disable=W0613
def __init__(self, *opts):
"""For the dummy handler, just need to make an empty results field."""
self.field = dict()
# pylint: enable=W0613
def __iter__(self):
"""Standard component of an iterator class"""
return self
def next(self):
"""The dummy iterator signals EOF when a record is requested."""
raise StopIteration
#
register_file(F_NULL_HANDLER, ProcNetNULL)
# ---
class FixedWhitespaceDelimRecs(object):
"""
Base class to read simple files with whitespace delimited columns,
consistent record format
"""
# pylint: disable=W0613
def extra_init(self, *opts):
"""No-op version of optional call-out from '__init__' method"""
# print "base:extra_init: {this}".format(this=str(self))
return
# pylint: enable=W0613
def extra_next(self, sio):
"""No-op version of optional call-out from 'next' method"""
# print "base:extra_next: {this}".format(this=str(self))
return sio.buff
def __init__(self, *opts):
# print "base:__init__: this{this} file{file}".format(this=str(self),
# file=show_handler_file_path(self))
if len(opts) > 0:
self.infile = proc_file_to_path(opts[0])
else:
self.infile = show_handler_file_path(self)
self.minfields = 0
self.skipped = ""
self.parse_rule = dict()
self.floating_rule = dict()
self.fixpos_hits = dict()
self.floating_hits = dict()
self.extra_init(*opts)
self.field = dict()
self.curr_sio = SeqFileIO.SeqFileIO()
# print "base:__init__: inp({infile})".format(infile=self.infile)
self.curr_sio.open_file(path=self.infile, min_words=self.minfields,
skip_line=self.skipped)
return
def __iter__(self):
return self
def next(self):
"""
Called to process and return the next logical record in the
currently open file.
"""
# print "base:next: {this}".format(this=str(self))
self.field = dict()
sio = self.curr_sio
sio.read_line()
__hit_rule = dict()
self.fixpos_hits = dict()
self.floating_hits = dict()
__fix_seq = 0
__flo_seq = 0
# -- for each word, see if a floating (pos independent) rule applies
for __off in range(0, sio.linewords):
__val = sio.lineparts[__off]
for __rulenum in self.parse_rule:
if self.floating_rule[__rulenum]:
__rinfo = self.parse_rule[__rulenum]
__cr = __rinfo[0]
try:
__ord = __rinfo[1]
except IndexError:
__ord = ""
__name = __cr[FIELD_NAME]
__match = True
if __cr.has_key(PREFIX_VAL):
__match = __match and __val.startswith(
__cr[PREFIX_VAL])
if __cr.has_key(SUFFIX_VAL):
__match = __match and __val.endswith(__cr[SUFFIX_VAL])
if __match:
self.field[__name] = conv_by_rules(__val, __cr, __ord)
__hit_rule[__rulenum] = 1
self.floating_hits[__flo_seq] = __name
__flo_seq += 1
# -- run through the rules and convert fixed columns as directed, this
# -- has to be done separately to make sure error values are set for
# -- fields that match columns past the ones we got in the last read.
for __rulenum in self.parse_rule:
__rinfo = self.parse_rule[__rulenum]
__cr = __rinfo[0]
try:
__ord = __rinfo[1]
except IndexError:
__ord = ""
if __cr.has_key(FIELD_NUMBER) and __cr.has_key(FIELD_NAME):
__hit_rule[__rulenum] = 1
__off = __cr[FIELD_NUMBER]
__name = __cr[FIELD_NAME]
if __off >= sio.linewords:
self.field[__name] = error_by_rule(__cr)
else:
# print "dbg:: nx/fixed: v({val}) r({rule})".format(
# val=sio.lineparts[__off], rule=str(__cr))
self.fixpos_hits[__fix_seq] = __name
__fix_seq += 1
self.field[__name] = conv_by_rules(sio.lineparts[__off],
__cr, __ord)
for __rulenum in self.parse_rule:
__rinfo = self.parse_rule[__rulenum]
__cr = __rinfo[0]
if __cr.has_key(FIELD_NAME) and not __hit_rule.has_key(__rulenum):
self.field[__cr[FIELD_NAME]] = error_by_rule(__cr)
# print "dbg:: nx/nohit: n:{rn:d} f({field}) r({rule})".format(
# rn=__rulenum, rule=str(__cr), field=__cr[FIELD_NAME])
return self.extra_next(sio)
# ---
class SingleNameValueList(object):
"""
Base class to read files where each line is two fields, one name and an
associated value
"""
# pylint: disable=W0613
def extra_init(self, *opts):
"""No-op version of optional call-out from '__init__' method"""
# print "base:extra_init: {this}".format(this=str(self))
return
# pylint: enable=W0613
def __init__(self, *opts):
# print "base:__init__: this{this} file{file}".format(this=str(self),
# file=show_handler_file_path(self))
if len(opts) > 0:
self.infile = proc_file_to_path(opts[0])
else:
self.infile = show_handler_file_path(self)
self.minfields = 2
self.skipped = ""
self.trim_tail = ""
self.debug_level = 0
self.extra_init(*opts)
self.field = dict()
self.curr_sio = SeqFileIO.SeqFileIO()
self.curr_sio.open_file(path=self.infile, min_words=self.minfields,
skip_line=self.skipped)
if self.debug_level >= 5:
print "dbg:: {name:s} reading '{infile}'".format(name=str(self),
infile=self.infile)
return
def __iter__(self):
return self
def next(self):
"""
Called to process and return the next logical record in the
currently open file.
"""
if self.debug_level > 0:
print "base:next: {this}".format(this=str(self))
__lines = self.curr_sio.read_all_lines()
if len(__lines) == 0:
raise StopIteration
else:
__hits = dict()
__seq = 0
for __keyval in __lines:
if len(__keyval) > 0:
__words = __keyval.split()
if len(__words) == 2:
__name = __words[0]
if __name[-1:] == self.trim_tail:
__name = __name[:-1]
if len(__name) > 0:
self.field[__name] = __words[1]
__hits[__seq] = __name
__seq += 1
self.field[PFC.F_HITS] = __hits
return self.field
# ---
class TwoLineLogicalRecs(object):
"""
Base class to read 'netstat', 'snmp' and any others with the same two-line
logical record format
"""
# pylint: disable=W0613
def extra_init(self, *opts):
"""No-op version of optional call-out from '__init__' method"""
# print "base:extra_init: {this}".format(this=str(self))
return
# pylint: enable=W0613
def __init__(self, *opts):
# print "base:__init__: this{this} file{file}".format(this=str(self),
# file=show_handler_file_path(self))
if len(opts) > 0:
self.infile = proc_file_to_path(opts[0])
else:
self.infile = show_handler_file_path(self)
self.minfields = 1
self.skipped = ""
self.protocol_type = ""
self.extra_init(*opts)
self.field = dict()
self.curr_sio = SeqFileIO.SeqFileIO()
# print "base:__init__: inp({infile})".format(infile=self.infile)
self.curr_sio.open_file(path=self.infile, min_words=self.minfields,
skip_line=self.skipped)
return
def __iter__(self):
return self
def next(self):
"""
Called to process and return the next logical record in the
currently open file.
"""
# print "base:next: {this}".format(this=str(self))
# Note: Only a couple of the /proc/net files use this format and it can't
# be parsed with the existing code. This one consists of a series
# of logical records, each one of which is two-lines in the file.
# The first line of each logical record starts with an id that names
# the logical record type, then has a list of field names that apply
# to that type. The second line starts with the same "type" field
# followed by the values for each of the fields. Both lines are
# blank delimited.
self.curr_sio.read_twoline_logical_record(self, F_PROTOCOL)
try:
self.protocol_type = self.field[F_PROTOCOL]
except KeyError:
self.protocol_type = ""
return(self.protocol_type, self.field)
# ---
class LabelledPairList(object):
"""
Base class to read 'sockstat', 'sockstat6' and others files w/ independent
records of name/value pairs
"""
# pylint: disable=W0613
def extra_init(self, *opts):
"""No-op version of optional call-out from '__init__' method"""
# print "base:extra_init: {this}".format(this=str(self))
return
# pylint: enable=W0613
def __init__(self, *opts):
# print "base:__init__: this{this} file{file}".format(this=str(self),
# file=show_handler_file_path(self))
if len(opts) > 0:
self.infile = proc_file_to_path(opts[0])
else:
self.infile = show_handler_file_path(self)
self.minfields = 1
self.skipped = ""
self.sock_type_list = ()
self.extra_init(*opts)
self.field = dict()
self.curr_sio = SeqFileIO.SeqFileIO()
# print "base:__init__: inp({infile})".format(infile=self.infile)
self.curr_sio.open_file(path=self.infile, min_words=self.minfields,
skip_line=self.skipped)
return
def __iter__(self):
return self
def next(self):
"""
Called to process and return the next logical record in the
currently open file.
"""
# -- Sample lines for reference...
# TCP: inuse 26 orphan 0 tw 1 alloc 30 mem 2
# UDP: inuse 3 mem 3
# UDPLITE: inuse 0
# RAW: inuse 0
# FRAG: inuse 0 memory 0
__result = set()
__unk_label = set()
self.field = dict()
for __sock_type in self.sock_type_list:
self.field[__sock_type] = dict()
__result, __unk_label = self.curr_sio.read_labelled_pair_list_file(
self, self.sock_type_list)
return __result
# ---
class ListOfTerms(object):
"""
Base class to read files that are just a list of terms, one per line,
like 'ip_tables_*' files
"""
# pylint: disable=W0613
def extra_init(self, *opts):
"""No-op version of optional call-out from '__init__' method"""
# print "base:extra_init: {this}".format(this=str(self))
return
# pylint: enable=W0613
def __init__(self, *opts):
# print "base:__init__: this{this} file{file}".format(this=str(self),
# file=show_handler_file_path(self))
if len(opts) > 0:
self.infile = proc_file_to_path(opts[0])
else:
self.infile = show_handler_file_path(self)
self.minfields = 1
self.skipped = ""
self.extra_init(*opts)
self.field = dict()
self.curr_sio = SeqFileIO.SeqFileIO()
# print "base:__init__: inp({infile})".format(infile=self.infile)
self.curr_sio.open_file(path=self.infile, min_words=self.minfields,
skip_line=self.skipped)
return
def __iter__(self):
return self
def next(self):
"""
Called to process and return the next logical record in the
currently open file.
"""
# -- Sample records. This file is only readable by root and is just
# -- a list of words, one per line.
# limit
# addrtype
# state
# hl
__lines = self.curr_sio.read_all_lines()
if len(__lines) == 0:
raise StopIteration
self.field[F_TERM_LIST] = __lines
return __lines
#
# ---
class FixedColumnRecs(object):
"""
Class to read files whose fields are consistently in specific columns
"""
# pylint: disable=W0613
def extra_init(self, *opts):
"""No-op version of optional call-out from '__init__' method"""
# print "base:extra_init: {this}".format(this=str(self))
return
# pylint: enable=W0613
def extra_next(self, sio):
"""No-op version of optional call-out from 'next' method"""
# print "base:extra_next: {this}".format(this=str(self))
return sio.buff
def __init__(self, *opts):
# print "base:__init__: this{this} file{file}".format(this=str(self),
# file=show_handler_file_path(self))
if len(opts) > 0:
self.infile = proc_file_to_path(opts[0])
else:
self.infile = show_handler_file_path(self)
self.minfields = 0
self.skipped = ""
self.fixedcols = dict()
self.extra_init(*opts)
self.field = dict()
self.curr_sio = SeqFileIO.SeqFileIO()
# print "base:__init__: inp({infile})".format(infile=self.infile)
self.curr_sio.open_file(path=self.infile, min_words=self.minfields,
skip_line=self.skipped)
return
def __iter__(self):
"""Standard iterator method"""
return self
def next(self):
"""
Called to process and return the next logical record in the
currently open file.
"""
# print "base:next: {this}".format(this=str(self))
sio = self.curr_sio
sio.read_line()
if type(self.fixedcols) == dict:
for __name in self.fixedcols:
__stcol = self.fixedcols[__name][0]
__encol = self.fixedcols[__name][1]
self.field[__name] = sio.buff[__stcol:__encol]
return self.extra_next(sio)
#
class SymLinkFile(object):
"""
Class to simulate an iterator that reads a file while
just returning the target of a symlink as the first line.
"""
# pylint: disable=W0613
def extra_init(self, *opts):
"""No-op version of optional call-out from '__init__' method"""
return
# pylint: enable=W0613
def __init__(self, *opts):
if len(opts) > 0:
__path = opts[0]
self.infile = proc_file_to_symlink(__path)
else:
self.infile = show_handler_file_path(self)
self.extra_init(*opts)
try:
self.symlink_target = os.readlink(self.infile)
self.complete = False
except OSError:
self.symlink_target = ""
self.complete = True
self.field = dict()
return
def __iter__(self):
"""Standard iterator method"""
return self
def next(self):
"""
Called to fetch a record, but for this class there's no
data to read from the file. The first call returns the
target of the symlink. All calls after that raise a
'StopIteration' condition.
"""
if self.complete:
raise StopIteration
self.complete = True
self.field[PFC.F_SYMLINK] = self.symlink_target
self.field[PFC.F_FILEPATH] = self.infile
return(self.infile, self.symlink_target)
#
class TaggedMultiLineFile(object):
"""
Read files where one logical records is made up of multiple physical recs
each of which has can be parsed with a 'parse_rule'. The call-out to the
'extra_next' method, which is where the extended class can perform more
complex parsing/transformations, is done only once the complete logical
record has been built.
"""
# pylint: disable=W0613
def extra_init(self, *opts):
"""No-op version of optional call-out from '__init__' method"""
return
# pylint: enable=W0613
def __init__(self, *opts):
if len(opts) > 0:
__path = opts[0]
self.infile = proc_file_to_path(__path)
else:
self.infile = show_handler_file_path(self)
self.at_eof = False
self.minfields = 0
self.skipped = ""