<commit_before>import logging
import re

from gi.repository import Gst

from lib.config import Config
from lib.sources.avsource import AVSource

class DeckLinkAVSource(AVSource):
    def __init__(self, name, outputs=None, has_audio=True, has_video=True):
        self.log = logging.getLogger('DecklinkAVSource[{}]'.format(name))
        super().__init__(name, outputs, has_audio, has_video)

        section = 'source.{}'.format(name)

        # Device number, default: 0
        self.device = Config.get(section, 'devicenumber', fallback=0)

        # Audio connection, default: Automatic
        self.aconn = Config.get(section, 'audio_connection', fallback='auto')

        # Video connection, default: Automatic
        self.vconn = Config.get(section, 'video_connection', fallback='auto')

        # Video mode, default: 1080i50
        self.vmode = Config.get(section, 'video_mode', fallback='1080i50')

        self.audiostream_map = self._parse_audiostream_map(section)
        self.log.info("audiostream_map: %s", self.audiostream_map)

        self.required_input_channels = self._calculate_required_input_channels()
        self.log.info("configuring decklink-input to %u channels", self.required_input_channels)

        minGstMultiChannels = (1, 12, 3)
        if self.required_input_channels > 2 and Gst.version() < minGstMultiChannels:
            self.log.warning(
                'GStreamer version %s is probably too to use more then 2 channels on your decklink source. officially supported since %s',
                tuple(Gst.version()), minGstMultiChannels)

        self.launch_pipeline()

    def _calculate_required_input_channels(self):
        required_input_channels = 0
        for audiostream, mapping in self.audiostream_map.items():
            left, right = self._parse_audiostream_mapping(mapping)
            required_input_channels = max(required_input_channels, left + 1)
            if right:
                required_input_channels = max(required_input_channels, right + 1)

        required_input_channels = self._round_decklink_channels(required_input_channels)

        return required_input_channels

    def _round_decklink_channels(self, required_input_channels):
        if required_input_channels > 16:
            raise RuntimeError("Decklink-Devices support up to 16 Channels, you requested {}".format(
                required_input_channels))

        elif required_input_channels > 8:
            required_input_channels = 16

        elif required_input_channels > 2:
            required_input_channels = 8

        else:
            required_input_channels = 2

        return required_input_channels

    def _parse_audiostream_map(self, config_section):
        audiostream_map = {}

        for key in Config[config_section]:
            value = Config.get(config_section, key)
            m = re.match('audiostream\[(\d+)\]', key)
            if m:
                audiostream = int(m.group(1))
                audiostream_map[audiostream] = value

        if len(audiostream_map) == 0:
            self.log.info("no audiostream-mapping defined, defaulting to mapping channel 0+1 to first stream")
            audiostream_map = {0: '0+1'}

        return audiostream_map

    def _parse_audiostream_mapping(self, mapping):
        m = re.match('(\d+)\+(\d+)', mapping)
        if m:
            return (int(m.group(1)), int(m.group(2)),)
        else:
            return (int(mapping), None,)

    def __str__(self):
        return 'DecklinkAVSource[{name}] reading card #{device}'.format(
            name=self.name,
            device=self.device
        )

    def launch_pipeline(self):
        # A video source is required even when we only need audio
        pipeline = """
            decklinkvideosrc
                {channels}
                device-number={device}
                connection={conn}
                mode={mode} !
        """.format(
            channels="channels={}".format(self.required_input_channels) if self.required_input_channels > 2 else "",
            device=self.device,
            conn=self.vconn,
            mode=self.vmode
        )

        if self.has_video:
            pipeline += """
                {deinterlacer}
                videoconvert !
                videoscale !
                videorate name=vout
            """.format(
                deinterlacer=self.build_deinterlacer()
            )
        else:
            pipeline += """
                fakesink
            """

        if self.has_audio:
            pipeline += """
                decklinkaudiosrc
                    device-number={device}
                    connection={conn} ! deinterleave name=aout
            """.format(
                device=self.device,
                conn=self.aconn
            )

            for audiostream, mapping in self.audiostream_map.items():
                left, right = self._parse_audiostream_mapping(mapping)
                if right != None:
                    self.log.info(
                        "mapping decklink input-channels {left} and {right} as left and right "
                        "to output-stream {audiostream}".format(
                            left=left,
                            right=right,
                            audiostream=audiostream))

                    pipeline += """
                        interleave name=i{audiostream}

                        aout.src_{left} ! queue ! i{audiostream}.sink_0
                        aout.src_{right} ! queue ! i{audiostream}.sink_1
                    """.format(
                        left=left,
                        right=right,
                        audiostream=audiostream
                    )
                else:
                    self.log.info(
                        "mapping decklink input-channel {channel} as left and right "
                        "to output-stream {audiostream}".format(
                            channel=left,
                            audiostream=audiostream))

                    pipeline += """
                        interleave name=i{audiostream}
                        aout.src_{channel} ! tee name=t{audiostream}

                        t{audiostream}. ! queue ! i{audiostream}.sink_0
                        t{audiostream}. ! queue ! i{audiostream}.sink_1
                    """.format(
                        channel=left,
                        audiostream=audiostream
                    )

        self.build_pipeline(pipeline)
        self.pipeline.set_state(Gst.State.PLAYING)

    def build_deinterlacer(self):
        deinterlacer = super().build_deinterlacer()
        if deinterlacer != '':
            deinterlacer += ' !'

        return deinterlacer

    def build_audioport(self, audiostream):
        if audiostream in self.audiostream_map:
            return 'i{}.'.format(audiostream)

    def build_videoport(self):
        return 'vout.'

    def restart(self):
        self.pipeline.set_state(Gst.State.NULL)
        self.launch_pipeline()
<commit_msg>move channels-property to audio-element ;)<commit_after>import logging
import re

from gi.repository import Gst

from lib.config import Config
from lib.sources.avsource import AVSource

class DeckLinkAVSource(AVSource):
    def __init__(self, name, outputs=None, has_audio=True, has_video=True):
        self.log = logging.getLogger('DecklinkAVSource[{}]'.format(name))
        super().__init__(name, outputs, has_audio, has_video)

        section = 'source.{}'.format(name)

        # Device number, default: 0
        self.device = Config.get(section, 'devicenumber', fallback=0)

        # Audio connection, default: Automatic
        self.aconn = Config.get(section, 'audio_connection', fallback='auto')

        # Video connection, default: Automatic
        self.vconn = Config.get(section, 'video_connection', fallback='auto')

        # Video mode, default: 1080i50
        self.vmode = Config.get(section, 'video_mode', fallback='1080i50')

        self.audiostream_map = self._parse_audiostream_map(section)
        self.log.info("audiostream_map: %s", self.audiostream_map)

        self.required_input_channels = self._calculate_required_input_channels()
        self.log.info("configuring decklink-input to %u channels", self.required_input_channels)

        minGstMultiChannels = (1, 12, 3)
        if self.required_input_channels > 2 and Gst.version() < minGstMultiChannels:
            self.log.warning(
                'GStreamer version %s is probably too to use more then 2 channels on your decklink source. officially supported since %s',
                tuple(Gst.version()), minGstMultiChannels)

        self.launch_pipeline()

    def _calculate_required_input_channels(self):
        required_input_channels = 0
        for audiostream, mapping in self.audiostream_map.items():
            left, right = self._parse_audiostream_mapping(mapping)
            required_input_channels = max(required_input_channels, left + 1)
            if right:
                required_input_channels = max(required_input_channels, right + 1)

        required_input_channels = self._round_decklink_channels(required_input_channels)

        return required_input_channels

    def _round_decklink_channels(self, required_input_channels):
        if required_input_channels > 16:
            raise RuntimeError("Decklink-Devices support up to 16 Channels, you requested {}".format(
                required_input_channels))

        elif required_input_channels > 8:
            required_input_channels = 16

        elif required_input_channels > 2:
            required_input_channels = 8

        else:
            required_input_channels = 2

        return required_input_channels

    def _parse_audiostream_map(self, config_section):
        audiostream_map = {}

        for key in Config[config_section]:
            value = Config.get(config_section, key)
            m = re.match('audiostream\[(\d+)\]', key)
            if m:
                audiostream = int(m.group(1))
                audiostream_map[audiostream] = value

        if len(audiostream_map) == 0:
            self.log.info("no audiostream-mapping defined, defaulting to mapping channel 0+1 to first stream")
            audiostream_map = {0: '0+1'}

        return audiostream_map

    def _parse_audiostream_mapping(self, mapping):
        m = re.match('(\d+)\+(\d+)', mapping)
        if m:
            return (int(m.group(1)), int(m.group(2)),)
        else:
            return (int(mapping), None,)

    def __str__(self):
        return 'DecklinkAVSource[{name}] reading card #{device}'.format(
            name=self.name,
            device=self.device
        )

    def launch_pipeline(self):
        # A video source is required even when we only need audio
        pipeline = """
            decklinkvideosrc
                device-number={device}
                connection={conn}
                mode={mode} !
        """.format(
            device=self.device,
            conn=self.vconn,
            mode=self.vmode
        )

        if self.has_video:
            pipeline += """
                {deinterlacer}
                videoconvert !
                videoscale !
                videorate name=vout
            """.format(
                deinterlacer=self.build_deinterlacer()
            )
        else:
            pipeline += """
                fakesink
            """

        if self.has_audio:
            pipeline += """
                decklinkaudiosrc
                    {channels}
                    device-number={device}
                    connection={conn} ! deinterleave name=aout
            """.format(
                channels="channels={}".format(self.required_input_channels) if self.required_input_channels > 2 else "",
                device=self.device,
                conn=self.aconn
            )

            for audiostream, mapping in self.audiostream_map.items():
                left, right = self._parse_audiostream_mapping(mapping)
                if right != None:
                    self.log.info(
                        "mapping decklink input-channels {left} and {right} as left and right "
                        "to output-stream {audiostream}".format(
                            left=left,
                            right=right,
                            audiostream=audiostream))

                    pipeline += """
                        interleave name=i{audiostream}

                        aout.src_{left} ! queue ! i{audiostream}.sink_0
                        aout.src_{right} ! queue ! i{audiostream}.sink_1
                    """.format(
                        left=left,
                        right=right,
                        audiostream=audiostream
                    )
                else:
                    self.log.info(
                        "mapping decklink input-channel {channel} as left and right "
                        "to output-stream {audiostream}".format(
                            channel=left,
                            audiostream=audiostream))

                    pipeline += """
                        interleave name=i{audiostream}
                        aout.src_{channel} ! tee name=t{audiostream}

                        t{audiostream}. ! queue ! i{audiostream}.sink_0
                        t{audiostream}. ! queue ! i{audiostream}.sink_1
                    """.format(
                        channel=left,
                        audiostream=audiostream
                    )

        self.build_pipeline(pipeline)
        self.pipeline.set_state(Gst.State.PLAYING)

    def build_deinterlacer(self):
        deinterlacer = super().build_deinterlacer()
        if deinterlacer != '':
            deinterlacer += ' !'

        return deinterlacer

    def build_audioport(self, audiostream):
        if audiostream in self.audiostream_map:
            return 'i{}.'.format(audiostream)

    def build_videoport(self):
        return 'vout.'

    def restart(self):
        self.pipeline.set_state(Gst.State.NULL)
        self.launch_pipeline()
