Index

lognestmonster / 8afd432

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
8128 Aug 2019 18:3495ce768Fix exit code behaviorJosh Stockin186N

Blob @ lognestmonster / parser / parse.py

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