Index

lognestmonster / f76cab2

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
7422 Aug 2019 20:50f76cab2update argument parsing for identifying unknown argumentsJosh Stockin1159N

Blob @ lognestmonster / parser / parse.py

application/x-python5954 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("", 2), "RESET"), ("1565561768752 - INFO - INIT - HELLO", "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 pad(margin(CONTROLS_MESSAGE), ":", screen_width)
141 ]
142 self.redraw()
143
144 input = self.get_input()
145
146 if input == "exit" or input == "q": # exit program on Ctrl + C or `q`
147 break;
148 finally:
149 curses_reset()
150
151def main():
152 options = parseargs(sys.argv[1:])
153
154 display_help = "help" in options
155 display_version = "version" in options
156 is_status = "status" in options
157
158 screen_size = term_size()
159 clines = screen_size[0]
160 ccols = screen_size[1] - 2
161
162 if display_help:
163 output(VERSION_SHORT)
164
165 usage = []
166 line = "usage: lognestmonster "
167 x = len(line)
168 width = ccols - x
169 wrapped = wrap(USAGE_MESSAGE, width, "&")
170 for l in wrapped:
171 line += l
172 usage.append(line)
173 line = (" "*x)
174 output_lines(usage)
175
176 output()
177
178 for paragraph in COMMAND_INFO.split("\n\n"):
179 output_lines(wrap(paragraph, ccols))
180
181 output()
182
183 args = []
184
185 div1 = ccols/3
186 div2 = ccols/3*2
187 for arg in ARGUMENT_OPTIONS:
188 arg_lines = []
189 indicators = wrap(", ".join(ARGUMENT_OPTIONS[arg]["indicators"]), div1)
190 description = wrap(ARGUMENT_OPTIONS[arg]["description"], div2)
191 l1 = indicators[0]
192 z = 0
193 for line in description:
194 try:
195 l1 = indicators[z]
196 except:
197 l1 = ""
198 z += 1
199 l = columnize([(div1, l1), (div2, line)], ccols)
200 arg_lines.append(l)
201 args += arg_lines
202
203 output_lines(args)
204 return
205 elif display_version:
206 output(VERSION_MESSAGE)
207 return
208 elif len(sys.argv) == 1 or type(options) is str: # argument error or no args passed
209 #Parser().loop()
210 output(VERSION_SHORT)
211 if type(options) is str: # print argument error is exists
212 output(options)
213 output(HELP_MESSAGE)
214 return
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 return
222
223
224 output("args: " + str(options))
225 output("file: " + positional)
226
227 p = Parser()
228 if positional is "-": positional = "stdin"
229 p.folder_name = positional
230 p.loop()
231
232
233if __name__ == "__main__":
234 main()
235