<|endoftext|>


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

filename 			 = "PitchingPost.csv"
filenameAcummYears 	 = "AcumAnnos.cvs"
filenameAcummPlayers = "AcumJugadores.cvs"
filenameOrdered 	 = "Ordenado.cvs"

# Write the csv to a file.
def writeFile(headerList, data, filename):
	valid = 0; # 0 means success | -1 = fails writing the file

	#TODO: Check if the file has .csv format. If not. Will return false
	try:
		outputFile = open(filename, 'w')
		csvWriter = csv.writer(outputFile, delimiter=',', dialect='excel'); # http://stackoverflow.com/questions/29335614/python-csv-writer-leave-a-empty-line-at-the-end-of-the-file
	 	csvWriter.writerow(headerList); # write the header to the csv file

	 	for index, player in enumerate(data):
			csvWriter.writerow(player);

		outputFile.close();

	except:
		valid = -1;

	return valid;

def readCSV(filename):

	#TODO: Check if the file has .csv format. If not. Will return false
	# Read csv contacts
	fs = open(filename)
	reader = csv.reader(fs)
	contacts = list(reader)

	return contacts;

def convertDictToList(inputDictionary):
	if (not isinstance(inputDictionary, dict)):
		return None; #is not a Dictionary

	# it's a Dictionary
	newList = []

	for key, value in inputDictionary.iteritems():
		aux = [key, value]
		newList.append(aux);

	return newList; # correct conversion

# TODO: return -1 or 0 if the function fails
def obtainYearFrecuency():
	global filename
	global filenameAcummYears

	frecuency = {} # dictionary to save the frecuncy for each year
	playerList = readCSV(filename); # loads player list from .csv file

	for index, player in enumerate(playerList):
		if(index >= 1):
			year = str(player[1]) # index = 1, is the year field on csv
			if year in frecuency:
				frecuency[year] += 1;
			else:
				frecuency[year] = 1;
		#else:
			# skip the header

	headerList = ["year", "frecuency"]
	yearFrecuncyList = convertDictToList(frecuency);

	writtenFile = writeFile(headerList, yearFrecuncyList, filenameAcummYears); # export the list to

	if (writtenFile == -1): print "Error writing a file"

def obtainPlayerFrecuency():
	global filename
	global filenameAcummPlayers

	playerFrecuency = {} # dictionary to save the frecuency for each Player
	playerList = readCSV(filename); # loads player list from .csv file

	for index, player in enumerate(playerList):
		if(index >= 1):
			Player = str(player[0]) # index = 0, is the PlayerId field on csv
			if Player in playerFrecuency:
				playerFrecuency[Player] += 1;
			else:
				playerFrecuency[Player] = 1;
		# else
			# skip first elem

	headerList = ["player", "frecuency"]
	playerFrecuencyList = convertDictToList(playerFrecuency);

	writtenFile = writeFile(headerList, playerFrecuencyList, filenameAcummPlayers); # export the list to

	if (writtenFile == -1):
		print "Error writing a file"

def orderPlayers():
	global filename
	global filenameOrdered

	csvFile = readCSV(filename); # loads player list from .csv file
	headerList = csvFile[0]; # loading header from the list
	playerList  = sorted(csvFile[1:]); # sorted player list (without header)

	writeFile(headerList, playerList, filenameOrdered);

orderPlayers();
obtainYearFrecuency();
obtainPlayerFrecuency();
<commit_msg>Pass filename via parameters<commit_after># -*- coding: utf-8 -*-
import csv

filename 			 = "PitchingPost.csv"
filenameAcummYears 	 = "AcumAnnos.cvs"
filenameAcummPlayers = "AcumJugadores.cvs"
filenameOrdered 	 = "Ordenado.cvs"

# Write the csv to a file.
def writeFile(headerList, data, filename):
	valid = 0; # 0 means success | -1 = fails writing the file

	#TODO: Check if the file has .csv format. If not. Will return false
	try:
		outputFile = open(filename, 'w')
		csvWriter = csv.writer(outputFile, delimiter=',', dialect='excel'); # http://stackoverflow.com/questions/29335614/python-csv-writer-leave-a-empty-line-at-the-end-of-the-file
	 	csvWriter.writerow(headerList); # write the header to the csv file

	 	for index, player in enumerate(data):
			csvWriter.writerow(player);

		outputFile.close();

	except:
		valid = -1;

	return valid;

def readCSV(filename):

	#TODO: Check if the file has .csv format. If not. Will return false
	# Read csv contacts
	fs = open(filename)
	reader = csv.reader(fs)
	contacts = list(reader)

	return contacts;

def convertDictToList(inputDictionary):
	if (not isinstance(inputDictionary, dict)):
		return None; #is not a Dictionary

	# it's a Dictionary
	newList = []

	for key, value in inputDictionary.iteritems():
		aux = [key, value]
		newList.append(aux);

	return newList; # correct conversion

# TODO: return -1 or 0 if the function fails
def obtainYearFrecuency(filename, outputFilename):

	frecuency = {} # dictionary to save the frecuncy for each year
	playerList = readCSV(filename); # loads player list from .csv file

	for index, player in enumerate(playerList):
		if(index >= 1):
			year = str(player[1]) # index = 1, is the year field on csv
			if year in frecuency:
				frecuency[year] += 1;
			else:
				frecuency[year] = 1;
		#else:
			# skip the header

	headerList = ["year", "frecuency"]
	yearFrecuncyList = convertDictToList(frecuency);

	writtenFile = writeFile(headerList, yearFrecuncyList, filenameAcummYears); # export the list to

	if (writtenFile == -1): print "Error writing a file"

def obtainPlayerFrecuency(filename, outputFilename):

	playerFrecuency = {} # dictionary to save the frecuency for each Player
	playerList = readCSV(filename); # loads player list from .csv file

	for index, player in enumerate(playerList):
		if(index >= 1):
			Player = str(player[0]) # index = 0, is the PlayerId field on csv
			if Player in playerFrecuency:
				playerFrecuency[Player] += 1;
			else:
				playerFrecuency[Player] = 1;
		# else
			# skip first elem

	headerList = ["player", "frecuency"]
	playerFrecuencyList = convertDictToList(playerFrecuency);

	writtenFile = writeFile(headerList, playerFrecuencyList, filenameAcummPlayers); # export the list to

	if (writtenFile == -1):
		print "Error writing a file"

def orderPlayers(filename, outputFilename):

	csvFile = readCSV(filename); # loads player list from .csv file
	headerList = csvFile[0]; # loading header from the list
	playerList  = sorted(csvFile[1:]); # sorted player list (without header)

	writeFile(headerList, playerList, filenameOrdered);

orderPlayers(filename, filenameOrdered);
obtainYearFrecuency(filename, filenameAcummYears);
obtainPlayerFrecuency(filename, filenameAcummPlayers);
<|endoftext|>


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

# Original author: Astin Choi <achoi@akamai.com>

# Copyright 2016 Akamai Technologies http://developer.akamai.com.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import base64
import hashlib
import hmac
import mmap
import ntpath
import os
import random
import sys
import time
if sys.version_info[0] >= 3:
    from urllib.parse import quote_plus, quote
else:
    from urllib import quote_plus, quote

import requests

class NetstorageError(Exception):
    """Base-class for all exceptions raised by Netstorage Class"""

