find aliases without importing modules
This commit is contained in:
parent
574735adab
commit
b956e8e2a6
9 changed files with 23 additions and 23 deletions
|
@ -16,16 +16,6 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from configparser import RawConfigParser
|
from configparser import RawConfigParser
|
||||||
|
|
||||||
def all_modules():
|
|
||||||
"""Return a list of available modules"""
|
|
||||||
result = []
|
|
||||||
path = os.path.dirname(bumblebee.modules.__file__)
|
|
||||||
for mod in [name for _, name, _ in pkgutil.iter_modules([path])]:
|
|
||||||
result.append({
|
|
||||||
"name": mod
|
|
||||||
})
|
|
||||||
return result
|
|
||||||
|
|
||||||
class Module(object):
|
class Module(object):
|
||||||
"""Module instance base class
|
"""Module instance base class
|
||||||
|
|
||||||
|
@ -231,14 +221,25 @@ class Engine(object):
|
||||||
button=button["id"], cmd=module.parameter(button["name"]))
|
button=button["id"], cmd=module.parameter(button["name"]))
|
||||||
|
|
||||||
def _read_aliases(self):
|
def _read_aliases(self):
|
||||||
|
"""Retruns a dictionary that maps every alias to its real module name"""
|
||||||
result = {}
|
result = {}
|
||||||
for module in all_modules():
|
m_path = os.path.abspath(bumblebee.modules.__file__)
|
||||||
|
m_path = os.path.dirname(m_path)
|
||||||
|
ALIASES_PATH = m_path + "/aliases/"
|
||||||
|
for name in os.listdir(ALIASES_PATH):
|
||||||
try:
|
try:
|
||||||
mod = importlib.import_module("bumblebee.modules.{}".format(module["name"]))
|
# listdir does not retur full paths
|
||||||
for alias in getattr(mod, "ALIASES", []):
|
f_path = ALIASES_PATH + name
|
||||||
result[alias] = module["name"]
|
# skip any directory
|
||||||
|
if not os.path.isfile(f_path): continue
|
||||||
|
with open(f_path) as f:
|
||||||
|
for alias in f.readlines():
|
||||||
|
alias = alias.strip()
|
||||||
|
# skip empty lines
|
||||||
|
if len(alias) == 0: continue
|
||||||
|
result[alias] = name
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
log.warning("failed to import {}: {}".format(module["name"], str(error)))
|
log.warning("failed to load aliases of {}: {}".format(name, str(error)))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _load_module(self, module_name, config_name=None):
|
def _load_module(self, module_name, config_name=None):
|
||||||
|
|
2
bumblebee/modules/aliases/datetime
Normal file
2
bumblebee/modules/aliases/datetime
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
date
|
||||||
|
time
|
2
bumblebee/modules/aliases/datetimetz
Normal file
2
bumblebee/modules/aliases/datetimetz
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
datetz
|
||||||
|
timetz
|
2
bumblebee/modules/aliases/pulseaudio
Normal file
2
bumblebee/modules/aliases/pulseaudio
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
pasink
|
||||||
|
pasource
|
1
bumblebee/modules/aliases/test
Normal file
1
bumblebee/modules/aliases/test
Normal file
|
@ -0,0 +1 @@
|
||||||
|
test-alias
|
|
@ -16,8 +16,6 @@ import datetime
|
||||||
import locale
|
import locale
|
||||||
import bumblebee.engine
|
import bumblebee.engine
|
||||||
|
|
||||||
ALIASES = ["date", "time"]
|
|
||||||
|
|
||||||
def default_format(module):
|
def default_format(module):
|
||||||
default = "%x %X"
|
default = "%x %X"
|
||||||
if module == "date":
|
if module == "date":
|
||||||
|
|
|
@ -26,8 +26,6 @@ import bumblebee.input
|
||||||
import bumblebee.output
|
import bumblebee.output
|
||||||
import bumblebee.engine
|
import bumblebee.engine
|
||||||
|
|
||||||
ALIASES = ["datetz", "timetz"]
|
|
||||||
|
|
||||||
def default_format(module):
|
def default_format(module):
|
||||||
default = "%x %X %Z"
|
default = "%x %X %Z"
|
||||||
if module == "datetz":
|
if module == "datetz":
|
||||||
|
|
|
@ -24,8 +24,6 @@ import bumblebee.input
|
||||||
import bumblebee.output
|
import bumblebee.output
|
||||||
import bumblebee.engine
|
import bumblebee.engine
|
||||||
|
|
||||||
ALIASES = ["pasink", "pasource"]
|
|
||||||
|
|
||||||
class Module(bumblebee.engine.Module):
|
class Module(bumblebee.engine.Module):
|
||||||
def __init__(self, engine, config):
|
def __init__(self, engine, config):
|
||||||
super(Module, self).__init__(engine, config,
|
super(Module, self).__init__(engine, config,
|
||||||
|
|
|
@ -5,8 +5,6 @@
|
||||||
|
|
||||||
import bumblebee.engine
|
import bumblebee.engine
|
||||||
|
|
||||||
ALIASES = ["test-alias"]
|
|
||||||
|
|
||||||
class Module(bumblebee.engine.Module):
|
class Module(bumblebee.engine.Module):
|
||||||
def __init__(self, engine, config):
|
def __init__(self, engine, config):
|
||||||
super(Module, self).__init__(engine, config,
|
super(Module, self).__init__(engine, config,
|
||||||
|
|
Loading…
Reference in a new issue