forked from platypush/platypush
[#331] Automatically initialize __init__.py in script dirs.
Closes: #331
This commit is contained in:
parent
fdeba9e53c
commit
228031c4ad
2 changed files with 13 additions and 12 deletions
|
@ -563,9 +563,7 @@ Procedures are pieces of custom logic that can be executed as atomic actions
|
||||||
using `procedure.<name>` as an action name.
|
using `procedure.<name>` as an action name.
|
||||||
|
|
||||||
They can be defined either in the `config.yaml` or as Python scripts stored
|
They can be defined either in the `config.yaml` or as Python scripts stored
|
||||||
under `~/.config/platypush/scripts` - provided that the procedure is also
|
under `~/.config/platypush/scripts`.
|
||||||
imported in `~/.config/platypush/scripts/__init__.py` so it can be discovered
|
|
||||||
by the service.
|
|
||||||
|
|
||||||
YAML example for a procedure that can be executed when we arrive home and turns
|
YAML example for a procedure that can be executed when we arrive home and turns
|
||||||
on the lights if the luminosity is lower that a certain thresholds, says a
|
on the lights if the luminosity is lower that a certain thresholds, says a
|
||||||
|
|
|
@ -233,19 +233,22 @@ class Config:
|
||||||
self._config['scripts_dir'] = os.path.join(
|
self._config['scripts_dir'] = os.path.join(
|
||||||
os.path.dirname(self.config_file), 'scripts'
|
os.path.dirname(self.config_file), 'scripts'
|
||||||
)
|
)
|
||||||
os.makedirs(self._config['scripts_dir'], mode=0o755, exist_ok=True)
|
|
||||||
|
|
||||||
# Create a default (empty) __init__.py in the scripts folder
|
scripts_dir = os.path.abspath(os.path.expanduser(self._config['scripts_dir']))
|
||||||
init_py = os.path.join(self._config['scripts_dir'], '__init__.py')
|
os.makedirs(scripts_dir, mode=0o755, exist_ok=True)
|
||||||
if not os.path.isfile(init_py):
|
|
||||||
with open(init_py, 'w') as f:
|
# Make sure that every folder with a .py file has an __init__.py file
|
||||||
f.write('# Auto-generated __init__.py - do not remove\n')
|
for root, _, files in os.walk(scripts_dir):
|
||||||
|
for file in files:
|
||||||
|
if file.endswith('.py'):
|
||||||
|
init_py = os.path.join(root, '__init__.py')
|
||||||
|
if not os.path.isfile(init_py):
|
||||||
|
with open(init_py, 'w') as f:
|
||||||
|
f.write('')
|
||||||
|
|
||||||
# Include scripts_dir parent in sys.path so members can be imported in scripts
|
# Include scripts_dir parent in sys.path so members can be imported in scripts
|
||||||
# through the `scripts` package
|
# through the `scripts` package
|
||||||
scripts_parent_dir = str(
|
scripts_parent_dir = str(pathlib.Path(scripts_dir).absolute().parent)
|
||||||
pathlib.Path(self._config['scripts_dir']).absolute().parent
|
|
||||||
)
|
|
||||||
sys.path = [scripts_parent_dir] + sys.path
|
sys.path = [scripts_parent_dir] + sys.path
|
||||||
|
|
||||||
def _init_dashboards_dir(self):
|
def _init_dashboards_dir(self):
|
||||||
|
|
Loading…
Reference in a new issue