|
|
@ -1,6 +1,6 @@ |
|
|
|
#!/usr/bin/env python2 |
|
|
|
|
|
|
|
''' Simple log system for youtube-dlG. ''' |
|
|
|
"""Youtubedlg module responsible for handling the log stuff. """ |
|
|
|
|
|
|
|
import os.path |
|
|
|
from time import strftime |
|
|
@ -12,37 +12,26 @@ from .utils import check_path |
|
|
|
|
|
|
|
class LogManager(object): |
|
|
|
|
|
|
|
''' |
|
|
|
OUT_OF_DATE |
|
|
|
Simple log manager for youtube-dlG. |
|
|
|
|
|
|
|
Params |
|
|
|
config_path: Absolute path where LogManager should store the log file. |
|
|
|
add_time: If True LogManager will also log the time. |
|
|
|
|
|
|
|
Accessible Methods |
|
|
|
log_size() |
|
|
|
Params: None |
|
|
|
|
|
|
|
Return: Log file size in Bytes |
|
|
|
|
|
|
|
clear() |
|
|
|
Params: None |
|
|
|
|
|
|
|
Return: None |
|
|
|
|
|
|
|
log() |
|
|
|
Params: Data to log |
|
|
|
|
|
|
|
Return: None |
|
|
|
|
|
|
|
Accessible Variables |
|
|
|
log_file: Absolute path to log file. |
|
|
|
''' |
|
|
|
"""Simple log manager for youtube-dl. |
|
|
|
|
|
|
|
This class is mainly used for loggin the youtube-dl STDERR. |
|
|
|
|
|
|
|
Attributes: |
|
|
|
LOG_FILENAME (string): Filename of the log file. |
|
|
|
TIME_TEMPLATE (string): Custom template to log the time. |
|
|
|
MAX_LOGSIZE (int): Maximum number (Bytes) of the log file. |
|
|
|
|
|
|
|
Args: |
|
|
|
config_path (string): Absolute path where LogManager should |
|
|
|
store the log file. |
|
|
|
|
|
|
|
add_time (boolean): If True LogManager will also log the time. |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
LOG_FILENAME = "log" |
|
|
|
TIME_TEMPLATE = "[{time}] {error_msg}" |
|
|
|
MAX_LOGSIZE = 524288 # 524288B = 512kB |
|
|
|
MAX_LOGSIZE = 524288 |
|
|
|
|
|
|
|
def __init__(self, config_path, add_time=False): |
|
|
|
self.config_path = config_path |
|
|
@ -52,22 +41,30 @@ class LogManager(object): |
|
|
|
self._auto_clear_log() |
|
|
|
|
|
|
|
def log_size(self): |
|
|
|
''' Return log file size in Bytes. ''' |
|
|
|
"""Return log file size in Bytes. """ |
|
|
|
if not os.path.exists(self.log_file): |
|
|
|
return 0 |
|
|
|
|
|
|
|
return os.path.getsize(self.log_file) |
|
|
|
|
|
|
|
def clear(self): |
|
|
|
''' Clear log file. ''' |
|
|
|
"""Clear log file. """ |
|
|
|
self._write('', 'w') |
|
|
|
|
|
|
|
def log(self, data): |
|
|
|
''' Log data to log file. ''' |
|
|
|
"""Log data to log file. """ |
|
|
|
self._write(data + '\n', 'a') |
|
|
|
|
|
|
|
def _write(self, data, mode): |
|
|
|
''' Write data to log file using mode. ''' |
|
|
|
"""Write data to log file. |
|
|
|
|
|
|
|
That's the main method for writing to the log file. |
|
|
|
|
|
|
|
Args: |
|
|
|
data (string): String to write on the log file. |
|
|
|
mode (string): Can be any IO mode supported by python. |
|
|
|
|
|
|
|
""" |
|
|
|
check_path(self.config_path) |
|
|
|
|
|
|
|
with open(self.log_file, mode) as log: |
|
|
@ -79,27 +76,28 @@ class LogManager(object): |
|
|
|
log.write(msg) |
|
|
|
|
|
|
|
def _init_log(self): |
|
|
|
''' Init log file if not exist. ''' |
|
|
|
"""Init log file if not exist. """ |
|
|
|
if not os.path.exists(self.log_file): |
|
|
|
self._write('', 'w') |
|
|
|
|
|
|
|
def _auto_clear_log(self): |
|
|
|
''' Auto clear log file. ''' |
|
|
|
"""Auto clear log file. """ |
|
|
|
if self.log_size() > self.MAX_LOGSIZE: |
|
|
|
self.clear() |
|
|
|
|
|
|
|
|
|
|
|
class LogGUI(wx.Frame): |
|
|
|
|
|
|
|
''' |
|
|
|
Simple GUI for LogManager |
|
|
|
|
|
|
|
Accessible Methods |
|
|
|
load() |
|
|
|
Params: File to load |
|
|
|
"""Simple GUI for youtube-dlg. |
|
|
|
|
|
|
|
Return: None |
|
|
|
''' |
|
|
|
Attributes: |
|
|
|
TITLE (string): Frame title. |
|
|
|
FRAME_SIZE (tuple): Tuple that holds the frame size (width, height). |
|
|
|
|
|
|
|
Args: |
|
|
|
parent (wx.Window): Frame parent. |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
TITLE = "Log Viewer" |
|
|
|
FRAME_SIZE = (650, 200) |
|
|
@ -119,6 +117,6 @@ class LogGUI(wx.Frame): |
|
|
|
panel.SetSizerAndFit(sizer) |
|
|
|
|
|
|
|
def load(self, filename): |
|
|
|
''' Load file on text area if file exists. ''' |
|
|
|
"""Load file content on text area. """ |
|
|
|
if os.path.exists(filename): |
|
|
|
self._text_area.LoadFile(filename) |