RASCSI/python/web
Uwe Seimet ddeede2beb
SASI code removal, error handling update, bug fixes, code cleanup (#806)
Summary ov most important changes triggered by the SASI code removal:

- Removed the SASI controller code
- New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests.
- New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests.
- The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes.
- The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI.
- scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device
- Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling.
- Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers.
- Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::.
- Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic.
- Use constructor member initialization lists (recommended for ISO C++)
- Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling
- Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow)
- Replaced NULL by nullptr (recommended for C++), resulting in better type safety
- Use more const member functions in order to avoid side effects
- The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs.
- Better encapsulation, updated access specifiers in many places
- Removed unused methods and method arguments
- Fixed a number of TODOs
- Added/updated unit tests for a lot of non-legacy classes
- Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 16:53:53 +02:00
..
mock/bin
service-infra Add HTTPS support to web UI 2022-08-01 16:09:01 +01:00
src SASI code removal, error handling update, bug fixes, code cleanup (#806) 2022-09-03 16:53:53 +02:00
tests
__init__.py
babel.cfg
README.md Allow custom drive sizes in bespoke UI; other sundry improvements (#813) 2022-08-27 20:38:23 -05:00
requirements.txt
start.sh Allow images to be extracted from StuffIt, tarball, gzip, and 7z archives 2022-08-12 04:19:02 +01: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.

(Optional) Pushing to the Pi via git

This is a setup for pushing code changes from your local development environment to the Raspberry Pi without a roundtrip to the remote GitHub repository.

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 internationalization (i18n).

It uses the 'pybabel' command line tool for extracting and compiling localizations. The Web Interface start script will automatically compile localizations upon launch.

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, it is time to localize the Python code. The below steps are derived from the Flask-Babel documentation.

First, generate the raw messages.pot file containing extracted strings.

$ pybabel extract -F babel.cfg -o messages.pot .

Initialize a new localization

When adding a localization for a new language, initialize the directory structure. Replace 'xx' with the two character code for the language.

$ pybabel init -i messages.pot -d src/translations -l xx

Update an existing loclization

After strings have been added or changed in the code, update the existing localizations.

pybabel update -i messages.pot -d src/translations

Then edit the updated messages.po file for your language. Make sure to update fuzzy strings and translate new ones.

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>."

Contributing to the project

New or updated localizations are treated just like any other code change. See the project README for further information.

(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.