<commit_before>import urllib

from pylons import c, g
from pylons.i18n import _, ungettext

from r2.lib import filters
from r2.lib.pages import (
    Reddit,
    UserTableItem,
    MediaEmbedBody,
    ModeratorPermissions,
)
from r2.lib.menus import NavMenu, NavButton
from r2.lib.template_helpers import add_sr
from r2.lib.memoize import memoize
from r2.lib.wrapped import Templated, Wrapped
from r2.models import Account, Subreddit, Link, NotFound, Listing, UserListing
from r2.lib.strings import strings
from r2.lib.utils import tup
from r2.lib.jsontemplates import (
    JsonTemplate,
    ObjectTemplate,
    ThingJsonTemplate,
)

from reddit_liveupdate.permissions import ContributorPermissionSet
from reddit_liveupdate.utils import pretty_time

class LiveUpdatePage(Reddit):
    extension_handling = False
    extra_stylesheets = Reddit.extra_stylesheets + ["liveupdate.less"]

    def __init__(self, content, websocket_url=None, **kwargs):
        extra_js_config = {
            "liveupdate_event": c.liveupdate_event._id,
            "liveupdate_pixel_domain": g.liveupdate_pixel_domain,
            "liveupdate_permissions": c.liveupdate_permissions,
            "media_domain": g.media_domain,
        }

        if websocket_url:
            extra_js_config["liveupdate_websocket"] = websocket_url

        title = c.liveupdate_event.title
        if c.liveupdate_event.state == "live":
            title = _("[live]") + " " + title

        Reddit.__init__(self,
            title=title,
            show_sidebar=False,
            content=content,
            extra_js_config=extra_js_config,
            **kwargs
        )

    def build_toolbars(self):
        toolbars = []

        if c.liveupdate_permissions:
            tabs = [
                NavButton(
                    _("updates"),
                    "/",
                ),
            ]

            if c.liveupdate_permissions.allow("settings"):
                tabs.append(NavButton(
                    _("settings"),
                    "/edit",
                ))

            if c.liveupdate_permissions.allow("manage"):
                tabs.append(NavButton(
                    _("contributors"),
                    "/contributors",
                ))

            toolbars.append(NavMenu(
                tabs,
                base_path="/live/" + c.liveupdate_event._id,
                type="tabmenu",
            ))

        return toolbars

class LiveUpdateEmbed(LiveUpdatePage):
    extra_page_classes = ["embed"]

class LiveUpdateEventJsonTemplate(ThingJsonTemplate):
    _data_attrs_ = ThingJsonTemplate.data_attrs(
        id="_id",
        state="state",
        viewer_count="viewer_count",
        viewer_count_fuzzed="viewer_count_fuzzed",
        title="title",
        description="description",
        description_html="description_html",
    )

    def thing_attr(self, thing, attr):
        if attr == "_fullname":
            return "LiveUpdateEvent_" + thing._id
        elif attr == "viewer_count":
            return thing.active_visitors
        elif attr == "viewer_count_fuzzed":
            return thing.active_visitors_fuzzed
        elif attr == "description_html":
            return filters.spaceCompress(
                filters.safemarkdown(thing.description) or "")
        else:
            return ThingJsonTemplate.thing_attr(self, thing, attr)

    def kind(self, wrapped):
        return "LiveUpdateEvent"

class LiveUpdateEventPage(Templated):
    def __init__(self, event, listing, show_sidebar):
        self.event = event
        self.listing = listing
        if show_sidebar:
            self.discussions = LiveUpdateOtherDiscussions()
        self.show_sidebar = show_sidebar

        contributor_accounts = Account._byID(event.contributors.keys(),
                                             data=True, return_dict=False)
        self.contributors = sorted((LiveUpdateAccount(e)
                                   for e in contributor_accounts),
                                   key=lambda e: e.name)

        Templated.__init__(self)

class LiveUpdateEventConfiguration(Templated):
    pass

class LiveUpdateContributorPermissions(ModeratorPermissions):
    def __init__(self, account, permissions, embedded=False):
        ModeratorPermissions.__init__(
            self,
            user=account,
            permissions_type=ContributorTableItem.type,
            permissions=permissions,
            editable=True,
            embedded=embedded,
        )

class ContributorTableItem(UserTableItem):
    type = "liveupdate_contributor"

    def __init__(self, contributor, event, editable):
        self.event = event
        self.render_class = ContributorTableItem
        self.permissions = LiveUpdateContributorPermissions(
            contributor.account, contributor.permissions)
        UserTableItem.__init__(self, contributor.account, editable=editable)

    @property
    def cells(self):
        if self.editable:
            return ("user", "sendmessage", "remove", "permissions",
                    "permissionsctl")
        else:
            return ("user",)

    @property
    def _id(self):
        return self.user._id

    @classmethod
    def add_props(cls, item, *k):
        return item

    @property
    def container_name(self):
        return self.event._id

    @property
    def remove_action(self):
        return "live/%s/rm_contributor" % self.event._id

class ContributorListing(UserListing):
    type = "liveupdate_contributor"
    permissions_form = LiveUpdateContributorPermissions(
        account=None,
        permissions=ContributorPermissionSet.SUPERUSER,
        embedded=True,
    )

    def __init__(self, event, builder, editable=True):
        self.event = event
        UserListing.__init__(self, builder, addable=editable, nextprev=False)

    @property
    def destination(self):
        return "live/%s/add_contributor" % self.event._id

    @property
    def form_title(self):
        return _("add contributor")

    @property
    def title(self):
        return _("current contributors")

    @property
    def container_name(self):
        return self.event._id

class LinkBackToLiveUpdate(Templated):
    pass

class LiveUpdateEventPageJsonTemplate(JsonTemplate):
    def render(self, thing=None, *a, **kwargs):
        return ObjectTemplate(thing.listing.render() if thing else {})

class LiveUpdateJsonTemplate(ThingJsonTemplate):
    _data_attrs_ = ThingJsonTemplate.data_attrs(
        id="_id",
        body="body",
        body_html="body_html",
        author="author",
        stricken="stricken",
        embeds="embeds",
    )

    def thing_attr(self, thing, attr):
        if attr == "_id":
            return str(thing._id)
        elif attr == "body_html":
            return filters.spaceCompress(filters.safemarkdown(thing.body))
        elif attr == "author":
            if not thing.author.deleted:
                return thing.author.name
            else:
                return None
        elif attr == "stricken":
            return bool(thing.stricken)
        return ThingJsonTemplate.thing_attr(self, thing, attr)

    def kind(self, wrapped):
        return "LiveUpdate"

class LiveUpdateAccount(Templated):
    def __init__(self, user):
        Templated.__init__(self,
            deleted=user._deleted,
            name=user.name,
            fullname=user._fullname,
        )

class LiveUpdateOtherDiscussions(Templated):
    max_links = 5

    def __init__(self):
        links = self.get_links(c.liveupdate_event._id)
        self.more_links = len(links) > self.max_links
        self.links = links[:self.max_links]
        self.submit_url = "/submit?" + urllib.urlencode({
            "url": add_sr("/live/" + c.liveupdate_event._id,
                          sr_path=False, force_hostname=True),
            "title": c.liveupdate_event.title.encode("utf-8"),
        })

        Templated.__init__(self)

    @classmethod
    @memoize("live_update_discussion_ids", time=60)
    def _get_related_link_ids(cls, event_id):
        url = add_sr("/live/%s" % event_id, sr_path=False, force_hostname=True)

        try:
            links = tup(Link._by_url(url, sr=None))
        except NotFound:
            links = []

        return [link._id for link in links]

    @classmethod
    def get_links(cls, event_id):
        link_ids = cls._get_related_link_ids(event_id)
        links = Link._byID(link_ids, data=True, return_dict=False)
        links.sort(key=lambda L: L.num_comments, reverse=True)

        sr_ids = set(L.sr_id for L in links)
        subreddits = Subreddit._byID(sr_ids, data=True)

        wrapped = []
        for link in links:
            w = Wrapped(link)

            if w._spam or w._deleted:
                continue

            w.subreddit = subreddits[link.sr_id]

            # ideally we'd check if the user can see the subreddit, but by
            # doing this we keep everything user unspecific which makes caching
            # easier.
            if w.subreddit.type == "private":
                continue

            comment_label = ungettext("comment", "comments", link.num_comments)
            w.comments_label = strings.number_label % dict(
                num=link.num_comments, thing=comment_label)

            wrapped.append(w)
        return wrapped

class LiveUpdateListing(Listing):
    pass

class LiveUpdateMediaEmbedBody(MediaEmbedBody):
    pass

def liveupdate_add_props(user, wrapped):
    account_ids = set(w.author_id for w in wrapped)
    accounts = Account._byID(account_ids, data=True)

    for item in wrapped:
        item.author = LiveUpdateAccount(accounts[item.author_id])

        item.date_str = pretty_time(item._date)
<commit_msg>Add simple way to prevent link from showing in discussions.<commit_after>import urllib

from pylons import c, g
from pylons.i18n import _, ungettext

from r2.lib import filters
from r2.lib.pages import (
    Reddit,
    UserTableItem,
    MediaEmbedBody,
    ModeratorPermissions,
)
from r2.lib.menus import NavMenu, NavButton
from r2.lib.template_helpers import add_sr
from r2.lib.memoize import memoize
from r2.lib.wrapped import Templated, Wrapped
from r2.models import Account, Subreddit, Link, NotFound, Listing, UserListing
from r2.lib.strings import strings
from r2.lib.utils import tup
from r2.lib.jsontemplates import (
    JsonTemplate,
    ObjectTemplate,
    ThingJsonTemplate,
)

from reddit_liveupdate.permissions import ContributorPermissionSet
from reddit_liveupdate.utils import pretty_time

class LiveUpdatePage(Reddit):
    extension_handling = False
    extra_stylesheets = Reddit.extra_stylesheets + ["liveupdate.less"]

    def __init__(self, content, websocket_url=None, **kwargs):
        extra_js_config = {
            "liveupdate_event": c.liveupdate_event._id,
            "liveupdate_pixel_domain": g.liveupdate_pixel_domain,
            "liveupdate_permissions": c.liveupdate_permissions,
            "media_domain": g.media_domain,
        }

        if websocket_url:
            extra_js_config["liveupdate_websocket"] = websocket_url

        title = c.liveupdate_event.title
        if c.liveupdate_event.state == "live":
            title = _("[live]") + " " + title

        Reddit.__init__(self,
            title=title,
            show_sidebar=False,
            content=content,
            extra_js_config=extra_js_config,
            **kwargs
        )

    def build_toolbars(self):
        toolbars = []

        if c.liveupdate_permissions:
            tabs = [
                NavButton(
                    _("updates"),
                    "/",
                ),
            ]

            if c.liveupdate_permissions.allow("settings"):
                tabs.append(NavButton(
                    _("settings"),
                    "/edit",
                ))

            if c.liveupdate_permissions.allow("manage"):
                tabs.append(NavButton(
                    _("contributors"),
                    "/contributors",
                ))

            toolbars.append(NavMenu(
                tabs,
                base_path="/live/" + c.liveupdate_event._id,
                type="tabmenu",
            ))

        return toolbars

class LiveUpdateEmbed(LiveUpdatePage):
    extra_page_classes = ["embed"]

