<commit_before>from django.shortcuts import render
from django.views.generic import View
from django.http import JsonResponse
from django.db import connection
from django.utils import timezone, dateparse

from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator

from beacon.models import Beacon, BeaconLog, Event

import json

# Create your views here.
class GetChartData(View):
    """ date used by C3js to draw timeseries """

    def __init__(self):
        self.context={}

    def getC3EventsData(self, start, end):

        events = GetEventsData().getEvents(start, end)
        eventsWithAttrNameChanged = []
        for event in events:
            eventsWithAttrNameChanged.append({
                'value': event['time'],
                'text': event['event'],
                'position': 'middle'
            })
        return eventsWithAttrNameChanged

    def getC3Data(self, start, end):

        AXIS_PREFIX = 'axis_'

        records = BeaconLog.objects.filter(time__range=(start, end))

        data = {}
        devices = set()
        xs = {}
        for record in records:
            beacon = record.beacon.getDict()
            deviceId = beacon['uuid'] + '-' + beacon['major'] + '-' + beacon['minor']

            if deviceId not in devices:
                devices.add(deviceId)
                data[AXIS_PREFIX + deviceId] = []
                data[deviceId] = []
                xs[deviceId] = AXIS_PREFIX + deviceId

            recordFormatted = record.getDict()
            data[AXIS_PREFIX + deviceId].append(recordFormatted['time'])
            data[deviceId].append(recordFormatted['rssi'])

        dataWithName = []
        for key in data.keys():
            data[key].insert(0, key)
            dataWithName.append(data[key])

        return xs, dataWithName

    def get(self, request):

        start = request.GET['start']
        end   = request.GET['end']

        start = dateparse.parse_datetime(start)
        start = timezone.make_aware(start)
        end   = dateparse.parse_datetime(end)
        end   = timezone.make_aware(end)

        response = {}
        response['xs'], response['columns'] = self.getC3Data(start, end)
        response['events'] = self.getC3EventsData(start, end)

        return JsonResponse(response, safe=False)

class GetEventsData(View):

    def __init__(self):
        self.context={}

    def getEvents(self, start, end):

        events = Event.objects.filter(time__range=(start, end))
        eventList = map(lambda x: x.getDict(), events)

        return eventList

    def get(self, request):

        start = request.GET['start']
        end   = request.GET['end']

        start = dateparse.parse_datetime(start)
        start = timezone.make_aware(start)
        end   = dateparse.parse_datetime(end)
        end   = timezone.make_aware(end)

        response = self.getEvents(start, end)

        return JsonResponse(response, safe=False)

class GetBeaconsData(View):

    def __init__(self):
        self.context={}

    def get(self, request):

        start = request.GET['start']
        end   = request.GET['end']

        start = dateparse.parse_datetime(start)
        start = timezone.make_aware(start)
        end   = dateparse.parse_datetime(end)
        end   = timezone.make_aware(end)

        beacons = Beacon.objects.filter(
                beaconlog__time__range=(start, end)).distinct()

        response = []
        for beacon in beacons:
            b = beacon.getDict()
            records = BeaconLog.objects.filter(beacon=beacon,
                    time__range=(start, end))
            b['log'] = map(lambda x: x.getDict(), records)
            response.append(b)

        return JsonResponse(response, safe=False)

class SaveData(View):

    def __init__(self):
        self.context={}

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
                return super(SaveData, self).dispatch(request, *args, **kwargs)

    def post(self, request):

        jsonEncoded = request.POST['data']
        jsonDecoded = json.loads(jsonEncoded)

        beacons = jsonDecoded['Beacons']
        events  = jsonDecoded['Events']

        for beacon in beacons:
            try:
                b = Beacon.objects.get(macAddr=beacon['Mac Address'])
            except Beacon.DoesNotExist:
                b = Beacon(
                    macAddr=beacon['Mac Address'],
                    uuid=beacon['UUID'],
                    major=beacon['Major'],
                    minor=beacon['Minor']
                    )
                b.save()

            for record in beacon['Log']:
                time = dateparse.parse_datetime(record['Time'])
                time = timezone.make_aware(time)
                r = BeaconLog(
                        time=time,
                        rssi=record['RSSI'],
                        measurePower=record['Measure Power'],
                        beacon=b
                        )
                r.save()

        for event in events:
            time = dateparse.parse_datetime(event['Time'])
            time = timezone.make_aware(time)
            e = Event(
                    time=time,
                    event=event['Event']
                    )
            e.save()

        response = {}
        response['status'] = 'ok'
        return JsonResponse(response, safe=False)

class Plot(View):

    def __init__(self):
        self.context={}

    def get(self, request):
        template = "beacon.html"

        return render(request, template, self.context)

<commit_msg>the requests to database consider a device identifier<commit_after>from django.shortcuts import render
from django.views.generic import View
from django.http import JsonResponse
from django.db import connection
from django.db.models.query import Prefetch
from django.utils import timezone, dateparse

from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator

from beacon.models import DetectorDevice, Beacon, BeaconLog, Event

import json

# Create your views here.
class GetChartData(View):
    """ date used by C3js to draw timeseries """

    def __init__(self):
        self.context={}

    def getC3EventsData(self, start, end, deviceId):

        events = GetEventsData().getEvents(start, end, deviceId)
        eventsWithAttrNameChanged = []
        for event in events:
            eventsWithAttrNameChanged.append({
                'value': event['time'],
                'text': event['event'],
                'position': 'middle'
            })
        return eventsWithAttrNameChanged

    def getC3Data(self, start, end, deviceId):

        AXIS_PREFIX = 'axis_'

        records = BeaconLog.objects.filter(
                time__range=(start, end), device__externalId=deviceId)

        data = {}
        devices = set()
        xs = {}
        for record in records:
            beacon = record.beacon.getDict()
            beaconId = beacon['uuid'] + '-' + beacon['major'] + '-' + beacon['minor']

            if beaconId not in devices:
                devices.add(beaconId)
                data[AXIS_PREFIX + beaconId] = []
                data[beaconId] = []
                xs[beaconId] = AXIS_PREFIX + beaconId

            recordFormatted = record.getDict()
            data[AXIS_PREFIX + beaconId].append(recordFormatted['time'])
            data[beaconId].append(recordFormatted['rssi'])

        dataWithName = []
        for key in data.keys():
            data[key].insert(0, key)
            dataWithName.append(data[key])

        return xs, dataWithName

    def get(self, request):

        start    = request.GET['start']
        end      = request.GET['end']
        deviceId = request.GET['device']

        start = dateparse.parse_datetime(start)
        start = timezone.make_aware(start)
        end   = dateparse.parse_datetime(end)
        end   = timezone.make_aware(end)

        response = {}
        response['xs'], response['columns'] = self.getC3Data(start, end, deviceId)
        response['events'] = self.getC3EventsData(start, end, deviceId)

        return JsonResponse(response, safe=False)

class GetEventsData(View):

    def __init__(self):
        self.context={}

    def getEvents(self, start, end, deviceId):

        events = Event.objects.filter(time__range=(start, end), device__externalId=deviceId)
        eventList = map(lambda x: x.getDict(), events)

        return eventList

    def get(self, request):

        start    = request.GET['start']
        end      = request.GET['end']
        deviceId = request.GET['device']

        start = dateparse.parse_datetime(start)
        start = timezone.make_aware(start)
        end   = dateparse.parse_datetime(end)
        end   = timezone.make_aware(end)

        response = self.getEvents(start, end, deviceId)

        return JsonResponse(response, safe=False)

class GetBeaconsData(View):

    def __init__(self):
        self.context={}

    def get(self, request):

        start    = request.GET['start']
        end      = request.GET['end']
        deviceId = request.GET['device']

        start = dateparse.parse_datetime(start)
        start = timezone.make_aware(start)
        end   = dateparse.parse_datetime(end)
        end   = timezone.make_aware(end)

        beacons = Beacon.objects.filter(
                beaconlog__time__range=(start, end),
                beaconlog__device__externalId=deviceId).distinct()

        response = []
        for beacon in beacons:
            b = beacon.getDict()
            records = BeaconLog.objects.filter(beacon=beacon,
                    time__range=(start, end), device__externalId=deviceId)
            b['log'] = map(lambda x: x.getDict(), records)
            response.append(b)

        return JsonResponse(response, safe=False)

class SaveData(View):

    def __init__(self):
        self.context={}

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super(SaveData, self).dispatch(request, *args, **kwargs)

    def post(self, request):

        jsonEncoded = request.POST['data']
        jsonDecoded = json.loads(jsonEncoded)

        deviceId = jsonDecoded['DeviceID']
        beacons  = jsonDecoded['MeasureData']['Beacons']
        events   = jsonDecoded['MeasureData']['Events']

        if DetectorDevice.objects.filter(externalId=deviceId).exists():
            device = DetectorDevice.objects.get(externalId=deviceId)
        else:
            device = DetectorDevice.objects.create(externalId=deviceId)

        for beacon in beacons:
            if Beacon.objects.filter(macAddr=beacon['Mac Address']).exists():
                beaconObj = Beacon.objects.get(macAddr=beacon['Mac Address'])
            else:
                beaconObj = Beacon.objects.create(
                    macAddr=beacon['Mac Address'],
                    uuid=beacon['UUID'],
                    major=beacon['Major'],
                    minor=beacon['Minor']
                    )

            for record in beacon['Log']:
                time = dateparse.parse_datetime(record['Time'])
                time = timezone.make_aware(time)
                r = BeaconLog.objects.create(
                        time=time,
                        rssi=record['RSSI'],
                        measurePower=record['Measure Power'],
                        beacon=beaconObj,
                        device=device
                        )

        for event in events:
            time = dateparse.parse_datetime(event['Time'])
            time = timezone.make_aware(time)
            e = Event.objects.create(
                    time=time,
                    event=event['Event'],
                    device=device
                    )

        response = {}
        response['status'] = 'ok'
        return JsonResponse(response, safe=False)

class Plot(View):

    def __init__(self):
        self.context={}

    def get(self, request):
        template = "beacon.html"

        return render(request, template, self.context)

<|endoftext|>


<commit_before>from django.conf import settings
from django.conf.urls import patterns, include, url
from CashCity import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# override django-registration registration form
from CashCity.forms import ExRegistrationForm
from registration.backends.default.views import RegistrationView

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # tagging autocomplete
    url(r'^taggit_autocomplete/', include('taggit_autocomplete.urls')),
    # admin urls
    url(r'^admin/', include(admin.site.urls)),
    # CashCity app urls,
    url(r'^cashcity/', include('CashCity.urls')),
)

