e-data 2.0.2.dev139__tar.gz → 2.0.2.dev140__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. {e_data-2.0.2.dev139/e_data.egg-info → e_data-2.0.2.dev140}/PKG-INFO +1 -1
  2. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140/e_data.egg-info}/PKG-INFO +1 -1
  3. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/database/migrations/legacy_json_1_3_3.py +46 -20
  4. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/tests/test_migrations.py +24 -0
  5. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/pyproject.toml +1 -1
  6. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/LICENSE +0 -0
  7. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/MANIFEST.in +0 -0
  8. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/README.md +0 -0
  9. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/e_data.egg-info/SOURCES.txt +0 -0
  10. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/e_data.egg-info/dependency_links.txt +0 -0
  11. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/e_data.egg-info/requires.txt +0 -0
  12. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/e_data.egg-info/top_level.txt +0 -0
  13. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/__init__.py +0 -0
  14. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/cli.py +0 -0
  15. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/core/__init__.py +0 -0
  16. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/core/completion.py +0 -0
  17. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/core/const.py +0 -0
  18. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/core/utils.py +0 -0
  19. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/database/__init__.py +0 -0
  20. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/database/controller.py +0 -0
  21. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/database/migrations/__init__.py +0 -0
  22. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/database/migrations/base.py +0 -0
  23. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/database/models.py +0 -0
  24. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/database/queries.py +0 -0
  25. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/database/utils.py +0 -0
  26. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/models/__init__.py +0 -0
  27. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/models/bill.py +0 -0
  28. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/models/data.py +0 -0
  29. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/models/supply.py +0 -0
  30. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/providers/__init__.py +0 -0
  31. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/providers/datadis.py +0 -0
  32. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/providers/redata.py +0 -0
  33. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/services/bill_service.py +0 -0
  34. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/services/data_service.py +0 -0
  35. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/tests/__init__.py +0 -0
  36. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/tests/test_completion.py +0 -0
  37. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/tests/test_controller.py +0 -0
  38. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/tests/test_datadis_connector.py +0 -0
  39. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/tests/test_incremental.py +0 -0
  40. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/tests/test_redata_connector.py +0 -0
  41. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/tests/test_services.py +0 -0
  42. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/edata/tests/test_utils.py +0 -0
  43. {e_data-2.0.2.dev139 → e_data-2.0.2.dev140}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: e-data
3
- Version: 2.0.2.dev139
3
+ Version: 2.0.2.dev140
4
4
  Summary: Python library for managing spanish energy data from various web providers
5
5
  Author-email: VMG <vmayorg@outlook.es>
6
6
  License: GNU GENERAL PUBLIC LICENSE
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: e-data
3
- Version: 2.0.2.dev139
3
+ Version: 2.0.2.dev140
4
4
  Summary: Python library for managing spanish energy data from various web providers
5
5
  Author-email: VMG <vmayorg@outlook.es>
6
6
  License: GNU GENERAL PUBLIC LICENSE
@@ -10,6 +10,8 @@ Self-contained on purpose: everything it needs lives here, so a future version
10
10
  can drop this file (and its entry in the registry) once the upgrade window closes.
