fab-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Fab-user] external role definitions


From: Michael Gliwinski
Subject: Re: [Fab-user] external role definitions
Date: Fri, 19 Nov 2010 09:53:56 +0000
User-agent: KMail/1.13.5 (Linux/2.6.35-22-generic; KDE/4.5.3; x86_64; ; )

On Friday 19 Nov 2010 02:41:28 Jeff Forcier wrote:
> Hey Michael,
> 
> As Morgan said, we'd love to see what you have, for sure! A couple
> additional points below.

OK, I don't have it anywhere public, it's really just few lines of code, so 
pasted them below.

[...]
> I'm not sure how much of the Fab source you peeked at, but roledefs
> are already lazy, so hopefully your load_from() method is just
> populating the dict values with callables -- that's all that should be
> necessary for this aspect of your setup to work, off the top of my
> head.

Yes, I knew about that, but in this case I'm actually loading the whole dict 
instead of each role individually.  I think it would actually be more 
complicated to try to split it, but see below.

[...]
> That's certainly valid. Part of me says the feature is a bit on the
> esoteric side, but on the other hand I've always been a fan of adding
> "power user" features provided the common use case isn't compromised
> and said feature doesn't add significant complexity. Based on your
> description, your change probably passes both those tests :)

Well, my use case probably is, but I'm thinking that this can be used for as 
simple thing as just separating role definitions into another file.

> Let us know where we can check it out and I'm sure we can offer
> further advice/questions/etc, and if it looks good I'll definitely add
> a ticket for figuring out how best to include it.

Well, here goes (stripped most docstrings for brevity):


from os import path

import contextlib
import functools
import urllib2
import urlparse

import yaml

def urlopen(url, *args, **kwargs):
    """Open URL and return a file-like object for its resource.

    This is just a wrapper for urllib2.urlopen to handle local paths (URLs
    without scheme) and make the returned object behave like a context 
manager.
    """
    if not urlparse.urlsplit(url).scheme:
        url = 'file://' + path.abspath(path.normpath(path.expanduser(url)))
    return contextlib.closing(urllib2.urlopen(url, *args, **kwargs))

class RoleDefs(dict):
    _loaded = False
    _sources = None

    def load_from(self, sources):
        if isinstance(sources, basestring) \
           or not hasattr(sources, '__iter__'):
            sources = [sources]
        self._sources = sources

    @staticmethod
    def source_loader(url):
        with urlopen(url, 'r') as fp:
            return yaml.load(fp)

    def __missing__(self, key):
        if self._loaded or not self._sources: raise KeyError(key)
        self._loaded = True
        sources = list(self._sources)
        while sources:
            src = sources.pop(0)
            loader = src
            if not callable(src):
                loader = functools.partial(self.source_loader, src)
            try:
                self.update(loader())
                break
            except Exception:
                if not sources: raise KeyError(key)
        return self[key]


-- 
Michael Gliwinski
Henderson Group Information Services
9-11 Hightown Avenue, Newtownabby, BT36 4RT
Phone: 028 9034 3319

**********************************************************************************************
The information in this email is confidential and may be legally privileged.  
It is intended solely for the addressee and access to the email by anyone else 
is unauthorised.
If you are not the intended recipient, any disclosure, copying, distribution or 
any action taken or omitted to be taken in reliance on it, is prohibited and 
may be unlawful.
When addressed to our clients, any opinions or advice contained in this e-mail 
are subject to the terms and conditions expressed  in the governing client 
engagement leter or contract.
If you have received this email in error please notify address@hidden

John Henderson (Holdings) Ltd
Registered office: 9 Hightown Avenue, Mallusk, County Antrim, Northern Ireland, 
BT36 4RT.
Registered in Northern Ireland
Registration Number NI010588
Vat No.: 814 6399 12
*********************************************************************************




reply via email to

[Prev in Thread] Current Thread [Next in Thread]