Source

gorilla / lib / gorilla / guts / catalog.py

import os, sys
from gorilla.guts import util


sys.path.insert(0, util.GORILLA_PATH)

class Card(object):
    """A useful wrapper around a card module."""
    
    class DoesNotExist(Exception):
        def __init__(self, name):
            self.name = name
            super(Card.DoesNotExist, self).__init__()
    
    def __init__(self, name):
        super(Card, self).__init__()
        name = name.lower()
        module_name = name.replace('-', '_')
        try:
            self.module = __import__('catalog.%s' % module_name, fromlist=[module_name])
        except ImportError:
            raise Card.DoesNotExist(name)
    
    
    @property
    def name(self):
        return self.module.__dict__.get('name', '')
    
    @property
    def homepage(self):
        return self.module.__dict__.get('homepage', '')
    
    @property
    def description(self):
        return self.module.__doc__
    
    @property
    def packages(self):
        return self.module.__dict__.get('packages', [])
    
    @property
    def scripts(self):
        return self.module.__dict__.get('scripts', [])
    
    @property
    def sources(self):
        return self.module.__dict__.get('sources', [])
    
    @property
    def build_commands(self):
        return self.module.__dict__.get('build_commands', [])
    
    @property
    def tests(self):
        return self.module.__dict__.get('tests', {})
    

def all_cards():
    """Return a generator for all Cards in the catalog."""
    
    catalog_path = os.path.join(util.GORILLA_PATH, 'catalog')
    filenames = os.listdir(catalog_path)
    card_files = filter(
        lambda f: not f.startswith('_') and f.endswith('.py'),
        filenames
    )
    card_module_names = [f[:-3] for f in card_files]
    
    return (Card(c) for c in card_module_names)