Hi all,
I’m trying to create a snap for my python application.
The snap is created successfully. But when I try to run the app using the command, I’m getting syntax error because of f-string formatting. An example of error line is:
prefix = f’{project_id.upper()}/OLI_{oli_box_id}/{device_type.upper()}’
^
SyntaxError: invalid syntax
any help how can I use f-string formatting in modules so that I can create snap without error?
thanks
Are you specifying a base in your snapcraft.yaml? f-strings are only available as of Python 3.6, but Xenial (i.e. base: core) is only 3.5. Bionic (base: core18) has 3.6.
It looks like that f-string depends on variables imported from olibox_core.olibox_core.olibox_pkg.environments. In turn, that module seems to load its globals from a TOML file:
If it can’t find a config.toml file in the current directory (such as the one in the root of your repository), you won’t have the project_id, oli_box_id, and device_type variables, producing the syntax error you got.
@jamesh thank you very much for your help.
Yes you are right, olibox_core.olibox_core.olibox_pkg.environments is loading variables for config.toml file that is located is the root directory.
I’m using setup.py for that I have to specify the path relative to setup.py. if I install the app using pip install . and run the command inside the directory, it works fine.
I just changed the path of config.toml file relative to the environments.py module (../../config.toml9) and remove and rebuild the snap, but still getting the same error.
You are not going to have access to the project source directory from your snap, so you will need to ensure it is installed, and that your app looks for it in an appropriate location.
Your app will be run with a number of environment variables that you can use to locate files. $SNAP will be set to the directory holding the (read-only) contents of your snap. If you intend for the user to modify the file, you might instead want to look for it in $SNAP_DATA or $SNAP_USER_DATA (depending on whether this is intended to be a system wide service or not).
If you are using $SNAP_DATA, you will want to do one of two things:
have your app fall back to a default configuration file in $SNAP if one in $SNAP_DATA is not found.
add an install hook to your snap that copies a default configuration to $SNAP_DATA.
I’m at the very early stage of snap building. If I run the app it will first the user for multiple inputs and 'll save those user inputs to the config.toml file. The app then read out variables form toml file and uses for some other functionality. can you please help me out a bit more how to proceed now?
many thanks
You won’t be able to write to any locations under /snap/olibox-core – it is all read only (your app is mounted as a squashfs file system). You will need to modify it so it can search for or store its configuration some place else. The environment variables I mentioned previously are probably what you want.
Currently, I can only see .yaml the file inside the snap directory even after creating the snap.
is this normal behavior or some other files should also be generated?
is that possible for you to provide a link to some resources or minimum example of storing user inputs and then using those configs later inside the snap?
When you built your snap, you should have ended up with a package with the file extension .snap. You can see the contents of the package by running unsquashfs -l filename.snap. This is what you will see at /snap/olibox-core/current when you install it. Nothing else in your repository will be available on the user’s system.
Judging by those errors, you’re trying to build against Python 3.5 again, which does not support f-strings. Are you still building against core18?
It also looks like a lot of your code is searching for configuration files relative to the current working directory still. So again: I suggest using the environment variables snapd makes available to you to decide where to put your configuration files.
yes, I’m using base: core18. This is the .yaml file:
name: olibox-core
base: core18
version: '0.1dev'
summary: Core functionality for Oli box
description: |
Olibox_core module provides the basic functionality
to wrtie data from IR sensor and REST API to json files
and publish this data to mqtt broker.
This utility is particularly useful for Oli box.
base: core
grade: devel
confinement: devmode
parts:
olibox-core:
source: https://github.com/olisystems/olibox_core.git
source-tag: 0.1dev
plugin: python
source: .
requirements: ['requirements.txt']
apps:
olibox-core:
command: bin/olibox-core
do I need to move config.toml file to some directory?
or do I need to make some changes to .yaml file to achieve this?