Can't load (non-image data file) from Gio.Resource

Hello all; I’m brand new to Gnome (and mostly to desktop app dev in general) and working on my first app. I’m using python as it’s my 25y language of choice, and I’m not ready to learn a new language and learn GTK, Meson, etc.

I’m using Gnome Builder and the python template.

My app has a data file - in JSON - that is stored in <project root>/data and which I have added to the default .gresources.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/com/github/sivy/MysteryThemeProject">
    <file preprocess="xml-stripblanks">window.ui</file>
    <file preprocess="xml-stripblanks">shortcuts-dialog.ui</file>
    <file preprocess="json-stripblanks">../data/icon-naming-spec.json</file>
  </gresource>
</gresources>

Build runs fine, but now I am trying to load the resource so that my python code can read the JSON file (I was making an educated guess on how):

# in Application.do_startup
resource = Gio.Resource.load('/com/github/sivy/MysteryThemeProject/icon-naming-spec.json')

I just get a file not found error. I suspected I need to try and load the actual .gresource file that the build creates, but I can’t find it.

  1. am I attempting to load the resources correctly? and
  2. where is Builder putting this file when it builds?

The compilation for the resources looks like:

gnome.compile_resources('mtp',
  'mtp.gresource.xml',
  gresource_bundle: true,
  install: true,
  install_dir: pkgdatadir,
)

pkgdatadir is /app/share/mtp but that doesn’t seem to exist, in the root fs OR in ~/Projects/.gnome-builder/

Any advice or help would really be appreciated.

Thanks

1 Like

The following line:

<file preprocess="json-stripblanks">../data/icon-naming-spec.json</file>

Means the file is at /com/github/sivy/MysteryThemeProject/../data/icon-naming-spec.json

So what you want is to alias it:

<file preprocess="json-stripblanks" alias="icon-naming-spec.json">../data/icon-naming-spec.json</file>

am I attempting to load the resources correctly?

You want to load the file (which is indeed located at /app/share/mtp for you, but that’s a path in the Flatpak’s filesystem, that’s why you can’t find it), and then query the Gio.Resource you get in return with Gio.Resource.lookup_data.

where is Builder putting this file when it builds?

I think it might be in ~/Projects/.gnome-builder somewhere, but that’s not too relevant here.

2 Likes

Oh thank you, that’s making sense. I will pop open the laptop after I wake up a bit more :wink: and try it out!

Progress!

Traceback (most recent call last):
File “/app/share/mtp/mtp/main.py”, line 46, in do_startup
resource = Gio.Resource.load(‘/app/share/mtp’)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
gi.repository.GLib.GError: g-file-error-quark: Failed to map /app/share/mtp’ /app/share/mtp’: mmap() failed: Permission denied (2)

1 Like

/app/share/mtp is the directory the .gresource file is in, but you need to load the file itself.

Thanks, that was it!