Skip to content

Commit

Permalink
Support for remote custom modules
Browse files Browse the repository at this point in the history
  • Loading branch information
bkbilly committed May 28, 2024
1 parent 1598a6f commit d89cbc0
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lnxlink/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import glob
import os
import sys
import requests

logger = logging.getLogger("lnxlink")

Expand All @@ -17,7 +18,7 @@ def autoload_modules(auto_exclude=None):
modules_path = f"{os.path.dirname(__file__)}/*.py"
for module_path in glob.glob(modules_path):
module = os.path.basename(module_path)
if "__" not in module and ".py" in module:
if "__" not in module and module.endswith(".py"):
module = module.replace(".py", "")
if module not in auto_exclude:
modules.append(module)
Expand All @@ -36,9 +37,15 @@ def parse_modules(list_modules=None, custom_modules=None, auto_exclude=None):
retries = 10
while retries >= 0:
try:
if ".py" in module_name:
module_path = os.path.dirname(module_name)
if module_name.endswith(".py"):
module_basename = os.path.basename(module_name)
if module_name.startswith("http"):
logger.info("Downloading custom module: %s", module_name)
module_data = requests.get(module_name, timeout=3).content
module_name = f"/tmp/{module_basename}"
with open(module_name, "wb") as handler:
handler.write(module_data)
module_path = os.path.dirname(module_name)
module_name = os.path.splitext(module_basename)[0]
sys.path.append(module_path)
addon = getattr(import_module(f"{module_name}"), "Addon")
Expand All @@ -55,7 +62,9 @@ def parse_modules(list_modules=None, custom_modules=None, auto_exclude=None):
)
retries = -1
except Exception as err:
logger.error("Error with module %s: %s", module_name, err)
logger.error(
"Error with module %s: %s", module_name, err, exc_info=True
)
time.sleep(2)
retries -= 1
logger.debug("Found addons: %s", ", ".join(modules))
Expand Down

0 comments on commit d89cbc0

Please sign in to comment.