class LiveUpdateEventJsonTemplate(ThingJsonTemplate):
    _data_attrs_ = ThingJsonTemplate.data_attrs(
        id="_id",
        state="state",
        viewer_count="viewer_count",
        viewer_count_fuzzed="viewer_count_fuzzed",
        title="title",
        description="description",
        description_html="description_html",
    )

    def thing_attr(self, thing, attr):
        if attr == "_fullname":
            return "LiveUpdateEvent_" + thing._id
        elif attr == "viewer_count":
            return thing.active_visitors
        elif attr == "viewer_count_fuzzed":
            return thing.active_visitors_fuzzed
        elif attr == "description_html":
            return filters.spaceCompress(
                filters.safemarkdown(thing.description) or "")
        else:
            return ThingJsonTemplate.thing_attr(self, thing, attr)

    def kind(self, wrapped):
        return "LiveUpdateEvent"

class LiveUpdateEventPage(Templated):
    def __init__(self, event, listing, show_sidebar):
        self.event = event
        self.listing = listing
        if show_sidebar:
            self.discussions = LiveUpdateOtherDiscussions()
        self.show_sidebar = show_sidebar

        contributor_accounts = Account._byID(event.contributors.keys(),
                                             data=True, return_dict=False)
        self.contributors = sorted((LiveUpdateAccount(e)
                                   for e in contributor_accounts),
                                   key=lambda e: e.name)

        Templated.__init__(self)

class LiveUpdateEventConfiguration(Templated):
    pass

class LiveUpdateContributorPermissions(ModeratorPermissions):
    def __init__(self, account, permissions, embedded=False):
        ModeratorPermissions.__init__(
            self,
            user=account,
            permissions_type=ContributorTableItem.type,
            permissions=permissions,
            editable=True,
            embedded=embedded,
        )

class ContributorTableItem(UserTableItem):
    type = "liveupdate_contributor"

    def __init__(self, contributor, event, editable):
        self.event = event
        self.render_class = ContributorTableItem
        self.permissions = LiveUpdateContributorPermissions(
            contributor.account, contributor.permissions)
        UserTableItem.__init__(self, contributor.account, editable=editable)

    @property
    def cells(self):
        if self.editable:
            return ("user", "sendmessage", "remove", "permissions",
                    "permissionsctl")
        else:
            return ("user",)

    @property
    def _id(self):
        return self.user._id

    @classmethod
    def add_props(cls, item, *k):
        return item

    @property
    def container_name(self):
        return self.event._id

    @property
    def remove_action(self):
        return "live/%s/rm_contributor" % self.event._id

class ContributorListing(UserListing):
    type = "liveupdate_contributor"
    permissions_form = LiveUpdateContributorPermissions(
        account=None,
        permissions=ContributorPermissionSet.SUPERUSER,
        embedded=True,
    )

    def __init__(self, event, builder, editable=True):
        self.event = event
        UserListing.__init__(self, builder, addable=editable, nextprev=False)

    @property
    def destination(self):
        return "live/%s/add_contributor" % self.event._id

    @property
    def form_title(self):
        return _("add contributor")

    @property
    def title(self):
        return _("current contributors")

    @property
    def container_name(self):
        return self.event._id

class LinkBackToLiveUpdate(Templated):
    pass

class LiveUpdateEventPageJsonTemplate(JsonTemplate):
    def render(self, thing=None, *a, **kwargs):
        return ObjectTemplate(thing.listing.render() if thing else {})

class LiveUpdateJsonTemplate(ThingJsonTemplate):
    _data_attrs_ = ThingJsonTemplate.data_attrs(
        id="_id",
        body="body",
        body_html="body_html",
        author="author",
        stricken="stricken",
        embeds="embeds",
    )

    def thing_attr(self, thing, attr):
        if attr == "_id":
            return str(thing._id)
        elif attr == "body_html":
            return filters.spaceCompress(filters.safemarkdown(thing.body))
        elif attr == "author":
            if not thing.author.deleted:
                return thing.author.name
            else:
                return None
        elif attr == "stricken":
            return bool(thing.stricken)
        return ThingJsonTemplate.thing_attr(self, thing, attr)

    def kind(self, wrapped):
        return "LiveUpdate"

class LiveUpdateAccount(Templated):
    def __init__(self, user):
        Templated.__init__(self,
            deleted=user._deleted,
            name=user.name,
            fullname=user._fullname,
        )

class LiveUpdateOtherDiscussions(Templated):
    max_links = 5

    def __init__(self):
        links = self.get_links(c.liveupdate_event._id)
        self.more_links = len(links) > self.max_links
        self.links = links[:self.max_links]
        self.submit_url = "/submit?" + urllib.urlencode({
            "url": add_sr("/live/" + c.liveupdate_event._id,
                          sr_path=False, force_hostname=True),
            "title": c.liveupdate_event.title.encode("utf-8"),
        })

        Templated.__init__(self)

    @classmethod
    @memoize("live_update_discussion_ids", time=60)
    def _get_related_link_ids(cls, event_id):
        url = add_sr("/live/%s" % event_id, sr_path=False, force_hostname=True)

        try:
            links = tup(Link._by_url(url, sr=None))
        except NotFound:
            links = []

        return [link._id for link in links]

    @classmethod
    def get_links(cls, event_id):
        link_ids = cls._get_related_link_ids(event_id)
        links = Link._byID(link_ids, data=True, return_dict=False)
        links.sort(key=lambda L: L.num_comments, reverse=True)

        sr_ids = set(L.sr_id for L in links)
        subreddits = Subreddit._byID(sr_ids, data=True)

        wrapped = []
        for link in links:
            w = Wrapped(link)

            if w._spam or w._deleted:
                continue

            if not getattr(w, "allow_liveupdate", True):
                continue

            w.subreddit = subreddits[link.sr_id]

            # ideally we'd check if the user can see the subreddit, but by
            # doing this we keep everything user unspecific which makes caching
            # easier.
            if w.subreddit.type == "private":
                continue

            comment_label = ungettext("comment", "comments", link.num_comments)
            w.comments_label = strings.number_label % dict(
                num=link.num_comments, thing=comment_label)

            wrapped.append(w)
        return wrapped

class LiveUpdateListing(Listing):
    pass

class LiveUpdateMediaEmbedBody(MediaEmbedBody):
    pass

def liveupdate_add_props(user, wrapped):
    account_ids = set(w.author_id for w in wrapped)
    accounts = Account._byID(account_ids, data=True)

    for item in wrapped:
        item.author = LiveUpdateAccount(accounts[item.author_id])

        item.date_str = pretty_time(item._date)
<|endoftext|>


<commit_before>import pylab as P
from matplotlib.toolkits.basemap import Basemap
from matplotlib.numerix import ma
from matplotlib.image import pil_to_array
from PIL import Image

# shows how to warp an image from one map projection to another.
# image from http://www.space-graphics.com/earth_topo-bathy.htm,
# from the readme.txt that comes with the image:
# e_topo-bathy - Global Earth planet imagery (c)2004
# Usage License
# This image is distributed as FREEWARE for Personal use only
# NO Commercial use or Commercial redistribution allowed in any form

# read in jpeg image to rgba array of normalized floats.
try:
    pilImage = Image.open('e_topo_bathy_4k.jpg')
except:
    raise IOError, 'please grab image from http://www.space-graphics.com/downloads/maps/e_topobathy/e_topo_bathy_4k.zip'
rgba = pil_to_array(pilImage)
rgba = rgba.astype(P.Float32)/255. # convert to normalized floats.

# define lat/lon grid that image spans (projection='cyl').
nlons = rgba.shape[1]; nlats = rgba.shape[0]
delta = 360./float(nlons)
lons = P.arange(-180.+0.5*delta,180.,delta)
lats = P.arange(-90.+0.5*delta,90.,delta)

# define cylindrical equidistant projection.
m = Basemap(projection='cyl',llcrnrlon=-180,llcrnrlat=-90,urcrnrlon=180,urcrnrlat=90,resolution='l')
# plot (unwarped) rgba image.
im = m.imshow(rgba)
# draw coastlines.
m.drawcoastlines(linewidth=0.5)
# draw lat/lon grid lines.
m.drawmeridians(P.arange(-180,180,60),labels=[0,0,0,1])
m.drawparallels(P.arange(-90,90,30),labels=[1,0,0,0])
P.title("Global earth topo-bathy image - native 'cyl' projection",fontsize=12)
P.show()

# define orthographic projection centered on North America.
m = Basemap(projection='ortho',lat_0=50,lon_0=-100,resolution='l')
# transform to nx x ny regularly spaced native projection grid
# nx and ny chosen to have roughly the same horizontal res as original image.
dx = 2.*P.pi*m.rmajor/float(nlons)
nx = int((m.xmax-m.xmin)/dx)+1; ny = int((m.ymax-m.ymin)/dx)+1
rgba_warped = ma.zeros((ny,nx,4),P.Float64)
# interpolate rgba values from proj='cyl' (geographic coords) to 'lcc'
# values outside of projection limb will be masked.
for k in range(4):
    rgba_warped[:,:,k] = m.transform_scalar(rgba[:,:,k],lons,lats,nx,ny,masked=True)
# make points outside projection limb transparent.
rgba_warped = rgba_warped.filled(0.)
# plot warped rgba image.
im = m.imshow(rgba_warped)
# draw coastlines.
m.drawcoastlines(linewidth=0.5)
# draw lat/lon grid lines every 30 degrees.
m.drawmeridians(P.arange(0,360,30))
m.drawparallels(P.arange(-90,90,30))
P.title("Global earth topo-bathy image warped from 'cyl' to 'ortho' projection",fontsize=12)
P.show()

# define Lambert Conformal basemap for North America.
m = Basemap(llcrnrlon=-145.5,llcrnrlat=1.,urcrnrlon=-2.566,urcrnrlat=46.352,\
            rsphere=(6378137.00,6356752.3142),lat_1=50.,lon_0=-107.,\
            resolution='i',area_thresh=1000.,projection='lcc')
# transform to nx x ny regularly spaced native projection grid
# nx and ny chosen to have roughly the same horizontal res as original image.
dx = 2.*P.pi*m.rmajor/float(nlons)
nx = int((m.xmax-m.xmin)/dx)+1; ny = int((m.ymax-m.ymin)/dx)+1
rgba_warped = P.zeros((ny,nx,4),P.Float64)
# interpolate rgba values from proj='cyl' (geographic coords) to 'lcc'
for k in range(4):
    rgba_warped[:,:,k] = m.transform_scalar(rgba[:,:,k],lons,lats,nx,ny)
# plot warped rgba image.
im = m.imshow(rgba_warped)
# draw coastlines.
m.drawcoastlines(linewidth=0.5)
# draw parallels and meridians.
# label on left, right and bottom of map.
parallels = P.arange(0.,80,20.)
m.drawparallels(parallels,labels=[1,1,0,1])
meridians = P.arange(10.,360.,30.)
m.drawmeridians(meridians,labels=[1,1,0,1])
P.title("Global earth topo-bathy image warped from 'cyl' to 'lcc' projection",fontsize=12)
P.show()
<commit_msg>use blue marble image<commit_after>import pylab as P
from matplotlib.toolkits.basemap import Basemap
from matplotlib.numerix import ma
from matplotlib.image import pil_to_array
from PIL import Image

