Skip to content

Commit 8ad98c2

Browse files
author
Linus Tibert
committed
relocate config file into ~/.config; add code to automatically migrate from old config file
1 parent f405c1d commit 8ad98c2

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

markdown/configuration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Configuration
2-
The program can be configured using the NFC43-Config.json file, which will be created in the installation dictionary when the script is executed for the first time.
2+
The program can be configured using the ~/.config/linux-file-converter-addon/config.json file, which will be created when the script is executed for the first time, as long as the program is able to write in the ~/.config directory.
33
Just modify the file, by changing the 'true' and 'false' values.
4-
If the program is installed in a root location, you need to change the configuration inside the script.
5-
<b>Don't forget to save your changes, and restart Nautilus after modifying the configuration!</b>
4+
This configuration should apply to all installations of linux-file-converter-addon at once.
5+
<b>Don't forget to save your changes, and restart Nautilus after modifying the configuration!</b> Relaunching is not necessary when using the adaption version for another program.
66
```bash
77
#Quit Nautilus
88
nautilus -q

markdown/errors-and-warnings.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ An error occurred during the update process of the config file.
5757
<br/><br/><b>Possible Effects:</b><br/>
5858
The config file may be missing some settings added by an update; some settings may be out of sync or using the default value.
5959
<br/><br/><b>How to solve?</b><br/>
60-
Consider checking the write-permissions in the installation folder, specifically the NFC43-Config.json file.
60+
Consider checking the write-permissions for the `~/.config/linux-file-converter-addon/config.json` file.
6161
If you can't solve the issue on your own, you can open an issue on Github.
6262

6363
### (402): No permission to self-update
@@ -74,16 +74,15 @@ To remove the release popup, you may disable the corresponding setting. To do th
7474

7575
### (403): No permission to write configuration file
7676
<b>Causes:</b><br/>
77-
The program has no permission to write in the dictionary where it is installed.
78-
<br/>This warning usually occurs when the script is located at "/usr/share/nautilus-python/extensions/".
77+
The program has no permission to write in the `~/.config/` dictionary, or it doesn't exist.
7978
<br/><br/><b>Possible Effects:</b><br/>
8079
The self-update function may not be available.
8180
<br/>The script needs to be configured by editing the script itself.
82-
<br/>If self-updating is enabled, the script's configuration will reset when a update is performed.
81+
<br/>If self-updating is enabled, the script's configuration will reset when an update is performed.
8382
<br/><br/><b>How to solve?</b><br/>
84-
To fix this, the script needs the permissions to write inside the folder, where it is located. This can be done by changing the folder permissions using [chmod](https://www.man7.org/linux/man-pages/man1/chmod.1.html) or by running the script as a privileged user.
85-
<br/>To use the configuration file, the user, who is executing the script (by starting Nautilus) needs permissions create and edit files inside the installation dictionary.
86-
<br/><br/>To prevent the settings from being reset, you can add a config file to the dictionary. Note that the file will not be update if new configurations are added.
83+
To fix this, the script needs the permissions to write inside the folder, where it is located. This can be done by changing the folder permissions using [chmod](https://www.man7.org/linux/man-pages/man1/chmod.1.html) or the change-permission dialogue of your file manager.
84+
<br/>To use the configuration file, the user, who is executing the script (by starting Nautilus) needs permissions create and edit files inside the `~/.config/` dictionary.
85+
<br/><br/>To prevent the settings from being reset, you can add the `~/.config/linux-file-converter-addon/config.json` file manually and make sure you have permissions to write to it.
8786

8887
#### All sections
8988
- [Main page](https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/README.md)

markdown/install-nautilus.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ sudo pacman -Sy python-nautilus
3535
```bash
3636
sudo mv nautilus-fileconverter.py /usr/share/nautilus-python/extensions/nautilus-fileconverter.py
3737
```
38-
<b>NOTE: Configuration and automatic updates are limited for root installations.</b> I recomend the other installation option. More information in the errors and warnings section [here](https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown%2Ferrors-and-warnings.md#003-no-permission-to-self-update) and [here](https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown%2Ferrors-and-warnings.md#004-no-permission-to-write-configuration-file)
38+
<b>NOTE: Automatic updates are limited for root installations.</b> I recomend the other installation option. More information in the errors and warnings section [here](https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown%2Ferrors-and-warnings.md#003-no-permission-to-self-update).
3939
- Now you only have to restart Nautilus using the following commands:
4040
```bash
4141
#Quit Nautilus

