core-admin/qubes/plugins.py

68 lines
2.0 KiB
Python
Raw Normal View History

2014-11-13 14:38:41 +01:00
#!/usr/bin/python2 -O
2015-01-19 18:03:23 +01:00
# vim: fileencoding=utf-8
#
# The Qubes OS Project, https://www.qubes-os.org/
#
# Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
# Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
2014-11-13 14:38:41 +01:00
2014-11-13 18:10:27 +01:00
'''Plugins helpers for Qubes
Qubes uses two types of plugins: virtual machines and extensions.
'''
2014-11-13 14:38:41 +01:00
import imp
import os
class Plugin(type):
2014-11-13 18:10:27 +01:00
'''Base metaclass for plugins'''
2014-11-13 14:38:41 +01:00
def __init__(cls, name, bases, dict_):
# pylint: disable=unused-argument
2014-11-13 14:38:41 +01:00
if hasattr(cls, 'register'):
cls.register[cls.__name__] = cls
else:
# we've got root class
cls.register = {}
def __getitem__(cls, name):
return cls.register[name]
def load(modfile):
2014-11-13 18:10:27 +01:00
'''Load (import) all plugins from subpackage.
This function should be invoked from ``__init__.py`` in a package like that:
>>> __all__ = qubes.plugins.load(__file__) # doctest: +SKIP
'''
2014-11-13 14:38:41 +01:00
path = os.path.dirname(modfile)
listdir = os.listdir(path)
ret = set()
# pylint: disable=unused-variable
2014-11-13 14:38:41 +01:00
for suffix, mode, type_ in imp.get_suffixes():
for filename in listdir:
if filename.endswith(suffix):
ret.add(filename[:-len(suffix)])
2014-11-13 14:38:41 +01:00
if '__init__' in ret:
ret.remove('__init__')
2014-11-13 14:38:41 +01:00
return list(sorted(ret))