# shows how to warp an image from one map projection to another.
# image from http://visibleearth.nasa.gov/

# read in jpeg image to rgba array of normalized floats.
pilImage = Image.open('land_shallow_topo_2048.jpg')
rgba = pil_to_array(pilImage)
rgba = rgba.astype(P.Float32)/255. # convert to normalized floats.

# define lat/lon grid that image spans (projection='cyl').
nlons = rgba.shape[1]; nlats = rgba.shape[0]
delta = 360./float(nlons)
lons = P.arange(-180.+0.5*delta,180.,delta)
lats = P.arange(-90.+0.5*delta,90.,delta)

# define cylindrical equidistant projection.
m = Basemap(projection='cyl',llcrnrlon=-180,llcrnrlat=-90,urcrnrlon=180,urcrnrlat=90,resolution='l')
# plot (unwarped) rgba image.
im = m.imshow(rgba)
# draw coastlines.
m.drawcoastlines(linewidth=0.5)
# draw lat/lon grid lines.
m.drawmeridians(P.arange(-180,180,60),labels=[0,0,0,1])
m.drawparallels(P.arange(-90,90,30),labels=[1,0,0,0])
P.title("Blue Marble image - native 'cyl' projection",fontsize=12)
P.show()

# define orthographic projection centered on North America.
m = Basemap(projection='ortho',lat_0=50,lon_0=-100,resolution='l')
# transform to nx x ny regularly spaced native projection grid
# nx and ny chosen to have roughly the same horizontal res as original image.
dx = 2.*P.pi*m.rmajor/float(nlons)
nx = int((m.xmax-m.xmin)/dx)+1; ny = int((m.ymax-m.ymin)/dx)+1
rgba_warped = ma.zeros((ny,nx,4),P.Float64)
# interpolate rgba values from proj='cyl' (geographic coords) to 'lcc'
# values outside of projection limb will be masked.
for k in range(4):
    rgba_warped[:,:,k] = m.transform_scalar(rgba[:,:,k],lons,lats,nx,ny,masked=True)
# make points outside projection limb transparent.
rgba_warped = rgba_warped.filled(0.)
# plot warped rgba image.
im = m.imshow(rgba_warped)
# draw coastlines.
m.drawcoastlines(linewidth=0.5)
# draw lat/lon grid lines every 30 degrees.
m.drawmeridians(P.arange(0,360,30))
m.drawparallels(P.arange(-90,90,30))
P.title("Blue Marble image warped from 'cyl' to 'ortho' projection",fontsize=12)
P.show()

# define Lambert Conformal basemap for North America.
m = Basemap(llcrnrlon=-145.5,llcrnrlat=1.,urcrnrlon=-2.566,urcrnrlat=46.352,\
            rsphere=(6378137.00,6356752.3142),lat_1=50.,lon_0=-107.,\
            resolution='i',area_thresh=1000.,projection='lcc')
# transform to nx x ny regularly spaced native projection grid
# nx and ny chosen to have roughly the same horizontal res as original image.
dx = 2.*P.pi*m.rmajor/float(nlons)
nx = int((m.xmax-m.xmin)/dx)+1; ny = int((m.ymax-m.ymin)/dx)+1
rgba_warped = P.zeros((ny,nx,4),P.Float64)
# interpolate rgba values from proj='cyl' (geographic coords) to 'lcc'
for k in range(4):
    rgba_warped[:,:,k] = m.transform_scalar(rgba[:,:,k],lons,lats,nx,ny)
# plot warped rgba image.
im = m.imshow(rgba_warped)
# draw coastlines.
m.drawcoastlines(linewidth=0.5)
# draw parallels and meridians.
# label on left, right and bottom of map.
parallels = P.arange(0.,80,20.)
m.drawparallels(parallels,labels=[1,1,0,1])
meridians = P.arange(10.,360.,30.)
m.drawmeridians(meridians,labels=[1,1,0,1])
P.title("Blue Marble image warped from 'cyl' to 'lcc' projection",fontsize=12)
P.show()
<|endoftext|>


<commit_before>"""Mail uncaught exceptions to developers.
"""

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
import socket
import traceback
import cgitb
import sys
import os
import os.path
import StringIO

__version__ = '0.2.1'

_log_name = os.getenv('USERNAME' if sys.platform == 'win32' else 'USER')

def _send_email(sender, receivers, subject, body, mail_server):
    """Send email to addresses.
    Note: authentication to SMTP server is currently not support yet.

    sender: sender's email address
    receivers: list of receivers email addresses
    body: string of email body
    mail_server: host of SMTP server

    This function should be robust.
    """
    msg = MIMEMultipart('alternative')
    msg['subject'] = str(subject).decode('utf-8')
    msg['From'] = sender
    msg['To'] = ','.join(receivers)
    msg.attach(MIMEText(body, 'html', 'utf-8'))

    s = smtplib.SMTP(mail_server)
    s.sendmail(msg['From'], receivers, msg.as_string())
    # Print to stderr to facilitate doctest.
    print >> sys.stderr, "'%s' sent to %s" % (subject, ','.join(receivers))
    s.quit()

def mail_exception(sender=_log_name, receivers=[_log_name],
                   mail_server='localhost', callback=None, args=(), kwargs={},
                   both=False):
    """Notify user when an exception is raised in the wrapped function.

    sender: sender's email address. Defaults to the name of the current user.
    receivers: list of receivers' email addresses. Defaults to the name of the
    current user.
    mail_server: host of SMTP server. Defaults to 'localhost'.
    callback: callback function when an exception is encountered. Defaults to
    None, then the exception information is mailed.
    args: argument tuple for the callback function invocation. Defaults to ().
    kwargs: keyword arguments dictionary for the callback function invocation.
    Defaults to {}.
    both: if True, both the mail routine and callback function will be called.
    Defaults to False.
    """
    def decorator(func):
        def wrapper(*fargs, **fkwargs):
            try:
                return func(*fargs, **fkwargs)
            except Exception:
                return _notify(sender, receivers, mail_server, callback, args,
                               kwargs, both)
        return wrapper
    return decorator

def _get_subject(evalue):
    """ Returns email subject.
    """
    hostname = socket.gethostname()
    # RFC 2822's hard limit is 998 characters per line. So, minus
    # "Subject: " the actual subject must be no longer than 989
    # characters. (Copied from Django source).
    subject = ('%s: %s: %s' % (hostname, os.path.basename(__file__),
                               evalue))[:989]
    return subject

def _get_body(exc_info=None):
    """ Returns email body.
    """
    # Write cgitb output to a variable
    bodyf = StringIO.StringIO()
    sys_stderr_orig = sys.stderr
    sys.stderr = bodyf
    cgitb.Hook(file=bodyf, context=7, format='html').handle(exc_info)
    sys.stderr = sys_stderr_orig
    body = bodyf.getvalue()
    bodyf.close()
    return body

def _notify(sender, receivers, mail_server, callback, args, kwargs, both):
    """ Returns exception email suject and body formatted by cgitb module.
    """
    traceback.print_exc()

    evalue = sys.exc_info()[1]
    subject = _get_subject(evalue)
    body = _get_body()

    if callback is None:
        _send_email(sender, receivers, subject, body, mail_server)
    elif both:
        _send_email(sender, receivers, subject, body, mail_server)
        return callback(*args, **kwargs)
    else:
        return callback(*args, **kwargs)

def _notifiy_hook(etype, evalue, etb):
    """ Exception hook that notifies exception info.
    sys.exc_info() is empty here, while the latest exception info is
    available in etype, evalue and etb.
    XXX: coverage does not count this function as covered.
    """
    # Default exception hook to display exception info.
    sys.__excepthook__(etype, evalue, etb)

    subject = _get_subject(evalue)
    body = _get_body((etype, evalue, etb))

    t = _notifiy_hook  # this function
    if t.callback is None:
        _send_email(t.sender, t.receivers, subject, body, t.mail_server)
    elif t.both:
        _send_email(t.sender, t.receivers, subject, body, t.mail_server)
        return t.callback(*t.args, **t.kwargs)
    else:
        return t.callback(*t.args, **t.kwargs)

def _hook_wrapper(sender, receivers, mail_server, callback,
                  args, kwargs, both):
    """ Returns a dummy exception hook, before it notifies exceptions.
    """
    # An (awkward) way to avoid unnecessary class.
    hook = _notifiy_hook
    setattr(hook, 'sender', sender)
    setattr(hook, 'receivers', receivers)
    setattr(hook, 'mail_server', mail_server)
    setattr(hook, 'callback', callback)
    setattr(hook, 'args', args)
    setattr(hook, 'kwargs', kwargs)
    setattr(hook, 'both', both)
    return hook

def enable(sender=_log_name, receivers=[_log_name], mail_server='localhost',
           callback=None, args=(), kwargs={}, both=False):
    """ Override default exception hook with our new hook.
    Arguments meaning are the same with mail_exception.

    Note that if both enable and mail_exception are in effect, enable will be
    shadowed by mail_exception, since exceptions are caught by
    mail_exception first.
    """
    sys.excepthook = _hook_wrapper(sender, receivers, mail_server, callback,
                                   args, kwargs, both)

def disable():
    """ Disable our exception hook and restore to the system default.
    """
    sys.excepthook = sys.__excepthook__
<commit_msg>Bump version to 0.3.0.<commit_after>"""Mail uncaught exceptions to developers.
"""

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
import socket
import traceback
import cgitb
import sys
import os
import os.path
import StringIO

__version__ = '0.3.0'

_log_name = os.getenv('USERNAME' if sys.platform == 'win32' else 'USER')

def _send_email(sender, receivers, subject, body, mail_server):
    """Send email to addresses.
    Note: authentication to SMTP server is currently not support yet.

    sender: sender's email address
    receivers: list of receivers email addresses
    body: string of email body
    mail_server: host of SMTP server

    This function should be robust.
    """
    msg = MIMEMultipart('alternative')
    msg['subject'] = str(subject).decode('utf-8')
    msg['From'] = sender
    msg['To'] = ','.join(receivers)
    msg.attach(MIMEText(body, 'html', 'utf-8'))

    s = smtplib.SMTP(mail_server)
    s.sendmail(msg['From'], receivers, msg.as_string())
    # Print to stderr to facilitate doctest.
    print >> sys.stderr, "'%s' sent to %s" % (subject, ','.join(receivers))
    s.quit()

def mail_exception(sender=_log_name, receivers=[_log_name],
                   mail_server='localhost', callback=None, args=(), kwargs={},
                   both=False):
    """Notify user when an exception is raised in the wrapped function.

    sender: sender's email address. Defaults to the name of the current user.
    receivers: list of receivers' email addresses. Defaults to the name of the
    current user.
    mail_server: host of SMTP server. Defaults to 'localhost'.
    callback: callback function when an exception is encountered. Defaults to
    None, then the exception information is mailed.
    args: argument tuple for the callback function invocation. Defaults to ().
    kwargs: keyword arguments dictionary for the callback function invocation.
    Defaults to {}.
    both: if True, both the mail routine and callback function will be called.
    Defaults to False.
    """
    def decorator(func):
        def wrapper(*fargs, **fkwargs):
            try:
                return func(*fargs, **fkwargs)
            except Exception:
                return _notify(sender, receivers, mail_server, callback, args,
                               kwargs, both)
        return wrapper
    return decorator

