RASCSI/python/web
Daniel Markstedt e8f392c3f1
Create SysCmds common class, and refactor Python codebase (#697)
* Move the oled script's PiCmds module to common, and rename it SysCmds.

* Use sys_cmds.get_ip_and_host() in web UI code.

* Move the auth_active() method to device_utils

* Rename device_utils to web_utils. Make auth_active() method take the group as argument.

* Migrate all pi_cmds methods to the SysCmds common class.

* Display hostname and ip in Web UI.

* Resolve or suppress pylint warnings.

* Resolve a pylint warning.

* Resolve or suppress pylint warnings.

* Import libraries at the top level for readability. In my testing on a Pi3B+, this leads to ~1.5k more memory being used by the python3 process.

* Change page title as requested by akuker.

* Reenable the import-outside-toplevel pylint rule.

* Resolve pylint warnings.

* Fix error following refactoring.

* Minor UI tweaks.

* Cleanup.

* Break out bridge config validation into a utility method.

* Move the dropzonejs method into the web_utils package

* Move get_logs method into SysCmds class.

* Improve get logs UI.

* Resolve pylint warning.

* Standardize class instance name.
2022-02-26 21:46:35 -08:00
..
mock/bin clean squashed restructuring branch. #455 2022-01-09 20:22:19 +01:00
service-infra clean squashed restructuring branch. #455 2022-01-09 20:22:19 +01:00
src Create SysCmds common class, and refactor Python codebase (#697) 2022-02-26 21:46:35 -08:00
tests clean squashed restructuring branch. #455 2022-01-09 20:22:19 +01:00
__init__.py restructuring towards python client library #455 (#613) 2022-01-21 15:08:29 -08:00
babel.cfg clean squashed restructuring branch. #455 2022-01-09 20:22:19 +01:00
README.md Updating README.md to reflect the project file structure (#633) 2022-02-01 20:51:51 -08:00
requirements.txt clean squashed restructuring branch. #455 2022-01-09 20:22:19 +01:00
start.sh restructuring towards python client library #455 (#613) 2022-01-21 15:08:29 -08:00

RaSCSI Web

Setup local dev env

# Change to python/web/src
$ cd python/web
# Make a virtual env named venv
$ python3 -m venv venv
# Use that virtual env in this shell
$ source venv/bin/activate
# Install requirements
$ pip install -r requirements.txt
# Use mocks and a temp dir - start the web server
$ BASE_DIR=/tmp/images/ PATH=$PATH:`pwd`/mock/bin/ cd python/web && PYTHON_COMMON_PATH=$(dirname $PWD)/common/src PYTHONPATH=$PWD/src:${PYTHON_COMMON_PATH} python3 src/web.py

Mocks for local development

You may edit the files under mock/bin to simulate Linux command responses. TODO: rascsi-web uses protobuf commands to send and receive data from rascsi. A separate mocking solution will be needed for this interface.

Pushing to the Pi via git

Setup a bare repo on the rascsi

$ ssh pi@rascsi
$ mkdir /home/pi/dev.git && cd /home/pi/dev.git
$ git --bare init
Initialized empty Git repository in /home/pi/dev.git

Locally

$ cd ~/source/RASCSI
$ git remote add pi ssh://pi@rascsi/home/pi/dev.git
$ git push pi master

Localizing the Web Interface

We use the Flask-Babel library and Flask/Jinja2 extension for i18n.

It uses the 'pybabel' command line tool for extracting and compiling localizations. Activate the Python venv in src/web/ to use it:

$ cd python/web
$ source venv/bin/activate
$ pybabel --help

To create a new localization, it needs to be added to the LANGAUGES constant in web/settings.py. To localize messages coming from the RaSCSI backend, update also code in raspberrypi/localizer.cpp in the RaSCSI C++ code.

Once this is done, follow the steps in the Flask-Babel documentation to generate the messages.po for the new language.

Updating the strings in an existing messages.po is also covered above.

When you are ready to contribute new or updated localizations, use the same Gitflow Workflow as used for any code contributions to submit PRs against the develop branch.

Working with PO files

See the GNU gettext documentation for an introduction to the PO file format.

We make heavy use of python-format for formatting, for instance:

#: file_cmds.py:353
#, python-format
msgid "%(file_name)s downloaded to %(save_dir)s"
msgstr "Laddade ner %(file_name)s till %(save_dir)s"

There are also a few instances of formatting in JavaScript:

#: templates/index.html:381
msgid "Server responded with code: {{statusCode}}"
msgstr "Servern svarade med kod: {{statusCode}}"

And with html tags:

#: templates/index.html:304
#, python-format
msgid ""
"Emulates a SCSI DaynaPORT Ethernet Adapter. <a href=\"%(url)s\">Host "
"drivers and configuration required</a>."
msgstr ""
"Emulerar en SCSI DaynaPORT ethernet-adapter. <a href=\"%(url)s\">Kräver "
"drivrutiner och inställningar</a>."

(Optional) See translation stats for a localization

Install the gettext package and use msgfmt to see the translation progress.

$ sudo apt install gettext
$ cd python/web/src
$ msgfmt --statistics translations/sv/LC_MESSAGES/messages.po
215 translated messages, 1 untranslated message.