class Netstorage:
    def __init__(self, hostname, keyname, key, ssl=False):
        if not (hostname and keyname and key):
            raise NetstorageError('[NetstorageError] You should input netstorage hostname, keyname and key all')

        self.hostname = hostname
        self.keyname = keyname
        self.key = key
        self.ssl = 's' if ssl else ''
        self.http_client = requests.Session()

    def _download_data_from_response(self, response, ns_path, local_destination, chunk_size=16*1024):
        if not local_destination:
            local_destination = ntpath.basename(ns_path)
        elif os.path.isdir(local_destination):
            local_destination = os.path.join(local_destination, ntpath.basename(ns_path))

        if response.status_code == 200:
            try:
                with open(local_destination, 'wb') as f:
                    for chunk in response.iter_content(chunk_size):
                        if chunk:
                            f.write(chunk)
            except Exception as e:
                raise NetstorageError(e)

    def _upload_data_to_request(self, source):
        mmapped_data = None
        try:
            with open(source, 'rb') as f:
                mmapped_data = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
        except Exception as e:
            if mmapped_data: mmapped_data.close()
            raise NetstorageError(e)

        return mmapped_data

    def _request(self, **kwargs):
        path = kwargs['path']
        if not path.startswith('/'):
            raise NetstorageError('[NetstorageError] Invalid netstorage path')

        path = quote(path)
        acs_action = "version=1&action={0}".format(kwargs['action'])
        acs_auth_data = "5, 0.0.0.0, 0.0.0.0, {0}, {1}, {2}".format(
            int(time.time()),
            str(random.getrandbits(32)),
            self.keyname)
        sign_string = "{0}\nx-akamai-acs-action:{1}\n".format(path, acs_action)
        message = acs_auth_data + sign_string
        hash_ = hmac.new(self.key.encode(), message.encode(), hashlib.sha256).digest()

        acs_auth_sign = base64.b64encode(hash_)

        request_url = "http{0}://{1}{2}".format(self.ssl, self.hostname, path)

        headers = {
            'X-Akamai-ACS-Action': acs_action,
            'X-Akamai-ACS-Auth-Data': acs_auth_data,
            'X-Akamai-ACS-Auth-Sign': acs_auth_sign,
            'Accept-Encoding': 'identity',
            'User-Agent': 'NetStorageKit-Python'
        }

        response = None
        if kwargs['method'] == 'GET':
            if kwargs['action'] == 'download':
                response = self.http_client.get(request_url, headers=headers, stream=True)
                if 'stream' not in kwargs.keys():
                    self._download_data_from_response(response, kwargs['path'], kwargs['destination'])
            else:
                response = self.http_client.get(request_url, headers=headers)

        elif kwargs['method'] == 'POST':
            response = self.http_client.post(request_url, headers=headers)

        elif kwargs['method'] == 'PUT': # Use only upload
            if 'stream' in kwargs.keys():
                response = self.http_client.put(request_url, headers=headers, data=kwargs['stream'])
            elif kwargs['action'] == 'upload':
                mmapped_data = self._upload_data_to_request(kwargs['source'])
                response = self.http_client.put(request_url, headers=headers, data=mmapped_data)
                mmapped_data.close()

        return response.status_code == 200, response

    def dir(self, ns_path):
        # if option and type(option) == 'dict':
        #     pass
        return self._request(action='dir&format=xml',
                            method='GET',
                            path=ns_path)

    def download(self, ns_source, local_destination=''):
        if ns_source.endswith('/'):
            raise NetstorageError("[NetstorageError] Nestorage download path shouldn't be a directory: {0}".format(ns_source))
        return self._request(action='download',
                            method='GET',
                            path=ns_source,
                            destination=local_destination)

    def stream_download(self, ns_source):
        return self._request(action='download',
                             method='GET',
                             path=ns_source,
                             stream=True)

    def du(self, ns_path):
        return self._request(action='du&format=xml',
                            method='GET',
                            path=ns_path)

    def stat(self, ns_path):
        return self._request(action='stat&format=xml',
                            method='GET',
                            path=ns_path)

    def mkdir(self, ns_path):
        return self._request(action='mkdir',
                            method='POST',
                            path=ns_path)

    def rmdir(self, ns_path):
        return self._request(action='rmdir',
                            method='POST',
                            path=ns_path)

    def mtime(self, ns_path, mtime):
        return self._request(action='mtime&format=xml&mtime={0}'.format(mtime),
                            method='POST',
                            path=ns_path)

    def delete(self, ns_path):
        return self._request(action='delete',
                            method='POST',
                            path=ns_path)

    def quick_delete(self, ns_path):
        return self._request(action='quick-delete&quick-delete=imreallyreallysure',
                            method='POST',
                            path=ns_path)

    def rename(self, ns_target, ns_destination):
        return self._request(action='rename&destination={0}'.format(quote_plus(ns_destination)),
                            method='POST',
                            path=ns_target)

    def symlink(self, ns_target, ns_destination):
        return self._request(action='symlink&target={0}'.format(quote_plus(ns_target)),
                            method='POST',
                            path=ns_destination)

    def upload(self, local_source, ns_destination, index_zip=False):
        if os.path.isfile(local_source):
            if ns_destination.endswith('/'):
                ns_destination = "{0}{1}".format(ns_destination, ntpath.basename(local_source))
        else:
          raise NetstorageError("[NetstorageError] {0} doesn't exist or is directory".format(local_source))

        action = 'upload'
        if index_zip: # Support only For File Store, not Object Store.
            action = action + '&index-zip=1'

        return self._request(action=action,
                            method='PUT',
                            source=local_source,
                            path=ns_destination)

    def stream_upload(self, data, ns_destination):
        return self._request(action='upload',
                            method='PUT',
                            stream=data,
                            path=ns_destination)
<commit_msg>Fix non-matching action during index-zip upload<commit_after># -*- coding: utf-8 -*-

# Original author: Astin Choi <achoi@akamai.com>

# Copyright 2016 Akamai Technologies http://developer.akamai.com.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import base64
import hashlib
import hmac
import mmap
import ntpath
import os
import random
import sys
import time
if sys.version_info[0] >= 3:
    from urllib.parse import quote_plus, quote
else:
    from urllib import quote_plus, quote

import requests

class NetstorageError(Exception):
    """Base-class for all exceptions raised by Netstorage Class"""

class Netstorage:
    def __init__(self, hostname, keyname, key, ssl=False):
        if not (hostname and keyname and key):
            raise NetstorageError('[NetstorageError] You should input netstorage hostname, keyname and key all')

        self.hostname = hostname
        self.keyname = keyname
        self.key = key
        self.ssl = 's' if ssl else ''
        self.http_client = requests.Session()

    def _download_data_from_response(self, response, ns_path, local_destination, chunk_size=16*1024):
        if not local_destination:
            local_destination = ntpath.basename(ns_path)
        elif os.path.isdir(local_destination):
            local_destination = os.path.join(local_destination, ntpath.basename(ns_path))

        if response.status_code == 200:
            try:
                with open(local_destination, 'wb') as f:
                    for chunk in response.iter_content(chunk_size):
                        if chunk:
                            f.write(chunk)
            except Exception as e:
                raise NetstorageError(e)

    def _upload_data_to_request(self, source):
        mmapped_data = None
        try:
            with open(source, 'rb') as f:
                mmapped_data = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
        except Exception as e:
            if mmapped_data: mmapped_data.close()
            raise NetstorageError(e)

        return mmapped_data

    def _request(self, **kwargs):
        path = kwargs['path']
        if not path.startswith('/'):
            raise NetstorageError('[NetstorageError] Invalid netstorage path')

        path = quote(path)
        acs_action = "version=1&action={0}".format(kwargs['action'])
        acs_auth_data = "5, 0.0.0.0, 0.0.0.0, {0}, {1}, {2}".format(
            int(time.time()),
            str(random.getrandbits(32)),
            self.keyname)
        sign_string = "{0}\nx-akamai-acs-action:{1}\n".format(path, acs_action)
        message = acs_auth_data + sign_string
        hash_ = hmac.new(self.key.encode(), message.encode(), hashlib.sha256).digest()

        acs_auth_sign = base64.b64encode(hash_)

        request_url = "http{0}://{1}{2}".format(self.ssl, self.hostname, path)

        headers = {
            'X-Akamai-ACS-Action': acs_action,
            'X-Akamai-ACS-Auth-Data': acs_auth_data,
            'X-Akamai-ACS-Auth-Sign': acs_auth_sign,
            'Accept-Encoding': 'identity',
            'User-Agent': 'NetStorageKit-Python'
        }

        response = None
        if kwargs['method'] == 'GET':
            if kwargs['action'] == 'download':
                response = self.http_client.get(request_url, headers=headers, stream=True)
                if 'stream' not in kwargs.keys():
                    self._download_data_from_response(response, kwargs['path'], kwargs['destination'])
            else:
                response = self.http_client.get(request_url, headers=headers)

        elif kwargs['method'] == 'POST':
            response = self.http_client.post(request_url, headers=headers)

        elif kwargs['method'] == 'PUT': # Use only upload
            if 'stream' in kwargs.keys():
                response = self.http_client.put(request_url, headers=headers, data=kwargs['stream'])
            elif kwargs['action'].startswith('upload'):
                mmapped_data = self._upload_data_to_request(kwargs['source'])
                response = self.http_client.put(request_url, headers=headers, data=mmapped_data)
                mmapped_data.close()

        return response.status_code == 200, response

    def dir(self, ns_path):
        # if option and type(option) == 'dict':
        #     pass
        return self._request(action='dir&format=xml',
                            method='GET',
                            path=ns_path)

    def download(self, ns_source, local_destination=''):
        if ns_source.endswith('/'):
            raise NetstorageError("[NetstorageError] Nestorage download path shouldn't be a directory: {0}".format(ns_source))
        return self._request(action='download',
                            method='GET',
                            path=ns_source,
                            destination=local_destination)

    def stream_download(self, ns_source):
        return self._request(action='download',
                             method='GET',
                             path=ns_source,
                             stream=True)

    def du(self, ns_path):
        return self._request(action='du&format=xml',
                            method='GET',
                            path=ns_path)

    def stat(self, ns_path):
        return self._request(action='stat&format=xml',
                            method='GET',
                            path=ns_path)

    def mkdir(self, ns_path):
        return self._request(action='mkdir',
                            method='POST',
                            path=ns_path)

    def rmdir(self, ns_path):
        return self._request(action='rmdir',
                            method='POST',
                            path=ns_path)

    def mtime(self, ns_path, mtime):
        return self._request(action='mtime&format=xml&mtime={0}'.format(mtime),
                            method='POST',
                            path=ns_path)

    def delete(self, ns_path):
        return self._request(action='delete',
                            method='POST',
                            path=ns_path)

    def quick_delete(self, ns_path):
        return self._request(action='quick-delete&quick-delete=imreallyreallysure',
                            method='POST',
                            path=ns_path)

    def rename(self, ns_target, ns_destination):
        return self._request(action='rename&destination={0}'.format(quote_plus(ns_destination)),
                            method='POST',
                            path=ns_target)

    def symlink(self, ns_target, ns_destination):
        return self._request(action='symlink&target={0}'.format(quote_plus(ns_target)),
                            method='POST',
                            path=ns_destination)

    def upload(self, local_source, ns_destination, index_zip=False):
        if os.path.isfile(local_source):
            if ns_destination.endswith('/'):
                ns_destination = "{0}{1}".format(ns_destination, ntpath.basename(local_source))
        else:
          raise NetstorageError("[NetstorageError] {0} doesn't exist or is directory".format(local_source))

        action = 'upload'
        if index_zip: # Support only For File Store, not Object Store.
            action = action + '&index-zip=1'

        return self._request(action=action,
                            method='PUT',
                            source=local_source,
                            path=ns_destination)

    def stream_upload(self, data, ns_destination):
        return self._request(action='upload',
                            method='PUT',
                            stream=data,
                            path=ns_destination)