11
11
  """
12
12
 
13
+ import asyncio
14
+ from dataclasses import dataclass
13
15
  from datetime import datetime
14
16
  import json
15
17
  import logging
@@ -83,44 +85,68 @@ def _to_pvpc(d: dict) -> EnergyPrice:
83
85
  )
84
86
 
85
87
 
88
+ @dataclass
89
+ class _LegacyRecords:
90
+ """Records parsed from a legacy JSON export."""
91
+
92
+ supplies: list[Supply]
93
+ contracts: list[Contract]
94
+ energy: list[Energy]
95
+ power: list[Power]
96
+ pvpc: list[EnergyPrice]
97
+
98
+
99
+ def _load(path: str) -> _LegacyRecords | None:
100
+ """Read and parse the legacy file; ``None`` if it does not exist.
101
+
102
+ Blocking (file I/O plus building tens of thousands of records), so callers
103
+ run it in a worker thread to keep the event loop free.
104
+ """
105
+
106
+ if not os.path.isfile(path):
107
+ return None
108
+ with open(path, encoding="utf-8") as f:
109
+ raw = json.load(f)
110
+ return _LegacyRecords(
111
+ supplies=[_to_supply(x) for x in raw.get("supplies", [])],
112
+ contracts=[_to_contract(x) for x in raw.get("contracts", [])],
113
+ energy=[_to_energy(x) for x in raw.get("consumptions", [])],
114
+ power=[_to_power(x) for x in raw.get("maximeter", [])],
115
+ pvpc=[_to_pvpc(x) for x in raw.get("pvpc", [])],
116
+ )
117
+
118
+
86
119
  async def apply(db: EdataDB, storage_dir: str, cups: str) -> MigrationResult | None:
87
120
  """Import a 1.3.3 JSON export. Returns None (no-op) if the file is absent."""
88
121
 
89
122
  path = _legacy_path(storage_dir, cups)
90
- if not os.path.isfile(path):
123
+ records = await asyncio.to_thread(_load, path)
124
+ if records is None:
91
125
  return None
92
126
 
93
127
  _LOGGER.info("Migrating legacy 1.3.3 storage from %s", path)
94
- with open(path, encoding="utf-8") as f:
95
- raw = json.load(f)
96
-
97
128
  result = MigrationResult(name=NAME)
98
129
 
99
- supplies = [_to_supply(x) for x in raw.get("supplies", [])]
100
- for supply in supplies:
130
+ for supply in records.supplies:
101
131
  await db.add_supply(supply)
102
- result.supplies = len(supplies)
132
+ result.supplies = len(records.supplies)
103
133
 
104
134
  # Contracts/energy/power reference a supply by cups; take it from the imported
105
135
  # supply record rather than the (lower-cased) filename id.
106
- target_cups = supplies[0].cups if supplies else cups
136
+ target_cups = records.supplies[0].cups if records.supplies else cups
107
137
 
108
- contracts = [_to_contract(x) for x in raw.get("contracts", [])]
109
- for contract in contracts:
138
+ for contract in records.contracts:
110
139
  await db.add_contract(target_cups, contract)
111
- result.contracts = len(contracts)
140
+ result.contracts = len(records.contracts)
112
141
 
113
- energy = [_to_energy(x) for x in raw.get("consumptions", [])]
114
- await db.add_energy_list(target_cups, energy)
115
- result.energy = len(energy)
142
+ await db.add_energy_list(target_cups, records.energy)
143
+ result.energy = len(records.energy)
116
144
 
117
- power = [_to_power(x) for x in raw.get("maximeter", [])]
118
- await db.add_power_list(target_cups, power)
119
- result.power = len(power)
145
+ await db.add_power_list(target_cups, records.power)
146
+ result.power = len(records.power)
120
147
 
121
- pvpc = [_to_pvpc(x) for x in raw.get("pvpc", [])]
122
- await db.add_pvpc_list(pvpc)
123
- result.pvpc = len(pvpc)
148
+ await db.add_pvpc_list(records.pvpc)
149
+ result.pvpc = len(records.pvpc)
124
150
 
125
151
  _LOGGER.info(
126
152
  "%s: imported %s supplies, %s contracts, %s energy, %s power, %s pvpc",
@@ -5,6 +5,7 @@ from datetime import datetime
5
5
  import json
6
6
  import os
7
7
  import shutil
8
+ import threading
8
9
  from unittest.mock import patch
9
10
 
10
11
  import pytest
@@ -175,3 +176,26 @@ async def test_run_migrations_no_legacy_file(data_service: DataService) -> None:
175
176
  """run_migrations is a no-op when there is no legacy file."""
176
177
  results = await data_service.run_migrations()
177
178
  assert results == []
179
+
180
+
181
+ @pytest.mark.asyncio
182
+ async def test_apply_reads_legacy_file_off_the_event_loop(
183
+ data_service: DataService, tmp_path
184
+ ) -> None:
185
+ """The file is read and parsed in a worker thread, not on the event loop.
186
+
187
+ Home Assistant flags blocking ``open`` calls made on its event loop.
188
+ """
189
+ _install_legacy_file(str(tmp_path))
190
+ loop_thread = threading.current_thread()
191
+ load_threads = []
192
+ real_load = json.load
193
+
194
+ def recording_load(*args, **kwargs):
195
+ load_threads.append(threading.current_thread())
196
+ return real_load(*args, **kwargs)
197
+
198
+ with patch.object(legacy.json, "load", recording_load):
199
+ assert await legacy.apply(data_service.db, str(tmp_path), CUPS) is not None
200
+
201
+ assert load_threads and all(t is not loop_thread for t in load_threads)
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "e-data"
7
- version = "2.0.2.dev139"
7
+ version = "2.0.2.dev140"
8
8
  description = "Python library for managing spanish energy data from various web providers"
9
9
  readme = "README.md"
10
10
  authors = [
File without changes
File without changes
File without changes
File without changes