urlpatterns += staticfiles_urlpatterns()

if settings.DEBUG:
    urlpatterns += patterns(
        'django.views.static',
        (r'media/(?P<path>.*)',
        'serve',
        {'document_root': settings.MEDIA_ROOT}), )

<commit_msg>Update urls.py<commit_after>from django.conf import settings
from django.conf.urls import patterns, include, url
from CashCity import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# override django-registration registration form
from CashCity.forms import ExRegistrationForm
from registration.backends.default.views import RegistrationView

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # tagging autocomplete
    url(r'^taggit_autocomplete/', include('taggit_autocomplete.urls')),
    # admin urls
    url(r'^admin/', include(admin.site.urls)),
    # CashCity app urls,
    url(r'^', include('CashCity.urls')),
)

urlpatterns += staticfiles_urlpatterns()

if settings.DEBUG:
    urlpatterns += patterns(
        'django.views.static',
        (r'media/(?P<path>.*)',
        'serve',
        {'document_root': settings.MEDIA_ROOT}), )

<|endoftext|>


<commit_before>"""
Module for generating section headings
"""

import tempfile
from pathlib import Path
from mako.template import Template

from npc import util

def get_sectioner(key, heading_level, prefs):
    """
    Helper to get the right built-in sectioner object

    Creates a new sectioner object based on `key`. The special key 'last' will
    get a LastInitialSectioner, otherwise you'll get a TagSectioner.

    Args:
        key (str): Tag name or special word to use for the sectioner.
        heading_level (int): Level of heading this sectioner is for. Stored for
            use in templates.
        prefs (Settings): Settings object used to look up templates and tag name
            translations.

    Returns:
        A BaseSectioner sub-object depending on the key given.
    """
    if key == 'last':
        return LastInitialSectioner(heading_level, prefs)
    else:
        return TagSectioner(key, heading_level, prefs)

class BaseSectioner:
    """
    Core sectioner logic. This is an abstract class and cannot function on its
    own. Extend it to make your own sectioners.

    Each BaseSectioner holds a temporary directory object open until removal.
    This is used to cache the Mako template.

    To get the current text of the object, use `current_text`.
    """
    def __init__(self, heading_level, prefs):
        """
        Create a sectioner object.

        Args:
            heading_level (int): Level of heading this sectioner is for. Stored
                for use in templates.
            prefs (Settings): Settings object used to look up templates and tag
                name translations.
        """
        self.current_text = None
        self.heading_level = heading_level
        self.prefs = prefs

        self.tempdir = tempfile.TemporaryDirectory()
        self.templates = {} # cache for template objects by format

    def __del__(self):
        """
        Destroy this sectioner object.

        Explicitly cleans up the Mako cache temporary directory.
        """
        self.tempdir.cleanup()

    def render_template(self, output_format, **encoding_options):
        """
        Render our template.

        This just gets the template and calls `render` on it with a `sectioner`
        argument. Override this method if your sectioner has different rendering
        logic or other arguments to its template.

        Args:
            output_format (str): Name of the format to use.
            encoding_options (dict): Dictionary of Mako encoding options. Passed
                directly to the Mako `render` call.
        Returns:
            String or bytes object as rendered by Mako.
        """
        return self.template(output_format, **encoding_options).render(sectioner=self)

    def template(self, output_format, **encoding_options):
        """
        Get the Mako template for a given output format

        This method caches the template object by its output format. This means
        that once requested, the encoding options cannot be changed.

        Args:
            output_format (str): Name of the format to use. Must be present in
                the settings.
            encoding_options (dict): Dictionary of Mako encoding options.

        Returns:
            Mako Template object.
        """
        if output_format in self.templates:
            return self.templates[output_format]

        template_path = str(Path(self.prefs.get("listing.templates.{output_format}.sections.simple".format(output_format=output_format))))
        self.templates[output_format] = Template(filename=template_path, module_directory=str(self.tempdir), **encoding_options)
        return self.templates[output_format]

    def would_change(self, character):
        """
        Whether the sectioner's text would be different for the given character

        Args:
            character (Character): Character object to test

        Returns:
            True if the output of `text_for` differs from our `current_text`.
        """
        return self.text_for(character) != self.current_text

    def update_text(self, character):
        """
        Update the stored `current_text` by generating text for the given
        character.

        Args:
            character (Character): Character object to use for generating new
                text.

        Returns:
            None
        """
        self.current_text = self.text_for(character)

    def text_for(self, character):
        """
        Generate text for a character.

        This method must be overridden in child sectioners. It should not modify
        `current_text`, merely generate a new string based on the given
        character object.

        Args:
            character (Character): Character object to use for generating new
                text.

        Returns:
            String of text data based on the given character.
        """
        raise NotImplementedError

class TagSectioner(BaseSectioner):
    """
    Tag-based sectioner

    This sectioner creates its text from the first value of the named `tag`. It
    translates that tag first if needed according to the user preferences.
    """
    def __init__(self, tag, heading_level, prefs):
        super().__init__(heading_level, prefs)

        self.tag_name = tag
        self.prefs = prefs

    def text_for(self, character):
        """
        Get text based on this object's tag.

        Args:
            character (Character): Character object to use for generating new
                text.

        Returns:
            First value for our tag in the passed character.
        """
        tag = self.prefs.translate_tag_for_character_type(character.type_key, self.tag_name)
        return character.get_first(tag, None)

class LastInitialSectioner(BaseSectioner):
    def text_for(self, character):
        """
        Get text based on the character's last initial.

        Args:
            character (Character): Character object to use for generating new
                text.

        Returns:
            First character of the last word in the character's first value for
            the name tag.
        """
        full_name = character.get_first('name')
        try:
            return None if full_name is None else full_name.split(' ')[-1][0]
        except IndexError as e:
            return None
<commit_msg>Fix sectioner temp directories usage<commit_after>"""
Module for generating section headings
"""

import tempfile
from pathlib import Path
from mako.template import Template

from npc import util

def get_sectioner(key, heading_level, prefs):
    """
    Helper to get the right built-in sectioner object

    Creates a new sectioner object based on `key`. The special key 'last' will
    get a LastInitialSectioner, otherwise you'll get a TagSectioner.

    Args:
        key (str): Tag name or special word to use for the sectioner.
        heading_level (int): Level of heading this sectioner is for. Stored for
            use in templates.
        prefs (Settings): Settings object used to look up templates and tag name
            translations.

    Returns:
        A BaseSectioner sub-object depending on the key given.
    """
    if key == 'last':
        return LastInitialSectioner(heading_level, prefs)
    else:
        return TagSectioner(key, heading_level, prefs)

class BaseSectioner:
    """
    Core sectioner logic. This is an abstract class and cannot function on its
    own. Extend it to make your own sectioners.

    Each BaseSectioner holds a temporary directory object open until removal.
    This is used to cache the Mako template.

    To get the current text of the object, use `current_text`.
    """
    def __init__(self, heading_level, prefs):
        """
        Create a sectioner object.

        Args:
            heading_level (int): Level of heading this sectioner is for. Stored
                for use in templates.
            prefs (Settings): Settings object used to look up templates and tag
                name translations.
        """
        self.current_text = None
        self.heading_level = heading_level
        self.prefs = prefs

        self.tempdir = tempfile.TemporaryDirectory()
        self.templates = {} # cache for template objects by format

    def __del__(self):
        """
        Destroy this sectioner object.

        Explicitly cleans up the Mako cache temporary directory.
        """
        self.tempdir.cleanup()

    def render_template(self, output_format, **encoding_options):
        """
        Render our template.

        This just gets the template and calls `render` on it with a `sectioner`
        argument. Override this method if your sectioner has different rendering
        logic or other arguments to its template.

        Args:
            output_format (str): Name of the format to use.
            encoding_options (dict): Dictionary of Mako encoding options. Passed
                directly to the Mako `render` call.
        Returns:
            String or bytes object as rendered by Mako.
        """
        return self.template(output_format, **encoding_options).render(sectioner=self)

    def template(self, output_format, **encoding_options):
        """
        Get the Mako template for a given output format

        This method caches the template object by its output format. This means
        that once requested, the encoding options cannot be changed.

        Args:
            output_format (str): Name of the format to use. Must be present in
                the settings.
            encoding_options (dict): Dictionary of Mako encoding options.

        Returns:
            Mako Template object.
        """
        if output_format in self.templates:
            return self.templates[output_format]

        template_path = str(Path(self.prefs.get("listing.templates.{output_format}.sections.simple".format(output_format=output_format))))
        self.templates[output_format] = Template(filename=template_path, module_directory=self.tempdir.name, **encoding_options)
        return self.templates[output_format]

    def would_change(self, character):
        """
        Whether the sectioner's text would be different for the given character

        Args:
            character (Character): Character object to test

        Returns:
            True if the output of `text_for` differs from our `current_text`.
        """
        return self.text_for(character) != self.current_text

    def update_text(self, character):
        """
        Update the stored `current_text` by generating text for the given
        character.

        Args:
            character (Character): Character object to use for generating new
                text.

        Returns:
            None
        """
        self.current_text = self.text_for(character)

    def text_for(self, character):
        """
        Generate text for a character.

        This method must be overridden in child sectioners. It should not modify
        `current_text`, merely generate a new string based on the given
        character object.

        Args:
            character (Character): Character object to use for generating new
                text.

        Returns:
            String of text data based on the given character.
        """
        raise NotImplementedError

class TagSectioner(BaseSectioner):
    """
    Tag-based sectioner

    This sectioner creates its text from the first value of the named `tag`. It
    translates that tag first if needed according to the user preferences.
    """
    def __init__(self, tag, heading_level, prefs):
        super().__init__(heading_level, prefs)

        self.tag_name = tag
        self.prefs = prefs

    def text_for(self, character):
        """
        Get text based on this object's tag.

        Args:
            character (Character): Character object to use for generating new
                text.

        Returns:
            First value for our tag in the passed character.
        """
        tag = self.prefs.translate_tag_for_character_type(character.type_key, self.tag_name)
        return character.get_first(tag, None)

class LastInitialSectioner(BaseSectioner):
    def text_for(self, character):
        """
        Get text based on the character's last initial.

        Args:
            character (Character): Character object to use for generating new
                text.

        Returns:
            First character of the last word in the character's first value for
            the name tag.
        """
        full_name = character.get_first('name')
        try:
            return None if full_name is None else full_name.split(' ')[-1][0]
        except IndexError as e:
            return None
