Skip to content

Commit

Permalink
Store config file in XDG_CONFIG_HOME
Browse files Browse the repository at this point in the history
  • Loading branch information
A S Lewis committed Aug 22, 2019
1 parent d81271e commit 454eb99
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 33 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file copied from
# https://python-packaging.readthedocs.io/en/latest/minimal.html?highlight=gitignore

# Compiled python modules.
*.pyc

# Setuptools distribution folder.
/dist/

# Python egg metadata, regenerated from source files by setuptools.
/*.egg-info
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Linux/BSD Installation requirements
- `Python 3 <https://www.python.org/downloads>`__
- `Gtk 3 <https://python-gtk-3-tutorial.readthedocs.io/en/latest/>`__
- `Python Requests module <https://3.python-requests.org/>`__
- `Python xdg module <https://pypi.org/project/xdg/>`__


Optional dependencies
~~~~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion hello_world_mswin.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Shell script to start the Gtk test programme on MS Windows, using the MSYS2
# environment provided by the Tartube installer
cd tartube
cd /home/user/tartube/
python3 hello_world.py
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
# Setup
setuptools.setup(
name='tartube',
version='1.1.008',
version='1.1.015',
description='GUI front-end for youtube-dl',
# long_description=long_description,
long_description="""Tartube is a GUI front-end for youtube-dl, partly based
Expand Down Expand Up @@ -89,7 +89,7 @@
),
include_package_data=True,
python_requires='>=3.0, <4',
install_requires=['requests'],
install_requires=['requests', 'xdg'],
scripts=[script_exec],
project_urls={
'Bug Reports': 'https://github.com/axcore/tartube/issues',
Expand Down
8 changes: 6 additions & 2 deletions tartube/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,13 @@ def load_text(self, full_path):
return None

with open(full_path, 'r') as text_file:
text = text_file.read()

return text
try:
text = text_file.read()
return text

except:
return None


def load_to_pixbuf(self, full_path, width=None, height=None):
Expand Down
85 changes: 65 additions & 20 deletions tartube/mainapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
except:
HAVE_MOVIEPY_FLAG = False

from xdg.BaseDirectory import xdg_config_home


# Import our modules
import __main__
Expand Down Expand Up @@ -90,6 +92,8 @@ def __init__(self, *args, **kwargs):
**kwargs)

# Debugging flags (can only be set by editing the source code)
# Force Tartube to use the pre-v1.1.014 location for the config file
self.debug_old_config_flag = False
# Delete the config file and the contents of Tartube's data directory
# on startup
self.debug_delete_data_flag = False
Expand Down Expand Up @@ -274,9 +278,25 @@ def __init__(self, *args, **kwargs):
),
)

# Name of the Tartube config file, found in the self.script_parent_dir
# directory
# Name of the Tartube config file
self.config_file_name = 'settings.json'
# Path to the config file. Before v1.1.014, the Tartube config file was
# stored in self.script_parent_dir; it is now stored in
# $XDG_CONFIG_HOME/tartube
self.config_file_path = os.path.abspath(
os.path.join(
xdg_config_home,
__main__.__packagename__,
self.config_file_name,
),
)
self.config_file_old_path = os.path.abspath(
os.path.join(self.script_parent_dir, self.config_file_name),
)

if self.debug_old_config_flag:
self.config_file_path = self.config_file_old_path

# Name of the Tartube database file (storing media data objects). The
# database file is always found in self.data_dir
self.db_file_name = __main__.__packagename__ + '.db'
Expand Down Expand Up @@ -981,13 +1001,12 @@ def start(self):

# Delete Tartube's config file and data directory, if the debugging
# flag is set
config_path = os.path.abspath(
os.path.join(self.script_parent_dir, self.config_file_name),
)

if self.debug_delete_data_flag:
if os.path.isfile(config_path):
os.remove(config_path)
if os.path.isfile(self.config_file_path):
os.remove(self.config_file_path)

if os.path.isfile(self.config_file_old_path):
os.remove(self.config_file_old_path)

if os.path.isdir(self.data_dir):
shutil.rmtree(self.data_dir)
Expand Down Expand Up @@ -1084,9 +1103,11 @@ def start(self):
]
self.ytdl_update_current = 'Update using pip3 (recommended)'

# If the config file exists, load it. If not, create it
# If the config file exists, load it (from either the default or the
# pre v1.1.014 location). If not, create it
new_mswin_flag = False
if os.path.isfile(config_path):
if os.path.isfile(self.config_file_path) \
or os.path.isfile(self.config_file_old_path):
self.load_config()
elif self.debug_no_dialogue_flag:
self.save_config()
Expand Down Expand Up @@ -1390,19 +1411,24 @@ def load_config(self):
if DEBUG_FUNC_FLAG:
print('ap 1012 load_config')

config_path = os.path.abspath(
os.path.join(self.script_parent_dir, self.config_file_name),
)
# Before v1.1.014, the Tartube config file was stored in
# self.script_parent_dir; it is now stored in
# $XDG_CONFIG_HOME/tartube
config_file_path = self.config_file_path
if not os.path.isfile(config_file_path):

# Try the old location
config_file_path = self.config_file_old_path

# Sanity check
if self.current_manager_obj \
or not os.path.isfile(config_path) \
or not os.path.isfile(config_file_path) \
or self.disable_load_save_flag:
return

# Try to load the config file
try:
with open(config_path) as infile:
with open(config_file_path) as infile:
json_dict = json.load(infile)

except:
Expand Down Expand Up @@ -1456,6 +1482,21 @@ def load_config(self):
+ ' config file is invalid',
)

# If the config file was loaded from the pre-v1.1.014 location, move it
if config_file_path == self.config_file_old_path:

destination_dir = os.path.abspath(
os.path.join(
xdg_config_home,
__main__.__packagename__,
),
)

if not os.path.isdir(destination_dir):
os.makedirs(destination_dir)

shutil.move(self.config_file_old_path, self.config_file_path)

# Set IVs to their new values
if version >= 5024: # v0.5.024
self.toolbar_squeeze_flag = json_dict['toolbar_squeeze_flag']
Expand Down Expand Up @@ -1569,10 +1610,6 @@ def save_config(self):

if DEBUG_FUNC_FLAG:
print('ap 1117 save_config')

config_path = os.path.abspath(
os.path.join(self.script_parent_dir, self.config_file_name),
)

# Sanity check
if self.current_manager_obj or self.disable_load_save_flag:
Expand Down Expand Up @@ -1648,7 +1685,7 @@ def save_config(self):

# Try to save the file
try:
with open(config_path, 'w') as outfile:
with open(self.config_file_path, 'w') as outfile:
json.dump(json_dict, outfile, indent=4)

except:
Expand Down Expand Up @@ -2854,6 +2891,14 @@ def refresh_manager_finished(self):
self.save_config()
self.save_db()

# If updates to the Video Index were disabled because of Gtk issues,
# we must now redraw the Video Index and Video Catalogue from
# scratch
if self.gtk_broken_flag:
self.main_win_obj.video_index_reset()
self.main_win_obj.video_catalogue_reset()
self.main_win_obj.video_index_populate()

# Then show a dialogue window, if allowed
if self.operation_dialogue_flag:

Expand Down
14 changes: 11 additions & 3 deletions tartube/mainwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2307,8 +2307,12 @@ def video_index_update_row_icon(self, media_data_obj):
return

# Because of Gtk issues, we don't update the Video Index during a
# download operation if the flag is set
if self.app_obj.download_manager_obj and self.app_obj.gtk_broken_flag:
# download/refresh operation if the flag is set
if self.app_obj.gtk_broken_flag \
and (
self.app_obj.download_manager_obj \
or self.app_obj.refresh_manager_obj
):
return

# Update the treeview row
Expand Down Expand Up @@ -2360,7 +2364,11 @@ def video_index_update_row_text(self, media_data_obj):

# Because of Gtk issues, we don't update the Video Index during a
# download operation if the flag is set
if self.app_obj.download_manager_obj and self.app_obj.gtk_broken_flag:
if self.app_obj.gtk_broken_flag \
and (
self.app_obj.download_manager_obj \
or self.app_obj.refresh_manager_obj
):
return

# Update the treeview row
Expand Down
4 changes: 2 additions & 2 deletions tartube/tartube
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import mainapp

# 'Global' variables
__packagename__ = 'tartube'
__version__ = '1.1.008'
__date__ = '19 Aug 2019'
__version__ = '1.1.015'
__date__ = '22 Aug 2019'
__copyright__ = 'Copyright \xa9 2019 A S Lewis'
__license__ = """
Copyright \xc2\xa9 2019 A S Lewis.
Expand Down
4 changes: 2 additions & 2 deletions tartube/tartube_debian
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import mainapp

# 'Global' variables
__packagename__ = 'tartube'
__version__ = '1.1.008'
__date__ = '19 Aug 2019'
__version__ = '1.1.015'
__date__ = '22 Aug 2019'
__copyright__ = 'Copyright \xa9 2019 A S Lewis'
__license__ = """
Copyright \xc2\xa9 2019 A S Lewis.
Expand Down
2 changes: 1 addition & 1 deletion tartube_mswin.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Shell script to start Tartube on MS Windows, using the MSYS2 environment
# provided by the Tartube installer
cd /home/user/tartube/
cd /home/user/tartube/tartube
python3 tartube

0 comments on commit 454eb99

Please sign in to comment.