F-string formatting syntaxError

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.

I want to make things a bit simple. as config file is already there (containing all the variables), I’m wondering why it is not readable?:frowning_face:

byte-compiling /root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/environments.py to environments.cpython-35.pyc
byte-compiling /root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/write_json.py to write_json.cpython-35.pyc
  File "/root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/write_json.py", line 12
    if os.path.isfile(f'../../tmp/{file}'):
SyntaxError: invalid syntax

byte-compiling /root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/oli_ir.py to oli_ir.cpython-35.pyc
  File "/root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/oli_ir.py", line 45
    print(f'Power load is {power_load}')
SyntaxError: invalid syntax

byte-compiling /root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/oli_restapi.py to oli_restapi.cpython-35.pyc
  File "/root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/oli_restapi.py", line 17
    url = f'http://{oli_restapi_ip}:{oli_restapi_port}{oli_restapi_query}'
SyntaxError: invalid syntax

byte-compiling /root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/oli_mqtt.py to oli_mqtt.cpython-35.pyc
  File "/root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/oli_mqtt.py", line 51
    client_id = f'OLI_{oli_box_id}_PUB'
SyntaxError: invalid syntax

byte-compiling /root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/modbus_config.py to modbus_config.cpython-35.pyc
byte-compiling /root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/smartcontract_config.py to smartcontract_config.cpython-35.pyc
byte-compiling /root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/__init__.py to __init__.cpython-35.pyc
byte-compiling /root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/helpers.py to helpers.cpython-35.pyc
  File "/root/parts/olibox-core/install/lib/python3.5/site-packages/olibox_core/olibox_core/olibox_pkg/helpers.py", line 78
    prefix = f'{project_id.upper()}/OLI_{oli_box_id}/{device_type.upper()}'
SyntaxError: invalid syntax

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

    source: https://github.com/olisystems/olibox_core.git
    source-tag: 0.1dev
    plugin: python
    source: .
    requirements: ['requirements.txt']

    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?

I’m using following versions:

Python 3.7.4

snap    2.43.2
snapd   2.43.2
series  16
ubuntu  19.10
kernel  5.3.0-29-generic

snapcraft, version 3.9.8

os ubuntu 19.10

what is the exact line you use to run a build (are you somehow preventing multipass builds ?)

inside the directory, I’m doing the following steps:
$ snapcraft --debug
$ sudo snap install olibox-core_0.1dev_amd64.snap --devmode --dangerous

run the app with following command:
$ olibox-core

after this command, error appears.

make sure to run snapcraft clean if you made changes and trigger a new build, so the multipass vm gets wiped …

I do that every time before starting a new build but no luck!

hmm, the source: tree from your snapcraft.yaml seems to not exist on github …

@ogra I’m not sure but you can find the .yaml file here.

oh, in fact, you seem to have two source: entries in the snapcraft.yaml … make sure to comment the one pointing to the non-existing github tree !

(i wonder why the snapcraft yaml parser does not refuse to build with this … it should surely catch such duplcated lines)

still getting the same error.

looking again, you also have two base: entries, drop the one pointing to “core” you can really only have one (and i guess snspcraft just picked the second one and defaulted to core instead of core18)

1 Like

I’m stupid.
I created a new repo (https://github.com/m-yahya/snap-tutorial.git) and dropping the extra core seems working.
So, at this moment I can read the config file.
I’ll open another topic to describe what I wanted to achieve at the end.
Thank you all for your precious time and help, very much appreciated.

1 Like

can you please say a few words about file path, how they are determine?
do I need to specify file path relative to modules or file path should be relative to home directory?
for example with the following app structure:

├── config.toml
├── environmentals
├── olibox_core
│   └── olibox_core
│       ├── __init__.py
│       ├── olibox_core.py
│       └── olibox_pkg
│           ├── config.py
│           ├── environments.py
│           ├── __init__.py
│           └── user.py
├── olibox-core_0.1dev_amd64.snap
├── README.md
├── requirements.txt
├── setup.py
├── snap
│   └── snapcraft.yaml
└── tests
    ├── helpers_tests.py
    └── oliboxcore_tests.py

if I want to access config.toml file inside environments.py then should I use '../../config.tml' or just config.toml?

if config.toml is copied into the root of your snap (either look inside the snap by unsquashing it, or use the snapcraft --debug feature to inspect the prime/ dir inside the builds (which is identical to the structure inside the snap) … you’d access it by using something like os.environ['SNAP'] to read the toplevel path of the snap from the environment.

but assuming your config file should actually be configurable ($SNAP is the readonly bit of your snap) you probably want to use a configure or install hook to copy it to $SNAP_DATA (which corresponds to /var/snap/<snapname>/current/ (needs root, due to being in /var)) or SNAP_USER_DATA (i.e.: ~/snap/<snapname>/current/)

and make the app read it from there …

1 Like

It’s really helpful, thanks
I’ll open a new topic and tag you there.

can you please look at this question: How to persist user config data to SNAP_USER_DATA?