markdown/install-thunar.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ __Make sure you have installed all [general dependencies](https://github.com/Lic
2323
```
2424

2525
## Enable the action in Thunar
26-
- Open Thunar and navigate Edit/Configure custom actions...
26+
- Open Thunar and navigate Edit>Configure custom actions...
2727
- Click the "+" button to add a new action
2828
- Set the following Basic settings...
2929
- Name: Convert to...
@@ -38,7 +38,7 @@ __Make sure you have installed all [general dependencies](https://github.com/Lic
3838
- [x] Video Files
3939
- [ ] Other Files
4040

41-
You should now see the 'Convert to...' action when selecting a image, audio or video file.
41+
You should now see the 'Convert to...' action when selecting an image, audio or video file.
4242
Note: It may occur to you that you don't see the action when selecting multiple files.
4343
4444
#### All sections

nautilus-fileconverter.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /usr/bin/python3 -OOt
22

33
# --- Version number ---
4-
converterVersion = "001003005" # Change the number if you want to trigger an update.
4+
converterVersion = "001003006" # Change the number if you want to trigger an update.
55
# --- Variable to enable debug mode ---
66
development_version = False
77

@@ -29,7 +29,9 @@
2929
import re
3030
from multiprocessing import Process
3131
import traceback
32-
32+
config_file = "~/.config/linux-file-converter-addon/config.json"
33+
config_file = str(Path(config_file).expanduser())
34+
config_dir = pathlib.Path(config_file).parent
3335
# --- Create magic object ---
3436
mime = magic.Magic(mime=True)
3537

@@ -67,11 +69,8 @@
6769
if not scriptUpdateable:
6870
print(f"ERROR(Nautilus-file-converter)(402): No permission to self-update; script at \"{currentPath}/{os.path.basename(__file__)}\" is not writeable. View https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown/errors-and-warnings.md for more information.")
6971

70-
if not os.access(currentPath, os.W_OK):
71-
print(f"ERROR(Nautilus-file-converter)(403): No permission to write configuration file; \"{currentPath}\" is not writeable. View https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown/errors-and-warnings.md for more information.")
72-
7372
# --- Set default configs ---
74-
_configPreset = { # These are the pre-defined default settings; edit NFC43-Config.json if the program has permission to write.
73+
_configPreset = { # These are the default settings and will reset with each update; edit ~/.config/linux-file-converter-addon/config.json if the program has permission to read and write it.
7574
"automaticUpdates": True,
7675
"showPatchNotes": True,
7776
"showPatchNoteButton": True,
@@ -84,13 +83,32 @@
8483
"showDummyOption": True,
8584
"displayFinishNotification": True
8685
}
86+
87+
# --- Move settings from old config file to new location if the old one exists ---
88+
if not os.path.isdir(config_dir):
89+
os.system(f'mkdir "{config_dir}"')
90+
if Path(f"{currentPath}/NFC43-Config.json").is_file() and os.access(f"{currentPath}/NFC43-Config.json", os.W_OK):
91+
with open(f"{currentPath}/NFC43-Config.json", 'r') as jsonFile:
92+
try:
93+
old_config = json.load(jsonFile)
94+
except json.decoder.JSONDecodeError:
95+
old_config = _configPreset
96+
jsonFile.close()
97+
_configPreset = old_config.copy()
98+
with open(f"{currentPath}/NFC43-Config.json", 'w') as old_config_file:
99+
old_config["comment"] = f"THIS FILE DOES NOT CONFIGURE ANYTHING ANYMORE. USE {config_file} INSTEAD!"
100+
old_config = json.dumps(old_config, indent=4)
101+
old_config_file.write(old_config)
102+
old_config_file.close()
103+
os.system(f'mv "{currentPath}/NFC43-Config.json" "{currentPath}/NFC43-Config.json.DISABLED"')
104+
os.system(f'notify-send --app-name="linux-file-converter-addon" "Config file moved." "Your new config file is here:\n{config_dir}"')
87105
_config = _configPreset
88106

89107
# --- Load or store configs json ---
90-
if scriptUpdateable:
108+
if os.access(config_dir, os.W_OK):
91109
try:
92-
if Path(f"{currentPath}/NFC43-Config.json").is_file():
93-
with open(f"{currentPath}/NFC43-Config.json", 'r') as jsonFile:
110+
if Path(config_file).is_file():
111+
with open(config_file, 'r') as jsonFile:
94112
try:
95113
configJson = json.load(jsonFile)
96114
except json.decoder.JSONDecodeError:
@@ -102,11 +120,13 @@
102120
configJson = json.dumps(_config, indent=4)
103121
else:
104122
configJson = json.dumps(_configPreset, indent=4)
105-
with open(f"{currentPath}/NFC43-Config.json", "w") as jsonFile:
123+
with open(config_file, "w") as jsonFile:
106124
jsonFile.write(configJson)
107125
except:
108126
print("ERROR(Nautilus-file-converter)(401): Something went wrong while loading or updating the configuration file.")
109127
print(f"{traceback.format_exc()}")
128+
else:
129+
print(f"ERROR(Nautilus-file-converter)(403): No permission to write configuration file; \"{config_dir}\" is not writeable. View https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown/errors-and-warnings.md for more information.")
110130

111131
# --- Check for updates and update if auto-update is enabled ---
112132
if _config["automaticUpdates"]:

0 commit comments

Comments
 (0)