<|endoftext|>


<commit_before># Copyright 2013 Big Switch Networks, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import mock
from oslo.config import cfg

import neutron.common.test_lib as test_lib
from neutron.plugins.bigswitch import config
from neutron.tests.unit.bigswitch import fake_server

RESTPROXY_PKG_PATH = 'neutron.plugins.bigswitch.plugin'
L3_RESTPROXY_PKG_PATH = 'neutron.plugins.bigswitch.l3_router_plugin'
NOTIFIER = 'neutron.plugins.bigswitch.plugin.AgentNotifierApi'
CERTFETCH = 'neutron.plugins.bigswitch.servermanager.ServerPool._fetch_cert'
SERVER_MANAGER = 'neutron.plugins.bigswitch.servermanager'
HTTPCON = 'neutron.plugins.bigswitch.servermanager.httplib.HTTPConnection'
SPAWN = 'neutron.plugins.bigswitch.plugin.eventlet.GreenPool.spawn_n'
CWATCH = SERVER_MANAGER + '.ServerPool._consistency_watchdog'

class BigSwitchTestBase(object):

    _plugin_name = ('%s.NeutronRestProxyV2' % RESTPROXY_PKG_PATH)
    _l3_plugin_name = ('%s.L3RestProxy' % L3_RESTPROXY_PKG_PATH)

    def setup_config_files(self):
        etc_path = os.path.join(os.path.dirname(__file__), 'etc')
        test_lib.test_config['config_files'] = [os.path.join(etc_path,
                                                'restproxy.ini.test')]
        self.addCleanup(cfg.CONF.reset)
        config.register_config()
        # Only try SSL on SSL tests
        cfg.CONF.set_override('server_ssl', False, 'RESTPROXY')
        cfg.CONF.set_override('ssl_cert_directory',
                              os.path.join(etc_path, 'ssl'), 'RESTPROXY')
        # The mock interferes with HTTP(S) connection caching
        cfg.CONF.set_override('cache_connections', False, 'RESTPROXY')
        cfg.CONF.set_override('service_plugins', ['bigswitch_l3'])

    def setup_patches(self):
        self.plugin_notifier_p = mock.patch(NOTIFIER)
        # prevent any greenthreads from spawning
        self.spawn_p = mock.patch(SPAWN, new=lambda *args, **kwargs: None)
        # prevent the consistency watchdog from starting
        self.watch_p = mock.patch(CWATCH, new=lambda *args, **kwargs: None)
        self.plugin_notifier_p.start()
        self.spawn_p.start()
        self.watch_p.start()

    def startHttpPatch(self):
        self.httpPatch = mock.patch(HTTPCON,
                                    new=fake_server.HTTPConnectionMock)
        self.httpPatch.start()
<commit_msg>Allow DHCPv6 reply from server to client<commit_after># Copyright 2013 Big Switch Networks, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import mock
from oslo.config import cfg

import neutron.common.test_lib as test_lib
from neutron.plugins.bigswitch import config
from neutron.tests.unit.bigswitch import fake_server

RESTPROXY_PKG_PATH = 'neutron.plugins.bigswitch.plugin'
L3_RESTPROXY_PKG_PATH = 'neutron.plugins.bigswitch.l3_router_plugin'
NOTIFIER = 'neutron.plugins.bigswitch.plugin.AgentNotifierApi'
CERTFETCH = 'neutron.plugins.bigswitch.servermanager.ServerPool._fetch_cert'
SERVER_MANAGER = 'neutron.plugins.bigswitch.servermanager'
HTTPCON = 'neutron.plugins.bigswitch.servermanager.httplib.HTTPConnection'
SPAWN = 'neutron.plugins.bigswitch.plugin.eventlet.GreenPool.spawn_n'
CWATCH = SERVER_MANAGER + '.ServerPool._consistency_watchdog'

class BigSwitchTestBase(object):

    _plugin_name = ('%s.NeutronRestProxyV2' % RESTPROXY_PKG_PATH)
    _l3_plugin_name = ('%s.L3RestProxy' % L3_RESTPROXY_PKG_PATH)

    def setup_config_files(self):
        etc_path = os.path.join(os.path.dirname(__file__), 'etc')
        test_lib.test_config['config_files'] = [os.path.join(etc_path,
                                                'restproxy.ini.test')]
        self.addCleanup(cfg.CONF.reset)
        config.register_config()
        # Only try SSL on SSL tests
        cfg.CONF.set_override('server_ssl', False, 'RESTPROXY')
        cfg.CONF.set_override('ssl_cert_directory',
                              os.path.join(etc_path, 'ssl'), 'RESTPROXY')
        # The mock interferes with HTTP(S) connection caching
        cfg.CONF.set_override('cache_connections', False, 'RESTPROXY')
        cfg.CONF.set_override('service_plugins', ['bigswitch_l3'])
        cfg.CONF.set_override('add_meta_server_route', False, 'RESTPROXY')

    def setup_patches(self):
        self.plugin_notifier_p = mock.patch(NOTIFIER)
        # prevent any greenthreads from spawning
        self.spawn_p = mock.patch(SPAWN, new=lambda *args, **kwargs: None)
        # prevent the consistency watchdog from starting
        self.watch_p = mock.patch(CWATCH, new=lambda *args, **kwargs: None)
        self.plugin_notifier_p.start()
        self.spawn_p.start()
        self.watch_p.start()

    def startHttpPatch(self):
        self.httpPatch = mock.patch(HTTPCON,
                                    new=fake_server.HTTPConnectionMock)
        self.httpPatch.start()
<|endoftext|>


<commit_before>import time
import logging

import RPi.GPIO as GPIO
from src.Notification.Subscriber.MQTTSubscriber import MQTTSubscriber

