Index

lognestmonster / e9e65d5

A general-purpose single-header C logging library and parser for event-based logs. (Incomplete)

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
8928 Aug 2019 20:09e9e65d5Update python command versions for Travis CI Windows testingJosh Stockin102N

Blob @ lognestmonster / parser / parse.py

application/x-python5968 bytesdownload raw
1# lognestmonster Copyright (c) 2019 Joshua 'joshuas3' Stockin
2# <https://github.com/JoshuaS3/lognestmonster/>.
3
4
5# This file is part of lognestmonster.
6
7# lognestmonster is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11
12# lognestmonster is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16
17# You should have received a copy of the GNU General Public License
18# along with lognestmonster. If not, see <https://www.gnu.org/licenses/>.
19
20import os
21from utils import *
22from format import *
23from text import *
24from args import *
25from parseargs import *
26
27class Parser:
28 screen = None
29 clines = None
30 ccols = None
31 lines = None
32 def __init__(self):
33 self.screen = curses_window()
34 self.get_size()
35
36 def get_size(self):
37 size = term_size()
38 self.clines = size[0]
39 self.ccols = size[1]
40
41 def redraw(self):
42 self.get_size()
43 curses_clear(self.screen)
44 l = 0
45 for line in self.lines:
46 if l == self.clines: break
47 self.screen.move(l, 0)
48 s = ""
49 for string in line:
50 # ("content string", "attributes string")
51 content = string[0]
52 s += content
53
54 self.screen.attrset(0)
55
56 try:
57 attr = string[1]
58 except:
59 attr = ""
60
61 if "RESET" in attr: # set curses attributes based on attributes string
62 self.screen.attron(curses.color_pair(0))
63 else:
64 if "BLACK" in attr:
65 self.screen.attron(curses.color_pair(1))
66 if "BLUE" in attr:
67 self.screen.attron(curses.color_pair(2))
68 if "CYAN" in attr:
69 self.screen.attron(curses.color_pair(3))
70 if "GREEN" in attr:
71 self.screen.attron(curses.color_pair(4))
72 if "MAGENTA" in attr:
73 self.screen.attron(curses.color_pair(5))
74 if "RED" in attr:
75 self.screen.attron(curses.color_pair(6))
76 if "WHITE" in attr:
77 self.screen.attron(curses.color_pair(7))
78 if "YELLOW" in attr:
79 self.screen.attron(curses.color_pair(8))
80 if "BOLD" in attr:
81 self.screen.attron(curses.A_BOLD)
82 if "STANDOUT" in attr:
83 self.screen.attron(curses.A_STANDOUT)
84 if "UNDERLINE" in attr:
85 self.screen.attron(curses.A_UNDERLINE)
86
87 self.screen.addstr(content[:self.ccols - len(s)])
88 self.screen.attrset(0)
89 l += 1
90 curses_refresh(self.screen)
91
92 def get_input(self):
93 input = getch()
94 if input == UP: input = "up"
95 elif input == DOWN: input = "down"
96 elif input == LEFT: input = "left"
97 elif input == RIGHT: input = "right"
98 elif input == CTRLC: input = "exit"
99 else: input = str(input).lower()
100 return input
101
102 folder_name = "log_1565561768719"
103 divider = " | "
104 title = TITLE
105
106 def loop(self):
107 def tab(string, count=1):
108 return " "*count + string
109 input = None
110 try:
111 while True:
112 self.get_size()
113 screen_width = self.ccols
114
115 self.lines = [
116 [(self.title + " - ", "RESET"), (self.folder_name, "BOLD YELLOW")],
117 "Size: 235 bytes | Timestamp: 1565561768719",
118 "7 Statements | 2 Events | 0 Unsaved Data Trees",
119 pad(" STATEMENT 5 ", ":", screen_width),
120 "",
121 "[[LOG START]]",
122 "v 7 ITEMS",
123 tab("1565561768752 - INFO - INIT - HELLO"),
124 tab("1565561768752 - INFO - INIT - HELLO"),
125 tab("v 4 ITEMS"),
126 tab("1565561768752 - INFO - INIT - HELLO", 2),
127 tab("1565561768752 - INFO - INIT - HELLO", 2),
128 "",
129 [(tab("1565561768752 - INFO - INIT - HELLO", 2), "BOLD")],
130 "",
131 tab("1565561768752 - INFO - INIT - HELLO", 2),
132 tab("1565561768752 - INFO - INIT - HELLO"),
133 "",
134 str(input),
135 "[[LOG END]",
136 "",
137 "",
138 "",
139 pad(margin(CONTROLS_MESSAGE), ":", screen_width)
140 ]
141 self.redraw()
142
143 input = self.get_input()
144
145 if input == "exit" or input == "q": # exit program on Ctrl + C or `q`
146 break;
147 finally:
148 curses_reset()
149
150def main():
151 options = parseargs(sys.argv[1:])
152
153 display_help = "help" in options
154 display_version = "version" in options
155 is_status = "status" in options
156
157 screen_size = term_size()
158 clines = screen_size[0]
159 ccols = screen_size[1] - 2
160
161 if display_help:
162 output(VERSION_SHORT)
163
164 usage = []
165 line = "usage: lognestmonster "
166 x = len(line)
167 width = ccols - x
168 wrapped = wrap(USAGE_MESSAGE, width, "&")
169 for l in wrapped:
170 line += l
171 usage.append(line)
172 line = (" "*x)
173 output_lines(usage)
174
175 output()
176
177 for paragraph in COMMAND_INFO.split("\n\n"):
178 output_lines(wrap(paragraph, ccols))
179
180 output()
181
182 args = []
183
184 div1 = ccols/3
185 div2 = ccols/3*2
186 for arg in ARGUMENT_OPTIONS:
187 arg_lines = []
188 indicators = wrap(", ".join(ARGUMENT_OPTIONS[arg]["indicators"]), div1)
189 description = wrap(ARGUMENT_OPTIONS[arg]["description"], div2)
190 l1 = indicators[0]
191 z = 0
192 for line in description:
193 try:
194 l1 = indicators[z]
195 except:
196 l1 = ""
197 z += 1
198 l = columnize([(div1, l1), (div2, line)], ccols)
199 arg_lines.append(l)
200 args += arg_lines
201
202 output_lines(args)
203 return
204 elif display_version:
205 output(VERSION_MESSAGE)
206 return
207 elif len(sys.argv) == 1 or type(options) is str: # argument error or no args passed
208 exitcode = 0
209 output(VERSION_SHORT)
210 if type(options) is str: # print argument error is exists
211 output(options)
212 exitcode = 1
213 output(HELP_MESSAGE)
214 exit(exitcode)
215
216 positional = sys.argv[-1]
217 if positional is not "-" and os.path.isfile(positional) is not True and os.path.isdir(positional) is not True:
218 output(VERSION_SHORT)
219 output(TEXT_RED + "error:" + RESET + " file unknown '" + positional + "'")
220 output(HELP_MESSAGE)
221 exit(1)
222
223
224 output("args: " + str(options))
225 output("file: " + positional)
226
227 if not is_status:
228 p = Parser()
229 if positional is "-": positional = "stdin"
230 p.folder_name = positional
231 p.loop()
232
233
234if __name__ == "__main__":
235 main()
236