You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #!/usr/bin/env python2
  2. ''' Simple log system for youtube-dlG. '''
  3. import os.path
  4. from time import strftime
  5. import wx
  6. from .utils import (
  7. check_path,
  8. fix_path
  9. )
  10. class LogManager(object):
  11. '''
  12. Simple log manager for youtube-dlG.
  13. Params
  14. config_path: Absolute path where LogManager should store the log file.
  15. add_time: If True LogManager will also log the time.
  16. Accessible Methods
  17. size()
  18. Params: None
  19. Return: Log file size in Bytes
  20. clear()
  21. Params: None
  22. Return: None
  23. log()
  24. Params: Data to log
  25. Return: None
  26. Accessible Variables
  27. log_file: Absolute path to log file.
  28. '''
  29. LOG_FILENAME = 'log'
  30. MAX_FILESIZE = 524288 # 524288B = 512kB
  31. def __init__(self, config_path, add_time=False):
  32. self.config_path = config_path
  33. self.add_time = add_time
  34. self.log_file = fix_path(config_path) + self.LOG_FILENAME
  35. self._init_log()
  36. self._auto_clear_log()
  37. def size(self):
  38. ''' Return log file size in Bytes. '''
  39. if not os.path.exists(self.log_file):
  40. return 0
  41. return os.path.getsize(self.log_file)
  42. def clear(self):
  43. ''' Clear log file. '''
  44. self._write('', 'w')
  45. def log(self, data):
  46. ''' Log data to log file. '''
  47. self._write(data + '\n', 'a')
  48. def _write(self, data, mode):
  49. ''' Write data to log file using mode. '''
  50. check_path(self.config_path)
  51. with open(self.log_file, mode) as log_file:
  52. if self.add_time:
  53. time = '[%s] ' % strftime('%c')
  54. log_file.write(time)
  55. log_file.write(data)
  56. def _init_log(self):
  57. ''' Init log file if not exist. '''
  58. if not os.path.exists(self.log_file):
  59. self._write('', 'w')
  60. def _auto_clear_log(self):
  61. ''' Auto clear log file. '''
  62. if self.size() > self.MAX_FILESIZE:
  63. self.clear()
  64. class LogGUI(wx.Frame):
  65. '''
  66. Simple GUI for LogManager
  67. Accessible Methods
  68. load()
  69. Params: File to load
  70. Return: None
  71. '''
  72. TITLE = 'Log Viewer'
  73. def __init__(self, parent=None):
  74. wx.Frame.__init__(self, parent, -1, self.TITLE, size=(650, 200))
  75. panel = wx.Panel(self)
  76. self._text_area = wx.TextCtrl(
  77. panel,
  78. -1,
  79. style=wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL
  80. )
  81. sizer = wx.BoxSizer()
  82. sizer.Add(self._text_area, 1, wx.EXPAND)
  83. panel.SetSizerAndFit(sizer)
  84. def load(self, filename):
  85. ''' Load file on text area if file exists. '''
  86. if os.path.exists(filename):
  87. self._text_area.LoadFile(filename)