class RGB(MQTTSubscriber):

    GREEN = 'green'
    RED = 'red'
    BLUE = 'blue'

    DEFAULT_COLOUR = GREEN
    DEFAULT_STYLE = 'flash'

    def __init__(self, configuration, notification_manager):
        logging.debug('Initialising RGB')
        self._notification_manager = notification_manager

        self._id = configuration['id']
        self._topics = configuration['mqtt']['topics']
        self._red_gpio = configuration['channels']['r']
        self._green_gpio = configuration['channels']['g']
        self._blue_gpio = configuration['channels']['b']

        self._channels = {
            self.RED: self._red_gpio,
            self.GREEN: self._green_gpio,
            self.BLUE: self._blue_gpio
        }

        self._rgb_colours = {
            self.RED: {
                'red': 255,
                'green': 0,
                'blue': 0
            },
            self.GREEN: {
                'red': 0,
                'green': 255,
                'blue': 0
            },
            self.BLUE: {
                'red': 0,
                'green': 0,
                'blue': 255
            }
        }

        self._valid_pulse_colours = [
            self._rgb_colours['red'],
            self._rgb_colours['green'],
            self._rgb_colours['blue']
        ]

        self._initialise_gpio()

    def __del__(self):
        logging.debug('Destroying RGB class')
        self._red.stop()
        self._green.stop()
        self._blue.stop()

    def notify(self, mosq, obj, msg):
        if self._notification_manager.is_satisfied() is False:
            return False

        rgb = self._rgb_colours[self.DEFAULT_COLOUR]
        style = self.DEFAULT_STYLE

        # for topic in self._topics:
        #     if topic['topic'] == msg.topic and 'colour' in topic:
        #         rgb = {
        #             'red': topic['colour']['red'],
        #             'green': topic['colour']['green'],
        #             'blue': topic['colour']['blue']
        #         }
        #     if topic['topic'] == msg.topic and 'style' in topic:
        #         style = topic['style']

        if style == 'pulse' and rgb not in self._valid_pulse_colours:
            raise Exception('colour must be one of {}'.format(self._valid_pulse_colours))

        if style == 'pulse':
            raise NotImplementedError
            # self._pulse(channel=self._map_rgb_to_single_channel(rgb=rgb))
        elif style == 'flash':
            self._flash(rgb=self._map_rgb_to_percentages(rgb=rgb))

        time.sleep(0.5)

    def _initialise_gpio(self):
        GPIO.setmode(GPIO.BCM)
        GPIO.setup([self._red_gpio, self._green_gpio, self._blue_gpio], GPIO.OUT, initial=GPIO.LOW)

        self._red = GPIO.PWM(self._red_gpio, 100)
        self._green = GPIO.PWM(self._green_gpio, 100)
        self._blue = GPIO.PWM(self._blue_gpio, 100)

        self._red.start(0)
        self._green.start(0)
        self._blue.start(0)

    def _flash(self, rgb, duration=0.1):
        logging.debug('Calling _flash')
        self._red.ChangeDutyCycle(rgb['red'])
        self._green.ChangeDutyCycle(rgb['green'])
        self._blue.ChangeDutyCycle(rgb['blue'])

        time.sleep(duration)

        self._red.ChangeDutyCycle(0)
        self._green.ChangeDutyCycle(0)
        self._blue.ChangeDutyCycle(0)

    # def _pulse(self, channel, frequency=100, speed=0.005, step=1):
    #     self._initialise_gpio()
    #     p = GPIO.PWM(channel, frequency)
    #     p.start(0)
    #     for duty_cycle in range(0, 100, step):
    #         p.ChangeDutyCycle(duty_cycle)
    #         time.sleep(speed)
    #     for duty_cycle in range(100, 0, -step):
    #         p.ChangeDutyCycle(duty_cycle)
    #         time.sleep(speed)
    #     p.stop()
    #     self._clean_up()

    def _map_rgb_to_single_channel(self, rgb):
        for component in rgb:
            if rgb[component] == 255:
                return self._channels[component]

    def _map_rgb_to_percentages(self, rgb):
        return {x: (rgb[x] / 255.0) * 100 for x in rgb}
<commit_msg>debug<commit_after>import time
import logging

import RPi.GPIO as GPIO
from src.Notification.Subscriber.MQTTSubscriber import MQTTSubscriber

class RGB(MQTTSubscriber):

    GREEN = 'green'
    RED = 'red'
    BLUE = 'blue'

    DEFAULT_COLOUR = GREEN
    DEFAULT_STYLE = 'flash'

    def __init__(self, configuration, notification_manager):
        logging.debug('Initialising RGB')
        self._notification_manager = notification_manager

        self._id = configuration['id']
        self._topics = configuration['mqtt']['topics']
        self._red_gpio = configuration['channels']['r']
        self._green_gpio = configuration['channels']['g']
        self._blue_gpio = configuration['channels']['b']

        self._channels = {
            self.RED: self._red_gpio,
            self.GREEN: self._green_gpio,
            self.BLUE: self._blue_gpio
        }

        self._rgb_colours = {
            self.RED: {
                'red': 255,
                'green': 0,
                'blue': 0
            },
            self.GREEN: {
                'red': 0,
                'green': 255,
                'blue': 0
            },
            self.BLUE: {
                'red': 0,
                'green': 0,
                'blue': 255
            }
        }

        self._valid_pulse_colours = [
            self._rgb_colours['red'],
            self._rgb_colours['green'],
            self._rgb_colours['blue']
        ]

        self._initialise_gpio()

    def __del__(self):
        logging.debug('Destroying RGB class')
        self._red.stop()
        self._green.stop()
        self._blue.stop()
        GPIO.cleanup()

    def notify(self, mosq, obj, msg):
        if self._notification_manager.is_satisfied() is False:
            return False

        rgb = self._rgb_colours[self.DEFAULT_COLOUR]
        style = self.DEFAULT_STYLE

        # for topic in self._topics:
        #     if topic['topic'] == msg.topic and 'colour' in topic:
        #         rgb = {
        #             'red': topic['colour']['red'],
        #             'green': topic['colour']['green'],
        #             'blue': topic['colour']['blue']
        #         }
        #     if topic['topic'] == msg.topic and 'style' in topic:
        #         style = topic['style']

        if style == 'pulse' and rgb not in self._valid_pulse_colours:
            raise Exception('colour must be one of {}'.format(self._valid_pulse_colours))

        if style == 'pulse':
            raise NotImplementedError
            # self._pulse(channel=self._map_rgb_to_single_channel(rgb=rgb))
        elif style == 'flash':
            self._flash(rgb=self._map_rgb_to_percentages(rgb=rgb))

        time.sleep(0.5)

    def _initialise_gpio(self):
        GPIO.setmode(GPIO.BCM)
        GPIO.setup([self._red_gpio, self._green_gpio, self._blue_gpio], GPIO.OUT, initial=GPIO.LOW)

        self._red = GPIO.PWM(self._red_gpio, 100)
        self._green = GPIO.PWM(self._green_gpio, 100)
        self._blue = GPIO.PWM(self._blue_gpio, 100)

        self._red.start(0)
        self._green.start(0)
        self._blue.start(0)

    def _flash(self, rgb, duration=0.1):
        logging.debug('Calling _flash')
        self._red.ChangeDutyCycle(rgb['red'])
        self._green.ChangeDutyCycle(rgb['green'])
        self._blue.ChangeDutyCycle(rgb['blue'])

        time.sleep(duration)

        self._red.ChangeDutyCycle(0)
        self._green.ChangeDutyCycle(0)
        self._blue.ChangeDutyCycle(0)

    # def _pulse(self, channel, frequency=100, speed=0.005, step=1):
    #     self._initialise_gpio()
    #     p = GPIO.PWM(channel, frequency)
    #     p.start(0)
    #     for duty_cycle in range(0, 100, step):
    #         p.ChangeDutyCycle(duty_cycle)
    #         time.sleep(speed)
    #     for duty_cycle in range(100, 0, -step):
    #         p.ChangeDutyCycle(duty_cycle)
    #         time.sleep(speed)
    #     p.stop()
    #     self._clean_up()

    def _map_rgb_to_single_channel(self, rgb):
        for component in rgb:
            if rgb[component] == 255:
                return self._channels[component]

    def _map_rgb_to_percentages(self, rgb):
        return {x: (rgb[x] / 255.0) * 100 for x in rgb}
<|endoftext|>


<commit_before>
#  IATI Data Quality, tools for Data QA on IATI-formatted  publications
#  by Mark Brough, Martin Keegan, Ben Webb and Jennifer Smith
#
#  Copyright (C) 2013  Publish What You Fund
#
#  This programme is free software; you may redistribute and/or modify
#  it under the terms of the GNU Affero General Public License v3.0

from iatidq import db

from sqlalchemy import func

import summary

import models
import csv
import util
import unicodecsv

def checkCondition(row):
    if (('packagegroup_condition' in row) and (row['packagegroup_condition']!='')):
        return row['packagegroup_condition']
    else:
        return None

def importOrganisationPackagesFromFile(filename, organisation_c=None, organisation_n=None):
    with file(filename) as fh:
        return _importOrganisationPackages(organisation_c, organisation_n, fh, True)

def _importOrganisationPackages(organisation_c, organisation_n, fh, local):

        data = unicodecsv.DictReader(fh)

        for row in data:
            condition = checkCondition(row)

            if organisation_c is None:
                checkP = organisations(row['organisation_code'])
                organisation_code = row['organisation_code']
                organisation_name = row['organisation_name']
            else:
                checkP = organisations(organisation_c)
                organisation_code = organisation_c
                organisation_name = organisation_n
            if checkP:
                organisation = checkP
            else:
                organisation = addOrganisation({"organisation_name": organisation_name,
                                          "organisation_code": organisation_code
                                        })
            print organisation_code
            packages = models.Package.query.filter(models.PackageGroup.publisher_iati_id==organisation_code
                        ).join(models.PackageGroup
                        ).all()

            if not packages:
                pass
            else:
                for package in packages:
                    organisationpackage = addOrganisationPackage({
                                "organisation_id" : organisation.id,
                                "package_id" : package.id,
                                "condition": condition
                            })

            packagegroups = models.PackageGroup.query.filter(models.PackageGroup.publisher_iati_id==organisation_code
                        ).all()
            if not packagegroups:
                pass
            else:
                for packagegroup in packagegroups:
                    organisationpackagegroup = addOrganisationPackageGroup({
                                "organisation_id" : organisation.id,
                                "packagegroup_id" : packagegroup.id,
                                "condition": condition
                            })
            if (('packagegroup_name' in row) and (row['packagegroup_name'] != "")):
                packagegroup = models.PackageGroup.query.filter(models.PackageGroup.name == row['packagegroup_name']
                        ).first()
                data = {
                    'packagegroup_id': packagegroup.id,
                    'organisation_id': organisation.id,
                    'condition': condition
                }
                addOrganisationPackageFromPackageGroup(data)

        print "Imported successfully"
        return True

def organisations(organisation_code=None):
    if organisation_code is None:
        return models.Organisation.query.all()
    else:
        return models.Organisation.query.filter_by(organisation_code=organisation_code).first()

