From a09601639d452dbc2cd5795183455637e54f8b17 Mon Sep 17 00:00:00 2001 From: Nils <> Date: Tue, 22 Mar 2022 20:27:10 +0100 Subject: [PATCH] adjust paths to local cbox --- engine/api.py | 2 +- engine/input_apcmini.py | 4 +--- engine/main.py | 4 +--- engine/pattern.py | 5 ++--- template/calfbox/nullbox.py | 36 ++++++++++++++++++++++++++++-------- template/start.py | 16 +--------------- 6 files changed, 34 insertions(+), 33 deletions(-) diff --git a/engine/api.py b/engine/api.py index ebb0e66..14d8fb5 100644 --- a/engine/api.py +++ b/engine/api.py @@ -26,7 +26,7 @@ import logging; logger = logging.getLogger(__name__); logger.info("import") from typing import List, Set, Dict, Tuple #Third Party Modules -from calfbox import cbox +from template.calfbox import cbox #Template Modules import template.engine.api #we need direct access to the module to inject data in the provided structures. but we also need the functions directly. next line: diff --git a/engine/input_apcmini.py b/engine/input_apcmini.py index f35d67b..ee55275 100644 --- a/engine/input_apcmini.py +++ b/engine/input_apcmini.py @@ -25,10 +25,8 @@ import logging; logging.info("import {}".format(__file__)) #Python Standard Library import atexit -#Third Party Modules -from calfbox import cbox - #Template Modules +from template.calfbox import cbox from template.engine.input_midi import MidiInput #Our Modules diff --git a/engine/main.py b/engine/main.py index e3c0a53..a1bf7dd 100644 --- a/engine/main.py +++ b/engine/main.py @@ -23,10 +23,8 @@ import logging; logger = logging.getLogger(__name__); logger.info("import") #Standard Library Modules -#Third Party Modules -from calfbox import cbox - #Template Modules +from template.calfbox import cbox from template.engine.data import Data as TemplateData import template.engine.sequencer from template.engine.sequencer import SequencerInterface #group tracks diff --git a/engine/pattern.py b/engine/pattern.py index 40c3e35..3bb5621 100644 --- a/engine/pattern.py +++ b/engine/pattern.py @@ -26,8 +26,8 @@ from typing import List, Set, Dict, Tuple from warnings import warn from statistics import median -#Third Party Modules -from calfbox import cbox +#Template Modules +from template.calfbox import cbox DEFAULT_VELOCITY = 90 @@ -426,4 +426,3 @@ class Pattern(object): return self #No export. Track uses pattern data directly in its own export. - diff --git a/template/calfbox/nullbox.py b/template/calfbox/nullbox.py index 38302c4..8d8ce7e 100644 --- a/template/calfbox/nullbox.py +++ b/template/calfbox/nullbox.py @@ -1,18 +1,23 @@ +#! /usr/bin/env python3 +# -*- coding: utf-8 -*- + +import importlib + class NullCalfbox(str): #iterable """A drop-in replacement for calfboxs python module. Use this for testing and development. At the start of your program, first file, insert: - import nullbox + import prefix.calfbox.nullbox or from SOMETHING import nullbox All further - from calfbox import cbox + from prefix.calfbox import cbox will use the null module. Even additional - import calfbox + import prefix.calfbox will use the nullbox module. """ @@ -55,13 +60,28 @@ class NullCalfbox(str): #iterable import sys -import nullbox -#Hack 'from calfbox import cbox' -sys.modules["calfbox"] = sys.modules["nullbox"] -import calfbox +try: + import nullbox +except ModuleNotFoundError: + from . import nullbox + +for key, value in sys.modules.items(): + if "nullbox" in key: + r = key + break +else: + raise ValueError("Nullbox Module not found") + +#r is the actual name of the calfbox parent modul. We cannot assume it to be "calfbox". +calfboxModuleName = r[:-len(".nullbox")] #remove suffix +sys.modules[calfboxModuleName] = sys.modules[r] #e.g. sys.modules["calfbox"] is now nullbox + +#Hack 'from prefix.calfbox import cbox' +importlib.import_module(calfboxModuleName) #Imported once here, all modules will import this variant later. +#import calfbox cbox = NullCalfbox("fake cbox null client") #Hack direct call 'import cbox' sys.modules["cbox"] = cbox -import cbox +import cbox #Imported once here, all modules will import this variant later. diff --git a/template/start.py b/template/start.py index 6303112..7c73498 100644 --- a/template/start.py +++ b/template/start.py @@ -111,20 +111,11 @@ if compiledVersion: "desktopfile": os.path.join(prefix, "share", "applications", METADATA["shortName"] + ".desktop"), # type: ignore #not ~/Desktop but our desktop file "share": os.path.join(prefix, "share", METADATA["shortName"]), # type: ignore "templateShare": os.path.join(prefix, "share", METADATA["shortName"], "template"), # type: ignore - #"lib": os.path.join(prefix, "lib", METADATA["shortName"]), #cbox is found via the PYTHONPATH } _root = os.path.dirname(__file__) _root = os.path.abspath(os.path.join(_root, "..")) - import zipfile - import tempfile - logger.info("Extracting shared library to temporary directory") - zipfilePath = get_script_dir().rstrip("/template") - assert zipfile.is_zipfile(zipfilePath), (zipfilePath) #in our tests this worked. but in lss this results not in a zip file header. linux file also says it is no zip. However, unzip works. - #Extract included .so to tmp dir, tmp dir gets garbage collected at the end of our program. - libsharedDir = tempfile.TemporaryDirectory() - #Not compiled, not installed. Running pure python directly in the source tree. else: _root = os.path.dirname(__file__) @@ -136,7 +127,6 @@ else: "desktopfile": os.path.join(_root, "desktop", "desktop.desktop"), #not ~/Desktop but our desktop file "share": os.path.join(_root, "engine", "resources"), "templateShare": os.path.join(_root, "template", "engine", "resources"), - #"lib": "", #use only system paths } logger.info("PATHS: {}".format(PATHS)) @@ -326,13 +316,9 @@ if args.mute: Needs adding of the local tree into python search path. 4) impossible: run program installed, calfbox in local tree - """ - #if not compiledVersion: - #import template.calfbox.py - # sys.modules["calfbox"] = sys.modules["template.calfbox.py"] - import template.calfbox.nullbox + logger.info(f"Using {sys.modules['template.calfbox']} as calfbox/nullbox") else: import ctypes.util libname = ctypes.util.find_library("calfbox-lss") #returns something like 'libcalfbox-lss.so.1' without a path, but with "lib"