Skip to content

Commit

Permalink
Add Logging system using logging.handlers.RotatingFileHandler
Browse files Browse the repository at this point in the history
Add flagart module
  • Loading branch information
oleksis committed Aug 5, 2020
1 parent 0715907 commit cdb2ff0
Show file tree
Hide file tree
Showing 8 changed files with 1,642 additions and 76 deletions.
5 changes: 3 additions & 2 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
- ChangeLog
- Man page
- Add locale (.mo) es_CU
- Add Flag art for Cuba
- Add flagart module
- Add Flag art for Cuba
- Add Logging system using the Python 'logging' module

### Fixed
- Imports for Internationalization with gettext
Expand All @@ -24,4 +26,3 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
- Build the binaries using PyInstaller. Fix path sep for each OS
- Build with updates disabled
- Set up for Windows/Linux
- Ignore wxPyDeprecationWarning
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ A cross platform front-end GUI of the popular [youtube-dl](https://rg3.github.io
* [GNU gettext](https://www.gnu.org/software/gettext/)

## Downloads
* [Source (.zip)](https://github.com/oleksis/youtube-dl-gui/archive/v1.0.1-alpha.zip)
* [Source (.tar.gz)](https://github.com/oleksis/youtube-dl-gui/archive/v1.0.1-alpha.tar.gz)
* [Source (.zip)](https://github.com/oleksis/youtube-dl-gui/archive/v1.1.0-alpha.zip)
* [Source (.tar.gz)](https://github.com/oleksis/youtube-dl-gui/archive/v1.1.0-alpha.tar.gz)

## Installation

### Install From Source
* Download & extract the source
* Change directory into *youtube-dl-gui-1.0.1-alpha*
* Change directory into *youtube-dl-gui-1.1.0-alpha*
* Create virtual environment
```
python3 -m venv env
Expand Down
3 changes: 1 addition & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Release 1.0.1
Release 1.2.0
=============
* Intergrity check youtube-dl bin
* Non-Windows shutdown using D-Bus instead of 'shutdown'
Expand Down Expand Up @@ -29,7 +29,6 @@ Other

* Refactor to Python3
* Review - rewrite threads communications
* Logging system using the Python 'logging' module
* Use youtube-dl directly from python instead of using the subprocess module


Expand Down
21 changes: 12 additions & 9 deletions youtube_dl_gui/downloadmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

from threading import (
Thread,
RLock,
Lock
RLock
)

from wx import CallAfter
Expand Down Expand Up @@ -379,10 +378,11 @@ def __init__(self, parent, download_list, opt_manager, log_manager=None):
self._running = True

# Init the custom workers thread pool
log_lock = None if log_manager is None else Lock()
wparams = (opt_manager, self._youtubedl_path(), log_manager, log_lock)
self._workers = [Worker(*wparams) for _ in range(opt_manager.options["workers_number"])]
wparams = (opt_manager, self._youtubedl_path(), log_manager)
self._workers = [Worker(*wparams, worker=worker)
for worker in range(1, int(opt_manager.options["workers_number"]) + 1)]

self.setName("DownloadManager")
self.start()

@property
Expand Down Expand Up @@ -560,13 +560,18 @@ class Worker(Thread):

WAIT_TIME = 0.1

def __init__(self, opt_manager, youtubedl, log_manager=None, log_lock=None):
def __init__(self, opt_manager, youtubedl, log_manager=None, worker=None):
super(Worker, self).__init__()
# Use Daemon ?
# self.setDaemon(True)
self.opt_manager = opt_manager
self.log_manager = log_manager
self.log_lock = log_lock
if worker:
self.worker = worker
else:
self.worker = 1

self.setName("Worker_" + str(worker))

self._downloader = YoutubeDLDownloader(youtubedl, self._data_hook, self._log_data)
self._options_parser = OptionsParser()
Expand Down Expand Up @@ -674,9 +679,7 @@ def _log_data(self, data):
"""
if self.log_manager is not None:
self.log_lock.acquire()
self.log_manager.log(data)
self.log_lock.release()

def _data_hook(self, data):
"""Callback method for self._downloader.
Expand Down
1,596 changes: 1,596 additions & 0 deletions youtube_dl_gui/flagart.py

Large diffs are not rendered by default.

58 changes: 22 additions & 36 deletions youtube_dl_gui/logmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"""Youtubedlg module responsible for handling the log stuff. """


import logging
from logging.handlers import RotatingFileHandler
import os.path
from time import strftime

from .utils import (
os_path_exists,
Expand Down Expand Up @@ -34,16 +35,30 @@ class LogManager(object):
"""

LOG_FILENAME = "log"
TIME_TEMPLATE = "[{time}] {error_msg}"
MAX_LOGSIZE = 524288 # Bytes

def __init__(self, config_path, add_time=False):
self.config_path = config_path
self.add_time = add_time
self.log_file = os.path.join(config_path, self.LOG_FILENAME)
self._encoding = get_encoding()
self._init_log()
self._auto_clear_log()
self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.DEBUG)

check_path(self.config_path)

self.handler = RotatingFileHandler(filename=self.log_file,
maxBytes=LogManager.MAX_LOGSIZE,
backupCount=5,
encoding=self._encoding)

fmt = "%(levelname)s-%(threadName)s-%(message)s"

if self.add_time:
fmt = "%(asctime)s-" + fmt

self.handler.setFormatter(logging.Formatter(fmt=fmt))
self.logger.addHandler(self.handler)

def log_size(self):
"""Return log file size in Bytes. """
Expand All @@ -54,7 +69,8 @@ def log_size(self):

def clear(self):
"""Clear log file. """
self._write('', 'w')
with open(self.log_file, "w") as log:
log.write("")

def log(self, data):
"""Log data to the log file.
Expand All @@ -63,34 +79,4 @@ def log(self, data):
data (string): String to write to the log file.
"""
self._write(str(data) + '\n', 'a')

def _write(self, data, mode):
"""Write data to the 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:
if mode == 'a' and self.add_time:
msg = self.TIME_TEMPLATE.format(time=strftime('%c'), error_msg=data)
else:
msg = data

log.write(msg)

def _init_log(self):
"""Initialize the log file if not exist. """
if not os_path_exists(self.log_file):
self._write('', 'w')

def _auto_clear_log(self):
"""Auto clear the log file. """
if self.log_size() > self.MAX_LOGSIZE:
self.clear()
self.logger.debug(str(data))
27 changes: 4 additions & 23 deletions youtube_dl_gui/optionsframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@


import os
import warnings

import wx
import wx.adv
from wx.lib.art import flagart
from wx.lib.embeddedimage import PyEmbeddedImage

from .flagart import catalog
# noinspection PyPep8Naming
from .utils import (
TwoWayOrderedDict as twodict,
Expand Down Expand Up @@ -227,27 +225,10 @@ def crt_bitmap_combobox(self, choices, size=(-1, -1), event_handler=None):
lang_code, lang_name = item
_lang, country = lang_code.split('_')

warnings.filterwarnings("ignore")

if country in flagart.catalog:
flag_bmp = flagart.catalog[country].getBitmap()
elif country == 'CU':
# Cuba Flag .png in base64encode.org
# Me dicen Cuba ;)
flag_bmp = PyEmbeddedImage(
"iVBORw0KGgoAAAANSUhEUgAAABAAAAALCAIAAAD5gJpuAAAABGdBTUEAAK/INwWK6QAAABl0R"
"Vh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHFSURBVHjaYrzNwKDquJyBjYfhxz"
"eGfwwMf/4w/PnH8AuI/sBIMPoBRL8Y2NgAAohFhYHhk831pRr+H7iF/v/7/+/f/z8Q8u8/IOP"
"Pn39///37/ef/73//gCLzc/YABBDjZwYG7uqqT8+e8yUl/LawYWD4D9T2/z8DFIOpf2CakZHx"
"/cePAAHEAnTF73//OX///jx9Bs/Xrwyu7v8ZGaAKYYgJTDIyMrAxMQAEEONHBgb29nZmM7Pfs"
"2f//P5jH5/WdvMwoON///kHcgaQ/AslpQQ5lhRuBQggkA0srq4MurrMv/+wTppk9/LMp11f56"
"gF/f4FhH9//fn7+/e/X0ANf/79lOBiYLgHEEAgDX927GD69On31Mk/f//ay6uz2ypa/B8DxFQ"
"Q+gOyAehjCREOBgYZgABifMvAwJWV9f/+/e9//vAmxP0PCfuPDTAwAP3A+ObNG4AAAjvpz58P"
"f/5wZaT/9vL9/+f/f2iogEhg+ILDiwESSt9+/AEIIBYeBoaPf/+3RfT9esvwZ+FNiO3AGPgNY"
"fwFxcPfv////vv/9z/DvuY5AAHEeJqBwVR0JjRSgdH5/w/QUzD0C0z+A5MMYJIJIMAA5qlT7L"
"92ZXAAAAAASUVORK5CYII=").getBitmap()
if country in catalog:
flag_bmp = catalog[country].GetBitmap()
else:
flag_bmp = flagart.catalog["BLANK"].getBitmap()
flag_bmp = catalog["BLANK"].GetBitmap()

combobox.Append(lang_name, flag_bmp)

Expand Down
2 changes: 1 addition & 1 deletion youtube_dl_gui/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-


__version__ = '1.0.1'
__version__ = '1.1.0'

0 comments on commit cdb2ff0

Please sign in to comment.