def organisationPackages(organisation_code=None):
    if organisation_code is not None:
        organisationpackages = db.session.query(models.Package,
                                             models.OrganisationPackage
                        ).filter(models.Organisation.organisation_code==organisation_code
                        ).join(models.OrganisationPackage
                        ).join(models.Organisation
                        ).all()
        return organisationpackages
    else:
        return False

def organisationPackageGroups(organisation_code=None):
    if organisation_code is not None:
        organisationpackagegroups = db.session.query(models.PackageGroup,
                                             models.OrganisationPackageGroup
                        ).filter(models.Organisation.organisation_code==organisation_code
                        ).join(models.OrganisationPackageGroup
                        ).join(models.Organisation
                        ).all()
        return organisationpackagegroups
    else:
        return False

def addOrganisation(data):
    checkP = models.Organisation.query.filter_by(organisation_code=data["organisation_code"]).first()
    if not checkP:
        newP = models.Organisation()
        newP.setup(
            organisation_name = data["organisation_name"],
            organisation_code = data["organisation_code"]
        )
        db.session.add(newP)
        db.session.commit()
        return newP
    else:
        return False

def updateOrganisation(organisation_code, data):
    checkP = models.Organisation.query.filter_by(organisation_code=organisation_code).first()
    if (checkP is not None):
        checkP.organisation_code = data["organisation_code"]
        checkP.organisation_name = data["organisation_code"]
        db.session.add(checkP)
        db.session.commit()
        return checkP
    else:
        return False

def addOrganisationPackage(data):
    checkPP=models.OrganisationPackage.query.filter_by(organisation_id=data['organisation_id'], package_id=data['package_id']
                ).first()
    if (checkPP is None):
        newPP = models.OrganisationPackage()
        newPP.setup(
            organisation_id = data["organisation_id"],
            package_id = data["package_id"],
            condition = data["condition"]
        )
        db.session.add(newPP)
        db.session.commit()
        return newPP
    else:
        return False

def addOrganisationPackageGroup(data):
    checkPG=models.OrganisationPackageGroup.query.filter_by(organisation_id=data['organisation_id'], packagegroup_id=data['packagegroup_id']
                ).first()
    if (checkPG is None):
        newPG = models.OrganisationPackageGroup()
        newPG.setup(
            organisation_id = data["organisation_id"],
            packagegroup_id = data["packagegroup_id"],
            condition = data["condition"]
        )
        db.session.add(newPG)
        db.session.commit()
        return newPG
    else:
        # Confirm that it's already been added
        return checkPG

def addOrganisationPackageFromPackageGroup(data):
    packages = models.Package.query.filter_by(package_group=data['packagegroup_id']
            ).all()
    count_packages = 0
    for package in packages:
        packagedata = {
            'organisation_id': data['organisation_id'],
            'package_id': package.id,
            'condition': data["condition"]
        }
        if addOrganisationPackage(packagedata):
            count_packages+=1
    if count_packages >0:
        return count_packages
    else:
        return False

def deleteOrganisationPackage(organisation_code, package_name, organisationpackage_id):
    checkPP = models.OrganisationPackage.query.filter_by(id=organisationpackage_id).first()
    if checkPP:
        db.session.delete(checkPP)
        db.session.commit()
        return checkPP
    else:
        return False

def addFeedback(data):
    checkF=models.OrganisationConditionFeedback.query.filter_by(uses=data["uses"], element=data["element"], where=data["where"]
            ).first()
    if not checkF:
        feedback = models.OrganisationConditionFeedback()
        feedback.organisation_id=data["organisation_id"]
        feedback.uses=data["uses"]
        feedback.element=data["element"]
        feedback.where=data["where"]
        db.session.add(feedback)
        db.session.commit()
        return feedback
    else:
        return False

def _organisation_detail_ungrouped(organisation):
    return db.session.query(models.Indicator,
                                     models.Test,
                                     models.AggregateResult.results_data,
                                     models.AggregateResult.results_num,
                                     models.AggregateResult.result_hierarchy,
                                     models.AggregateResult.package_id,
                                     func.max(models.AggregateResult.runtime_id)
        ).filter(models.Organisation.id==organisation.id)

def _organisation_detail(organisation):
    aggregate_results = _organisation_detail_ungrouped(organisation)\
        .group_by(models.Indicator,
                   models.AggregateResult.result_hierarchy,
                   models.Test,
                   models.AggregateResult.package_id,
                   models.AggregateResult.results_data,
                   models.AggregateResult.results_num
        ).join(models.IndicatorTest
        ).join(models.Test
        ).join(models.AggregateResult
        ).join(models.Package
        ).join(models.OrganisationPackage
        ).join(models.Organisation
        ).all()

    pconditions = models.OrganisationCondition.query.filter_by(organisation_id=organisation.id
            ).all()

    db.session.commit()
    return summary.agr_results(aggregate_results,
                                   conditions=pconditions,
                                   mode="publisher")

def _organisation_indicators(organisation, aggregation_type=2):
    aggregate_results = db.session.query(models.Indicator,
                                     models.Test,
                                     models.AggregateResult.results_data,
                                     models.AggregateResult.results_num,
                                     models.AggregateResult.result_hierarchy,
                                     models.AggregateResult.package_id,
                                     func.max(models.AggregateResult.runtime_id)
        ).filter(models.Organisation.organisation_code==organisation.organisation_code
        ).filter(models.AggregateResult.aggregateresulttype_id == aggregation_type
        ).filter(models.AggregateResult.organisation_id == organisation.id
        ).group_by(models.AggregateResult.result_hierarchy,
                   models.Test,
                   models.AggregateResult.package_id,
                   models.Indicator,
                   models.AggregateResult.results_data,
                   models.AggregateResult.results_num,
                   models.AggregateResult.package_id
        ).join(models.IndicatorTest
        ).join(models.Test
        ).join(models.AggregateResult
        ).join(models.Package
        ).join(models.OrganisationPackage
        ).join(models.Organisation
        ).all()

    pconditions = models.OrganisationCondition.query.filter_by(organisation_id=organisation.id
            ).all()

    return summary.agr_results(aggregate_results,
                                                conditions=pconditions,
                                                mode="publisher_indicators")

def _organisation_indicators_split(organisation, aggregation_type=2):
    results = _organisation_indicators(organisation, aggregation_type)

    zero = lambda kv: not kv[1]["results_pct"]
    non_zero = lambda kv: kv[1]["results_pct"]

    zero_results = dict(filter(zero, results.iteritems()))
    non_zero_results = dict(filter(non_zero, results.iteritems()))

    return { "zero": zero_results,
             "non_zero": non_zero_results }
<commit_msg>short circuit else<commit_after>
#  IATI Data Quality, tools for Data QA on IATI-formatted  publications
#  by Mark Brough, Martin Keegan, Ben Webb and Jennifer Smith
#
#  Copyright (C) 2013  Publish What You Fund
#
#  This programme is free software; you may redistribute and/or modify
#  it under the terms of the GNU Affero General Public License v3.0

from iatidq import db

from sqlalchemy import func

import summary

import models
import csv
import util
import unicodecsv

def checkCondition(row):
    if (('packagegroup_condition' in row) and (row['packagegroup_condition']!='')):
        return row['packagegroup_condition']
    else:
        return None

def importOrganisationPackagesFromFile(filename, organisation_c=None, organisation_n=None):
    with file(filename) as fh:
        return _importOrganisationPackages(organisation_c, organisation_n, fh, True)

def _importOrganisationPackages(organisation_c, organisation_n, fh, local):

        data = unicodecsv.DictReader(fh)

        for row in data:
            condition = checkCondition(row)

            if organisation_c is None:
                checkP = organisations(row['organisation_code'])
                organisation_code = row['organisation_code']
                organisation_name = row['organisation_name']
            else:
                checkP = organisations(organisation_c)
                organisation_code = organisation_c
                organisation_name = organisation_n
            if checkP:
                organisation = checkP
            else:
                organisation = addOrganisation({"organisation_name": organisation_name,
                                          "organisation_code": organisation_code
                                        })
            print organisation_code
            packages = models.Package.query.filter(models.PackageGroup.publisher_iati_id==organisation_code
                        ).join(models.PackageGroup
                        ).all()

            if not packages:
                pass
            else:
                for package in packages:
                    organisationpackage = addOrganisationPackage({
                                "organisation_id" : organisation.id,
                                "package_id" : package.id,
                                "condition": condition
                            })

            packagegroups = models.PackageGroup.query.filter(models.PackageGroup.publisher_iati_id==organisation_code
                        ).all()
            if not packagegroups:
                pass
            else:
                for packagegroup in packagegroups:
                    organisationpackagegroup = addOrganisationPackageGroup({
                                "organisation_id" : organisation.id,
                                "packagegroup_id" : packagegroup.id,
                                "condition": condition
                            })
            if (('packagegroup_name' in row) and (row['packagegroup_name'] != "")):
                packagegroup = models.PackageGroup.query.filter(models.PackageGroup.name == row['packagegroup_name']
                        ).first()
                data = {
                    'packagegroup_id': packagegroup.id,
                    'organisation_id': organisation.id,
                    'condition': condition
                }
                addOrganisationPackageFromPackageGroup(data)

        print "Imported successfully"
        return True

def organisations(organisation_code=None):
    if organisation_code is None:
        return models.Organisation.query.all()
    else:
        return models.Organisation.query.filter_by(organisation_code=organisation_code).first()

