gmyth-stream/server/0.2/lib/utils.py
author morphbr
Thu Apr 19 22:38:46 2007 +0100 (2007-04-19)
branchtrunk
changeset 577 7dc357cbaa40
parent 565 ed34b1dab103
child 585 a1783dab9ba6
permissions -rw-r--r--
[svn r582] * GMyth-Streamer (version 0.2):
- Mencoder plugin 100% working (without mythtv stuff)
- Debug messages updated
- Bug fixes inside server/utils
morphbr@565
     1
#!/usr/bin/env
morphbr@565
     2
morphbr@565
     3
__author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza"
morphbr@565
     4
__author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br"
morphbr@565
     5
__license__ = "GPL"
morphbr@565
     6
__version__ = "0.3"
morphbr@565
     7
morphbr@565
     8
import os
morphbr@565
     9
import stat
morphbr@565
    10
import sys
morphbr@565
    11
import logging
morphbr@565
    12
import urllib
morphbr@565
    13
import gobject
morphbr@565
    14
import imp
morphbr@565
    15
morphbr@577
    16
log = logging.getLogger("gms.utils")
morphbr@565
    17
morphbr@565
    18
__all__ = ("which", "load_plugins", "PluginSet")
morphbr@565
    19
morphbr@565
    20
def which(app):
morphbr@565
    21
    """function to implement which(1) unix command"""
morphbr@565
    22
    pl = os.environ["PATH"].split(os.pathsep)
morphbr@565
    23
    for p in pl:
morphbr@565
    24
        path = os.path.join(p, app)
morphbr@565
    25
        if os.path.isfile(path):
morphbr@565
    26
            st = os.stat(path)
morphbr@565
    27
            if st[stat.ST_MODE] & 0111:
morphbr@565
    28
                return path
morphbr@565
    29
    return ""
morphbr@565
    30
# which()
morphbr@565
    31
morphbr@565
    32
morphbr@565
    33
def _load_module(pathlist, name):
morphbr@565
    34
    fp, path, desc = imp.find_module(name, pathlist)
morphbr@565
    35
    try:
morphbr@565
    36
        module = imp.load_module(name, fp, path, desc)
morphbr@565
    37
        return module
morphbr@565
    38
    finally:
morphbr@565
    39
        if fp:
morphbr@565
    40
            fp.close()
morphbr@565
    41
# _load_module()
morphbr@565
    42
morphbr@565
    43
morphbr@565
    44
class PluginSet(object):
morphbr@565
    45
    def __init__(self, basetype, *items):
morphbr@565
    46
        self.basetype = basetype
morphbr@565
    47
        self.map = {}
morphbr@565
    48
        self.list = []
morphbr@565
    49
morphbr@565
    50
        for i in items:
morphbr@565
    51
            self._add(i)
morphbr@565
    52
        self._sort()
morphbr@565
    53
    # __init__()
morphbr@565
    54
morphbr@565
    55
morphbr@565
    56
    def _add(self, item):
morphbr@565
    57
        self.map[item.name] = item
morphbr@565
    58
        self.list.append(item)
morphbr@565
    59
    # _add()
morphbr@565
    60
morphbr@565
    61
morphbr@565
    62
    def add(self, item):
morphbr@565
    63
        self._add()
morphbr@565
    64
        self._sort()
morphbr@565
    65
    # add()
morphbr@565
    66
morphbr@565
    67
morphbr@565
    68
    def __getitem__(self, spec):
morphbr@565
    69
        if isinstance(spec, basestring):
morphbr@565
    70
            return self.map[spec]
morphbr@565
    71
        else:
morphbr@565
    72
            return self.list[spec]
morphbr@565
    73
    # __getitem__()
morphbr@565
    74
morphbr@565
    75
morphbr@565
    76
    def get(self, name, default=None):
morphbr@577
    77
        return self.map.get(name, default)
morphbr@565
    78
    # get()
morphbr@565
    79
morphbr@565
    80
morphbr@565
    81
    def __iter__(self):
morphbr@565
    82
        return self.list.__iter__()
morphbr@565
    83
    # __iter__()
morphbr@565
    84
morphbr@565
    85
morphbr@565
    86
    def __len__(self):
morphbr@565
    87
        return len(self.list)
morphbr@565
    88
    # __len__()
morphbr@565
    89
morphbr@565
    90
morphbr@565
    91
    def _sort(self):
morphbr@565
    92
        self.list.sort(lambda a, b: cmp(a.priority, b.priority))
morphbr@565
    93
    # _sort()
morphbr@565
    94
morphbr@565
    95
morphbr@565
    96
    def update(self, pluginset):
morphbr@565
    97
        self.map.update(pluginset.map)
morphbr@565
    98
        self.list.extend(pluginset.list)
morphbr@565
    99
        self._sort()
morphbr@565
   100
    # update()
morphbr@565
   101
morphbr@565
   102
morphbr@565
   103
    def load_from_directory(self, directory):
morphbr@565
   104
        for i in load_plugins(directory, self.basetype):
morphbr@565
   105
            self._add(i)
morphbr@565
   106
        self._sort()
morphbr@565
   107
    # load_from_directory()
morphbr@565
   108
morphbr@565
   109
morphbr@565
   110
    def __str__(self):
morphbr@565
   111
        lst = []
morphbr@565
   112
        for o in self.list:
morphbr@565
   113
            lst.append('"%s" (%s)' % (o.name, o.__name__))
morphbr@565
   114
morphbr@565
   115
        return "%s(basetype=%s, items=[%s])" % \
morphbr@565
   116
               (self.__class__.__name__,
morphbr@565
   117
                self.basetype.__name__,
morphbr@565
   118
                ", ".join(lst))
morphbr@565
   119
    # __str__()
morphbr@565
   120
# PluginSet
morphbr@565
   121
morphbr@565
   122
morphbr@565
   123
def load_plugins(directory, basetype):
morphbr@565
   124
    tn = basetype.__name__
morphbr@565
   125
    log.debug("Loading plugins from %s, type=%s" % (directory, tn))
morphbr@565
   126
morphbr@565
   127
morphbr@565
   128
    plugins = []
morphbr@565
   129
    for d in os.listdir(directory):
morphbr@565
   130
        if not d.endswith(".py"):
morphbr@565
   131
            continue
morphbr@565
   132
morphbr@565
   133
        name = d[0: -3]
morphbr@565
   134
        if name == "__init__":
morphbr@565
   135
            continue
morphbr@565
   136
morphbr@565
   137
        directory.replace(os.path.sep, ".")
morphbr@565
   138
        mod = _load_module([directory], name)
morphbr@565
   139
        for sym in dir(mod):
morphbr@565
   140
            cls = getattr(mod, sym)
morphbr@565
   141
            if isinstance(cls, type) and issubclass(cls, basetype) and \
morphbr@565
   142
                cls != basetype:
morphbr@565
   143
                plugins.append(cls)
morphbr@565
   144
                log.info("Loaded %s (%s) from %s" % \
morphbr@565
   145
                         (cls.__name__, tn, os.path.join(directory, d)))
morphbr@565
   146
morphbr@565
   147
    return plugins
morphbr@565
   148
# load_plugins()