def _get_subject(evalue):
    """ Returns email subject.
    """
    hostname = socket.gethostname()
    # RFC 2822's hard limit is 998 characters per line. So, minus
    # "Subject: " the actual subject must be no longer than 989
    # characters. (Copied from Django source).
    subject = ('%s: %s: %s' % (hostname, os.path.basename(__file__),
                               evalue))[:989]
    return subject

def _get_body(exc_info=None):
    """ Returns email body.
    """
    # Write cgitb output to a variable
    bodyf = StringIO.StringIO()
    sys_stderr_orig = sys.stderr
    sys.stderr = bodyf
    cgitb.Hook(file=bodyf, context=7, format='html').handle(exc_info)
    sys.stderr = sys_stderr_orig
    body = bodyf.getvalue()
    bodyf.close()
    return body

def _notify(sender, receivers, mail_server, callback, args, kwargs, both):
    """ Returns exception email suject and body formatted by cgitb module.
    """
    traceback.print_exc()

    evalue = sys.exc_info()[1]
    subject = _get_subject(evalue)
    body = _get_body()

    if callback is None:
        _send_email(sender, receivers, subject, body, mail_server)
    elif both:
        _send_email(sender, receivers, subject, body, mail_server)
        return callback(*args, **kwargs)
    else:
        return callback(*args, **kwargs)

def _notifiy_hook(etype, evalue, etb):
    """ Exception hook that notifies exception info.
    sys.exc_info() is empty here, while the latest exception info is
    available in etype, evalue and etb.
    XXX: coverage does not count this function as covered.
    """
    # Default exception hook to display exception info.
    sys.__excepthook__(etype, evalue, etb)

    subject = _get_subject(evalue)
    body = _get_body((etype, evalue, etb))

    t = _notifiy_hook  # this function
    if t.callback is None:
        _send_email(t.sender, t.receivers, subject, body, t.mail_server)
    elif t.both:
        _send_email(t.sender, t.receivers, subject, body, t.mail_server)
        return t.callback(*t.args, **t.kwargs)
    else:
        return t.callback(*t.args, **t.kwargs)

def _hook_wrapper(sender, receivers, mail_server, callback,
                  args, kwargs, both):
    """ Returns a dummy exception hook, before it notifies exceptions.
    """
    # An (awkward) way to avoid unnecessary class.
    hook = _notifiy_hook
    setattr(hook, 'sender', sender)
    setattr(hook, 'receivers', receivers)
    setattr(hook, 'mail_server', mail_server)
    setattr(hook, 'callback', callback)
    setattr(hook, 'args', args)
    setattr(hook, 'kwargs', kwargs)
    setattr(hook, 'both', both)
    return hook

def enable(sender=_log_name, receivers=[_log_name], mail_server='localhost',
           callback=None, args=(), kwargs={}, both=False):
    """ Override default exception hook with our new hook.
    Arguments meaning are the same with mail_exception.

    Note that if both enable and mail_exception are in effect, enable will be
    shadowed by mail_exception, since exceptions are caught by
    mail_exception first.
    """
    sys.excepthook = _hook_wrapper(sender, receivers, mail_server, callback,
                                   args, kwargs, both)

def disable():
    """ Disable our exception hook and restore to the system default.
    """
    sys.excepthook = sys.__excepthook__
<|endoftext|>


<commit_before>from efm2riot import patches, filters

# Static copy actions (source, destination).
STATICS = [
    {
        "type": "file",
        "source": "{{ root }}/static/README.md",
        "target": "README.md"
    },
    {
        "type": "directory",
        "source": "{{ root }}/static/cpu/efm32",
        "target": "cpu/efm32"
    },
    {
        "type": "directory",
        "source": "{{ root }}/static/doc/images",
        "target": "doc/images"
    },
    {
        "type": "directory",
        "source": "{{ root }}/static/examples",
        "target": "examples"
    }
]

# Template files.
TEMPLATES = [
    {
        "type": "file",
        "when": "per_family",
        "source": "{{ sdk }}/Device/SiliconLabs/{{ family|upper }}/Source/system_{{ family }}.c",  # noqa
        "target": "cpu/efm32/families/{{ family }}/system.c"
    },
    {
        "type": "file",
        "when": "per_family",
        "source": "{{ root }}/templates/cpu/efm32/families/family/Makefile",
        "target": "cpu/efm32/families/{{ family }}/Makefile"
    },
    {
        "type": "file",
        "when": "per_family",
        "source": "{{ root }}/templates/cpu/efm32/families/family/vectors.c",
        "target": "cpu/efm32/families/{{ family }}/vectors.c"
    },
    {
        "type": "glob",
        "when": "per_board",
        "source": "{{ root }}/templates/boards/board/dist/*",
        "target": "boards/{{ board }}/dist",
        "filters": {
            "{{ root }}/templates/boards/board/dist/board.resc": filters.only_for_development(),  # noqa
        }
    },
    {
        "type": "glob",
        "when": "per_board",
        "source": "{{ root }}/templates/boards/board/include/*",
        "target": "boards/{{ board }}/include",
    },
    {
        "type": "glob",
        "when": "per_board",
        "source": "{{ root }}/templates/boards/board/*",
        "target": "boards/{{ board }}",
    },
    {
        "type": "file",
        "when": "per_board",
        "source": "{{ root }}/templates/doc/Board.md",
        "target": "doc/{{ board_display_name }}.md",
    },
    {
        "type": "glob",
        "when": "once",
        "source": "{{ root }}/templates/contrib/*",
        "target": "contrib/",
    },
    {
        "type": "file",
        "when": "per_family",
        "source": "{{ root }}/templates/contrib/emul8/DeviceInformation.cs",
        "target": "contrib/emul8/{{ family|upper }}DeviceInformation.cs",
        "filters": {
            "{{ root }}/templates/contrib/emul8/DeviceInformation.cs": filters.only_for_development(),  # noqa
        }
    },
    {
        "type": "file",
        "when": "per_board",
        "source": "{{ root }}/templates/contrib/renode/platforms/boards/board.repl",  # noqa
        "target": "contrib/renode/platforms/boards/{{ board }}.repl",
        "filters": {
            "{{ root }}/templates/contrib/renode/platforms/boards/board.repl": filters.only_for_development(),  # noqa
        }
    },
    {
        "type": "file",
        "when": "per_cpu",
        "source": "{{ root }}/templates/contrib/renode/platforms/cpus/cpu.repl",  # noqa
        "target": "contrib/renode/platforms/cpus/{{ cpu }}.repl",
        "filters": {
            "{{ root }}/templates/contrib/renode/platforms/cpus/cpu.repl": filters.only_for_development(),  # noqa
        }
    },
]

# Files that need a patch.
PATCHES = [
    {
        "type": "glob",
        "when": "per_family",
        "source": "{{ sdk }}/Device/SiliconLabs/{{ family|upper }}/Include/*.h",  # noqa
        "target": "cpu/efm32/families/{{ family }}/include/vendor",
        "methods": [patches.add_extern_c, patches.fix_arm_math]
    }
]

