Warning, /firebird/doc/pyrobird.md is written in an unsupported language. File is not indexed.
0001 # pyrobird
0002
0003 [](https://pypi.org/project/pyrobird)
0004 [](https://pypi.org/project/pyrobird)
0005
0006 -----
0007
0008 ## Installation
0009
0010 ```bash
0011 pip install pyrobird
0012 ```
0013
0014 Optional dependencies:
0015
0016 - `batch` - install playwright, that allows to make screenshots in batch mode
0017 - `xrootd` - install libraries to read xrootd located files and URLs starting with `root://`
0018 - `test` - install pytest, mainly to run tests in development build
0019
0020 > If using `batch` for screenshots, after installing playwright, you need to install browser binaries:
0021 > ```bash
0022 > python -m playwright install chromium
0023 > ```
0024
0025 > If installed via pip, `xrootd` library requires compilation, so the system should have cmake,
0026 > compiler and some xrootd dependencies installed.
0027
0028 Development installation
0029
0030 ```bash
0031 python -m pip install --editable .[test,batch]
0032 ```
0033
0034 Running with Gunicorn (development mode)
0035
0036 ```bash
0037 gunicorn --bind 0.0.0.0:5454 pyrobird.server:flask_app --log-level debug --capture-output
0038 ```
0039
0040
0041 ## Contributing
0042
0043 - [PEP8](https://peps.python.org/pep-0008/) is required
0044 - [Use Numpy style dockstring comments](https://numpydoc.readthedocs.io/en/latest/format.html)
0045 - [pytest](https://docs.pytest.org/en/latest/) is used for unit tests. Aim for comprehensive coverage of the new code.
0046 - Utilize [type hints](https://docs.python.org/3/library/typing.html) wherever is possible to enhance readability and reduce errors.
0047 - Use of specific exceptions for error handling is better. As described in the [Python documentation](https://docs.python.org/3/tutorial/errors.html) rather than general exceptions.
0048 - Contributions are subject to code review. Please submit pull requests (PRs) against the `main` branch for any contributions.
0049 - Manage dependencies appropriately. Add new dependencies to `pyproject.toml`. Provide a justification for new dependencies
0050
0051 ## Testing
0052
0053 To install dependencies with testing libraries included
0054
0055 ```bash
0056 pip install .[test]
0057 ```
0058
0059 Navigate to the pyrobird/tests and execute:
0060
0061 ```bash
0062 pytest
0063
0064 # To stop immediately on error and enter debugging mode
0065 pytest -x --pdb
0066 ```
0067
0068 ## Development install
0069
0070 ```
0071 python -m pip install --upgrade --editable .[test]
0072 ```
0073
0074 # Pyrobird Server
0075
0076 This server allows Firebird to work with local files and the local file system as
0077 well as to complement frontend features such as opening XRootD files, etc.
0078
0079 Serve Firobird locally and have access to files in current directory:
0080
0081 ```bash
0082 pyrobird serve
0083 ```
0084
0085 **pyrobird** (backend) allows **Firebird** (frontend) to access certain files on your system.
0086 For this reason pyrobird server has multiple endpoints such as `/api/v1/download`
0087 which allows to download files. There are library files served and by default Firebird
0088 has access to files in your current directory or a directory provided via `--work-path` flag
0089
0090
0091 #### **Available Options**
0092
0093 | Option | Short | Type | Default | Description |
0094 |----------------------------|-------|---------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
0095 | `--allow-any-file` | | Flag | `False` | Allow unrestricted access to download files in the system. When enabled, the server allows downloads of all files which the running user has access to. **Use with caution**: It is considered dangerous in production environments. |
0096 | `--allow-cors` | | Flag | `False` | Enable CORS for downloaded files. This option should be used if you need to support web applications from different domains accessing the files, such as serving your server from a central Firebird server. |
0097 | `--disable-files` | | Flag | `False` | Disable all file downloads from the server. This option will prevent any file from being downloaded, enhancing security by restricting file access. |
0098 | `--work-path TEXT` | | String | `CWD` | Set the base directory path for file downloads. Defaults to the current working directory. Use this option to specify where the server should look for files when handling download requests. |
0099
0100
0101 > `--allow-any-file` - allows unrestricted access to download files in a system.
0102 > When enabled, the server allows downloads of all files that user has access to.
0103 > When disabled - only files in `--work-path` and its subdirectories are allowed to be downloaded.
0104 > This option could be considered safe on personal machines with a single user,
0105 > who runs localhost server in a terminal
0106 >
0107 > (!) It is considered dangerous in all other cases: farms, interactive nodes, production environments, servers, etc.
0108 > Just think `/etc/passwd` will be accessible through `localhost:port/api/v1/download?f=/etc/passwd`
0109 >
0110 > So security wise, it is better to use `--work-path` than `--allow-any-file`
0111
0112
0113 - Start server with default settings, Firebird works with files in current directory:
0114 ```bash
0115 fbd serve
0116 ```
0117
0118 - Set where Firebird will take files from:
0119
0120 ```bash
0121 fbd serve --work-path=/home/username/datafiles
0122 ```
0123
0124 Now if you set file `local://filename.root` in Firebird UI,
0125 the file `/home/username/datafiles/filename.root` will be opened
0126
0127
0128 ## API Documentation
0129
0130 This is technical explanation of what is under the hood of the server part
0131
0132 ## Features
0133
0134 - **Secure File Downloading**: Download files with access control to prevent unauthorized access.
0135 - **EDM4eic Event Processing**: Extract and convert specific events from EDM4eic files to JSON.
0136 - **Static File Serving**: Serve frontend assets seamlessly alongside API endpoints.
0137 - **Dynamic Configuration**: Serve configuration files with real-time server information.
0138 - **CORS Support**: Enable Cross-Origin Resource Sharing for specified routes.
0139
0140 ### Configuration Options
0141 - **DOWNLOAD_PATH**: `str[getcwd()]`, Specifies the directory from which files can be downloaded when using relative paths.
0142 - **PYROBIRD_DOWNLOAD_IS_DISABLED**: `bool[False]` If set to `True`, all download functionalities are disabled.
0143 - **PYROBIRD_DOWNLOAD_IS_UNRESTRICTED**: `bool[False]`, allows unrestricted access to download any file, including sensitive ones.
0144 - **CORS_IS_ALLOWED**: `bool[False]`, If set to `True`, enables Cross-Origin Resource Sharing (CORS) for download routes.
0145
0146
0147
0148
0149 The API provides endpoints for downloading files, processing EDM4eic events, and serving configuration files. It also includes static file serving for frontend assets.
0150
0151 ---
0152
0153 ### Download File
0154
0155 #### **Endpoint**
0156
0157 ```
0158 GET /api/v1/download
0159 GET /api/v1/download/<path:filename>
0160 ```
0161
0162 #### **Description**
0163
0164 Allows users to download specified files. The download can be restricted based on configuration settings to prevent unauthorized access to sensitive files.
0165
0166 #### **Parameters**
0167
0168 - **Query Parameters**:
0169 - `filename` (optional): The name or path of the file to download.
0170 - `f` (optional): An alternative parameter for the filename.
0171
0172 - **Path Parameters**:
0173 - `filename` (optional): The path of the file to download.
0174
0175 **Note**: You can provide the filename either as a query parameter or as part of the URL path.
0176
0177 #### **Usage**
0178
0179 1. **Download via Query Parameter**
0180
0181 ```bash
0182 curl -O "http://localhost:5454/api/v1/download?filename=example.txt"
0183 ```
0184
0185 2. **Download via URL Path**
0186
0187 ```bash
0188 curl -O "http://localhost:5454/api/v1/download/example.txt"
0189 ```
0190
0191 #### **Security Considerations**
0192
0193 - **Access Control**: Ensure that `DOWNLOAD_ALLOW_UNRESTRICTED` is set appropriately to prevent unauthorized access.
0194 - **Path Traversal**: The server sanitizes file paths to prevent directory traversal attacks.
0195
0196 ---
0197
0198 ### Open EDM4eic Event
0199
0200 #### **Endpoint**
0201
0202 ```
0203 GET /api/v1/convert/edm4eic/<int:event_number>
0204 GET /api/v1/convert/edm4eic/<int:event_number>/<path:filename>
0205 ```
0206
0207 #### **Description**
0208
0209 Processes an EDM4eic file to extract a specific event and returns the event data in JSON format. Supports both local and remote files.
0210
0211 #### **Parameters**
0212
0213 - **Path Parameters**:
0214 - `event_number` (required): The number of the event to extract.
0215 - `filename` (optional): The path or URL of the EDM4eic file.
0216
0217 - **Query Parameters**:
0218 - `filename` (optional): The name or path of the file to process.
0219 - `f` (optional): An alternative parameter for the filename.
0220
0221 **Note**: You can provide the filename either as a query parameter or as part of the URL path.
0222
0223 #### **Usage**
0224
0225 1. **Process Local File via Query Parameter**
0226
0227 ```bash
0228 curl "http://localhost:5454/api/v1/convert/edm4eic/5?filename=path/to/file.edm4eic.root"
0229 ```
0230
0231 2. **Process Remote File via URL Path**
0232
0233 ```bash
0234 curl "http://localhost:5454/api/v1/convert/edm4eic/5/http://example.com/data/file.edm4eic.root"
0235 ```
0236
0237 ### Asset Configuration
0238
0239 #### **Endpoint**
0240
0241 ```
0242 GET /assets/config.jsonc
0243 ```
0244
0245 #### **Description**
0246
0247 Serves the asset configuration file (`config.jsonc`) with additional server information injected dynamically.
0248
0249 #### **Usage**
0250
0251 ```bash
0252 curl "http://localhost:5454/assets/config.jsonc"
0253 ```
0254
0255 ### Publishing
0256
0257 ```bash
0258 hatch build
0259 hatch publish
0260
0261 # You will have to setup your pip authentication key
0262 ```