def organisationPackages(organisation_code=None):
    if organisation_code is None:
        return False
    else:
        organisationpackages = db.session.query(models.Package,
                                             models.OrganisationPackage
                        ).filter(models.Organisation.organisation_code==organisation_code
                        ).join(models.OrganisationPackage
                        ).join(models.Organisation
                        ).all()
        return organisationpackages

def organisationPackageGroups(organisation_code=None):
    if organisation_code is not None:
        organisationpackagegroups = db.session.query(models.PackageGroup,
                                             models.OrganisationPackageGroup
                        ).filter(models.Organisation.organisation_code==organisation_code
                        ).join(models.OrganisationPackageGroup
                        ).join(models.Organisation
                        ).all()
        return organisationpackagegroups
    else:
        return False

def addOrganisation(data):
    checkP = models.Organisation.query.filter_by(organisation_code=data["organisation_code"]).first()
    if not checkP:
        newP = models.Organisation()
        newP.setup(
            organisation_name = data["organisation_name"],
            organisation_code = data["organisation_code"]
        )
        db.session.add(newP)
        db.session.commit()
        return newP
    else:
        return False

def updateOrganisation(organisation_code, data):
    checkP = models.Organisation.query.filter_by(organisation_code=organisation_code).first()
    if (checkP is not None):
        checkP.organisation_code = data["organisation_code"]
        checkP.organisation_name = data["organisation_code"]
        db.session.add(checkP)
        db.session.commit()
        return checkP
    else:
        return False

def addOrganisationPackage(data):
    checkPP=models.OrganisationPackage.query.filter_by(organisation_id=data['organisation_id'], package_id=data['package_id']
                ).first()
    if (checkPP is None):
        newPP = models.OrganisationPackage()
        newPP.setup(
            organisation_id = data["organisation_id"],
            package_id = data["package_id"],
            condition = data["condition"]
        )
        db.session.add(newPP)
        db.session.commit()
        return newPP
    else:
        return False

def addOrganisationPackageGroup(data):
    checkPG=models.OrganisationPackageGroup.query.filter_by(organisation_id=data['organisation_id'], packagegroup_id=data['packagegroup_id']
                ).first()
    if (checkPG is None):
        newPG = models.OrganisationPackageGroup()
        newPG.setup(
            organisation_id = data["organisation_id"],
            packagegroup_id = data["packagegroup_id"],
            condition = data["condition"]
        )
        db.session.add(newPG)
        db.session.commit()
        return newPG
    else:
        # Confirm that it's already been added
        return checkPG

def addOrganisationPackageFromPackageGroup(data):
    packages = models.Package.query.filter_by(package_group=data['packagegroup_id']
            ).all()
    count_packages = 0
    for package in packages:
        packagedata = {
            'organisation_id': data['organisation_id'],
            'package_id': package.id,
            'condition': data["condition"]
        }
        if addOrganisationPackage(packagedata):
            count_packages+=1
    if count_packages >0:
        return count_packages
    else:
        return False

def deleteOrganisationPackage(organisation_code, package_name, organisationpackage_id):
    checkPP = models.OrganisationPackage.query.filter_by(id=organisationpackage_id).first()
    if checkPP:
        db.session.delete(checkPP)
        db.session.commit()
        return checkPP
    else:
        return False

def addFeedback(data):
    checkF=models.OrganisationConditionFeedback.query.filter_by(uses=data["uses"], element=data["element"], where=data["where"]
            ).first()
    if not checkF:
        feedback = models.OrganisationConditionFeedback()
        feedback.organisation_id=data["organisation_id"]
        feedback.uses=data["uses"]
        feedback.element=data["element"]
        feedback.where=data["where"]
        db.session.add(feedback)
        db.session.commit()
        return feedback
    else:
        return False

def _organisation_detail_ungrouped(organisation):
    return db.session.query(models.Indicator,
                                     models.Test,
                                     models.AggregateResult.results_data,
                                     models.AggregateResult.results_num,
                                     models.AggregateResult.result_hierarchy,
                                     models.AggregateResult.package_id,
                                     func.max(models.AggregateResult.runtime_id)
        ).filter(models.Organisation.id==organisation.id)

def _organisation_detail(organisation):
    aggregate_results = _organisation_detail_ungrouped(organisation)\
        .group_by(models.Indicator,
                   models.AggregateResult.result_hierarchy,
                   models.Test,
                   models.AggregateResult.package_id,
                   models.AggregateResult.results_data,
                   models.AggregateResult.results_num
        ).join(models.IndicatorTest
        ).join(models.Test
        ).join(models.AggregateResult
        ).join(models.Package
        ).join(models.OrganisationPackage
        ).join(models.Organisation
        ).all()

    pconditions = models.OrganisationCondition.query.filter_by(organisation_id=organisation.id
            ).all()

    db.session.commit()
    return summary.agr_results(aggregate_results,
                                   conditions=pconditions,
                                   mode="publisher")

def _organisation_indicators(organisation, aggregation_type=2):
    aggregate_results = db.session.query(models.Indicator,
                                     models.Test,
                                     models.AggregateResult.results_data,
                                     models.AggregateResult.results_num,
                                     models.AggregateResult.result_hierarchy,
                                     models.AggregateResult.package_id,
                                     func.max(models.AggregateResult.runtime_id)
        ).filter(models.Organisation.organisation_code==organisation.organisation_code
        ).filter(models.AggregateResult.aggregateresulttype_id == aggregation_type
        ).filter(models.AggregateResult.organisation_id == organisation.id
        ).group_by(models.AggregateResult.result_hierarchy,
                   models.Test,
                   models.AggregateResult.package_id,
                   models.Indicator,
                   models.AggregateResult.results_data,
                   models.AggregateResult.results_num,
                   models.AggregateResult.package_id
        ).join(models.IndicatorTest
        ).join(models.Test
        ).join(models.AggregateResult
        ).join(models.Package
        ).join(models.OrganisationPackage
        ).join(models.Organisation
        ).all()

    pconditions = models.OrganisationCondition.query.filter_by(organisation_id=organisation.id
            ).all()

    return summary.agr_results(aggregate_results,
                                                conditions=pconditions,
                                                mode="publisher_indicators")

def _organisation_indicators_split(organisation, aggregation_type=2):
    results = _organisation_indicators(organisation, aggregation_type)

    zero = lambda kv: not kv[1]["results_pct"]
    non_zero = lambda kv: kv[1]["results_pct"]

    zero_results = dict(filter(zero, results.iteritems()))
    non_zero_results = dict(filter(non_zero, results.iteritems()))

    return { "zero": zero_results,
             "non_zero": non_zero_results }
<|endoftext|>


<commit_before># -*- encoding: utf-8 -*-
import logging
try:
    from urllib.parse import urlencode
except ImportError:
    from urllib import urlencode
import re
import time

import requests

log = logging.getLogger('facebook')
log.setLevel(logging.WARN)

MESSAGE_URL = 'https://www.facebook.com/ajax/mercury/send_messages.php'
UPLOAD_URL = 'https://upload.facebook.com/ajax/mercury/upload.php?'

def send_group(fb, thread, body, pic=None, sticker=None):
    data = {
        "message_batch[0][action_type]": "ma-type:user-generated-message",
        "message_batch[0][author]": "fbid:{}".format(fb.user_id),
        "message_batch[0][source]": "source:chat:web",
        "message_batch[0][body]": body,
        "message_batch[0][signatureID]": "3c132b09",
        "message_batch[0][ui_push_phase]": "V3",
        "message_batch[0][status]": "0",
        "message_batch[0][thread_fbid]": thread,
        "client": "mercury",
        "__user": fb.user_id,
        "__a": "1",
        "__dyn": "7n8anEBQ9FoBUSt2u6aAix97xN6yUgByV9GiyFqzQC-C26m6oDAyoSnx2ubhHAyXBBzEy5E",
        "__req": "c",
        "fb_dtsg": fb.dtsg,
        "ttstamp": "26581691011017411284781047297",
        "__rev": "1436610",
    }

    if pic:
        # upload the picture and get picture form data
        pic_data = upload_picture(fb, pic)
        if pic_data:
            # merge together to send message with picture
            data.update(pic_data)

    # add sticker if sticker is available
    if sticker:
        data["message_batch[0][sticker_id]"] = sticker

    fb.session.post(MESSAGE_URL, data)

def send_person(fb, person, body, pic=None, sticker=None):
    data = {
        "message_batch[0][action_type]": "ma-type:user-generated-message",
        "message_batch[0][author]": "fbid:{}".format(fb.user_id),
        "message_batch[0][source]": "source:chat:web",
        "message_batch[0][body]": body,
        "message_batch[0][signatureID]": "3c132b09",
        "message_batch[0][ui_push_phase]": "V3",
        "message_batch[0][status]": "0",
        "message_batch[0][specific_to_list][0]": "fbid:{}".format(person),
        "message_batch[0][specific_to_list][1]": "fbid:{}".format(fb.user_id),
        "client": "mercury",
        "__user": fb.user_id,
        "__a": "1",
        "__dyn": "7n8anEBQ9FoBUSt2u6aAix97xN6yUgByV9GiyFqzQC-C26m6oDAyoSnx2ubhHAyXBBzEy5E",
        "__req": "c",
        "fb_dtsg": fb.dtsg,
        "ttstamp": "26581691011017411284781047297",
        "__rev": "1436610",
    }

    if pic:
        # upload the picture and get picture form data
        pic_data = upload_picture(fb, pic)
        if pic_data:
            # merge together to send message with picture
            data.update(pic_data)

    # add sticker if sticker is available
    if sticker:
        data["message_batch[0][sticker_id]"] = sticker

    fb.session.post(MESSAGE_URL, data)