<|endoftext|>


<commit_before># Copyright (C) 2010  Francois Marier <francois@libravatar.org>
#
# This file is part of Libravatar
#
# Libravatar is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Libravatar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Libravatar.  If not, see <http://www.gnu.org/licenses/>.

from urllib2 import urlopen, HTTPError
from hashlib import md5
import xml.dom.minidom as minidom

def identica_photo(email):
    image_url = ''
    screen_name = ''

    try:
        fh = urlopen('http://identi.ca/api/users/show.xml?email=' + email)
    except HTTPError:
        return False

    contents = fh.read()
    response = minidom.parseString(contents)

    elements = response.getElementsByTagName('profile_image_url')
    for element in elements:
        textnode = element.firstChild
        if minidom.Node.TEXT_NODE == textnode.nodeType:
            image_url = textnode.nodeValue

    elements = response.getElementsByTagName('screen_name')
    for element in elements:
        textnode = element.firstChild
        if minidom.Node.TEXT_NODE == textnode.nodeType:
            screen_name = textnode.nodeValue

    # get the larger-format image from the profile page
    if image_url:
        image_url = image_url.replace('-48-', '-96-')

    if image_url and screen_name:
        return {'thumbnail_url' : image_url, 'image_url' : image_url, 'width' : 96, 'height' : 96,
                'service_url' : 'http://identi.ca/' + screen_name, 'service_name' : 'Identica'}

    return False

def gravatar_photo(email):
    thumbnail_url = 'https://secure.gravatar.com/avatar/' + md5(email.lower()).hexdigest() + '?s=80&d=404'
    image_url = 'https://secure.gravatar.com/avatar/' + md5(email.lower()).hexdigest() + '?s=512&d=404'

    # Will redirect to the public profile URL if it exists
    service_url = 'http://www.gravatar.com/' + md5(email.lower()).hexdigest()

    try:
        urlopen(image_url)
    except HTTPError:
        return False

    return {'thumbnail_url' : thumbnail_url, 'image_url' : image_url, 'width' : 80, 'height' : 80,
            'service_url' : service_url, 'service_name' : 'Gravatar'}
<commit_msg>Photo imports: add urlopen timeouts and display warnings when errors occur<commit_after># Copyright (C) 2010  Francois Marier <francois@libravatar.org>
#
# This file is part of Libravatar
#
# Libravatar is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Libravatar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Libravatar.  If not, see <http://www.gnu.org/licenses/>.

from urllib2 import urlopen, HTTPError, URLError
from hashlib import md5
import xml.dom.minidom as minidom

URL_TIMEOUT = 5 # in seconds

def identica_photo(email):
    image_url = ''
    screen_name = ''

    try:
        fh = urlopen('http://identi.ca/api/users/show.xml?email=' + email, timeout=URL_TIMEOUT)
    except HTTPError as e:
        print 'Identica fetch failed with an HTTP error: %s' % e.code
        return False
    except URLError as e:
        print 'Identica fetch failed: %s' % e.reason
        return False

    contents = fh.read()
    response = minidom.parseString(contents)

    elements = response.getElementsByTagName('profile_image_url')
    for element in elements:
        textnode = element.firstChild
        if minidom.Node.TEXT_NODE == textnode.nodeType:
            image_url = textnode.nodeValue

    elements = response.getElementsByTagName('screen_name')
    for element in elements:
        textnode = element.firstChild
        if minidom.Node.TEXT_NODE == textnode.nodeType:
            screen_name = textnode.nodeValue

    # get the larger-format image from the profile page
    if image_url:
        image_url = image_url.replace('-48-', '-96-')

    if image_url and screen_name:
        return {'thumbnail_url' : image_url, 'image_url' : image_url, 'width' : 96, 'height' : 96,
                'service_url' : 'http://identi.ca/' + screen_name, 'service_name' : 'Identica'}

    return False

def gravatar_photo(email):
    thumbnail_url = 'https://secure.gravatar.com/avatar/' + md5(email.lower()).hexdigest() + '?s=80&d=404'
    image_url = 'https://secure.gravatar.com/avatar/' + md5(email.lower()).hexdigest() + '?s=512&d=404'

    # Will redirect to the public profile URL if it exists
    service_url = 'http://www.gravatar.com/' + md5(email.lower()).hexdigest()

    try:
        urlopen(image_url, timeout=URL_TIMEOUT)
    except HTTPError as e:
        if e.code != 404:
            print 'Gravatar fetch failed with an unexpected %s HTTP error' % e.code
        return False
    except URLError as e:
        print 'Gravatar fetch failed: %s' % e.reason
        return False

    return {'thumbnail_url' : thumbnail_url, 'image_url' : image_url, 'width' : 80, 'height' : 80,
            'service_url' : service_url, 'service_name' : 'Gravatar'}
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest
import os
import shutil
import tarfile
import tempfile
import reusables

test_root = os.path.abspath(os.path.dirname(__file__))

test_structure_tar = os.path.join(test_root, "test_structure.tar.gz")
test_structure_zip = os.path.join(test_root, "test_structure.zip")
test_structure_rar = os.path.join(test_root, "test_structure.rar")
test_structure = os.path.join(test_root, "test_structure")

