PREFS resources
What are PREFS resources?
If you have ever build a Qt application you may have heard of something called QResource
, which is a resource file for your application, the application resources are in the application itself so you don't need to have those files to make your app work.
So if you have created a PREFS file manually and you want to build your app you will need to use PREFS resources.
Note
PREFS resources won't work when you build your app with PyInstaller, it's just a way to convert a PREFS file into a Python module. See PyInstaller section to bundle your PREFS files with PyInstaller.
PyInstaller
To bundle your PREFS file when building your app with PyInstaller you can simply add your PREFS file as a data:
From spec
file:
a = Analysis(...
datas=[('theme.prefs', '.')],
...
)
Where the first element in the tuple is the path to your PREFS file and the second the directory to write it in
Another example with the PREFS file inside a folder:
a = Analysis(...
datas=[('Prefs/theme.prefs', 'Prefs')],
...
)
Info
A dot .
means the current directory.
From the command line:
pyinstaller --add-data 'theme.prefs:.' myscript.py
Another example with the PREFS file inside a folder:
pyinstaller --add-data 'Prefs/theme.prefs:Prefs' myscript.py
Info
In the command line separate the PREFS path and the destination directory with a colon :
.
More info
More info about datas in PyInstaller documentation.
How to create a PREFS resource file?
Lets see how you're reading your PREFS file in your Python module:
import PREFS
theme = PREFS.read_prefs_file("theme.prefs")
print(theme)
Here's how theme.prefs
looks like:
#PREFS
font_family="UbuntuMono"
light=>
background_color="#dcdee0"
font_color="#000000"
link_color="#0000EE"
dark=>
background_color="#25282d"
font_color="#ffffff"
link_color="#006FEE"
To bundle this PREFS file open your terminal and type:
PREFS bundle path
Where path
is the path to your PREFS file, in our case theme.prefs
.
After running that command you should be able to see a new Python module called theme_resource.py
. It should look like:
# PREFS resource file
# Created using PREFS Python library
# https://patitotective.github.io/PREFS/
# Do not modify this file
VERSION = 'v0.2.65'
PREFS = {'font_family': 'UbuntuMono', 'light': {'background_color': '#dcdee0', 'font_color': '#000000', 'link_color': '#0000EE'}, 'dark': {'background_color': '#25282d', 'font_color': '#ffffff', 'link_color': '#006FEE'}}
ALIAS = 'theme.prefs'
You need to import that module into your Python module:
import PREFS
import theme_resource
theme = PREFS.read_prefs_file("theme.prefs")
print(theme)
Then just add :/
to the beginning of your PREFS file path (prefs.prefs -> :/prefs.prefs
):
import PREFS
import theme_resource
theme = PREFS.read_prefs_file(":/theme.prefs")
print(theme)
Info
See bundle
's API Reference for more options.