# Board definitions.
BOARDS = [
    {
        "cpu": "efm32pg1b200f256gm48",
        "cpu_datasheet": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32PG1-DataSheet.pdf",  # noqa
        "board": "slstk3401a",
        "board_display_name": "SLSTK3401A",
        "board_manual": "https://www.silabs.com/documents/public/user-guides/ug154-stk3401-user-guide.pdf",  # noqa
        "board_schematic": "",
        "family": "efm32pg",
        "family_display_name": "EFM32PG",
        "family_display_name_full": "EFM32 Pearl Gecko",
        "family_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32PG1-ReferenceManual.pdf",  # noqa
        "freq": {
            "hfxo": 40000000,
            "hfrco": 19000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EFM32 Pearl Gecko PG1 Starter Kit"
    },
    {
        "cpu": "efm32pg12b500f1024gl125",
        "cpu_datasheet": "https://www.silabs.com/documents/public/data-sheets/efm32pg12-datasheet.pdf",  # noqa
        "board": "slstk3402a",
        "board_display_name": "SLSTK3402A",
        "board_manual": "https://www.silabs.com/documents/public/user-guides/ug257-stk3402-usersguide.pdf",  # noqa
        "board_schematic": "",
        "family": "efm32pg",
        "family_display_name": "EFM32PG",
        "family_display_name_full": "EFM32 Pearl Gecko",
        "family_manual": "https://www.silabs.com/documents/public/reference-manuals/efm32pg12-rm.pdf",  # noqa
        "freq": {
            "hfxo": 40000000,
            "hfrco": 19000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EFM32 Pearl Gecko PG12 Starter Kit"
    },
    {
        "cpu": "efr32mg1p132f256gm48",
        "cpu_datasheet": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFR32MG1-DataSheet.pdf",  # noqa
        "board": "sltb001a",
        "board_display_name": "SLTB001A",
        "board_manual": "http://www.silabs.com/Support%20Documents/TechnicalDocs/UG250.pdf",  # noqa
        "board_schematic": "http://www.silabs.com/Support%20Documents/TechnicalDocs/UG250.pdf",  # noqa
        "family": "efm32mg",
        "family_display_name": "EFR32MG",
        "family_display_name_full": "EFM32 Mighty Gecko Gecko",
        "family_manual": "http://www.silabs.com/Support%20Documents/TechnicalDocs/EFR32-ReferenceManual.pdf",  # noqa
        "freq": {
            "hfxo": 38400000,
            "hfrco": 14000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "Thunderboard Sense"
    },
    {
        "cpu": "ezr32wg330f256r60",
        "cpu_datasheet": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EZR32WG330_DataSheet.pdf",  # noqa
        "board": "slwstk6220a",
        "board_display_name": "SLWSTK6220A",
        "board_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/wstk6220_EZR32WG_868MHz_Wireless_Starter_Kit.pdf",  # noqa
        "board_schematic": "http://www.silabs.com/Support%20Documents/TechnicalDocs/BRD2400A_A00.pdf",  # noqa
        "family": "ezr32wg",
        "family_display_name": "EZR32WG",
        "family_display_name_full": "EZR32 Wonder Gecko",
        "family_manual": "http://www.silabs.com/Support%20Documents/TechnicalDocs/EZR32LG-RM.pdf",  # noqa
        "freq": {
            "hfxo": 48000000,
            "hfrco": 14000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EZR32WG 868MHz Wireless Starter Kit"
    },
    {
        "cpu": "efm32zg222f32",
        "cpu_datasheet": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32ZG222.pdf",  # noqa
        "board": "stk3200",
        "board_display_name": "STK3200",
        "board_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/efm32zg-stk3200-ug.pdf",  # noqa
        "board_schematic": "https://www.silabs.com/Support%20Documents/TechnicalDocs/BRD2010A_B01.pdf",  # noqa
        "family_display_name": "EFM32ZG",
        "family_display_name_full": "EFM32 Zero Gecko",
        "family_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32ZG-RM.pdf",  # noqa
        "freq": {
            "hfxo": 24000000,
            "hfrco": 14000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EFM32 Zero Gecko Starter Kit"
    },
    {
        "cpu": "efm32lg990f256",
        "cpu_datasheet": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32LG990.pdf",  # noqa
        "board": "stk3600",
        "board_display_name": "STK3600",
        "board_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/efm32lg-stk3600-ug.pdf",  # noqa
        "board_schematic": "https://www.silabs.com/Support%20Documents/TechnicalDocs/BRD2201A_A02.pdf",  # noqa
        "family": "efm32lg",
        "family_display_name": "EFM32LG",
        "family_display_name_full": "EFM32 Leoppard Gecko",
        "family_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32LG-RM.pdf",  # noqa
        "freq": {
            "hfxo": 48000000,
            "hfrco": 14000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EFM32 Leopard Gecko Starter Kit"
    },
    {
        "cpu": "efm32gg990f1024",
        "cpu_datasheet": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32GG990.pdf",  # noqa
        "board": "stk3700",
        "board_display_name": "STK3700",
        "board_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/efm32gg-stk3700-ug.pdf",  # noqa
        "board_schematic": "https://www.silabs.com/Support%20Documents/TechnicalDocs/BRD2200A_A03.pdf",  # noqa
        "family": "efm32gg",
        "family_display_name": "EFM32GG",
        "family_display_name_full": "EFM32 Giant Gecko",
        "family_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32GG-RM.pdf",  # noqa
        "freq": {
            "hfxo": 48000000,
            "hfrco": 14000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EFM32 Giant Gecko Starter Kit"
    },
    {
        "cpu": "efm32wg990f256",
        "cpu_datasheet": "http://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32WG990.pdf",  # noqa
        "board": "stk3800",
        "board_display_name": "STK3800",
        "board_manual": "http://www.silabs.com/Support%20Documents/TechnicalDocs/efm32wg-stk3800-ug.pdf",  # noqa
        "board_schematic": "http://www.silabs.com/Support%20Documents/TechnicalDocs/BRD2400A_A00.pdf",  # noqa
        "family": "efm32wg",
        "family_display_name": "EFM32WG",
        "family_display_name_full": "EFM32 Wonder Gecko",
        "family_manual": "http://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32WG-RM.pdf",  # noqa
        "freq": {
            "hfxo": 48000000,
            "hfrco": 14000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EFM32 Wonder Gecko Starter Kit"
    }
]
<commit_msg>Fix configuration file.<commit_after>from efm2riot import patches, filters

# Static copy actions (source, destination).
STATICS = [
    {
        "type": "file",
        "source": "{{ root }}/static/README.md",
        "target": "README.md"
    },
    {
        "type": "directory",
        "source": "{{ root }}/static/cpu/efm32",
        "target": "cpu/efm32"
    },
    {
        "type": "directory",
        "source": "{{ root }}/static/doc/images",
        "target": "doc/images"
    },
    {
        "type": "directory",
        "source": "{{ root }}/static/examples",
        "target": "examples"
    }
]

# Template files.
TEMPLATES = [
    {
        "type": "file",
        "when": "per_family",
        "source": "{{ sdk }}/Device/SiliconLabs/{{ family|upper }}/Source/system_{{ family }}.c",  # noqa
        "target": "cpu/efm32/families/{{ family }}/system.c"
    },
    {
        "type": "file",
        "when": "per_family",
        "source": "{{ root }}/templates/cpu/efm32/families/family/Makefile",
        "target": "cpu/efm32/families/{{ family }}/Makefile"
    },
    {
        "type": "file",
        "when": "per_family",
        "source": "{{ root }}/templates/cpu/efm32/families/family/vectors.c",
        "target": "cpu/efm32/families/{{ family }}/vectors.c"
    },
    {
        "type": "glob",
        "when": "per_board",
        "source": "{{ root }}/templates/boards/board/dist/*",
        "target": "boards/{{ board }}/dist",
        "filters": {
            "{{ root }}/templates/boards/board/dist/board.resc": filters.only_for_development(),  # noqa
        }
    },
    {
        "type": "glob",
        "when": "per_board",
        "source": "{{ root }}/templates/boards/board/include/*",
        "target": "boards/{{ board }}/include",
    },
    {
        "type": "glob",
        "when": "per_board",
        "source": "{{ root }}/templates/boards/board/*",
        "target": "boards/{{ board }}",
    },
    {
        "type": "file",
        "when": "per_board",
        "source": "{{ root }}/templates/doc/Board.md",
        "target": "doc/{{ board_display_name }}.md",
    },
    {
        "type": "glob",
        "when": "once",
        "source": "{{ root }}/templates/contrib/*",
        "target": "contrib/",
    },
    {
        "type": "file",
        "when": "per_family",
        "source": "{{ root }}/templates/contrib/emul8/DeviceInformation.cs",
        "target": "contrib/emul8/{{ family|upper }}DeviceInformation.cs",
        "filters": {
            "{{ root }}/templates/contrib/emul8/DeviceInformation.cs": filters.only_for_development(),  # noqa
        }
    },
    {
        "type": "file",
        "when": "per_board",
        "source": "{{ root }}/templates/contrib/renode/platforms/boards/board.repl",  # noqa
        "target": "contrib/renode/platforms/boards/{{ board }}.repl",
        "filters": {
            "{{ root }}/templates/contrib/renode/platforms/boards/board.repl": filters.only_for_development(),  # noqa
        }
    },
    {
        "type": "file",
        "when": "per_cpu",
        "source": "{{ root }}/templates/contrib/renode/platforms/cpus/cpu.repl",  # noqa
        "target": "contrib/renode/platforms/cpus/{{ cpu }}.repl",
        "filters": {
            "{{ root }}/templates/contrib/renode/platforms/cpus/cpu.repl": filters.only_for_development(),  # noqa
        }
    },
]

# Files that need a patch.
PATCHES = [
    {
        "type": "glob",
        "when": "per_family",
        "source": "{{ sdk }}/Device/SiliconLabs/{{ family|upper }}/Include/*.h",  # noqa
        "target": "cpu/efm32/families/{{ family }}/include/vendor",
        "methods": [patches.add_extern_c, patches.fix_arm_math]
    }
]

# Board definitions.
BOARDS = [
    {
        "cpu": "efm32pg1b200f256gm48",
        "cpu_datasheet": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32PG1-DataSheet.pdf",  # noqa
        "board": "slstk3401a",
        "board_display_name": "SLSTK3401A",
        "board_manual": "https://www.silabs.com/documents/public/user-guides/ug154-stk3401-user-guide.pdf",  # noqa
        "board_schematic": "",
        "family": "efm32pg",
        "family_display_name": "EFM32PG",
        "family_display_name_full": "EFM32 Pearl Gecko",
        "family_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32PG1-ReferenceManual.pdf",  # noqa
        "freq": {
            "hfxo": 40000000,
            "hfrco": 19000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EFM32 Pearl Gecko PG1 Starter Kit"
    },
    {
        "cpu": "efm32pg12b500f1024gl125",
        "cpu_datasheet": "https://www.silabs.com/documents/public/data-sheets/efm32pg12-datasheet.pdf",  # noqa
        "board": "slstk3402a",
        "board_display_name": "SLSTK3402A",
        "board_manual": "https://www.silabs.com/documents/public/user-guides/ug257-stk3402-usersguide.pdf",  # noqa
        "board_schematic": "",
        "family": "efm32pg",
        "family_display_name": "EFM32PG",
        "family_display_name_full": "EFM32 Pearl Gecko",
        "family_manual": "https://www.silabs.com/documents/public/reference-manuals/efm32pg12-rm.pdf",  # noqa
        "freq": {
            "hfxo": 40000000,
            "hfrco": 19000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EFM32 Pearl Gecko PG12 Starter Kit"
    },
    {
        "cpu": "efr32mg1p132f256gm48",
        "cpu_datasheet": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFR32MG1-DataSheet.pdf",  # noqa
        "board": "sltb001a",
        "board_display_name": "SLTB001A",
        "board_manual": "http://www.silabs.com/Support%20Documents/TechnicalDocs/UG250.pdf",  # noqa
        "board_schematic": "http://www.silabs.com/Support%20Documents/TechnicalDocs/UG250.pdf",  # noqa
        "family": "efr32mg",
        "family_display_name": "EFR32MG",
        "family_display_name_full": "EFR32 Mighty Gecko",
        "family_manual": "http://www.silabs.com/Support%20Documents/TechnicalDocs/EFR32-ReferenceManual.pdf",  # noqa
        "freq": {
            "hfxo": 38400000,
            "hfrco": 19000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "Thunderboard Sense"
    },
    {
        "cpu": "ezr32wg330f256r60",
        "cpu_datasheet": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EZR32WG330_DataSheet.pdf",  # noqa
        "board": "slwstk6220a",
        "board_display_name": "SLWSTK6220A",
        "board_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/wstk6220_EZR32WG_868MHz_Wireless_Starter_Kit.pdf",  # noqa
        "board_schematic": "http://www.silabs.com/Support%20Documents/TechnicalDocs/BRD2400A_A00.pdf",  # noqa
        "family": "ezr32wg",
        "family_display_name": "EZR32WG",
        "family_display_name_full": "EZR32 Wonder Gecko",
        "family_manual": "http://www.silabs.com/Support%20Documents/TechnicalDocs/EZR32LG-RM.pdf",  # noqa
        "freq": {
            "hfxo": 48000000,
            "hfrco": 14000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EZR32WG 868MHz Wireless Starter Kit"
    },
    {
        "cpu": "efm32zg222f32",
        "cpu_datasheet": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32ZG222.pdf",  # noqa
        "board": "stk3200",
        "board_display_name": "STK3200",
        "board_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/efm32zg-stk3200-ug.pdf",  # noqa
        "board_schematic": "https://www.silabs.com/Support%20Documents/TechnicalDocs/BRD2010A_B01.pdf",  # noqa
        "family_display_name": "EFM32ZG",
        "family_display_name_full": "EFM32 Zero Gecko",
        "family_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32ZG-RM.pdf",  # noqa
        "freq": {
            "hfxo": 24000000,
            "hfrco": 14000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EFM32 Zero Gecko Starter Kit"
    },
    {
        "cpu": "efm32lg990f256",
        "cpu_datasheet": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32LG990.pdf",  # noqa
        "board": "stk3600",
        "board_display_name": "STK3600",
        "board_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/efm32lg-stk3600-ug.pdf",  # noqa
        "board_schematic": "https://www.silabs.com/Support%20Documents/TechnicalDocs/BRD2201A_A02.pdf",  # noqa
        "family": "efm32lg",
        "family_display_name": "EFM32LG",
        "family_display_name_full": "EFM32 Leoppard Gecko",
        "family_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32LG-RM.pdf",  # noqa
        "freq": {
            "hfxo": 48000000,
            "hfrco": 14000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EFM32 Leopard Gecko Starter Kit"
    },
    {
        "cpu": "efm32gg990f1024",
        "cpu_datasheet": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32GG990.pdf",  # noqa
        "board": "stk3700",
        "board_display_name": "STK3700",
        "board_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/efm32gg-stk3700-ug.pdf",  # noqa
        "board_schematic": "https://www.silabs.com/Support%20Documents/TechnicalDocs/BRD2200A_A03.pdf",  # noqa
        "family": "efm32gg",
        "family_display_name": "EFM32GG",
        "family_display_name_full": "EFM32 Giant Gecko",
        "family_manual": "https://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32GG-RM.pdf",  # noqa
        "freq": {
            "hfxo": 48000000,
            "hfrco": 14000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EFM32 Giant Gecko Starter Kit"
    },
    {
        "cpu": "efm32wg990f256",
        "cpu_datasheet": "http://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32WG990.pdf",  # noqa
        "board": "stk3800",
        "board_display_name": "STK3800",
        "board_manual": "http://www.silabs.com/Support%20Documents/TechnicalDocs/efm32wg-stk3800-ug.pdf",  # noqa
        "board_schematic": "http://www.silabs.com/Support%20Documents/TechnicalDocs/BRD2400A_A00.pdf",  # noqa
        "family": "efm32wg",
        "family_display_name": "EFM32WG",
        "family_display_name_full": "EFM32 Wonder Gecko",
        "family_manual": "http://www.silabs.com/Support%20Documents/TechnicalDocs/EFM32WG-RM.pdf",  # noqa
        "freq": {
            "hfxo": 48000000,
            "hfrco": 14000000,
            "lfxo": 32768,
            "lfrco": 32768,
            "ulfrco": 1000
        },
        "marketing_display_name": "EFM32 Wonder Gecko Starter Kit"
    }
]
<|endoftext|>


<commit_before>from constants import *
from event import Event
from drivnal import server
import threading
import logging
import time
import uuid

logger = logging.getLogger(APP_NAME)
task_threads = {}

_STR_DATABASE_VARIABLES = ['volume_id', 'type', 'state']
_INT_DATABASE_VARIABLES = ['time', 'snapshot_id']

class Task:
    def __init__(self, id=None, volume=None):
        if id is None:
            self.id = uuid.uuid1().hex
            self.type = None
            self.state = None
            self.time = None
            self.volume_id = None
            task_threads[self.id] = None
        else:
            self.id = id

        if volume is None:
            self.volume = None
        else:
            self.volume_id = volume.id
            self.volume = volume

        self.update()

    def __setattr__(self, name, value):
        if name == 'thread':
            task_threads[self.id] = value
        elif name in _STR_DATABASE_VARIABLES:
            server.app_db.set('tasks', self.id, name, value)
        elif name in _INT_DATABASE_VARIABLES:
            server.app_db.set('tasks', self.id, name, str(value))
        else:
            self.__dict__[name] = value

        if name == 'state' and value:
            Event(volume_id=self.volume_id, type=TASKS_UPDATED)

    def __getattr__(self, name):
        if name == 'thread':
            if self.id not in task_threads:
                return None
            return task_threads[self.id]
        elif name in _STR_DATABASE_VARIABLES:
            return server.app_db.get('tasks', self.id, name)
        elif name in _INT_DATABASE_VARIABLES:
            value = server.app_db.get('tasks', self.id, name)
            if value:
                return int(value)
            return None
        elif name not in self.__dict__:
            raise AttributeError('Object instance has no attribute %r' % name)
        return self.__dict__[name]

    def abort(self):
        logger.debug('Aborting task. %r' % {
            'volume_id': self.volume_id,
            'task_id': self.id,
        })
        self.state = ABORTING
        Event(volume_id=self.volume_id, type=TASKS_UPDATED)

    def aborted(self):
        logger.debug('Task aborted. %r' % {
            'volume_id': self.volume_id,
            'task_id': self.id,
        })
        self.state = ABORTED
        self.thread = None
        Event(volume_id=self.volume_id, type=TASKS_UPDATED)

    def remove(self):
        logger.debug('Removing task. %r' % {
            'volume_id': self.volume_id,
            'task_id': self.id,
        })
        server.app_db.remove('tasks', self.id)
        Event(volume_id=self.volume_id, type=TASKS_UPDATED)

    def run(self, *args, **kwargs):
        raise NotImplementedError('Derived class must override run method')

    def _run(self, *args, **kwargs):
        try:
            self.run(*args, **kwargs)
            if self.state == PENDING:
                self.state = COMPLETE
            elif self.state == ABORTING:
                self.state = ABORTED
            self.thread = None

        except:
            logger.exception('Task failed. %r' % {
                'volume_id': self.volume_id,
                'task_id': self.id,
            })
            self.state = FAILED
            self.thread = None
        finally:
            if self.state == PENDING:
                self.state = FAILED
                self.thread = None

    def start(self, *args, **kwargs):
        self.volume_id = self.volume_id
        self.time = int(time.time())
        self.state = PENDING
        self.thread = threading.Thread(
            target=self._run, args=args, kwargs=kwargs)
        self.thread.start()

    def update(self):
        if self.state not in [PENDING, ABORTING]:
            return

        logger.debug('Updating task state. %r' % {
            'volume_id': self.volume_id,
            'task_id': self.id,
        })

        if not self.thread or not self.thread.is_alive():
            logger.warning('Task failed, thread ended unexpectable. %r' % {
                'volume_id': self.volume_id,
                'task_id': self.id,
            })
            self.state = FAILED

    @staticmethod
    def _validate(data):
        if 'time' not in data or not data['time']:
            return False

        try:
            data_time = int(data['time'])
        except ValueError:
            return False

        if 'type' not in data or not data['type']:
            return False

        if 'volume_id' not in data or not data['volume_id']:
            return False

        if 'state' not in data or not data['state']:
            return False

        return True

    @staticmethod
    def _get_tasks(volume, type, states=[]):
        tasks = []
        tasks_dict = {}
        tasks_time = []

        logger.debug('Getting tasks for volume. %r' % {
            'volume_id': volume.id,
        })

        tasks_query = server.app_db.get('tasks')
        for task_id in tasks_query:
            task = tasks_query[task_id]

            # Remove broken events
            if not Task._validate(task):
                logger.debug('Removing broken task from database. %r' % {
                    'volume_id': volume.id,
                    'task_id': task_id,
                })
                server.app_db.remove('tasks', task_id)
                continue

            task['time'] = int(task['time'])

            if type and task['type'] != type:
                continue
            if task['volume_id'] != volume.id:
                continue
            if states and task['state'] not in states:
                continue
            if not states and not task['state']:
                continue

            # Task update occurs here recheck state
            task = Task(id=task_id)
            if states and task.state not in states:
                continue
            tasks_dict[task.time] = task
            tasks_time.append(task.time)

        for task_time in sorted(tasks_time):
            tasks.append(tasks_dict[task_time])

        return tasks

    @staticmethod
    def get_tasks(volume, type=None):
        return Task._get_tasks(volume, type)

    @staticmethod
    def get_failed_tasks(volume, type=None):
        return Task._get_tasks(volume, type, [FAILED])

    @staticmethod
    def get_aborted_tasks(volume, type=None):
        return Task._get_tasks(volume, type, [ABORTED])

    @staticmethod
    def get_pending_tasks(volume, type=None):
        return Task._get_tasks(volume, type, [PENDING, ABORTING])

    @staticmethod
    def get_completed_tasks(volume, type=None):
        return Task._get_tasks(volume, type, [COMPLETE])
<commit_msg>Fix spacing<commit_after>from constants import *
from event import Event
from drivnal import server
import threading
import logging
import time
import uuid

logger = logging.getLogger(APP_NAME)
task_threads = {}

_STR_DATABASE_VARIABLES = ['volume_id', 'type', 'state']
_INT_DATABASE_VARIABLES = ['time', 'snapshot_id']

class Task:
    def __init__(self, id=None, volume=None):
        if id is None:
            self.id = uuid.uuid1().hex
            self.type = None
            self.state = None
            self.time = None
            self.volume_id = None
            task_threads[self.id] = None
        else:
            self.id = id

        if volume is None:
            self.volume = None
        else:
            self.volume_id = volume.id
            self.volume = volume

        self.update()

    def __setattr__(self, name, value):
        if name == 'thread':
            task_threads[self.id] = value
        elif name in _STR_DATABASE_VARIABLES:
            server.app_db.set('tasks', self.id, name, value)
        elif name in _INT_DATABASE_VARIABLES:
            server.app_db.set('tasks', self.id, name, str(value))
        else:
            self.__dict__[name] = value

        if name == 'state' and value:
            Event(volume_id=self.volume_id, type=TASKS_UPDATED)

    def __getattr__(self, name):
        if name == 'thread':
            if self.id not in task_threads:
                return None
            return task_threads[self.id]
        elif name in _STR_DATABASE_VARIABLES:
            return server.app_db.get('tasks', self.id, name)
        elif name in _INT_DATABASE_VARIABLES:
            value = server.app_db.get('tasks', self.id, name)
            if value:
                return int(value)
            return None
        elif name not in self.__dict__:
            raise AttributeError('Object instance has no attribute %r' % name)
        return self.__dict__[name]

    def abort(self):
        logger.debug('Aborting task. %r' % {
            'volume_id': self.volume_id,
            'task_id': self.id,
        })
        self.state = ABORTING
        Event(volume_id=self.volume_id, type=TASKS_UPDATED)

    def aborted(self):
        logger.debug('Task aborted. %r' % {
            'volume_id': self.volume_id,
            'task_id': self.id,
        })
        self.state = ABORTED
        self.thread = None
        Event(volume_id=self.volume_id, type=TASKS_UPDATED)

    def remove(self):
        logger.debug('Removing task. %r' % {
            'volume_id': self.volume_id,
            'task_id': self.id,
        })
        server.app_db.remove('tasks', self.id)
        Event(volume_id=self.volume_id, type=TASKS_UPDATED)

    def run(self, *args, **kwargs):
        raise NotImplementedError('Derived class must override run method')

    def _run(self, *args, **kwargs):
        try:
            self.run(*args, **kwargs)
            if self.state == PENDING:
                self.state = COMPLETE
            elif self.state == ABORTING:
                self.state = ABORTED
            self.thread = None
        except:
            logger.exception('Task failed. %r' % {
                'volume_id': self.volume_id,
                'task_id': self.id,
            })
            self.state = FAILED
            self.thread = None
        finally:
            if self.state == PENDING:
                self.state = FAILED
                self.thread = None

    def start(self, *args, **kwargs):
        self.volume_id = self.volume_id
        self.time = int(time.time())
        self.state = PENDING
        self.thread = threading.Thread(
            target=self._run, args=args, kwargs=kwargs)
        self.thread.start()

    def update(self):
        if self.state not in [PENDING, ABORTING]:
            return

        logger.debug('Updating task state. %r' % {
            'volume_id': self.volume_id,
            'task_id': self.id,
        })

        if not self.thread or not self.thread.is_alive():
            logger.warning('Task failed, thread ended unexpectable. %r' % {
                'volume_id': self.volume_id,
                'task_id': self.id,
            })
            self.state = FAILED

    @staticmethod
    def _validate(data):
        if 'time' not in data or not data['time']:
            return False

        try:
            data_time = int(data['time'])
        except ValueError:
            return False

        if 'type' not in data or not data['type']:
            return False

        if 'volume_id' not in data or not data['volume_id']:
            return False

        if 'state' not in data or not data['state']:
            return False

        return True

    @staticmethod
    def _get_tasks(volume, type, states=[]):
        tasks = []
        tasks_dict = {}
        tasks_time = []

        logger.debug('Getting tasks for volume. %r' % {
            'volume_id': volume.id,
        })

        tasks_query = server.app_db.get('tasks')
        for task_id in tasks_query:
            task = tasks_query[task_id]

            # Remove broken events
            if not Task._validate(task):
                logger.debug('Removing broken task from database. %r' % {
                    'volume_id': volume.id,
                    'task_id': task_id,
                })
                server.app_db.remove('tasks', task_id)
                continue

            task['time'] = int(task['time'])

            if type and task['type'] != type:
                continue
            if task['volume_id'] != volume.id:
                continue
            if states and task['state'] not in states:
                continue
            if not states and not task['state']:
                continue

            # Task update occurs here recheck state
            task = Task(id=task_id)
            if states and task.state not in states:
                continue
            tasks_dict[task.time] = task
            tasks_time.append(task.time)

        for task_time in sorted(tasks_time):
            tasks.append(tasks_dict[task_time])

        return tasks

    @staticmethod
    def get_tasks(volume, type=None):
        return Task._get_tasks(volume, type)

    @staticmethod
    def get_failed_tasks(volume, type=None):
        return Task._get_tasks(volume, type, [FAILED])

    @staticmethod
    def get_aborted_tasks(volume, type=None):
        return Task._get_tasks(volume, type, [ABORTED])

    @staticmethod
    def get_pending_tasks(volume, type=None):
        return Task._get_tasks(volume, type, [PENDING, ABORTING])

    @staticmethod
    def get_completed_tasks(volume, type=None):
        return Task._get_tasks(volume, type, [COMPLETE])
<|endoftext|>


<commit_before># -*- coding: utf-8  -*-

from __future__ import unicode_literals

import numpy as np

def linear_to_mel(frequency):
    """
    Given a frequency in the linear scale, returns the corresponding frequency
    in the Mel scale.
    """
    return 1127.01048 * np.log(1.0 + frequency / 700.0)

def mel_to_linear(frequency):
    """
    Given a frequency in the Mel scale, returns the corresponding frequency
    in the linear scale.
    """
    return 700.0 * (np.exp(frequency / 1127.01048) - 1.0)

class MelFilter(object):
    """
    A single Mel-frequency filter.
    """

    def __init__(self, sample_frequency):
        self.sample_frequency = float(sample_frequency)

    def create_filter(self, filter_num, mel_filter_width, size):
        mel_min_freq = filter_num * mel_filter_width / 2.0
        mel_center_freq = mel_min_freq + mel_filter_width / 2.0
        mel_max_freq = mel_min_freq + mel_filter_width
        self.min_freq = mel_to_linear(mel_min_freq)
        self.center_freq = mel_to_linear(mel_center_freq)
        self.max_freq = mel_to_linear(mel_max_freq)
        self.generate_spectrum(size)

    def generate_spectrum(self, size):
        self.spectrum = np.zeros(size)
        min_pos = int(size * self.min_freq / self.sample_frequency)
        max_pos = int(size * self.max_freq / self.sample_frequency)
        max_pos = min(max_pos, size)
        for k in range(min_pos, max_pos + 1):
            current_freq = k * self.sample_frequency / size
            if current_freq < self.min_freq:
                continue
            if current_freq < self.center_freq:
                self.spectrum[k] = (current_freq - self.min_freq) / (self.center_freq - self.min_freq)
            elif current_freq < self.max_freq:
                self.spectrum[k] = (self.max_freq - current_freq) / (self.max_freq - self.center_freq)

class MelFilterBank(object):
    """
    A bank of several Mel-frequency filters.
    """

    def __init__(self, sample_frequency, size, mel_filter_width=200.0, bank_size=24):
        self.sample_frequency = sample_frequency
        self.size = size
        self.filters = []
        for i in range(bank_size):
            mel_filter = MelFilter(self.sample_frequency)
            mel_filter.create_filter(i, mel_filter_width, self.size)
            self.filters.append(mel_filter)
<commit_msg>Added apply() method to both MelFilter and MelFilterBank.<commit_after># -*- coding: utf-8  -*-

from __future__ import unicode_literals

import numpy as np

def linear_to_mel(frequency):
    """
    Given a frequency in the linear scale, returns the corresponding frequency
    in the Mel scale.
    """
    return 1127.01048 * np.log(1.0 + frequency / 700.0)

def mel_to_linear(frequency):
    """
    Given a frequency in the Mel scale, returns the corresponding frequency
    in the linear scale.
    """
    return 700.0 * (np.exp(frequency / 1127.01048) - 1.0)

class MelFilter(object):
    """
    A single Mel-frequency filter.
    """

    def __init__(self, sample_frequency):
        self.sample_frequency = float(sample_frequency)

    def create_filter(self, filter_num, mel_filter_width, size):
        mel_min_freq = filter_num * mel_filter_width / 2.0
        mel_center_freq = mel_min_freq + mel_filter_width / 2.0
        mel_max_freq = mel_min_freq + mel_filter_width
        self.min_freq = mel_to_linear(mel_min_freq)
        self.center_freq = mel_to_linear(mel_center_freq)
        self.max_freq = mel_to_linear(mel_max_freq)
        self.generate_spectrum(size)

    def generate_spectrum(self, size):
        self.spectrum = np.zeros(size)
        min_pos = int(size * self.min_freq / self.sample_frequency)
        max_pos = int(size * self.max_freq / self.sample_frequency)
        max_pos = min(max_pos, size)
        for k in range(min_pos, max_pos + 1):
            current_freq = k * self.sample_frequency / size
            if current_freq < self.min_freq:
                continue
            if current_freq < self.center_freq:
                self.spectrum[k] = (current_freq - self.min_freq) / (self.center_freq - self.min_freq)
            elif current_freq < self.max_freq:
                self.spectrum[k] = (self.max_freq - current_freq) / (self.max_freq - self.center_freq)

    def apply(self, x):
        return self.spectrum.dot(x)

class MelFilterBank(object):
    """
    A bank of several Mel-frequency filters.
    """

    def __init__(self, sample_frequency, size, mel_filter_width=200.0, bank_size=24):
        self.sample_frequency = sample_frequency
        self.size = size
        self.filters = []
        for i in range(bank_size):
            mel_filter = MelFilter(self.sample_frequency)
            mel_filter.create_filter(i, mel_filter_width, self.size)
            self.filters.append(mel_filter)

    def apply(self, x):
        padded = np.zeros(self.size)
        padded[0:x.size] = x
        return [mel_filter.apply(padded) for mel_filter in self.filters]
<|endoftext|>


<commit_before>from xml.dom import pulldom
import xml.sax
import xml.sax.handler
from xml.sax.saxutils import unescape
from types import StringTypes
from xml.dom import Node

xml.sax.handler.feature_validation = False

def addtodict(dictionary, key, value):
    if not isinstance(dictionary, dict):
        raise TypeError("having trouble with some mixed content at node %s" % key)
    if key not in dictionary:
        dictionary[key] = value
    else:
        if not isinstance(dictionary[key], list):
            dictionary[key] = [dictionary[key], value]
        else:
            dictionary[key].append(value)

def getmixed(nodelist):
    out = ""
    if nodelist[0].nodeType == Node.TEXT_NODE:
        nodelist[0].data = nodelist[0].data.lstrip()
    if nodelist[-1].nodeType == Node.TEXT_NODE:
        nodelist[-1].data = nodelist[-1].data.lstrip()
    for node in nodelist:
        out += unescape(node.toxml())
    return out

def tonum(string):
    try:
        out = int(string)
    except ValueError:
        try:
            out = float(string)
        except ValueError:
            out = string
    return out

class SimpleXMLParser(object):
    """
    Simple XML parser class that completely disregards attributes, and
    mostly disregards order. Text nodes are returned as strings, elements
    are returned as dict entries. Elements with mixed content must be identified
    with the `mixed` argument (or with add_mixed and delete_mixed methods),
    and are returned as a string of their contents.

    Multiple sibling elements with the same name append their content to a
    list containing all of their previous siblings' content. In this way,
    siblings with the same node name have their order retained among
    themselves, but other items have indeterminate order, as with all dicts.

    This is intended to exist after a transform into the simple content form,
    yielding as compact a form for data as possible.

    For example, the following xml::
        <node>
            <title>My Book</title>
            <author>Me</author>
            <date>23 April 2009</date>
            <content>
Chapter 1

These are the times that
                try mens' soles....
</content>
            <endnote>This is the first.</endnote>
            <endnote>This is the last.</endnote>
        </node>

    is parsed with::
        p = SimpleXMLParser()
        p.parse(file("sample.xml"))

    and results in::
        {u'author': u'Me',
         u'content': u"
Chapter 1

These are the times that\n    try mens' soles....
",
         u'date': u'23 April 2009',
         u'endnote': [u'This is the first.', u'This is the last.'],
         u'title': u'My Book'}

    """
    def __init__(self, mixed=set(['body', 'content']), skip=set()):
        if isinstance(mixed, set):
            self.mixed = mixed
        else:
            self.mixed = set(mixed)
        self.skip = skip
        self.events = None
        self._parser = xml.sax.make_parser()
        self._parser.setFeature(xml.sax.handler.feature_validation, 0)
        self._parser.setFeature(xml.sax.handler.feature_external_ges, 0)
        self._parser.setFeature(xml.sax.handler.feature_external_pes, 0)

    def add_mixed(self, *args):
        for item in args:
            self.mixed.add(item)

    def add_skip(self, *args):
        for item in args:
            self.skip.add(item)

    def delete_mixed(self, *args):
        for item in args:
            self.mixed.discard(item)

    def delete_skip(self, *args):
        for item in args:
            self.skip.discard(item)

    def get_simple_xml_content(self, nodename):
        d = {}
        (event, currnode) = self.events.next()
        while currnode:
            if event == 'START_ELEMENT' and currnode.nodeName in self.mixed:
                self.events.expandNode(currnode)
                addtodict(d, currnode.nodeName, getmixed(currnode.childNodes))
            elif event == 'START_ELEMENT' and currnode.nodeName not in self.skip:
                addtodict(d, currnode.nodeName, self.get_simple_xml_content(currnode.nodeName))
            elif event == 'CHARACTERS' and currnode.data.strip():
                if isinstance(d, StringTypes):
                    d += unescape(currnode.data.lstrip())
                elif not d.keys():
                    d = unescape(currnode.data.lstrip())
                else:
                    raise TypeError("having trouble with some mixed content at node %s:\n%s\n\ntry SimpleXML.add_mixed('%s')"
                                    % (nodename, currnode.data.strip(), nodename))
            elif event == 'END_ELEMENT' and currnode.nodeName == nodename:
                if isinstance(d, dict):
                    return d
                else:
                    return tonum(d)
            (event, currnode) = self.events.next()

    def parse(self, stream_or_string):
        self._parser.reset()
        if isinstance(stream_or_string, basestring):
            stream = open(stream_or_string)
        else:
            stream = stream_or_string
        self.events = pulldom.DOMEventStream(stream, self._parser, pulldom.default_bufsize)
        depth = 0
        d = {}
        for event, node in self.events:
            if event == 'START_ELEMENT' and depth == 0:
                depth += 1
            elif event == 'START_ELEMENT' and node.nodeName in self.mixed:
                self.events.expandNode(node)
                addtodict(d, node.nodeName, getmixed(node.childNodes))
            elif event == 'START_ELEMENT' and node.nodeName not in self.skip:
                addtodict(d, node.nodeName, self.get_simple_xml_content(node.nodeName))
        return d

<commit_msg>Sort through imports<commit_after>from xml.dom import pulldom
import xml.sax
import xml.sax.handler
from xml.sax.saxutils import unescape
from xml.dom import Node
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

def addtodict(dictionary, key, value):
    if not isinstance(dictionary, dict):
        raise TypeError("having trouble with some mixed content at node %s" % key)
    if key not in dictionary:
        dictionary[key] = value
    else:
        if not isinstance(dictionary[key], list):
            dictionary[key] = [dictionary[key], value]
        else:
            dictionary[key].append(value)

def getmixed(nodelist):
    out = ""
    if nodelist[0].nodeType == Node.TEXT_NODE:
        nodelist[0].data = nodelist[0].data.lstrip()
    if nodelist[-1].nodeType == Node.TEXT_NODE:
        nodelist[-1].data = nodelist[-1].data.lstrip()
    for node in nodelist:
        out += unescape(node.toxml())
    return out

def tonum(string):
    try:
        out = int(string)
    except ValueError:
        try:
            out = float(string)
        except ValueError:
            out = string
    return out

class SimpleXMLParser(object):
    """
    Simple XML parser class that completely disregards attributes, and
    mostly disregards order. Text nodes are returned as strings, elements
    are returned as dict entries. Elements with mixed content must be identified
    with the `mixed` argument (or with add_mixed and delete_mixed methods),
    and are returned as a string of their contents.

    Multiple sibling elements with the same name append their content to a
    list containing all of their previous siblings' content. In this way,
    siblings with the same node name have their order retained among
    themselves, but other items have indeterminate order, as with all dicts.

    This is intended to exist after a transform into the simple content form,
    yielding as compact a form for data as possible.

    For example, the following xml::
        <node>
            <title>My Book</title>
            <author>Me</author>
            <date>23 April 2009</date>
            <content>
Chapter 1

These are the times that
                try mens' soles....
</content>
            <endnote>This is the first.</endnote>
            <endnote>This is the last.</endnote>
        </node>

    is parsed with::
        p = SimpleXMLParser()
        p.parse(file("sample.xml"))

    and results in::
        {u'author': u'Me',
         u'content': u"
Chapter 1

These are the times that\n    try mens' soles....
",
         u'date': u'23 April 2009',
         u'endnote': [u'This is the first.', u'This is the last.'],
         u'title': u'My Book'}

    """
    def __init__(self, mixed=set(['body', 'content']), skip=set()):
        if isinstance(mixed, set):
            self.mixed = mixed
        else:
            self.mixed = set(mixed)
        self.skip = skip
        self.events = None
        self._parser = xml.sax.make_parser()
        self._parser.setFeature(xml.sax.handler.feature_validation, 0)
        self._parser.setFeature(xml.sax.handler.feature_external_ges, 0)
        self._parser.setFeature(xml.sax.handler.feature_external_pes, 0)

    def add_mixed(self, *args):
        for item in args:
            self.mixed.add(item)

    def add_skip(self, *args):
        for item in args:
            self.skip.add(item)

    def delete_mixed(self, *args):
        for item in args:
            self.mixed.discard(item)

    def delete_skip(self, *args):
        for item in args:
            self.skip.discard(item)

    def get_simple_xml_content(self, nodename):
        d = {}
        (event, currnode) = self.events.next()
        while currnode:
            if event == 'START_ELEMENT' and currnode.nodeName in self.mixed:
                self.events.expandNode(currnode)
                addtodict(d, currnode.nodeName, getmixed(currnode.childNodes))
            elif event == 'START_ELEMENT' and currnode.nodeName not in self.skip:
                addtodict(d, currnode.nodeName, self.get_simple_xml_content(currnode.nodeName))
            elif event == 'CHARACTERS' and currnode.data.strip():
                if isinstance(d, basestring):
                    d += unescape(currnode.data.lstrip())
                elif not d.keys():
                    d = unescape(currnode.data.lstrip())
                else:
                    raise TypeError("having trouble with some mixed content at node %s:\n%s\n\ntry SimpleXML.add_mixed('%s')"
                                    % (nodename, currnode.data.strip(), nodename))
            elif event == 'END_ELEMENT' and currnode.nodeName == nodename:
                if isinstance(d, dict):
                    return d
                else:
                    return tonum(d)
            (event, currnode) = self.events.next()

    def parse(self, stream_or_string):
        self._parser.reset()
        if isinstance(stream_or_string, basestring):
            stream = open(stream_or_string)
        else:
            stream = stream_or_string
        self.events = pulldom.DOMEventStream(stream, self._parser, pulldom.default_bufsize)
        depth = 0
        d = {}
        for event, node in self.events:
            if event == 'START_ELEMENT' and depth == 0:
                depth += 1
            elif event == 'START_ELEMENT' and node.nodeName in self.mixed:
                self.events.expandNode(node)
                addtodict(d, node.nodeName, getmixed(node.childNodes))
            elif event == 'START_ELEMENT' and node.nodeName not in self.skip:
                addtodict(d, node.nodeName, self.get_simple_xml_content(node.nodeName))
        return d

<|endoftext|>


<commit_before>"""Listen for user commands to Dusty

Usage:
  dusty -d [--suppress-warnings] [--preflight-only]

Options:
  --suppress-warnings  Do not display run time warnings to the client
  --preflight-only  Only run the preflight_check, then exit
"""

import os
import atexit
import logging
import socket

from docopt import docopt

from .preflight import preflight_check
from .log import configure_logging, make_socket_logger, close_socket_logger
from .constants import SOCKET_PATH, SOCKET_TERMINATOR, SOCKET_ERROR_TERMINATOR
from .payload import Payload
from .warnings import daemon_warnings
from .config import refresh_config_warnings

def _clean_up_existing_socket(socket_path):
    try:
        os.unlink(socket_path)
    except OSError:
        if os.path.exists(socket_path):
            raise

def _send_warnings_to_client(connection, suppress_warnings):
    if daemon_warnings.has_warnings and not suppress_warnings:
        connection.sendall("{}\n".format(daemon_warnings.pretty()))

def _listen_on_socket(socket_path, suppress_warnings):
    _clean_up_existing_socket(socket_path)

    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.bind(socket_path)
    os.chmod(socket_path, 0777) # don't delete the 0, it makes Python interpret this as octal
    sock.listen(1)
    logging.info('Listening on socket at {}'.format(socket_path))

    while True:
        try:
            connection, client_address = sock.accept()
            make_socket_logger(connection)
            try:
                while True:
                    data = connection.recv(1024)
                    if not data:
                        break
                    fn, args, kwargs = Payload.deserialize(data)
                    logging.info('Received command. fn: {} args: {} kwargs: {}'.format(fn.__name__, args, kwargs))
                    try:
                        _send_warnings_to_client(connection, suppress_warnings)
                        fn(*args, **kwargs)
                    except Exception as e:
                        logging.exception("Daemon encountered exception while processing command")
                        error_msg = e.message if e.message else str(e)
                        _send_warnings_to_client(connection, suppress_warnings)
                        connection.sendall('ERROR: {}\n'.format(error_msg).encode('utf-8'))
                        connection.sendall(SOCKET_ERROR_TERMINATOR)
                    else:
                        connection.sendall(SOCKET_TERMINATOR)
            finally:
                close_socket_logger()
                connection.close()
        except KeyboardInterrupt:
            break
        except:
            logging.exception('Exception on socket listen')

def main():
    args = docopt(__doc__)
    configure_logging()
    preflight_check()
    if args['--preflight-only']:
        return
    refresh_config_warnings()
    _listen_on_socket(SOCKET_PATH, args['--suppress-warnings'])

if __name__ == '__main__':
    main()
<commit_msg>add in check when starting up dusty daemon to ensure tyou can talk to github over ssh<commit_after>"""Listen for user commands to Dusty

Usage:
  dusty -d [--suppress-warnings] [--preflight-only]

Options:
  --suppress-warnings  Do not display run time warnings to the client
  --preflight-only  Only run the preflight_check, then exit
"""

import os
import atexit
import logging
import socket

from docopt import docopt

from .preflight import preflight_check
from .log import configure_logging, make_socket_logger, close_socket_logger
from .constants import SOCKET_PATH, SOCKET_TERMINATOR, SOCKET_ERROR_TERMINATOR
from .payload import Payload
from .warnings import daemon_warnings
from .config import refresh_config_warnings, get_config_value
from .subprocess import check_and_log_output_and_error
from . import constants

def _clean_up_existing_socket(socket_path):
    try:
        os.unlink(socket_path)
    except OSError:
        if os.path.exists(socket_path):
            raise

def _send_warnings_to_client(connection, suppress_warnings):
    if daemon_warnings.has_warnings and not suppress_warnings:
        connection.sendall("{}\n".format(daemon_warnings.pretty()))

def _listen_on_socket(socket_path, suppress_warnings):
    _clean_up_existing_socket(socket_path)

    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.bind(socket_path)
    os.chmod(socket_path, 0777) # don't delete the 0, it makes Python interpret this as octal
    sock.listen(1)
    logging.info('Listening on socket at {}'.format(socket_path))

    while True:
        try:
            connection, client_address = sock.accept()
            make_socket_logger(connection)
            try:
                while True:
                    data = connection.recv(1024)
                    if not data:
                        break
                    fn, args, kwargs = Payload.deserialize(data)
                    logging.info('Received command. fn: {} args: {} kwargs: {}'.format(fn.__name__, args, kwargs))
                    try:
                        _send_warnings_to_client(connection, suppress_warnings)
                        fn(*args, **kwargs)
                    except Exception as e:
                        logging.exception("Daemon encountered exception while processing command")
                        error_msg = e.message if e.message else str(e)
                        _send_warnings_to_client(connection, suppress_warnings)
                        connection.sendall('ERROR: {}\n'.format(error_msg).encode('utf-8'))
                        connection.sendall(SOCKET_ERROR_TERMINATOR)
                    else:
                        connection.sendall(SOCKET_TERMINATOR)
            finally:
                close_socket_logger()
                connection.close()
        except KeyboardInterrupt:
            break
        except:
            logging.exception('Exception on socket listen')

def ensure_github_known_host():
    known_hosts_path = os.path.expanduser('~{}/.ssh/known_hosts'.format(get_config_value(constants.CONFIG_MAC_USERNAME_KEY)))
    with open(known_hosts_path) as f:
        contents = f.read()
        if 'github.com' not in contents:
            logging.info('Adding github ssh key to ssh known_hosts file')
            command = ['sh', '-c', 'ssh-keyscan -t rsa github.com >> {}'.format(known_hosts_path)]
            check_and_log_output_and_error(command)

def main():
    args = docopt(__doc__)
    configure_logging()
    preflight_check()
    if args['--preflight-only']:
        return
    ensure_github_known_host()
    refresh_config_warnings()
    _listen_on_socket(SOCKET_PATH, args['--suppress-warnings'])

if __name__ == '__main__':
    main()
<|endoftext|>