Skip to content

Commit

Permalink
Solve the problem of the portable mode. Closes issue #65.
Browse files Browse the repository at this point in the history
  • Loading branch information
eliluminado committed Mar 1, 2016
1 parent f56797a commit e9ffa9a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
31 changes: 31 additions & 0 deletions tests/test_optionmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import os
import unittest

import mock

from youtube_dl_gui.optionsmanager import get_config_path


Expand Down Expand Up @@ -72,5 +74,34 @@ def test_get_settings_json_win(self):
'Application Data\\youtube-dlg\\settings.json'
)


class TestConfigPathPortableMode(unittest.TestCase):

def setUp(self):
test_file_path = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.abspath(
os.path.join(test_file_path, os.pardir)
)
self.test_path = os.path.join(
parent_dir,
'youtube_dl_gui/settings'
)

@mock.patch('os.path.isfile')
def test_get_path_portable_mode(self, mock_isfile):
mock_isfile.return_value = True
self.assertEqual(
get_config_path(),
unicode(self.test_path)
)

@mock.patch('os.path.isfile')
def test_get_settings__json_portable_mode(self, mock_isfile):
mock_isfile.return_value = True
self.assertEqual(
get_config_path(settings_file=True),
unicode(os.path.join(self.test_path, 'settings.json'))
)

if __name__ == "__main__":
unittest.main()
31 changes: 24 additions & 7 deletions youtube_dl_gui/optionsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,42 @@ def get_config_path(platform=None, settings_file=False):
Windows = %AppData% + app_name
Linux = ~/.config + app_name
SETTINGS_FILENAME (string): Filename of the settings file.
settings_filename (string): Filename of the settings file.
Portable mode:
Placing a file with name '. portable' in the root directory of the
application, this forces the application to search the files of
configuration in the 'settings' directory and not in the default path.
"""
SETTINGS_FILENAME = 'settings.json'
settings_filename = 'settings.json'
portable_mode = False

if os.path.isfile('.portable'):
portable_mode = True
settings_path = os.path.dirname(os.path.abspath(__file__))

if not platform:
platform = PLATFORM
if platform == 'nt':
import ntpath as path
settings_path = os.getenv('APPDATA').decode('utf-8')
if not portable_mode:
settings_path = os.getenv('APPDATA').decode('utf-8')
elif platform == 'posix':
import posixpath as path
settings_path = path.join(os.getenv('HOME').decode('utf-8'), '.config')
if not portable_mode:
settings_path = path.join(
os.getenv('HOME').decode('utf-8'), '.config'
)
else:
raise Exception("Unsupported platform")
settings_path = path.join(settings_path, __appname__.lower())

if portable_mode:
settings_path = path.join(settings_path, 'settings')
else:
settings_path = path.join(settings_path, __appname__.lower())
if settings_file:
# TODO: Devolver con archivo settings.json
return path.join(settings_path, SETTINGS_FILENAME)
return path.join(settings_path, settings_filename)
else:
return settings_path

Expand Down

0 comments on commit e9ffa9a

Please sign in to comment.