class TestReuse(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        config_file = """[Section1]
key 1 = value 1
Key2 = Value2

[Section 2]
"""
        with open(os.path.join(test_root, "test_config.cfg"), "w") as oc:
            oc.write(config_file)
        if os.path.exists(test_structure):
            shutil.rmtree(test_structure)

    @classmethod
    def tearDownClass(cls):
        os.unlink(os.path.join(test_root, "test_config.cfg"))
        if os.path.exists(test_structure):
            shutil.rmtree(test_structure)

    def test_join_path_clean(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        resp = reusables.join_paths('/test/', 'clean/', 'path')
        assert resp == '/test/clean/path/', resp

    def test_join_path_dirty(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        resp = reusables.join_paths('/test/', '/dirty/', ' path.file ')
        assert resp == '/test/dirty/path.file', resp

    def test_join_path_clean_strict(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        resp = reusables.join_paths('/test/', 'clean/', 'path/', strict=True)
        assert resp == '/test/clean/path/', resp

    def test_join_path_dirty_strict(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        resp = reusables.join_paths('/test/', '/dirty/',
                                    ' path.file ', strict=True)
        assert resp == '/dirty/ path.file ', resp

    def test_join_root(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        resp = reusables.join_root('clean/')
        path = os.path.abspath(os.path.join(".", 'clean/'))
        assert resp == path, (resp, path)

    def test_get_config_dict(self):
        resp = reusables.config_dict(os.path.join(test_root, 'test_config.cfg'))
        assert resp['Section1']['key 1'] == 'value 1'
        assert resp['Section 2'] == {}

    def test_get_config_dict_auto(self):
        resp = reusables.config_dict(auto_find=True)
        assert resp.get('Section1') == {'key 1': 'value 1', 'key2': 'Value2'}

    def test_get_config_dict_no_verify(self):
        resp = reusables.config_dict('bad_loc.cfg', verify=False)
        assert resp == {}, resp

    def test_get_config_dict_multiple(self):
        resp = reusables.config_dict([os.path.join(test_root, 'test_config.cfg')])
        assert resp == {'Section1': {'key 1': 'value 1', 'key2': 'Value2'}, 'Section 2': {}}, resp

    def test_get_config_namespace(self):
        resp = reusables.config_namespace(os.path.join(test_root,
                                                   'test_config.cfg'))
        assert resp.Section1.key2 == 'Value2', str(resp.Section1)

    def test_check_bad_filename(self):
        resp = reusables.check_filename("safeFile?.text")
        assert not resp

    def test_check_good_filename(self):
        resp = reusables.check_filename("safeFile.text")
        assert resp

    def test_safe_bad_filename(self):
        resp = reusables.safe_filename("<\">ThatsNotaFileName.\0ThatsASpaceShip^&*")
        assert not [x for x in ("\"", "?", "\0", "<", ">", "*") if x in resp], resp
        assert reusables.check_filename(resp)

    def test_safe_good_filename(self):
        infilename = "SafeFile.txt"
        resp = reusables.safe_filename(infilename)
        assert resp == infilename, resp

    def test_safe_bad_path(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        path = "/var/lib\\/test/p?!ath/fi\0lename.txt"
        expected = "/var/lib_/test/p__ath/fi_lename.txt"
        resp = reusables.safe_path(path)
        assert not [x for x in ("!", "?", "\0", "^", "&", "*") if x in resp], resp
        assert resp == expected, resp

    def test_safe_good_path(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        path = "/var/lib/test/path/filename.txt"
        resp = reusables.safe_path(path)
        assert resp == path, resp

    def test_sorting(self):
        al = [{"name": "a"}, {"name": "c"}, {"name": "b"}]
        resp = reusables.sort_by(al, "name")
        assert resp[0]['name'] == 'a'
        assert resp[1]['name'] == 'b'
        assert resp[2]['name'] == 'c'

    def test_type_errors(self):
        self.assertRaises(TypeError, reusables.config_dict, dict(config='1'))
        self.assertRaises(TypeError, reusables.check_filename, tuple())
        self.assertRaises(TypeError, reusables.safe_filename, set())
        self.assertRaises(TypeError, reusables.safe_path, dict())

    def test_hash_file(self):
        valid = "81dc9bdb52d04dc20036dbd8313ed055"
        hash_file = "1234"
        with open(os.path.join(test_root, "test_hash"), 'w') as out_hash:
            out_hash.write(hash_file)
        resp = reusables.file_hash(os.path.join(test_root, "test_hash"))
        os.unlink(os.path.join(test_root, "test_hash"))
        assert resp == valid, (resp, valid)

    def test_bad_hash_type(self):
        self.assertRaises(ValueError, reusables.file_hash, "", hash_type="sham5")

    def test_count_files(self):
        self._extract_structure()
        resp = reusables.count_all_files(test_root, ext=".cfg")
        assert resp == 1, resp

    def test_count_name(self):
        self._extract_structure()
        resp = reusables.count_all_files(test_root, name="file_")
        assert resp == 4, resp

    def test_fail_count_files(self):
        self._extract_structure()
        try:
            reusables.count_all_files(test_root, ext={"ext": ".cfg"})
        except TypeError:
            pass
        else:
            raise AssertionError("Should raise type error")

    def test_find_files(self):
        resp = reusables.find_all_files(test_root, ext=".cfg")
        assert [x for x in resp if x.endswith(os.path.join(test_root, "test_config.cfg"))]

    def test_find_files_multi_ext(self):
        resp = reusables.find_all_files(test_root, ext=[".cfg", ".nope"])
        assert [x for x in resp if x.endswith(os.path.join(test_root, "test_config.cfg"))]

    def test_find_files_name(self):
        resp = reusables.find_all_files(test_root, name="test_config")
        assert [x for x in resp if x.endswith(os.path.join(test_root, "test_config.cfg"))]

    def test_find_files_bad_ext(self):
        resp = iter(reusables.find_all_files_generator(test_root,
                                                   ext={'test': '.txt'}))
        self.assertRaises(TypeError, next, resp)

    def test_find_files_iterator(self):
        resp = reusables.find_all_files_generator(test_root, ext=".cfg")
        assert not isinstance(resp, list)
        resp = [x for x in resp]
        assert [x for x in resp if x.endswith(os.path.join(test_root, "test_config.cfg"))]

    def test_main(self):
        reusables.main(["--safe-filename",
                        "tease.txt",
                         "--safe-path",
                        "/var/lib"])

    def test_path_single(self):
        resp = reusables.safe_path('path')
        assert resp == 'path', resp

    def _extract_structure(self):
        tar = tarfile.open(test_structure_tar)
        tar.extractall(path=test_root)
        tar.close()
        assert os.path.exists(test_structure)
        assert os.path.isdir(test_structure)

    def _remove_structure(self):
        shutil.rmtree(test_structure)
        assert not os.path.exists(test_structure)

    def test_remove_directories(self):
        self._extract_structure()
        delete = reusables.remove_empty_directories(test_structure)
        assert len(delete) == 8, (len(delete), delete)
        assert not [x for x in delete if "empty" not in x.lower()]
        self._remove_structure()

    def test_remove_files(self):
        self._extract_structure()
        delete = reusables.remove_empty_files(test_structure)
        assert len(delete) == 3, (len(delete), delete)
        assert not [x for x in delete if "file" not in x.lower()]
        self._remove_structure()

    def test_extract_all(self):
        assert os.path.exists(test_structure_tar)
        reusables.extract_all(test_structure_tar, path=test_root, dnd=True)
        assert os.path.exists(test_structure)
        assert os.path.isdir(test_structure)
        shutil.rmtree(test_structure)

    def test_extract_all_zip(self):
        assert os.path.exists(test_structure_zip)
        reusables.extract_all(test_structure_zip, path=test_root, dnd=True)
        assert os.path.exists(test_structure)
        assert os.path.isdir(test_structure)
        shutil.rmtree(test_structure)

    def test_extract_all_rar(self):
        if reusables.win_based:
            import rarfile
            rarfile.UNRAR_TOOL = "UnRAR.exe"
        assert os.path.exists(test_structure_rar)
        reusables.extract_all(test_structure_rar, path=test_root, dnd=True, enable_rar=True)
        assert os.path.exists(test_structure)
        assert os.path.isdir(test_structure)
        shutil.rmtree(test_structure)

    def test_extract_all_bad(self):
        self._extract_structure()
        path = os.path.join(test_structure, "Files", "file_1")
        assert os.path.exists(path)
        try:
            reusables.extract_all(path, path=test_root, dnd=True)
        except TypeError:
            pass
        else:
            raise AssertionError("Extracted a bad file?")

    def test_extract_empty(self):
        empt = tempfile.mktemp()
        open(empt, "a").close()
        try:
            reusables.extract_all(empt)
            assert False, "Should have failed"
        except OSError:
            assert True
        os.unlink(empt)

    def test_auto_delete(self):
        tmpdir = tempfile.mkdtemp()
        fname = tmpdir + "test.zip"
        shutil.copy(test_structure_zip, fname)
        reusables.extract_all(fname, path=tmpdir, dnd=False)
        assert not os.path.exists(fname)
        shutil.rmtree(tmpdir)

    def test_os_tree(self):
        dir = tempfile.mkdtemp(suffix="dir1")
        dir2 = tempfile.mkdtemp(suffix="dir2", dir=dir)
        without_files = reusables.os_tree(dir)
        answer = {dir.replace(tempfile.tempdir, "", 1).lstrip(os.sep): {dir2.lstrip(dir).lstrip(os.sep): {}}}
        assert without_files == answer, "{0} != {1}".format(without_files, answer)

    def test_os_tree_bad_dir(self):
        lol_Im_not_a_dir = open("fake_dir", "w")
        try:
            reusables.os_tree("fake_dir")
        except OSError:
            return True
        else:
            assert False
        finally:
            lol_Im_not_a_dir.close()

    def test_os_tree_no_dir(self):
        try:
            reusables.os_tree("I am pretty sure this directory does not exist, at least I hope not, dear goodness")
        except OSError:
            return True
        else:
            assert False

    def test_bad_file_hash_type(self):
        try:
            reusables.file_hash("test_file", hash_type="There is no way this is a valid hash type")
        except ValueError:
            return True
        else:
            assert False

    def test_cant_delete_empty_file(self):
        dir = tempfile.mkdtemp(suffix="a_dir")
        tf = os.path.join(dir, "test_file")
        file = open(tf, "w")
        try:
            os.chmod(tf, 0o444)
        except Exception:
            pass

        try:
            reusables.remove_empty_files(dir, ignore_errors=False)
        except OSError:
            return True
        else:
            assert False
        finally:
            file.close()

    def test_cant_delete_empty_file_ignore_errors(self):
        dir = tempfile.mkdtemp(suffix="a_dir")
        file = open(os.path.join(dir, "test_file"), "w")
        try:
            reusables.remove_empty_files(dir, ignore_errors=True)
        finally:
            file.close()

if reusables.win_based:
    class TestReuseWindows(unittest.TestCase):
            # Windows based path tests

        def test_win_join_path_clean(self):
            resp = reusables.join_paths('C:\\test', 'clean\\', 'path').rstrip("\\")
            assert resp == 'C:\\test\\clean\\path', resp

        def test_win_join_path_dirty(self):
            resp = reusables.join_paths('C:\\test\\', 'D:\\dirty', ' path.file ')
            assert resp == 'D:\\dirty\\path.file', resp

        def test_win_join_path_clean_strict(self):
            resp = reusables.join_paths('C:\\test', 'clean\\', 'path', strict=True).rstrip("\\")
            assert resp == 'C:\\test\\clean\\path', resp

        def test_win_join_path_dirty_strict(self):
            resp = reusables.join_paths('C:\\test\\', 'D:\\dirty',
                                        ' path.file ', strict=True)
            assert resp == 'D:\\dirty\\ path.file ', resp

        def test_win_join_root(self):
            if not reusables.win_based:
                self.skipTest("Windows based test")
            resp = reusables.join_root('clean\\')
            path = os.path.abspath(os.path.join(".", 'clean\\'))
            assert resp == path, (resp, path)

if __name__ == "__main__":
    unittest.main()<commit_msg>Moving empty file deletion to windows only testing.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest
import os
import shutil
import tarfile
import tempfile
import reusables

test_root = os.path.abspath(os.path.dirname(__file__))

test_structure_tar = os.path.join(test_root, "test_structure.tar.gz")
test_structure_zip = os.path.join(test_root, "test_structure.zip")
test_structure_rar = os.path.join(test_root, "test_structure.rar")
test_structure = os.path.join(test_root, "test_structure")

class TestReuse(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        config_file = """[Section1]
key 1 = value 1
Key2 = Value2

[Section 2]
"""
        with open(os.path.join(test_root, "test_config.cfg"), "w") as oc:
            oc.write(config_file)
        if os.path.exists(test_structure):
            shutil.rmtree(test_structure)

    @classmethod
    def tearDownClass(cls):
        os.unlink(os.path.join(test_root, "test_config.cfg"))
        if os.path.exists(test_structure):
            shutil.rmtree(test_structure)

    def test_join_path_clean(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        resp = reusables.join_paths('/test/', 'clean/', 'path')
        assert resp == '/test/clean/path/', resp

    def test_join_path_dirty(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        resp = reusables.join_paths('/test/', '/dirty/', ' path.file ')
        assert resp == '/test/dirty/path.file', resp

    def test_join_path_clean_strict(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        resp = reusables.join_paths('/test/', 'clean/', 'path/', strict=True)
        assert resp == '/test/clean/path/', resp

    def test_join_path_dirty_strict(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        resp = reusables.join_paths('/test/', '/dirty/',
                                    ' path.file ', strict=True)
        assert resp == '/dirty/ path.file ', resp

    def test_join_root(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        resp = reusables.join_root('clean/')
        path = os.path.abspath(os.path.join(".", 'clean/'))
        assert resp == path, (resp, path)

    def test_get_config_dict(self):
        resp = reusables.config_dict(os.path.join(test_root, 'test_config.cfg'))
        assert resp['Section1']['key 1'] == 'value 1'
        assert resp['Section 2'] == {}

    def test_get_config_dict_auto(self):
        resp = reusables.config_dict(auto_find=True)
        assert resp.get('Section1') == {'key 1': 'value 1', 'key2': 'Value2'}

    def test_get_config_dict_no_verify(self):
        resp = reusables.config_dict('bad_loc.cfg', verify=False)
        assert resp == {}, resp

    def test_get_config_dict_multiple(self):
        resp = reusables.config_dict([os.path.join(test_root, 'test_config.cfg')])
        assert resp == {'Section1': {'key 1': 'value 1', 'key2': 'Value2'}, 'Section 2': {}}, resp

    def test_get_config_namespace(self):
        resp = reusables.config_namespace(os.path.join(test_root,
                                                   'test_config.cfg'))
        assert resp.Section1.key2 == 'Value2', str(resp.Section1)

    def test_check_bad_filename(self):
        resp = reusables.check_filename("safeFile?.text")
        assert not resp

    def test_check_good_filename(self):
        resp = reusables.check_filename("safeFile.text")
        assert resp

    def test_safe_bad_filename(self):
        resp = reusables.safe_filename("<\">ThatsNotaFileName.\0ThatsASpaceShip^&*")
        assert not [x for x in ("\"", "?", "\0", "<", ">", "*") if x in resp], resp
        assert reusables.check_filename(resp)

    def test_safe_good_filename(self):
        infilename = "SafeFile.txt"
        resp = reusables.safe_filename(infilename)
        assert resp == infilename, resp

    def test_safe_bad_path(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        path = "/var/lib\\/test/p?!ath/fi\0lename.txt"
        expected = "/var/lib_/test/p__ath/fi_lename.txt"
        resp = reusables.safe_path(path)
        assert not [x for x in ("!", "?", "\0", "^", "&", "*") if x in resp], resp
        assert resp == expected, resp

    def test_safe_good_path(self):
        if not reusables.nix_based:
            self.skipTest("Linux based test")
        path = "/var/lib/test/path/filename.txt"
        resp = reusables.safe_path(path)
        assert resp == path, resp

    def test_sorting(self):
        al = [{"name": "a"}, {"name": "c"}, {"name": "b"}]
        resp = reusables.sort_by(al, "name")
        assert resp[0]['name'] == 'a'
        assert resp[1]['name'] == 'b'
        assert resp[2]['name'] == 'c'

    def test_type_errors(self):
        self.assertRaises(TypeError, reusables.config_dict, dict(config='1'))
        self.assertRaises(TypeError, reusables.check_filename, tuple())
        self.assertRaises(TypeError, reusables.safe_filename, set())
        self.assertRaises(TypeError, reusables.safe_path, dict())

    def test_hash_file(self):
        valid = "81dc9bdb52d04dc20036dbd8313ed055"
        hash_file = "1234"
        with open(os.path.join(test_root, "test_hash"), 'w') as out_hash:
            out_hash.write(hash_file)
        resp = reusables.file_hash(os.path.join(test_root, "test_hash"))
        os.unlink(os.path.join(test_root, "test_hash"))
        assert resp == valid, (resp, valid)

    def test_bad_hash_type(self):
        self.assertRaises(ValueError, reusables.file_hash, "", hash_type="sham5")

    def test_count_files(self):
        self._extract_structure()
        resp = reusables.count_all_files(test_root, ext=".cfg")
        assert resp == 1, resp

    def test_count_name(self):
        self._extract_structure()
        resp = reusables.count_all_files(test_root, name="file_")
        assert resp == 4, resp

    def test_fail_count_files(self):
        self._extract_structure()
        try:
            reusables.count_all_files(test_root, ext={"ext": ".cfg"})
        except TypeError:
            pass
        else:
            raise AssertionError("Should raise type error")

    def test_find_files(self):
        resp = reusables.find_all_files(test_root, ext=".cfg")
        assert [x for x in resp if x.endswith(os.path.join(test_root, "test_config.cfg"))]

    def test_find_files_multi_ext(self):
        resp = reusables.find_all_files(test_root, ext=[".cfg", ".nope"])
        assert [x for x in resp if x.endswith(os.path.join(test_root, "test_config.cfg"))]

    def test_find_files_name(self):
        resp = reusables.find_all_files(test_root, name="test_config")
        assert [x for x in resp if x.endswith(os.path.join(test_root, "test_config.cfg"))]

    def test_find_files_bad_ext(self):
        resp = iter(reusables.find_all_files_generator(test_root,
                                                   ext={'test': '.txt'}))
        self.assertRaises(TypeError, next, resp)

    def test_find_files_iterator(self):
        resp = reusables.find_all_files_generator(test_root, ext=".cfg")
        assert not isinstance(resp, list)
        resp = [x for x in resp]
        assert [x for x in resp if x.endswith(os.path.join(test_root, "test_config.cfg"))]

    def test_main(self):
        reusables.main(["--safe-filename",
                        "tease.txt",
                         "--safe-path",
                        "/var/lib"])

    def test_path_single(self):
        resp = reusables.safe_path('path')
        assert resp == 'path', resp

    def _extract_structure(self):
        tar = tarfile.open(test_structure_tar)
        tar.extractall(path=test_root)
        tar.close()
        assert os.path.exists(test_structure)
        assert os.path.isdir(test_structure)

    def _remove_structure(self):
        shutil.rmtree(test_structure)
        assert not os.path.exists(test_structure)

    def test_remove_directories(self):
        self._extract_structure()
        delete = reusables.remove_empty_directories(test_structure)
        assert len(delete) == 8, (len(delete), delete)
        assert not [x for x in delete if "empty" not in x.lower()]
        self._remove_structure()

    def test_remove_files(self):
        self._extract_structure()
        delete = reusables.remove_empty_files(test_structure)
        assert len(delete) == 3, (len(delete), delete)
        assert not [x for x in delete if "file" not in x.lower()]
        self._remove_structure()

    def test_extract_all(self):
        assert os.path.exists(test_structure_tar)
        reusables.extract_all(test_structure_tar, path=test_root, dnd=True)
        assert os.path.exists(test_structure)
        assert os.path.isdir(test_structure)
        shutil.rmtree(test_structure)

    def test_extract_all_zip(self):
        assert os.path.exists(test_structure_zip)
        reusables.extract_all(test_structure_zip, path=test_root, dnd=True)
        assert os.path.exists(test_structure)
        assert os.path.isdir(test_structure)
        shutil.rmtree(test_structure)

    def test_extract_all_rar(self):
        if reusables.win_based:
            import rarfile
            rarfile.UNRAR_TOOL = "UnRAR.exe"
        assert os.path.exists(test_structure_rar)
        reusables.extract_all(test_structure_rar, path=test_root, dnd=True, enable_rar=True)
        assert os.path.exists(test_structure)
        assert os.path.isdir(test_structure)
        shutil.rmtree(test_structure)

    def test_extract_all_bad(self):
        self._extract_structure()
        path = os.path.join(test_structure, "Files", "file_1")
        assert os.path.exists(path)
        try:
            reusables.extract_all(path, path=test_root, dnd=True)
        except TypeError:
            pass
        else:
            raise AssertionError("Extracted a bad file?")

    def test_extract_empty(self):
        empt = tempfile.mktemp()
        open(empt, "a").close()
        try:
            reusables.extract_all(empt)
            assert False, "Should have failed"
        except OSError:
            assert True
        os.unlink(empt)

    def test_auto_delete(self):
        tmpdir = tempfile.mkdtemp()
        fname = tmpdir + "test.zip"
        shutil.copy(test_structure_zip, fname)
        reusables.extract_all(fname, path=tmpdir, dnd=False)
        assert not os.path.exists(fname)
        shutil.rmtree(tmpdir)

    def test_os_tree(self):
        dir = tempfile.mkdtemp(suffix="dir1")
        dir2 = tempfile.mkdtemp(suffix="dir2", dir=dir)
        without_files = reusables.os_tree(dir)
        answer = {dir.replace(tempfile.tempdir, "", 1).lstrip(os.sep): {dir2.lstrip(dir).lstrip(os.sep): {}}}
        assert without_files == answer, "{0} != {1}".format(without_files, answer)

    def test_os_tree_bad_dir(self):
        lol_Im_not_a_dir = open("fake_dir", "w")
        try:
            reusables.os_tree("fake_dir")
        except OSError:
            return True
        else:
            assert False
        finally:
            lol_Im_not_a_dir.close()

    def test_os_tree_no_dir(self):
        try:
            reusables.os_tree("I am pretty sure this directory does not exist, at least I hope not, dear goodness")
        except OSError:
            return True
        else:
            assert False

    def test_bad_file_hash_type(self):
        try:
            reusables.file_hash("test_file", hash_type="There is no way this is a valid hash type")
        except ValueError:
            return True
        else:
            assert False

if reusables.win_based:
    class TestReuseWindows(unittest.TestCase):
            # Windows based path tests

        def test_win_join_path_clean(self):
            resp = reusables.join_paths('C:\\test', 'clean\\', 'path').rstrip("\\")
            assert resp == 'C:\\test\\clean\\path', resp

        def test_win_join_path_dirty(self):
            resp = reusables.join_paths('C:\\test\\', 'D:\\dirty', ' path.file ')
            assert resp == 'D:\\dirty\\path.file', resp

        def test_win_join_path_clean_strict(self):
            resp = reusables.join_paths('C:\\test', 'clean\\', 'path', strict=True).rstrip("\\")
            assert resp == 'C:\\test\\clean\\path', resp

        def test_win_join_path_dirty_strict(self):
            resp = reusables.join_paths('C:\\test\\', 'D:\\dirty',
                                        ' path.file ', strict=True)
            assert resp == 'D:\\dirty\\ path.file ', resp

        def test_win_join_root(self):
            if not reusables.win_based:
                self.skipTest("Windows based test")
            resp = reusables.join_root('clean\\')
            path = os.path.abspath(os.path.join(".", 'clean\\'))
            assert resp == path, (resp, path)

        def test_cant_delete_empty_file(self):
            dir = tempfile.mkdtemp(suffix="a_dir")
            tf = os.path.join(dir, "test_file")
            file = open(tf, "w")
            try:
                os.chmod(tf, 0o444)
            except Exception:
                pass

            try:
                reusables.remove_empty_files(dir, ignore_errors=False)
            except OSError:
                return True
            else:
                assert False
            finally:
                file.close()

        def test_cant_delete_empty_file_ignore_errors(self):
            dir = tempfile.mkdtemp(suffix="a_dir")
            file = open(os.path.join(dir, "test_file"), "w")
            try:
                reusables.remove_empty_files(dir, ignore_errors=True)
            finally:
                file.close()

if __name__ == "__main__":
    unittest.main()<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Drive-It competition simulation environment
@author: Jean-Claude Manoli
"""

from os import path
import math
import numpy as np
from numpy import cos, sin, pi, clip #pylint: disable=W0611
import gym
from gym import spaces
from gym.utils import seeding
from gym.envs.classic_control import rendering
from car import Car, steer_actions
from utils import * #pylint: disable=W0401,W0614
from DriveItCircuit import * #pylint: disable=W0401,W0614

blue_threshold = 0.9
fps = 60.0
dt = 1.0 / fps

out_reward = -checkpoint_median_length
throttle_override_reward = -dt

max_compass_bias = 0.017
compass_deviation = max_compass_bias / 100.0
velocity_deviation = 0.001

class DriveItEnvMulti(gym.Env):
    metadata = {
        'render.modes': ['human', 'rgb_array'],
        'video.frames_per_second': fps
    }

    def __init__(self, cars=(Car()), time_limit=10, noisy=True, random_position=True, max_speed_deviation=0.0):
        assert len(cars) <= 4, 'Maximum number of cars is 4'
        self.cars = cars
        self.time_limit = time_limit
        self.noisy = noisy
        self.random_position = random_position
        self.max_speed_deviation = max_speed_deviation
        self.v_max = [car.specs.v_max for car in cars]
        self.car_num = len(cars)
        self.dt = dt

        self.viewer = None

        high = np.array([ 1.0,  0.0 * pi      , cars[0].specs.v_max,  cars[0].specs.K_max, 1.0 ], dtype=np.float32)
        low  = np.array([ 0.0, -3.0 * pi / 2.0,                 0.0, -cars[0].specs.K_max, 0.0 ], dtype=np.float32)
        self.action_space = spaces.Discrete(len(steer_actions))
        self.observation_space = spaces.Box(low, high)

        fname = path.join(path.dirname(__file__), "track.png")
        self._init_track(fname, width=2.0, height=2.0)

        self._seed()
        self.time = -1.0
        self.observations = {}
        self.state = {}

    def _seed(self, seed=None):
        self.np_random, seed = seeding.np_random(seed)
        for car in self.cars:
            car.set_noise(self.noisy, self.np_random)
        return [seed]

    def is_clear(self, car, x, y, i):
        # keep 2 car length distance in front of cars (3 measuring from the car centers)
        min_dist, max_angle = 3.0 * car.length, pi / 4.0 + 0.05
        for j in range(i):
            d, alpha = self.cars[j].distance(x, y)
            if d < car.length or (d < min_dist and abs(alpha) < max_angle):
                return False
            d, alpha = car.part_distance(self.cars[j])
            if d < min_dist and abs(alpha) < max_angle:
                return False
        return True

    def _reset_car(self, i):
        car = self.cars[i]

        y_m, theta_m = 0.0, 0.0
        if self.noisy:
            car.specs.v_max = self.v_max[i] * (1 - self.np_random.uniform(0, self.max_speed_deviation))
            y_m += self.np_random.uniform(-0.01, 0.01)
            theta_m += self.np_random.uniform(-pi / 36.0, pi / 36.0)

        if self.random_position:
            k = 0
            while True:
                # random position along the track median
                x_m = self.np_random.uniform(-checkpoint_median_length, checkpoint_median_length)
                x, y, theta = median_to_cartesian(x_m, y_m, theta_m)
                car.set_position(x, y, theta)
                if self.is_clear(car, x, y, i): break
                k += 1
                assert k < 1000, 'Failed to keep safe distance between cars'
        else:
            space = lap_median_length / len(self.cars)
            x_m = wrap(i * space, -checkpoint_median_length, checkpoint_median_length) - car.length / 2.0
            x, y, theta = median_to_cartesian(x_m, y_m, theta_m)

        K = track_curvature(x_m, y_m)
        throttle = 0.0
        steer = car.specs.curvature_steer(K)
        if self.noisy:
            steer += clip(self.np_random.choice((-1, 0, 1)) * car.specs.steer_step, -1.0, 1.0)

        if self.random_position:
            throttle = car.safe_throttle(steer)
            if self.noisy:
                throttle = self.np_random.random_integers(
                    0, throttle / car.specs.throttle_step) * car.specs.throttle_step

        return x_m, car.reset(x, y, theta, steer, throttle)

    def _reset(self):
        '''
        Resets the simulation.
        '''

        self.time = 0.0
        self.observations = {}
        self.state = {}

        for i in range(len(self.cars)):
            x_m, (x, y, theta, _, _, v, K) = self._reset_car(i)
            car = self.cars[i]
            if self.noisy:
                compass_bias = self.np_random.uniform(-max_compass_bias, max_compass_bias)
                theta -= compass_bias
            else:
                compass_bias = 0.0
            blue = self._blueness(x, y)
            checkpoint = 1.0 if x_m < 0.0 else 0.0
            self.observations[car] = blue, theta, v, K, checkpoint
            self.state[car] = x_m, compass_bias, 0 #laps

        return self.observations

    def _step(self, actions):

        if len(actions) != self.car_num:
            raise ValueError('Wrong number of actions.')

        self.time += dt
        rewards = {}
        exits = []
        info = {}
        done = False

        for car in self.cars:
            action = actions[car]
            x_m_, compass_bias, laps = self.state[car]

            # move the car
            x, y, theta, _, _, v, K_hat, throttle_override = car.step(action, dt)

            # read sensors
            blue = self._blueness(x, y)

            if self.noisy:
                # add observation noise
                compass_bias = clip(self.np_random.normal(compass_bias, compass_deviation), -max_compass_bias, max_compass_bias)
                theta_hat = theta + compass_bias
                v_noise = 0.0 if v <= 0 else self.np_random.normal(0, v * velocity_deviation)
                v_hat = v + v_noise
            else:
                theta_hat = theta
                v_hat = v

            # check progress along the track
            x_m, y_m, _ = cartesian_to_median(x, y, theta)
            lap_threshold = x_m > 0.0 and x_m_ <= 0.0
            checkpoint_threshold = x_m < 0.0 and x_m_ > 0.0
            checkpoint = 1.0 if x_m < 0.0 else 0.0
            dx_m = x_m - x_m_
            if lap_threshold:
                laps += 1
                info['threshold'] = 'lap'
            if checkpoint_threshold:
                dx_m += lap_median_length
                info['threshold'] = 'checkpoint'

            # reward progress
            reward = dx_m
            if throttle_override > 0.0:
                reward += throttle_override_reward * throttle_override
            if abs(y_m) - half_track_width > 0.0:
                reward = out_reward
                done = True
                info['done'] = 'out'
                exits.append(car)

            self.observations[car] = blue, theta_hat, v_hat, K_hat, checkpoint
            rewards[car] = reward
            self.state[car] = x_m, compass_bias, laps

        # are we done yet?
        if self.time >= self.time_limit:
            done = True
            info['done'] = 'complete'

        # collision detection
        if self.car_num > 1:
            for car1 in self.cars:
                car2 = car1.detect_collision(self.cars)
                if car2 is not None:
                    rewards[car1] = out_reward
                    rewards[car2] = out_reward
                    # if self.state[car1][0] < 0 and self.state[car2][0] > 0:
                    #     rewards[car2] = out_reward
                    # else:
                    #     rewards[car1] = out_reward
                    done = True
                    info['done'] = 'crash'

        if done:
            info['laps'] = laps

        return self.observations, rewards, done, info

    def _render(self, mode='human', close=False):
        '''
        Draws the track and the car.
        '''
        if close:
            if self.viewer is not None:
                self.viewer.close()
                self.viewer = None
            return

        if self.viewer is None:
            screen_width = 600
            screen_height = 600

            self.viewer = rendering.Viewer(screen_width, screen_height)
            self.viewer.set_bounds(-1.1, 1.1, -1.1, 1.1)

            self.track.set_color(1, 1, 1)
            self.viewer.add_geom(self.track)

            for c in self.cars:
                c.init_rendering(self.viewer)

        for c in self.cars:
            c.render(self.viewer)

        return self.viewer.render(return_rgb_array=(mode == 'rgb_array'))

    def _img_color(self, img_x, img_y):
        pos = img_x * 4 + img_y * self.track.img.width * 4
        if pos < 0 or pos > len(self.track.img.data) - 4:
            return None
        else:
            return self.track.img.data[pos:pos + 4]

    def _init_track(self, fname, width: float, height: float):
        self.track = rendering.Image(fname, width, height)
        self.img_offset = width / 2.0, height / 2.0
        self.img_scale = self.track.img.width / width, self.track.img.height / height

    def _track_to_image_coordinates(self, x, y):
        ox, oy = self.img_offset
        scalex, scaley = self.img_scale
        img_x = math.trunc((x + ox) * scalex)
        img_y = math.trunc((oy - y) * scaley)
        return img_x, img_y

    def _track_color(self, x, y):
        img_x, img_y = self._track_to_image_coordinates(x, y)
        return self._img_color(img_x, img_y)

    def _track_color_average(self, x, y, n=4):
        '''
        Gets the track color at the specified coordinates, averaging the pixels around that position.
        '''
        img_x, img_y = self._track_to_image_coordinates(x, y)

        count = 0
        b, g, r, a = 0, 0, 0, 0
        for i in range(img_x - n, img_x + n + 1):
            for j in range(img_y - n, img_y + n + 1):
                c = self._img_color(i, j)
                if c is not None:
                    b_, g_, r_, a_ = c
                    b += b_; g += g_; r += r_; a += a_
                    count += 1

        if count == 0:
            return (0, 0, 0, 0)
        else:
            return (b / count, g / count, r / count, a / count)

    def _blueness(self, x, y):
        '''
        Gets the blueness of the track at the specified coordinates.

        The blueness is the normalized difference between the blue and the red
        channels of the (simulated) RGB color sensor.
        '''
        b, _, r, _ = self._track_color(x, y)
        return (b - r) / 217

class DriveItEnv(DriveItEnvMulti):

    def __init__(self, car=Car(), bots=None, time_limit=10, noisy=True, random_position=True):
        self.car = car
        self.bots = [] if bots is None else bots
        cars = []
        cars.append(car)
        for bot in self.bots:
            cars.append(bot.car)
        super().__init__(cars, time_limit, noisy, random_position)

    def _reset(self):
        obs = super()._reset()
        for i in range(1, self.car_num):
            self.bots[i-1].reset(self.state[self.cars[i]][0], obs[self.cars[i]])
        return obs[self.car]

    def _step(self, action):
        actions = {}
        actions[self.car] = action
        for i in range(1, self.car_num):
            actions[self.cars[i]] = self.bots[i-1].act()

        obs, rewards, done, info = super()._step(actions)

        for i in range(1, self.car_num):
            self.bots[i-1].observe(obs[self.cars[i]], dt)

        return obs[self.car], rewards[self.car], done, info
<commit_msg>Avoid deprecated function.<commit_after># -*- coding: utf-8 -*-
"""
Drive-It competition simulation environment
@author: Jean-Claude Manoli
"""

from os import path
import math
import numpy as np
from numpy import cos, sin, pi, clip #pylint: disable=W0611
import gym
from gym import spaces
from gym.utils import seeding
from gym.envs.classic_control import rendering
from car import Car, steer_actions
from utils import * #pylint: disable=W0401,W0614
from DriveItCircuit import * #pylint: disable=W0401,W0614

blue_threshold = 0.9
fps = 60.0
dt = 1.0 / fps

out_reward = -checkpoint_median_length
throttle_override_reward = -dt

max_compass_bias = 0.017
compass_deviation = max_compass_bias / 100.0
velocity_deviation = 0.001

class DriveItEnvMulti(gym.Env):
    metadata = {
        'render.modes': ['human', 'rgb_array'],
        'video.frames_per_second': fps
    }

    def __init__(self, cars=(Car()), time_limit=10, noisy=True, random_position=True, max_speed_deviation=0.0):
        assert len(cars) <= 4, 'Maximum number of cars is 4'
        self.cars = cars
        self.time_limit = time_limit
        self.noisy = noisy
        self.random_position = random_position
        self.max_speed_deviation = max_speed_deviation
        self.v_max = [car.specs.v_max for car in cars]
        self.car_num = len(cars)
        self.dt = dt

        self.viewer = None

        high = np.array([ 1.0,  0.0 * pi      , cars[0].specs.v_max,  cars[0].specs.K_max, 1.0 ], dtype=np.float32)
        low  = np.array([ 0.0, -3.0 * pi / 2.0,                 0.0, -cars[0].specs.K_max, 0.0 ], dtype=np.float32)
        self.action_space = spaces.Discrete(len(steer_actions))
        self.observation_space = spaces.Box(low, high)

        fname = path.join(path.dirname(__file__), "track.png")
        self._init_track(fname, width=2.0, height=2.0)

        self._seed()
        self.time = -1.0
        self.observations = {}
        self.state = {}

    def _seed(self, seed=None):
        self.np_random, seed = seeding.np_random(seed)
        for car in self.cars:
            car.set_noise(self.noisy, self.np_random)
        return [seed]

    def is_clear(self, car, x, y, i):
        # keep 2 car length distance in front of cars (3 measuring from the car centers)
        min_dist, max_angle = 3.0 * car.length, pi / 4.0 + 0.05
        for j in range(i):
            d, alpha = self.cars[j].distance(x, y)
            if d < car.length or (d < min_dist and abs(alpha) < max_angle):
                return False
            d, alpha = car.part_distance(self.cars[j])
            if d < min_dist and abs(alpha) < max_angle:
                return False
        return True

    def _reset_car(self, i):
        car = self.cars[i]

        y_m, theta_m = 0.0, 0.0
        if self.noisy:
            car.specs.v_max = self.v_max[i] * (1 - self.np_random.uniform(0, self.max_speed_deviation))
            y_m += self.np_random.uniform(-0.01, 0.01)
            theta_m += self.np_random.uniform(-pi / 36.0, pi / 36.0)

        if self.random_position:
            k = 0
            while True:
                # random position along the track median
                x_m = self.np_random.uniform(-checkpoint_median_length, checkpoint_median_length)
                x, y, theta = median_to_cartesian(x_m, y_m, theta_m)
                car.set_position(x, y, theta)
                if self.is_clear(car, x, y, i): break
                k += 1
                assert k < 1000, 'Failed to keep safe distance between cars'
        else:
            space = lap_median_length / len(self.cars)
            x_m = wrap(i * space, -checkpoint_median_length, checkpoint_median_length) - car.length / 2.0
            x, y, theta = median_to_cartesian(x_m, y_m, theta_m)

        K = track_curvature(x_m, y_m)
        throttle = 0.0
        steer = car.specs.curvature_steer(K)
        if self.noisy:
            steer += clip(self.np_random.choice((-1, 0, 1)) * car.specs.steer_step, -1.0, 1.0)

        if self.random_position:
            throttle = car.safe_throttle(steer)
            if self.noisy:
                throttle = self.np_random.randint(
                    0, throttle / car.specs.throttle_step + 1) * car.specs.throttle_step

        return x_m, car.reset(x, y, theta, steer, throttle)

    def _reset(self):
        '''
        Resets the simulation.
        '''

        self.time = 0.0
        self.observations = {}
        self.state = {}

        for i in range(len(self.cars)):
            x_m, (x, y, theta, _, _, v, K) = self._reset_car(i)
            car = self.cars[i]
            if self.noisy:
                compass_bias = self.np_random.uniform(-max_compass_bias, max_compass_bias)
                theta -= compass_bias
            else:
                compass_bias = 0.0
            blue = self._blueness(x, y)
            checkpoint = 1.0 if x_m < 0.0 else 0.0
            self.observations[car] = blue, theta, v, K, checkpoint
            self.state[car] = x_m, compass_bias, 0 #laps

        return self.observations

    def _step(self, actions):

        if len(actions) != self.car_num:
            raise ValueError('Wrong number of actions.')

        self.time += dt
        rewards = {}
        exits = []
        info = {}
        done = False

        for car in self.cars:
            action = actions[car]
            x_m_, compass_bias, laps = self.state[car]

            # move the car
            x, y, theta, _, _, v, K_hat, throttle_override = car.step(action, dt)

            # read sensors
            blue = self._blueness(x, y)

            if self.noisy:
                # add observation noise
                compass_bias = clip(self.np_random.normal(compass_bias, compass_deviation), -max_compass_bias, max_compass_bias)
                theta_hat = theta + compass_bias
                v_noise = 0.0 if v <= 0 else self.np_random.normal(0, v * velocity_deviation)
                v_hat = v + v_noise
            else:
                theta_hat = theta
                v_hat = v

            # check progress along the track
            x_m, y_m, _ = cartesian_to_median(x, y, theta)
            lap_threshold = x_m > 0.0 and x_m_ <= 0.0
            checkpoint_threshold = x_m < 0.0 and x_m_ > 0.0
            checkpoint = 1.0 if x_m < 0.0 else 0.0
            dx_m = x_m - x_m_
            if lap_threshold:
                laps += 1
                info['threshold'] = 'lap'
            if checkpoint_threshold:
                dx_m += lap_median_length
                info['threshold'] = 'checkpoint'

            # reward progress
            reward = dx_m
            if throttle_override > 0.0:
                reward += throttle_override_reward * throttle_override
            if abs(y_m) - half_track_width > 0.0:
                reward = out_reward
                done = True
                info['done'] = 'out'
                exits.append(car)

            self.observations[car] = blue, theta_hat, v_hat, K_hat, checkpoint
            rewards[car] = reward
            self.state[car] = x_m, compass_bias, laps

        # are we done yet?
        if self.time >= self.time_limit:
            done = True
            info['done'] = 'complete'

        # collision detection
        if self.car_num > 1:
            for car1 in self.cars:
                car2 = car1.detect_collision(self.cars)
                if car2 is not None:
                    rewards[car1] = out_reward
                    rewards[car2] = out_reward
                    # if self.state[car1][0] < 0 and self.state[car2][0] > 0:
                    #     rewards[car2] = out_reward
                    # else:
                    #     rewards[car1] = out_reward
                    done = True
                    info['done'] = 'crash'

        if done:
            info['laps'] = laps

        return self.observations, rewards, done, info

    def _render(self, mode='human', close=False):
        '''
        Draws the track and the car.
        '''
        if close:
            if self.viewer is not None:
                self.viewer.close()
                self.viewer = None
            return

        if self.viewer is None:
            screen_width = 600
            screen_height = 600

            self.viewer = rendering.Viewer(screen_width, screen_height)
            self.viewer.set_bounds(-1.1, 1.1, -1.1, 1.1)

            self.track.set_color(1, 1, 1)
            self.viewer.add_geom(self.track)

            for c in self.cars:
                c.init_rendering(self.viewer)

        for c in self.cars:
            c.render(self.viewer)

        return self.viewer.render(return_rgb_array=(mode == 'rgb_array'))

    def _img_color(self, img_x, img_y):
        pos = img_x * 4 + img_y * self.track.img.width * 4
        if pos < 0 or pos > len(self.track.img.data) - 4:
            return None
        else:
            return self.track.img.data[pos:pos + 4]

    def _init_track(self, fname, width: float, height: float):
        self.track = rendering.Image(fname, width, height)
        self.img_offset = width / 2.0, height / 2.0
        self.img_scale = self.track.img.width / width, self.track.img.height / height

    def _track_to_image_coordinates(self, x, y):
        ox, oy = self.img_offset
        scalex, scaley = self.img_scale
        img_x = math.trunc((x + ox) * scalex)
        img_y = math.trunc((oy - y) * scaley)
        return img_x, img_y

    def _track_color(self, x, y):
        img_x, img_y = self._track_to_image_coordinates(x, y)
        return self._img_color(img_x, img_y)

    def _track_color_average(self, x, y, n=4):
        '''
        Gets the track color at the specified coordinates, averaging the pixels around that position.
        '''
        img_x, img_y = self._track_to_image_coordinates(x, y)

        count = 0
        b, g, r, a = 0, 0, 0, 0
        for i in range(img_x - n, img_x + n + 1):
            for j in range(img_y - n, img_y + n + 1):
                c = self._img_color(i, j)
                if c is not None:
                    b_, g_, r_, a_ = c
                    b += b_; g += g_; r += r_; a += a_
                    count += 1

        if count == 0:
            return (0, 0, 0, 0)
        else:
            return (b / count, g / count, r / count, a / count)

    def _blueness(self, x, y):
        '''
        Gets the blueness of the track at the specified coordinates.

        The blueness is the normalized difference between the blue and the red
        channels of the (simulated) RGB color sensor.
        '''
        b, _, r, _ = self._track_color(x, y)
        return (b - r) / 217

class DriveItEnv(DriveItEnvMulti):

    def __init__(self, car=Car(), bots=None, time_limit=10, noisy=True, random_position=True):
        self.car = car
        self.bots = [] if bots is None else bots
        cars = []
        cars.append(car)
        for bot in self.bots:
            cars.append(bot.car)
        super().__init__(cars, time_limit, noisy, random_position)

    def _reset(self):
        obs = super()._reset()
        for i in range(1, self.car_num):
            self.bots[i-1].reset(self.state[self.cars[i]][0], obs[self.cars[i]])
        return obs[self.car]

    def _step(self, action):
        actions = {}
        actions[self.car] = action
        for i in range(1, self.car_num):
            actions[self.cars[i]] = self.bots[i-1].act()

        obs, rewards, done, info = super()._step(actions)

        for i in range(1, self.car_num):
            self.bots[i-1].observe(obs[self.cars[i]], dt)

        return obs[self.car], rewards[self.car], done, info
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
    This program redoes the WL of the E-MILES models. The spacing is now 0.9A
    Small mistakes corrected.
    @author:  Maria Luiza L. Dantas
    @date:    2017.04.28
    @version: 0.0.2
"""

import numpy as np
import pandas as pd
import os
import fileinput

# ======================================================================================================================
# Main thread

if __name__ == '__main__':

    # Loading the datasets ---------------------------------------------------------------------------------------------
    emiles_models_path     = '/home/mldantas/Dropbox/DoutoradoIAG/Challenge/emiles_ascii'
    emiles_new_models_path = '/home/mldantas/Dropbox/DoutoradoIAG/Challenge/emiles_ascii/emiles_ascii_wlcorr'
    emiles_models_list     = np.loadtxt('/home/mldantas/Dropbox/DoutoradoIAG/Challenge/emiles_list.txt', dtype=str)
    header = ('wavelength', 'T00p0300', 'T00p0400', 'T00p0500', 'T00p0600', 'T00p0700', 'T00p0800', 'T00p0900',
              'T00p1000', 'T00p1500', 'T00p2000', 'T00p2500', 'T00p3000', 'T00p3500', 'T00p4000', 'T00p4500',
              'T00p5000', 'T00p6000', 'T00p7000', 'T00p8000', 'T00p9000', 'T01p0000', 'T01p2500', 'T01p5000',
              'T01p7500', 'T02p0000', 'T02p2500', 'T02p5000', 'T02p7500', 'T03p0000', 'T03p2500', 'T03p5000',
              'T03p7500', 'T04p0000', 'T04p5000', 'T05p0000', 'T05p5000', 'T06p0000', 'T06p5000', 'T07p0000',
              'T07p5000', 'T08p0000', 'T08p5000', 'T09p0000', 'T09p5000', 'T10p0000', 'T10p5000', 'T11p0000',
              'T11p5000', 'T12p0000', 'T12p5000', 'T13p0000', 'T13p5000', 'T14p0000')

    for each_model in emiles_models_list:
        wavelength    = np.loadtxt(os.path.join(emiles_models_path, each_model), comments='#', usecols=[0])
        all_the_rest  = np.loadtxt(os.path.join(emiles_models_path, each_model), comments='#', dtype=str)[:, 1:]
        current_model_in = os.path.join(emiles_models_path, each_model)
        current_model_out = os.path.join(emiles_new_models_path, each_model)
        new_wavelength = []
        for w in range(wavelength.size):
            if w == 0:
                new_wavelength_i = round(wavelength[w], 1)
            elif w > 0:
                previous_wavelength = wavelength[w-1]
                new_wavelength_temp = round(wavelength[w], 1)
                if new_wavelength_temp == round(previous_wavelength+0.9, 1):
                    new_wavelength_i = new_wavelength_temp
                else:
                    new_wavelength_i = round(previous_wavelength+0.9, 1)
                    print 'WL was corrected'
            new_wavelength.append(new_wavelength_i)
            # with open(current_model_out, "w") as file_out:
            #     with open(current_model_in, "r") as in_file:
            #         i = 0
            #         for line in in_file:
            #             new_line = ''
            #             new_line = line.replace(wavelength[w], '') + all_the_rest[w]
            #             i+=1
            #             file_out.write(new_line)

            # for wl, line in zip(new_wavelength, file_out):
            #     fixed_cols = line.split()
            #     print "{}\t{}\t".format(new_wavelength,)

        new_wavelength = np.array(new_wavelength)
        new_model = np.column_stack((new_wavelength, all_the_rest))
        new_model_df = pd.DataFrame(new_model)
        new_model_df.to_csv(os.path.join(emiles_models_path, os.path.splitext(each_model)[0:4][0]+'.csv'), sep=',',
                             header=header, index=False)

__author_ = 'Maria Luiza Linhares Dantas'

<commit_msg>Update reset_wl.py<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
    This program redoes the WL of the E-MILES models. The spacing is now 0.9A
    Corrections and forced 5 decimals to the wl column.
    @author:  Maria Luiza L. Dantas
    @date:    2017.04.28
    @version: 0.0.3
"""

import numpy as np
import pandas as pd
import os
import fileinput

# ======================================================================================================================
# Main thread

if __name__ == '__main__':

    # Loading the datasets ---------------------------------------------------------------------------------------------
    emiles_models_path     = '/home/mldantas/Dropbox/DoutoradoIAG/Challenge/emiles_ascii'
    emiles_new_models_path = '/home/mldantas/Dropbox/DoutoradoIAG/Challenge/emiles_ascii/emiles_ascii_wlcorr'
    emiles_models_list     = np.loadtxt('/home/mldantas/Dropbox/DoutoradoIAG/Challenge/emiles_list.txt', dtype=str)
    header = ['wavelength', 'T00p0300', 'T00p0400', 'T00p0500', 'T00p0600', 'T00p0700', 'T00p0800', 'T00p0900',
              'T00p1000', 'T00p1500', 'T00p2000', 'T00p2500', 'T00p3000', 'T00p3500', 'T00p4000', 'T00p4500',
              'T00p5000', 'T00p6000', 'T00p7000', 'T00p8000', 'T00p9000', 'T01p0000', 'T01p2500', 'T01p5000',
              'T01p7500', 'T02p0000', 'T02p2500', 'T02p5000', 'T02p7500', 'T03p0000', 'T03p2500', 'T03p5000',
              'T03p7500', 'T04p0000', 'T04p5000', 'T05p0000', 'T05p5000', 'T06p0000', 'T06p5000', 'T07p0000',
              'T07p5000', 'T08p0000', 'T08p5000', 'T09p0000', 'T09p5000', 'T10p0000', 'T10p5000', 'T11p0000',
              'T11p5000', 'T12p0000', 'T12p5000', 'T13p0000', 'T13p5000', 'T14p0000']
    header = np.array(header)

    for each_model in emiles_models_list:
        wavelength    = np.loadtxt(os.path.join(emiles_models_path, each_model), comments='#', usecols=[0])
        all_the_rest  = np.loadtxt(os.path.join(emiles_models_path, each_model), comments='#', dtype=str)[:, 1:]
        current_model_in = os.path.join(emiles_models_path, each_model)
        current_model_out = os.path.join(emiles_new_models_path, each_model)
        new_wavelength = []
        for w in range(wavelength.size):
            if w == 0:
                new_wavelength_i = round(wavelength[w], 1)
                new_wavelength_i = str(new_wavelength_i)+'0000'
            elif w > 0:
                previous_wavelength = wavelength[w-1]
                new_wavelength_temp = round(wavelength[w], 1)
                if new_wavelength_temp == round(previous_wavelength+0.9, 1):
                    new_wavelength_i = str(new_wavelength_temp)+'0000'
                else:
                    new_wavelength_i = round(previous_wavelength+0.9, 1)
                    new_wavelength_i = str(new_wavelength_i) + '0000'
                    print 'WL was corrected'
            new_wavelength.append(new_wavelength_i)
            # with open(current_model_out, "w") as file_out:
            #     with open(current_model_in, "r") as in_file:
            #         i = 0
            #         for line in in_file:
            #             new_line = ''
            #             new_line = line.replace(wavelength[w], '') + all_the_rest[w]
            #             i+=1
            #             file_out.write(new_line)

            # for wl, line in zip(new_wavelength, file_out):
            #     fixed_cols = line.split()
            #     print "{}\t{}\t".format(new_wavelength,)

        new_wavelength = np.array(new_wavelength)
        new_model      = np.column_stack((new_wavelength, all_the_rest))
        # new_model      = np.ma.row_stack(((header, new_model)))
        new_model_df   = pd.DataFrame(new_model)
        # new_model_df['wavelength'] = new_model_df['wavelength'].map(lambda x: '{0:.5}'.format(x))
        new_model_df.to_csv(os.path.join(emiles_models_path, os.path.splitext(each_model)[0:4][0]+'.csv'), sep=',',
                             header= header,index=False, float_format='%.5f')

__author_ = 'Maria Luiza Linhares Dantas'

<|endoftext|>


<commit_before>import discord
from discord.ext import commands

class PingAlerts():
    def __init__(self, bot):
        self.bot = bot

    async def on_message(self, message):
        searchStrings = ["(l.?v.?ls?.*?\s?)(\d{1,2}).{1,3}?(\d{1,2})"]

    @commands.command(pass_context=True)
    async def alert(self, ctx, minlevel : int, maxlevel : int):

        if minlevel > maxlevel:
            medlevel = maxlevel
            maxlevel = minlevel
            minlevel = medlevel
        if minlevel < 3:
            minlevel = 3
        if minlevel > 20:
            minlevel = 20
        if maxlevel < 3:
            maxlevel = 3
        if maxlevel > 20:
            maxlevel = 20

        await self.bot.say('{} has requested a notification be sent out to all players possessing the following roles for a posted quest!: {}'
        .format(ctx.message.author.mention, roleStringGenerator(minlevel, maxlevel)))

def roleStringGenerator(_minlevel : int, _maxlevel : int):
    output = ''
    for x in range(minlevel, maxlevel+1):
        new_name = "Lvl {}".format(x)
        new_role = discord.utils.get(ctx.message.server.roles, name=new_name)
        mentionString = new_role.mention
        output += '{} '.format(mentionString)
    return output

def setup(bot):
    bot.add_cog(PingAlerts(bot))<commit_msg>Updated variable names<commit_after>import discord
from discord.ext import commands

class PingAlerts():
    def __init__(self, bot):
        self.bot = bot

    async def on_message(self, message):
        searchStrings = ["(l.?v.?ls?.*?\s?)(\d{1,2}).{1,3}?(\d{1,2})"]

    @commands.command(pass_context=True)
    async def alert(self, ctx, minlevel : int, maxlevel : int):

        if minlevel > maxlevel:
            medlevel = maxlevel
            maxlevel = minlevel
            minlevel = medlevel
        if minlevel < 3:
            minlevel = 3
        if minlevel > 20:
            minlevel = 20
        if maxlevel < 3:
            maxlevel = 3
        if maxlevel > 20:
            maxlevel = 20

        await self.bot.say('{} has requested a notification be sent out to all players possessing the following roles for a posted quest!: {}'
        .format(ctx.message.author.mention, roleStringGenerator(minlevel, maxlevel)))

def roleStringGenerator(_minlevel : int, _maxlevel : int):
    output = ''
    for x in range(_minlevel, _maxlevel+1):
        new_name = "Lvl {}".format(x)
        new_role = discord.utils.get(ctx.message.server.roles, name=new_name)
        mentionString = new_role.mention
        output += '{} '.format(mentionString)
    return output

def setup(bot):
    bot.add_cog(PingAlerts(bot))<|endoftext|>