From 72e2d72776ecd42bfadb7f1c1cf7d88c0c2bf5ba Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Sat, 27 Aug 2011 08:05:20 +0300 Subject: [PATCH] ppf/new: Add skeleton methods to LibTorrentLogParser. --- ppf/new/parsing.py | 76 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/ppf/new/parsing.py b/ppf/new/parsing.py index d3763cf..55220f4 100644 --- a/ppf/new/parsing.py +++ b/ppf/new/parsing.py @@ -44,6 +44,8 @@ class SessionLogParser(object): def __init__(self, path): self.path = path + # parsing: file currently being parsed + self.parsing = None def get_next_message(self): """ @@ -53,6 +55,10 @@ class SessionLogParser(object): """ return None + def get_current_parsing_filename(): + """Return the name of the log file being currently parsed.""" + return self.parsing + class LibtorrentLogParser(object): """ @@ -68,26 +74,26 @@ class LibtorrentLogParser(object): # to_parse: list of files to be parsed # have_parsed: list of files that have been parsed - # parsing: file currently being parsed self.to_parse = [] self.have_parsed = [] - self.parsing = None - for entry in os.listdir(path): - entry_path = os.path.join(path, entry) + self.f = None # handler to file being parsed + + for entry in os.listdir(self.path): + entry_path = os.path.join(self.path, entry) if os.path.isfile(entry_path): # TODO: If entry is file and name is IP_PORT.log add it to list. if True: - to_parse.append(entry_path) + self.to_parse.append(entry_path) - status_file_path = os.path.join(path, "status.log") + status_file_path = os.path.join(self.path, "status.log") # TODO: Check if status file exists and is a file. # List functions as a stack. First files go to the end. if priority == "verbose": - to_parse.insert(0, status_file_path) + self.to_parse.insert(0, status_file_path) else: - to_parse.append(status_file_path) + self.to_parse.append(status_file_path) open_next_file() @@ -99,21 +105,51 @@ class LibtorrentLogParser(object): if self.parsing is None: # first call pass else: - f.close() - have_parsed.append(parsing) + self.f.close() + self.have_parsed.append(parsing) - parsing = to_parse.pop() - f = open(parsing, 'r') + parsing = self.to_parse.pop() + self.f = open(parsing, 'r') # TODO: Log this information somewhere for snapshotting purpose. # In case an error occurs parsing would resume from that point. + def is_status_log_line(self): + return False + + def is_peer_status_log_line(self): + return False + + def is_verbose_log_line(self): + return False + def parse_status_log_line(self): return None + def parse_peer_status_log_line(self): + return None + def parse_verbose_log_line(self): return None + def parse_log_line(self, line): + """Parse a log line and establish its type. + + Type may be status, verbose or peer status. + Return message in line, in case of message line, or None in case + of no message line.""" + + # Check log line type and call appropriate method. + if is_status_log_line(line): + return parse_status_log_line(line) + elif is_peer_status_log_line(line): + return parse_peer_status_log_line(line) + elif is_verbose_log_line(line): + return parse_verbose_log_line(line) + + # Return None in case of unknown/non-existent message type line. + return None + def get_next_message(self): """ Go through all files in libtorrent log folder and parse them. @@ -123,22 +159,26 @@ class LibtorrentLogParser(object): while True: # Find first message. while True: # Find first available file. - line = f.readline() + line = self.f.readline() if line is not None: break # end of file reached - open_next_file - continue + try: + open_next_file() + except IndexError, e: + # In case of no more files, return None. + return None + else: + continue - # TODO: Use a flag to distinguish between status and verbose logs. - msg = parse_status_log_line(line) - #msg = parse_verbose_log_line(line) + msg = parse_log_line(line) # Go around in case line is bogus (msg is None). if msg is not None: break + # Caller has to distinguish between message types. return msg -- 2.20.1