Browse Source

Refactor Utils.py

doc-issue-template
MrS0m30n3 10 years ago
parent
commit
31d2f07a84
3 changed files with 24 additions and 25 deletions
  1. 2
      youtube_dl_gui/SignalHandler.py
  2. 32
      youtube_dl_gui/Utils.py
  3. 15
      youtube_dl_gui/YoutubeDLGUI.py

2
youtube_dl_gui/SignalHandler.py

@ -1,7 +1,7 @@
#! /usr/bin/env python
from .Utils import (
remove_spaces,
remove_empty_items,
string_to_array,
get_filename
)

32
youtube_dl_gui/Utils.py

@ -3,19 +3,19 @@
import os
import sys
def remove_spaces(array):
def remove_empty_items(array):
return [x for x in array if x != '']
def remove_spaces(string):
return string.replace(' ', '')
def string_to_array(string, char=' '):
return string.split(char)
def get_encoding():
if sys.version_info >= (3, 0):
return None
if sys.platform == 'win32':
return sys.getfilesystemencoding()
else:
return None
return None
def encode_list(data_list, encoding):
return [x.encode(encoding, 'ignore') for x in data_list]
@ -33,8 +33,7 @@ def get_path_seperator():
return '\\' if os.name == 'nt' else '/'
def fix_path(path):
if path != '':
if path[-1:] != get_path_seperator():
if path != '' and path[-1:] != get_path_seperator():
path += get_path_seperator()
return path
@ -44,13 +43,12 @@ def get_HOME():
def add_PATH(path):
os.environ["PATH"] += os.pathsep + path
def get_abs_path(path):
def abs_path(path):
path_list = path.split(get_path_seperator())
for i in range(len(path_list)):
if path_list[i] == '~':
path_list[i] = get_HOME()
path = get_path_seperator().join(path_list)
return path
return get_path_seperator().join(path_list)
def file_exist(filename):
return os.path.exists(filename)
@ -64,13 +62,13 @@ def get_filesize(path):
def makedir(path):
os.makedirs(path)
def get_icon_path(icon_path, file_path):
path = os.path.abspath(file_path)
path = path.split(get_path_seperator())
for i in range(len(icon_path)):
path[(i+1)*-1] = icon_path[i]
path = get_path_seperator().join(path)
return path
def icon_path(icon_path, file_path):
icon_path = icon_path.split(get_path_seperator())
L = len(icon_path)
file_path = os.path.abspath(file_path).split(get_path_seperator())
for index, item in reversed(list(enumerate(icon_path))):
file_path[index - L] = item
return get_path_seperator().join(file_path)
def get_filename(path):
return path.split(get_path_seperator())[-1]

15
youtube_dl_gui/YoutubeDLGUI.py

@ -36,8 +36,9 @@ from .Utils import (
get_os_type,
file_exist,
fix_path,
get_abs_path,
get_icon_path
abs_path,
icon_path,
remove_spaces
)
if get_os_type() == 'nt':
@ -73,7 +74,7 @@ LANGUAGES = ["English",
"Spanish",
"German"]
ICON = get_icon_path(['ytube.png', 'icons'], __file__)
ICON = icon_path('icons/ytube.png', __file__)
class MainFrame(wx.Frame):
@ -209,7 +210,7 @@ class MainFrame(wx.Frame):
def load_tracklist(self, trackList):
for url in trackList:
url = url.replace(' ', '')
url = remove_spaces(url)
if url != '':
self.urlList.append(url)
self.statusList._add_item(url)
@ -248,7 +249,7 @@ class MainFrame(wx.Frame):
''' For each url in current url list '''
for url in curList:
''' Remove spaces from url '''
url = url.replace(' ', '')
url = remove_spaces(url)
''' If url is not in self.urlList (original downloads list) and url is not empty '''
if url not in self.urlList and url != '':
''' Add url into original download list '''
@ -465,7 +466,7 @@ download the latest youtube-dl.'''
self.autoUpdateChk.SetValue(self.optList.autoUpdate)
def save_options(self):
self.optList.updatePath = get_abs_path(self.updatePathBox.GetValue())
self.optList.updatePath = abs_path(self.updatePathBox.GetValue())
self.optList.autoUpdate = self.autoUpdateChk.GetValue()
class PlaylistPanel(wx.Panel):
@ -1073,7 +1074,7 @@ For more information, please refer to <http://unlicense.org/>'''
self.savePathBox.SetValue(self.optList.savePath)
def save_options(self):
self.optList.savePath = get_abs_path(self.savePathBox.GetValue())
self.optList.savePath = abs_path(self.savePathBox.GetValue())
class OtherPanel(wx.Panel):

Loading…
Cancel
Save