Skip to content

Commit 9d07367

Browse files
committed
loader rework, fix #12
1 parent 2fe060c commit 9d07367

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ This **experimental** project offers a solution to enhance PyScript script devel
1414

1515

1616
## Installation
17-
- **Create Mock File**: In the `pyscript/modules/` directory, create an empty file named `pyscript_mock.py`.
1817
- **Update PyScript Configuration**: Enable `allow_all_imports` and `hass_is_global` in your PyScript [configuration](https://github.com/custom-components/pyscript#configuration)
1918
- **Configure PyScript**: Add the following to your PyScript configuration in `configuration.yaml`
2019
- add to pyscript configuration
@@ -24,20 +23,18 @@ This **experimental** project offers a solution to enhance PyScript script devel
2423
```
2524
- Copy the `apps/pyscript_autocomplete` folder to your PyScript directory.
2625
- **Generate Autocomplete Data**: Use the Home Assistant UI to call the `pyscript.autocomplete_generator` service.
27-
- **Integrate with Local Project**: Copy the `pyscript_mock` directory from your PyScript directory to your local project directory.
26+
- **Integrate with Local Project**: add `pyscript/modules` as source root in your IDE.
2827
- **Import in PyScript Files**: In any PyScript file, add the line `from pyscript_mock import *` for autocomplete functionality.
2928

3029
## Advanced Configuration
3130

32-
- `target_path:` Specifies the directory for writing generated files. The default is `pyscript/pyscript_mock/`.
3331
- `exclude:` A list of regular expressions used to exclude specific services, sensors, and attributes. By default, this list is empty, meaning nothing is excluded.
3432
- `include:` A list of regular expressions to specifically include certain services, sensors, and attributes. By default, all are included.
3533

3634
Example:
3735
```yaml
3836
apps:
3937
pyscript_autocomplete:
40-
target_path: /tmp/pyscript_mock/
4138
exclude:
4239
- person\.test\.friendly_name
4340
include:

apps/pyscript_autocomplete/__init__.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
from homeassistant.helpers import service as ha_service
1212
from homeassistant.helpers.entity_registry import EntityRegistry
1313

14-
from .loader import *
14+
from pyscript_mock import *
15+
# noinspection PyBroadException
16+
try:
17+
"""works in pyscript environment only"""
18+
# noinspection PyUnresolvedReferences,PyUnboundLocalVariable
19+
task.current_task()
20+
except:
21+
from pyscript_builtins import *
1522

1623

1724
class Generator:
@@ -303,15 +310,20 @@ def autocomplete_generator():
303310

304311
error_list = []
305312

306-
empty_module = hass.config.path(FOLDER + f"/modules/{module_name}.py")
307-
if not os.path.exists(empty_module):
308-
error_list.append(f"{empty_module} does not exists")
313+
"""Check for old loader files"""
314+
old_files = (hass.config.path(FOLDER + f"/modules/{module_name}.py"), hass.config.path(FOLDER + f"/{module_name}/"))
315+
for old_file in old_files:
316+
if os.path.exists(old_file):
317+
error_list.append(f"{old_file} exists, please remove it")
309318

310-
target_path = cfg.get("target_path")
311-
if not target_path:
312-
target_path = hass.config.path(FOLDER + f"/{module_name}/")
313-
if not os.access(target_path, os.W_OK):
314-
error_list.append(f"{target_path} does not exist or is not writable")
319+
target_path = hass.config.path(FOLDER + f"/modules/{module_name}/")
320+
try:
321+
os.makedirs(target_path, exist_ok=True)
322+
except OSError:
323+
error_list.append(f"Creation of the directory {target_path} failed")
324+
325+
if os.path.exists(target_path) and not os.access(target_path, os.W_OK):
326+
error_list.append(f"{target_path} is not writable")
315327

316328
include = cfg.get("include") or []
317329
include_regexes = []
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
from pyscript_generated import *
2-
from pyscript_builtins import *
1+
# noinspection PyBroadException
2+
try:
3+
"""works in pyscript environment only"""
4+
# noinspection PyUnresolvedReferences,PyUnboundLocalVariable
5+
task.current_task()
6+
except:
7+
from pyscript_generated import *
8+
from pyscript_builtins import *

modules/pyscript_mock.py

Whitespace-only changes.

0 commit comments

Comments
 (0)