def upload_picture(fb, pic):
    params = {
        "__user": fb.user_id,
        "__a": "1",
        "__dyn": "7n8anEBQ9FoBUSt2u6aAix97xN6yUgByV9GiyFqzQC-C26m6oDAyoSnx2ubhHAyXBBzEy5E",
        "__req": "c",
        "fb_dtsg": fb.dtsg,
        "ttstamp": "26581691011017411284781047297",
        "__rev": "1436610",
        'ft[tn]': '+M',
    }

    # upload the image to facebook server, filename should be unique
    res = fb.session.post(UPLOAD_URL + urlencode(params), files={
        'images_only': 'true',
        'upload_1024': (str(time.time()), requests.get(pic).content, 'image/jpeg')
    })

    # check status code
    if res.status_code != 200:
        return

    # check image_id is valid
    m = re.search(r'"image_id":(\d+),', res.text)
    if not m:
        return

    image_id = m.group(1)
    return {
        "message_batch[0][has_attachment]": "true",
        "message_batch[0][preview_attachments][0][upload_id]": "upload_1024",
        "message_batch[0][preview_attachments][0][attach_type]": "photo",
        "message_batch[0][preview_attachments][0][preview_uploading]": "true",
        "message_batch[0][preview_attachments][0][preview_width]": "540",
        "message_batch[0][preview_attachments][0][preview_height]": "720",
        "message_batch[0][image_ids][0]": image_id,
    }
<commit_msg>add like sticker<commit_after># -*- encoding: utf-8 -*-
import logging
try:
    from urllib.parse import urlencode
except ImportError:
    from urllib import urlencode
import re
import time

import requests

log = logging.getLogger('facebook')
log.setLevel(logging.WARN)

MESSAGE_URL = 'https://www.facebook.com/ajax/mercury/send_messages.php'
UPLOAD_URL = 'https://upload.facebook.com/ajax/mercury/upload.php?'

# define like sticker id
LIKE_STICKER = {
    'l': '369239383222810',
    'm': '369239343222814',
    's': '369239263222822'
}

def send_group(fb, thread, body, pic=None, sticker=None, like=None):
    data = {
        "message_batch[0][action_type]": "ma-type:user-generated-message",
        "message_batch[0][author]": "fbid:{}".format(fb.user_id),
        "message_batch[0][source]": "source:chat:web",
        "message_batch[0][body]": body,
        "message_batch[0][signatureID]": "3c132b09",
        "message_batch[0][ui_push_phase]": "V3",
        "message_batch[0][status]": "0",
        "message_batch[0][thread_fbid]": thread,
        "client": "mercury",
        "__user": fb.user_id,
        "__a": "1",
        "__dyn": "7n8anEBQ9FoBUSt2u6aAix97xN6yUgByV9GiyFqzQC-C26m6oDAyoSnx2ubhHAyXBBzEy5E",
        "__req": "c",
        "fb_dtsg": fb.dtsg,
        "ttstamp": "26581691011017411284781047297",
        "__rev": "1436610",
    }

    if pic:
        # upload the picture and get picture form data
        pic_data = upload_picture(fb, pic)
        if pic_data:
            # merge together to send message with picture
            data.update(pic_data)

    # add sticker if sticker is available
    if any([sticker, like]):
        # if like is not None, find the corresponding sticker id
        if like:
            try:
                sticker = LIKE_STICKER[like.lower()]
            except KeyError:
                # if user doesn't enter l or m or s, then use the large one
                sticker = LIKE_STICKER['l']

        data["message_batch[0][sticker_id]"] = sticker

    fb.session.post(MESSAGE_URL, data)

def send_person(fb, person, body, pic=None, sticker=None, like=None):
    data = {
        "message_batch[0][action_type]": "ma-type:user-generated-message",
        "message_batch[0][author]": "fbid:{}".format(fb.user_id),
        "message_batch[0][source]": "source:chat:web",
        "message_batch[0][body]": body,
        "message_batch[0][signatureID]": "3c132b09",
        "message_batch[0][ui_push_phase]": "V3",
        "message_batch[0][status]": "0",
        "message_batch[0][specific_to_list][0]": "fbid:{}".format(person),
        "message_batch[0][specific_to_list][1]": "fbid:{}".format(fb.user_id),
        "client": "mercury",
        "__user": fb.user_id,
        "__a": "1",
        "__dyn": "7n8anEBQ9FoBUSt2u6aAix97xN6yUgByV9GiyFqzQC-C26m6oDAyoSnx2ubhHAyXBBzEy5E",
        "__req": "c",
        "fb_dtsg": fb.dtsg,
        "ttstamp": "26581691011017411284781047297",
        "__rev": "1436610",
    }

    if pic:
        # upload the picture and get picture form data
        pic_data = upload_picture(fb, pic)
        if pic_data:
            # merge together to send message with picture
            data.update(pic_data)

    # add sticker if sticker is available
    if any([sticker, like]):
        # if like is not None, find the corresponding sticker id
        if like:
            try:
                sticker = LIKE_STICKER[like.lower()]
            except KeyError:
                # if user doesn't enter l or m or s, then use the large one
                sticker = LIKE_STICKER['l']

        data["message_batch[0][sticker_id]"] = sticker

    fb.session.post(MESSAGE_URL, data)

def upload_picture(fb, pic):
    params = {
        "__user": fb.user_id,
        "__a": "1",
        "__dyn": "7n8anEBQ9FoBUSt2u6aAix97xN6yUgByV9GiyFqzQC-C26m6oDAyoSnx2ubhHAyXBBzEy5E",
        "__req": "c",
        "fb_dtsg": fb.dtsg,
        "ttstamp": "26581691011017411284781047297",
        "__rev": "1436610",
        'ft[tn]': '+M',
    }

    # upload the image to facebook server, filename should be unique
    res = fb.session.post(UPLOAD_URL + urlencode(params), files={
        'images_only': 'true',
        'upload_1024': (str(time.time()), requests.get(pic).content, 'image/jpeg')
    })

    # check status code
    if res.status_code != 200:
        return

    # check image_id is valid
    m = re.search(r'"image_id":(\d+),', res.text)
    if not m:
        return

    image_id = m.group(1)
    return {
        "message_batch[0][has_attachment]": "true",
        "message_batch[0][preview_attachments][0][upload_id]": "upload_1024",
        "message_batch[0][preview_attachments][0][attach_type]": "photo",
        "message_batch[0][preview_attachments][0][preview_uploading]": "true",
        "message_batch[0][preview_attachments][0][preview_width]": "540",
        "message_batch[0][preview_attachments][0][preview_height]": "720",
        "message_batch[0][image_ids][0]": image_id,
    }
<|endoftext|>


<commit_before>"""
Plugins are responsible for fetching and parsing the stats from the service
being profiled.

"""
available = {
    'apache_http': 'newrelic_plugin_agent.plugins.apache_httpd.ApacheHTTPD',
    'couchdb': 'newrelic_plugin_agent.plugins.couchdb.CouchDB',
    'edgecast': 'newrelic_plugin_agent.plugins.edgecast.Edgecast',
    'memcached': 'newrelic_plugin_agent.plugins.memcached.Memcached',
    'mongodb': 'newrelic_plugin_agent.plugins.mongodb.MongoDB',
    'nginx': 'newrelic_plugin_agent.plugins.nginx.Nginx',
    'pgbouncer': 'newrelic_plugin_agent.plugins.pgbouncer.PgBouncer',
    'php_apc': 'newrelic_plugin_agent.plugins.php_apc.APC',
    'postgresql': 'newrelic_plugin_agent.plugins.postgresql.PostgreSQL',
    'rabbitmq': 'newrelic_plugin_agent.plugins.rabbitmq.RabbitMQ',
    'redis': 'newrelic_plugin_agent.plugins.redis.Redis',
    'riak': 'newrelic_plugin_agent.plugins.riak.Riak',
    'uwsgi': 'newrelic_plugin_agent.plugins.uwsgi.uWSGI'}
<commit_msg>Fix apache https entry<commit_after>"""
Plugins are responsible for fetching and parsing the stats from the service
being profiled.

"""
available = {
    'apache_httpd': 'newrelic_plugin_agent.plugins.apache_httpd.ApacheHTTPD',
    'couchdb': 'newrelic_plugin_agent.plugins.couchdb.CouchDB',
    'edgecast': 'newrelic_plugin_agent.plugins.edgecast.Edgecast',
    'memcached': 'newrelic_plugin_agent.plugins.memcached.Memcached',
    'mongodb': 'newrelic_plugin_agent.plugins.mongodb.MongoDB',
    'nginx': 'newrelic_plugin_agent.plugins.nginx.Nginx',
    'pgbouncer': 'newrelic_plugin_agent.plugins.pgbouncer.PgBouncer',
    'php_apc': 'newrelic_plugin_agent.plugins.php_apc.APC',
    'postgresql': 'newrelic_plugin_agent.plugins.postgresql.PostgreSQL',
    'rabbitmq': 'newrelic_plugin_agent.plugins.rabbitmq.RabbitMQ',
    'redis': 'newrelic_plugin_agent.plugins.redis.Redis',
    'riak': 'newrelic_plugin_agent.plugins.riak.Riak',
    'uwsgi': 'newrelic_plugin_agent.plugins.uwsgi.uWSGI'}
<|endoftext|>