diff --git a/CHANGELOG.md b/CHANGELOG.md index af891a77..2c6292df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ 1. [#733](https://github.com/influxdata/influxdb-client-python/pull/733): Write the last batch when the batching `WriteApi` is closed in an `atexit` handler instead of dropping it with "cannot schedule new futures after interpreter shutdown". 1. [#734](https://github.com/influxdata/influxdb-client-python/pull/734): Match `data_frame_timestamp_column` by exact column name, so a field whose name is part of the timestamp column name (e.g. `Open` with `OpenTime`) is no longer dropped. 1. [#739](https://github.com/influxdata/influxdb-client-python/pull/739): Async CSV query cancellation no longer raises `AttributeError` with pre 3.11 release Python platforms. +1. [#741](https://github.com/influxdata/influxdb-client-python/pull/741): Escape `\`, `"` and `${` in the task name and cron of `TasksApi.create_task_every`/`create_task_cron`, so the generated Flux `option task` stays a valid string literal. ## 1.50.0 [2026-01-23] diff --git a/influxdb_client/client/tasks_api.py b/influxdb_client/client/tasks_api.py index 5ca18fbd..90855c3a 100644 --- a/influxdb_client/client/tasks_api.py +++ b/influxdb_client/client/tasks_api.py @@ -64,6 +64,11 @@ def create_task(self, task: Task = None, task_create_request: TaskCreateRequest raise ValueError("task or task_create_request must be not None") + @staticmethod + def _escape_flux_string(value: str) -> str: + """Escape a value to be used inside a Flux string literal.""" + return value.replace('\\', '\\\\').replace('"', '\\"').replace('${', '\\${') + @staticmethod def _create_task(name: str, flux: str, every, cron, org_id: str) -> Task: @@ -76,9 +81,10 @@ def _create_task(name: str, flux: str, every, cron, org_id: str) -> Task: if cron is not None: repetition += "cron: " - repetition += '"' + cron + '"' + repetition += '"' + TasksApi._escape_flux_string(cron) + '"' - flux_with_options = '{} \n\noption task = {{name: "{}", {}}}'.format(flux, name, repetition) + escaped_name = TasksApi._escape_flux_string(name) + flux_with_options = '{} \n\noption task = {{name: "{}", {}}}'.format(flux, escaped_name, repetition) task.flux = flux_with_options return task diff --git a/tests/test_TasksApi.py b/tests/test_TasksApi.py index dc936dfd..1eb18110 100644 --- a/tests/test_TasksApi.py +++ b/tests/test_TasksApi.py @@ -1,15 +1,31 @@ import datetime import time +import unittest import pytest -from influxdb_client import PermissionResource, Permission, Task, InfluxDBClient +from influxdb_client import PermissionResource, Permission, Task, InfluxDBClient, TasksApi from influxdb_client.rest import ApiException from tests.base_test import BaseTest TASK_FLUX = 'from(bucket: "my-bucket") |> range(start: -2m) |> last()' +class TasksApiFluxOptionTest(unittest.TestCase): + + def test_create_task_escapes_name_and_cron(self): + task = TasksApi._create_task(name='my "task" \\ ${x}', flux=TASK_FLUX, every=None, + cron='0 2 * * * "', org_id="org") + + self.assertEqual(TASK_FLUX + ' \n\noption task = {name: "my \\"task\\" \\\\ \\${x}", ' + 'cron: "0 2 * * * \\""}', task.flux) + + def test_create_task_every_escapes_name(self): + task = TasksApi._create_task(name='a"b', flux=TASK_FLUX, every="1h", cron=None, org_id="org") + + self.assertEqual(TASK_FLUX + ' \n\noption task = {name: "a\\"b", every: 1h}', task.flux) + + class TasksApiTest(BaseTest): def setUp(self) -> None: