e-data 2.0.2.dev139__py3-none-any.whl → 2.0.2.dev140__py3-none-any.whl

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.
@@ -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,4 +1,4 @@
1
- e_data-2.0.2.dev139.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
1
+ e_data-2.0.2.dev140.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
2
2
  edata/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  edata/cli.py,sha256=CsBYDggt0grkLb4cebrckOvJyHr9PaGTnlWqT4yjuFo,5215
4
4
  edata/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -12,7 +12,7 @@ edata/database/queries.py,sha256=XziDSG2V-PVFmvRkj8UrEvgrc9Qc-ShhnxgsPHIsyik,830
12
12
  edata/database/utils.py,sha256=V-FJfyAh8YJBGHB85BbWtG3Ilq09pM9Gl-L86TffB4g,1062
13
13
  edata/database/migrations/__init__.py,sha256=P8VXZzfyceOV3GknYXif2HEMB8Bo4B7f7vKdIC02ZO0,1203
14
14
  edata/database/migrations/base.py,sha256=V7jfMaTl6XPCwM6Wei4bNxfOKSKanIHF30UgH-_CJNM,444
15
- edata/database/migrations/legacy_json_1_3_3.py,sha256=yGWLkhmsHJ9s5rOi9SqdCpDi1koULcnUxo2cWYApVpw,4272
15
+ edata/database/migrations/legacy_json_1_3_3.py,sha256=xTNCELpBuwmtStHi-zHRFvvVs2c8HdhJ5UOyT1o_yE4,5047
16
16
  edata/models/__init__.py,sha256=nMPPPkGjUsF51PBFlChgHyb5C3aQiccTRiVHUdJjNj4,153
17
17
  edata/models/bill.py,sha256=-qZ4l9f8BnPFlykFRoNYnmRe6-ELWmuNh6usks_k4X0,2674
18
18
  edata/models/data.py,sha256=O1aj0H_G92mnJjm6mYhC8fnl8ZOoatGiokRf8b7ziD8,2388
@@ -27,11 +27,11 @@ edata/tests/test_completion.py,sha256=-iyVt-juU3U1HqMuLUJXJthil2-n_4ddWiJq2GwnXT
27
27
  edata/tests/test_controller.py,sha256=CZmT6TC1vfQsxfBNRq1sKKwD4kIWDWW6OYTYeIpLKpc,4147
28
28
  edata/tests/test_datadis_connector.py,sha256=jsSFo7c30ZL7D4HAwja5-4_NTeEkpySWEV66G_9qV2w,15857
29
29
  edata/tests/test_incremental.py,sha256=MPRc7pPSIUuBSMFzlVTpV_wafeRat4kV3wy8p6NmyFQ,3280
30
- edata/tests/test_migrations.py,sha256=P8Qaas8u1Nwu2qNLSoqUAGL0WFqx9d59gs4Bp6L8RtY,6219
30
+ edata/tests/test_migrations.py,sha256=9VgDYE7vOPCiQlOndgbC1mTkOR2e0ZtEOs-UcS-ZrMw,7024
31
31
  edata/tests/test_redata_connector.py,sha256=SBvxsma5t-qvz2drxNisNM2P1STTurqEs1TSDgwaPJ8,508
32
32
  edata/tests/test_services.py,sha256=DiXA8Y9zZgtAs9HGiMOfystz2shaGAGEs444GJ2AymE,8321
33
33
  edata/tests/test_utils.py,sha256=h6yBzT7j3eA42zbXJ-AK9lXcZkjCMf16wnob1oqGvUE,2307
34
- e_data-2.0.2.dev139.dist-info/METADATA,sha256=F0A-u7spTG4nQy4enNmribG2a9tgeoS8Y1WT2w6P5UU,48041
35
- e_data-2.0.2.dev139.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
36
- e_data-2.0.2.dev139.dist-info/top_level.txt,sha256=Ez-fReWtUVTMcFuH0dzCnoKIgMBHgjLaBuba8QiKC7g,6
37
- e_data-2.0.2.dev139.dist-info/RECORD,,
34
+ e_data-2.0.2.dev140.dist-info/METADATA,sha256=005JynX9uvw7f6x46nu-5-OKIAxqUBxHoS7bt_jhn4o,48041
35
+ e_data-2.0.2.dev140.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
36
+ e_data-2.0.2.dev140.dist-info/top_level.txt,sha256=Ez-fReWtUVTMcFuH0dzCnoKIgMBHgjLaBuba8QiKC7g,6
37
+ e_data-2.0.2.dev140.dist-info/RECORD,,
@@ -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)