<commit_before>#!/usr/bin/python

# -*- coding: utf-8 -*-

# Copyright (C) 2009-2012:
#    Gabes Jean, naparuba@gmail.com
#    Gerhard Lausser, Gerhard.Lausser@consol.de
#    Gregory Starck, g.starck@gmail.com
#    Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken 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.
#
# Shinken 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 Shinken.  If not, see <http://www.gnu.org/licenses/>.

"""
This class is for linking the WebUI with Graphite,
for mainly get graphs and links.
"""

import re
import socket
import os

from string import Template
from shinken.basemodule import BaseModule
from datetime import datetime

# print "Loaded AD module"

properties = {
    'daemons': ['webui'],
    'type': 'graphite_webui'
    }

# called by the plugin manager
def get_instance(plugin):
    print "Get an GRAPITE UI module for plugin %s" % plugin.get_name()

    instance = Graphite_Webui(plugin)
    return instance

class Graphite_Webui(BaseModule):
    def __init__(self, modconf):
        BaseModule.__init__(self, modconf)
        self.uri = getattr(modconf, 'uri', None)
        self.templates_path = getattr(modconf, 'templates_path', '/tmp')

        if not self.uri:
            raise Exception('The WebUI Graphite module is missing uri parameter.')

        self.uri = self.uri.strip()
        if not self.uri.endswith('/'):
            self.uri += '/'

        # Change YOURSERVERNAME by our server name if we got it
        if 'YOURSERVERNAME' in self.uri:
            my_name = socket.gethostname()
            self.uri = self.uri.replace('YOURSERVERNAME', my_name)

        # optional "sub-folder" in graphite to hold the data of a specific host
        self.graphite_data_source = self.illegal_char.sub('_',
                                    getattr(modconf, 'graphite_data_source', ''))

    # Try to connect if we got true parameter
    def init(self):
        pass

    # To load the webui application
    def load(self, app):
        self.app = app

    # Give the link for the GRAPHITE UI, with a Name
    def get_external_ui_link(self):
        return {'label': 'Graphite', 'uri': self.uri}

    # For a perf_data like /=30MB;4899;4568;1234;0  /var=50MB;4899;4568;1234;0 /toto=
    # return ('/', '30'), ('/var', '50')
    def get_metric_and_value(self, perf_data):
        res = []
        s = perf_data.strip()
        # Get all metrics non void
        elts = s.split(' ')
        metrics = [e for e in elts if e != '']

        for e in metrics:
            #print "Graphite: groking: ", e
            elts = e.split('=', 1)
            if len(elts) != 2:
                continue
            name = self.illegal_char.sub('_', elts[0])
            raw = elts[1]
            # get the first value of ;
            if ';' in raw:
                elts = raw.split(';')
                name_value = {name: elts[0], name + '_warn': elts[1], name + '_crit': elts[2]}
            else:
                value = raw
                name_value = {name: raw}
            # bailout if need
            if name_value[name] == '':
                continue

            # Try to get the int/float in it :)
            for key, value in name_value.items():
                m = re.search("(\d*\.*\d*)(.*)", value)
                if m:
                    name_value[key] = m.groups(0)
                else:
                    continue
#            print "graphite: got in the end:", name, value
            for key, value in name_value.items():
                res.append((key, value))
        return res

    # Private function to replace the fontsize uri parameter by the correct value
    # or add it if not present.
    def _replaceFontSize ( self, url, newsize ):

    # Do we have fontSize in the url alreadu, or not ?
        if re.search('fontSize=',url) is None:
            url = url + '&fontSize=' + newsize
        else:
            url = re.sub(r'(fontSize=)[^\&]+',r'\g<1>' + newsize , url);
        return url

    # Ask for an host or a service the graph UI that the UI should
    # give to get the graph image link and Graphite page link too.
    def get_graph_uris(self, elt, graphstart, graphend, source = 'detail'):
        # Ugly to hard-code such values. But where else should I put them ?
        fontsize={ 'detail': '8', 'dashboard': '18'}
        if not elt:
            return []

        t = elt.__class__.my_type
        r = []

        # Format the start & end time (and not only the date)
        d = datetime.fromtimestamp(graphstart)
        d = d.strftime('%H:%M_%Y%m%d')
        e = datetime.fromtimestamp(graphend)
        e = e.strftime('%H:%M_%Y%m%d')

        filename = elt.check_command.get_name().split('!')[0] + '.graph'

        # Do we have a template for the given source?
        thefile = os.path.join(self.templates_path, source, filename)

        # If not try to use the one for the parent folder
        if not os.path.isfile(thefile):
            thefile = os.path.join(self.templates_path, filename)

        if os.path.isfile(thefile):
            template_html = ''
            with open(thefile, 'r') as template_file:
                template_html += template_file.read()
            # Read the template file, as template string python object

            html = Template(template_html)
            # Build the dict to instanciate the template string
            values = {}
            if t == 'host':
                values['host'] = self.illegal_char.sub("_", elt.host_name)
                values['service'] = '__HOST__'
            if t == 'service':
                values['host'] = self.illegal_char.sub("_", elt.host.host_name)
                values['service'] = self.illegal_char.sub("_", elt.service_description)
            values['uri'] = self.uri
            # Split, we may have several images.
            for img in html.substitute(values).split('\n'):
                if not img == "":
                    v = {}
                    v['link'] = self.uri
                    v['img_src'] = img.replace('"', "'") + "&from=" + d + "&until=" + e
                    v['img_src'] = self._replaceFontSize(v['img_src'], fontsize[source])
                    r.append(v)
            # No need to continue, we have the images already.
            return r

        # If no template is present, then the usual way

        if t == 'host':
            couples = self.get_metric_and_value(elt.perf_data)

            # If no values, we can exit now
            if len(couples) == 0:
                return []

            # Remove all non alpha numeric character
            host_name = self.illegal_char.sub('_', elt.host_name)

            # Send a bulk of all metrics at once
            for (metric, _) in couples:
                uri = self.uri + 'render/?width=586&height=308&lineMode=connected&from=' + d + "&until=" + e
                if re.search(r'_warn|_crit', metric):
                    continue
                if self.graphite_data_source:
                    uri += "&target=%s.%s.__HOST__.%s" % (
                        host_name, self.graphite_data_source, metric)
                else:
                    uri += "&target=%s.__HOST__.%s" % (host_name, metric)
                v = {}
                v['link'] = self.uri
                v['img_src'] = uri
                v['img_src'] = self._replaceFontSize(v['img_src'], fontsize[source])
                r.append(v)

            return r
        if t == 'service':
            couples = self.get_metric_and_value(elt.perf_data)

            # If no values, we can exit now
            if len(couples) == 0:
                return []

            # Remove all non alpha numeric character
            desc = self.illegal_char.sub('_', elt.service_description)
            host_name = self.illegal_char.sub('_', elt.host.host_name)

            # Send a bulk of all metrics at once
            for (metric, value) in couples:
                uri = self.uri + 'render/?width=586&height=308&lineMode=connected&from=' + d + "&until=" + e
                if re.search(r'_warn|_crit', metric):
                    continue
                elif value[1] == '%':
                    uri += "&yMin=0&yMax=100"
                if self.graphite_data_source:
                    uri += "&target=%s.%s.%s.%s" % (host_name,
                                                    self.graphite_data_source,
                                                    desc, metric)
                else:
                    uri += "&target=%s.%s.%s" % (host_name, desc, metric)
                v = {}
                v['link'] = self.uri
                v['img_src'] = uri
                v['img_src'] = self._replaceFontSize(v['img_src'], fontsize[source])
                r.append(v)
            return r

        # Oups, bad type?
        return []
<commit_msg>Fix my cleanup of this file by merging the removed lines and the new code added since.<commit_after>#!/usr/bin/python

# -*- coding: utf-8 -*-

# Copyright (C) 2009-2012:
#    Gabes Jean, naparuba@gmail.com
#    Gerhard Lausser, Gerhard.Lausser@consol.de
#    Gregory Starck, g.starck@gmail.com
#    Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken 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.
#
# Shinken 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 Shinken.  If not, see <http://www.gnu.org/licenses/>.

"""
This class is for linking the WebUI with Graphite,
for mainly get graphs and links.
"""

import re
import socket
import os

from string import Template
from shinken.basemodule import BaseModule
from datetime import datetime

# print "Loaded AD module"

properties = {
    'daemons': ['webui'],
    'type': 'graphite_webui'
    }

# called by the plugin manager
def get_instance(plugin):
    print "Get an GRAPITE UI module for plugin %s" % plugin.get_name()

    instance = Graphite_Webui(plugin)
    return instance

class Graphite_Webui(BaseModule):
    def __init__(self, modconf):
        BaseModule.__init__(self, modconf)
        self.uri = getattr(modconf, 'uri', None)
        self.templates_path = getattr(modconf, 'templates_path', '/tmp')

        if not self.uri:
            raise Exception('The WebUI Graphite module is missing uri parameter.')

        self.uri = self.uri.strip()
        if not self.uri.endswith('/'):
            self.uri += '/'

        # Change YOURSERVERNAME by our server name if we got it
        if 'YOURSERVERNAME' in self.uri:
            my_name = socket.gethostname()
            self.uri = self.uri.replace('YOURSERVERNAME', my_name)

        # optional "sub-folder" in graphite to hold the data of a specific host
        self.graphite_data_source = self.illegal_char.sub('_',
                                    getattr(modconf, 'graphite_data_source', ''))

    # Try to connect if we got true parameter
    def init(self):
        pass

    # To load the webui application
    def load(self, app):
        self.app = app

    # Give the link for the GRAPHITE UI, with a Name
    def get_external_ui_link(self):
        return {'label': 'Graphite', 'uri': self.uri}

    # For a perf_data like /=30MB;4899;4568;1234;0  /var=50MB;4899;4568;1234;0 /toto=
    # return ('/', '30'), ('/var', '50')
    def get_metric_and_value(self, perf_data):
        res = []
        s = perf_data.strip()
        # Get all metrics non void
        elts = s.split(' ')
        metrics = [e for e in elts if e != '']

        for e in metrics:
            #print "Graphite: groking: ", e
            elts = e.split('=', 1)
            if len(elts) != 2:
                continue
            name = self.illegal_char.sub('_', elts[0])
            raw = elts[1]
            # get the first value of ;
            if ';' in raw:
                elts = raw.split(';')
                name_value = {name: elts[0], name + '_warn': elts[1], name + '_crit': elts[2]}
            else:
                value = raw
                name_value = {name: raw}
            # bailout if need
            if name_value[name] == '':
                continue

            # Try to get the int/float in it :)
            for key, value in name_value.items():
                m = re.search("(\d*\.*\d*)(.*)", value)
                if m:
                    name_value[key] = m.groups(0)
                else:
                    continue
#            print "graphite: got in the end:", name, value
            for key, value in name_value.items():
                res.append((key, value))
        return res

    # Private function to replace the fontsize uri parameter by the correct value
    # or add it if not present.
    def _replaceFontSize ( self, url, newsize ):

    # Do we have fontSize in the url alreadu, or not ?
        if re.search('fontSize=',url) is None:
            url = url + '&fontSize=' + newsize
        else:
            url = re.sub(r'(fontSize=)[^\&]+',r'\g<1>' + newsize , url);
        return url

    # Ask for an host or a service the graph UI that the UI should
    # give to get the graph image link and Graphite page link too.
    def get_graph_uris(self, elt, graphstart, graphend, source = 'detail'):
        # Ugly to hard-code such values. But where else should I put them ?
        fontsize={ 'detail': '8', 'dashboard': '18'}
        if not elt:
            return []

        t = elt.__class__.my_type
        r = []

        # Format the start & end time (and not only the date)
        d = datetime.fromtimestamp(graphstart)
        d = d.strftime('%H:%M_%Y%m%d')
        e = datetime.fromtimestamp(graphend)
        e = e.strftime('%H:%M_%Y%m%d')

        filename = elt.check_command.get_name().split('!')[0] + '.graph'

        # Do we have a template for the given source?
        thefile = os.path.join(self.templates_path, source, filename)

        # If not try to use the one for the parent folder
        if not os.path.isfile(thefile):
            thefile = os.path.join(self.templates_path, filename)

        if os.path.isfile(thefile):
            template_html = ''
            with open(thefile, 'r') as template_file:
                template_html += template_file.read()
            # Read the template file, as template string python object

            html = Template(template_html)
            # Build the dict to instanciate the template string
            values = {}
            if t == 'host':
                values['host'] = self.illegal_char.sub("_", elt.host_name)
                values['service'] = '__HOST__'
            if t == 'service':
                values['host'] = self.illegal_char.sub("_", elt.host.host_name)
                values['service'] = self.illegal_char.sub("_", elt.service_description)
            values['uri'] = self.uri
            # Split, we may have several images.
            for img in html.substitute(values).split('\n'):
                if not img == "":
                    v = {}
                    v['link'] = self.uri
                    v['img_src'] = img.replace('"', "'") + "&from=" + d + "&until=" + e
                    v['img_src'] = self._replaceFontSize(v['img_src'], fontsize[source])
                    r.append(v)
            # No need to continue, we have the images already.
            return r

        # If no template is present, then the usual way

        if t == 'host':
            couples = self.get_metric_and_value(elt.perf_data)

            # If no values, we can exit now
            if len(couples) == 0:
                return []

            # Remove all non alpha numeric character
            host_name = self.illegal_char.sub('_', elt.host_name)

            # Send a bulk of all metrics at once
            for (metric, _) in couples:
                uri = self.uri + 'render/?width=586&height=308&lineMode=connected&from=' + d + "&until=" + e
                if re.search(r'_warn|_crit', metric):
                    continue
                if self.graphite_data_source:
                    target = "&target=%s.%s.__HOST__.%s" % (host_name, self.graphite_data_source, metric)
                else:
                    target = "&target=%s.__HOST__.%s" % (host_name, metric)
                uri += target + target + "?????"
                v = {}
                v['link'] = self.uri
                v['img_src'] = uri
                v['img_src'] = self._replaceFontSize(v['img_src'], fontsize[source])
                r.append(v)

            return r
        if t == 'service':
            couples = self.get_metric_and_value(elt.perf_data)

            # If no values, we can exit now
            if len(couples) == 0:
                return []

            # Remove all non alpha numeric character
            desc = self.illegal_char.sub('_', elt.service_description)
            host_name = self.illegal_char.sub('_', elt.host.host_name)

            # Send a bulk of all metrics at once
            for (metric, value) in couples:
                uri = self.uri + 'render/?width=586&height=308&lineMode=connected&from=' + d + "&until=" + e
                if re.search(r'_warn|_crit', metric):
                    continue
                elif value[1] == '%':
                    uri += "&yMin=0&yMax=100"
                if self.graphite_data_source:
                    target = "&target=%s.%s.%s.%s" % (host_name,
                                                    self.graphite_data_source,
                                                    desc, metric)
                else:
                    target = "&target=%s.%s.%s" % (host_name, desc, metric)
                uri += target + target + "?????"
                v = {}
                v['link'] = self.uri
                v['img_src'] = uri
                v['img_src'] = self._replaceFontSize(v['img_src'], fontsize[source])
                r.append(v)
            return r

        # Oups, bad type?
        return []
<|endoftext|>


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

"""
    colorful
    ~~~~~~~~

    Terminal string styling done right, in Python.

    :copyright: (c) 2017 by Timo Furrer <tuxtimo@gmail.com>
    :license: MIT, see LICENSE for more details.
"""

import os

from . import ansi
from . import rgb
from . import utils
from . import terminal

# For the ANSI escape code sequences please consult
# https://en.wikipedia.org/wiki/ANSI_escape_code

#: Holds the color names mapped to RGB channels
COLOR_NAMES = rgb.parse_rgb_txt_file()

#: Holds the modifier names in the correct order
MODIFIER_NAMES = ['reset', 'bold', 'dimmed', 'italic', 'underlined',
                  'blinkslow', 'blinkrapid', 'inversed', 'concealed', 'struckthrough']

#: Holds the start index for the fore- and background colors
BACKGROUND_COLOR_OFFSET = 40
FOREGROUND_COLOR_OFFSET = 30

#: Holds the base ANSI escape code
ANSI_ESCAPE_CODE = '\033[{code}m'

class ColorfulError(Exception):
    """
    Exception which is raised for Colorful specific
    usage errors.
    """
    pass

def translate_rgb_to_ansi_code(red, green, blue, offset, colormode):
    """
    Translate the given RGB color into the appropriate ANSI escape code
    for the given color mode.
    The offset is used for the base color which is used.

    The ``colormode`` has to be one of:
        * 0: no colors / disabled
        * 8: use ANSI 8bit colors
        * 16: use ANSI 16bit colors (same as 8bit but with brightness)
        * 256: use ANSI 256 colors
        * 0xFFFFFF / 16777215: use 16 Million true colors

    :param int red: the red channel value
    :param int green: the green channel value
    :param int blue: the blue channel value
    :param int offset: the offset to use for the base color
    :param int colormode: the color mode to use. See explanation above
    """
    if colormode == terminal.NO_COLORS:  # colors are disabled, thus return empty string
        return ''

    if colormode == terminal.ANSI_8BIT_COLORS or colormode == terminal.ANSI_16BIT_COLORS:
        color_code = ansi.rgb_to_ansi16(red, green, blue)
        return ANSI_ESCAPE_CODE.format(code=color_code + offset - FOREGROUND_COLOR_OFFSET)

    if colormode == terminal.ANSI_256BIT_COLORS:
        color_code = ansi.rgb_to_ansi265(red, green, blue)
        return ANSI_ESCAPE_CODE.format(code='{base};5;{code}'.format(
            base=8 + offset, code=color_code))

    if colormode == terminal.TRUE_COLORS:
        return ANSI_ESCAPE_CODE.format(code='{base};2;{red};{green};{blue}'.format(
            base=8 + offset, red=red, green=green, blue=blue))

    raise ColorfulError('invalid color mode "{0}"'.format(colormode))

def translate_colorname_to_ansi_code(colorname, offset, colormode):
    """
    Translate the given color name to a valid
    ANSI escape code.

    :parma str colorname: the name of the color to resolve
    :parma str offset: the offset for the color code
    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``

    :returns str: the color as ANSI escape code

    :raises ColorfulError: if the given color name is invalid
    """
    try:
        red, green, blue = COLOR_NAMES[colorname]
    except KeyError:
        raise ColorfulError('the color "{0}" is unknown. Use X11 rgb.txt valid color name'.format(
            colorname))
    else:
        return translate_rgb_to_ansi_code(red, green, blue, offset, colormode)

def translate_hex_to_ansi_code(value, offset, colormode):
    """
    Translate the given RGB hex value to a valid
    ANSI escape code.

    :param str value: the RGB hex value
    :param str offset: the offset for the color code
    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``

    :returns str: the color as ANSI escape code
    """
    red, green, blue = utils.hex_to_rgb(value)
    return translate_rgb_to_ansi_code(red, green, blue, offset, colormode)

def resolve_modifier_to_ansi_code(modifiername):
    """
    Resolve the given modifier name to a valid
    ANSI escape code.

    :param str modifiername: the name of the modifier to resolve

    :returns str: the ANSI escape code for the modifier

    :raises ColorfulError: if the given modifier name is invalid
    """
    try:
        code = MODIFIER_NAMES.index(modifiername)
    except ValueError:
        raise ColorfulError('the modifier "{0}" is unknown. Use one of: {1}'.format(
            modifiername, MODIFIER_NAMES))
    else:
        return ANSI_ESCAPE_CODE.format(code=code)

def translate_style(style, colormode):
    """
    Translate the given style to an ANSI escape code
    sequence.

    ``style`` examples are:

    * green
    * bold
    * red_on_black
    * bold_green
    * italic_yellow_on_cyan

    :param str style: the style to translate
    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``
    """
    style_parts = iter(style.split('_'))

    ansi_sequence = []

    try:
        # consume all modifiers
        part = None
        for mod_part in style_parts:
            part = mod_part
            if part not in MODIFIER_NAMES:
                break  # all modifiers have been consumed

            ansi_sequence.append(resolve_modifier_to_ansi_code(part))
        else:  # we've consumed all parts, thus we can exit
            raise StopIteration()

        # next part has to be a foreground color or the 'on' keyword
        # which means we have to consume background colors
        if part != 'on':
            ansi_sequence.append(translate_colorname_to_ansi_code(
                part, FOREGROUND_COLOR_OFFSET, colormode))
            # consume the required 'on' keyword after the foreground color
            next(style_parts)

        # next part has to be the background color
        part = next(style_parts)
        ansi_sequence.append(translate_colorname_to_ansi_code(
            part, BACKGROUND_COLOR_OFFSET, colormode))
    except StopIteration:  # we've consumed all parts of the styling string
        pass

    # construct and return ANSI escape code sequence
    return ''.join(ansi_sequence)

def style_string(string, ansi_style):
    """
    Style the given string according to the given
    ANSI style string.

    :param str string: the string to style
    :param str ansi_style: the styling string returned by ``translate_style``

    :returns: a string containing proper ANSI sequence
    """
    return '{style}{string}{reset}'.format(
        style=ansi_style,
        string=string,
        reset=resolve_modifier_to_ansi_code('reset'))

class Colorful(object):
    """
    Provides methods to style strings for terminal
    output.

    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``
    """
    def __init__(self, colormode=None):
        if colormode is None:  # try to auto-detect color mode
            colormode = terminal.detect_color_support(env=os.environ)

        #: Holds the color mode to use for this Colorful object.
        self.colormode = colormode

    class ColorfulStyle(object):
        """
        Represents a colorful style
        """
        def __init__(self, style):
            self.style = style

        def __str__(self):
            return self.style

        def __call__(self, string):
            return style_string(string, self.style)

    def __getattr__(self, name):
        # translate the given name into an ANSI escape code sequence
        style = translate_style(name, self.colormode)
        style_wrapper = self.ColorfulStyle(style)
        return style_wrapper
<commit_msg>Support color mode for modifiers as well<commit_after># -*- coding: utf-8 -*-

"""
    colorful
    ~~~~~~~~

    Terminal string styling done right, in Python.

    :copyright: (c) 2017 by Timo Furrer <tuxtimo@gmail.com>
    :license: MIT, see LICENSE for more details.
"""

import os

from . import ansi
from . import rgb
from . import utils
from . import terminal

# For the ANSI escape code sequences please consult
# https://en.wikipedia.org/wiki/ANSI_escape_code

#: Holds the color names mapped to RGB channels
COLOR_NAMES = rgb.parse_rgb_txt_file()

#: Holds the modifier names in the correct order
MODIFIER_NAMES = ['reset', 'bold', 'dimmed', 'italic', 'underlined',
                  'blinkslow', 'blinkrapid', 'inversed', 'concealed', 'struckthrough']

#: Holds the start index for the fore- and background colors
BACKGROUND_COLOR_OFFSET = 40
FOREGROUND_COLOR_OFFSET = 30

#: Holds the base ANSI escape code
ANSI_ESCAPE_CODE = '\033[{code}m'

class ColorfulError(Exception):
    """
    Exception which is raised for Colorful specific
    usage errors.
    """
    pass

def translate_rgb_to_ansi_code(red, green, blue, offset, colormode):
    """
    Translate the given RGB color into the appropriate ANSI escape code
    for the given color mode.
    The offset is used for the base color which is used.

    The ``colormode`` has to be one of:
        * 0: no colors / disabled
        * 8: use ANSI 8bit colors
        * 16: use ANSI 16bit colors (same as 8bit but with brightness)
        * 256: use ANSI 256 colors
        * 0xFFFFFF / 16777215: use 16 Million true colors

    :param int red: the red channel value
    :param int green: the green channel value
    :param int blue: the blue channel value
    :param int offset: the offset to use for the base color
    :param int colormode: the color mode to use. See explanation above
    """
    if colormode == terminal.NO_COLORS:  # colors are disabled, thus return empty string
        return ''

    if colormode == terminal.ANSI_8BIT_COLORS or colormode == terminal.ANSI_16BIT_COLORS:
        color_code = ansi.rgb_to_ansi16(red, green, blue)
        return ANSI_ESCAPE_CODE.format(code=color_code + offset - FOREGROUND_COLOR_OFFSET)

    if colormode == terminal.ANSI_256_COLORS:
        color_code = ansi.rgb_to_ansi265(red, green, blue)
        return ANSI_ESCAPE_CODE.format(code='{base};5;{code}'.format(
            base=8 + offset, code=color_code))

    if colormode == terminal.TRUE_COLORS:
        return ANSI_ESCAPE_CODE.format(code='{base};2;{red};{green};{blue}'.format(
            base=8 + offset, red=red, green=green, blue=blue))

    raise ColorfulError('invalid color mode "{0}"'.format(colormode))

def translate_colorname_to_ansi_code(colorname, offset, colormode):
    """
    Translate the given color name to a valid
    ANSI escape code.

    :parma str colorname: the name of the color to resolve
    :parma str offset: the offset for the color code
    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``

    :returns str: the color as ANSI escape code

    :raises ColorfulError: if the given color name is invalid
    """
    try:
        red, green, blue = COLOR_NAMES[colorname]
    except KeyError:
        raise ColorfulError('the color "{0}" is unknown. Use X11 rgb.txt valid color name'.format(
            colorname))
    else:
        return translate_rgb_to_ansi_code(red, green, blue, offset, colormode)

def translate_hex_to_ansi_code(value, offset, colormode):
    """
    Translate the given RGB hex value to a valid
    ANSI escape code.

    :param str value: the RGB hex value
    :param str offset: the offset for the color code
    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``

    :returns str: the color as ANSI escape code
    """
    red, green, blue = utils.hex_to_rgb(value)
    return translate_rgb_to_ansi_code(red, green, blue, offset, colormode)

def resolve_modifier_to_ansi_code(modifiername, colormode):
    """
    Resolve the given modifier name to a valid
    ANSI escape code.

    :param str modifiername: the name of the modifier to resolve
    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``

    :returns str: the ANSI escape code for the modifier

    :raises ColorfulError: if the given modifier name is invalid
    """
    if colormode == terminal.NO_COLORS:  # return empty string if colors are disabled
        return ''

    try:
        code = MODIFIER_NAMES.index(modifiername)
    except ValueError:
        raise ColorfulError('the modifier "{0}" is unknown. Use one of: {1}'.format(
            modifiername, MODIFIER_NAMES))
    else:
        return ANSI_ESCAPE_CODE.format(code=code)

def translate_style(style, colormode):
    """
    Translate the given style to an ANSI escape code
    sequence.

    ``style`` examples are:

    * green
    * bold
    * red_on_black
    * bold_green
    * italic_yellow_on_cyan

    :param str style: the style to translate
    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``
    """
    style_parts = iter(style.split('_'))

    ansi_sequence = []

    try:
        # consume all modifiers
        part = None
        for mod_part in style_parts:
            part = mod_part
            if part not in MODIFIER_NAMES:
                break  # all modifiers have been consumed

            ansi_sequence.append(resolve_modifier_to_ansi_code(part, colormode))
        else:  # we've consumed all parts, thus we can exit
            raise StopIteration()

        # next part has to be a foreground color or the 'on' keyword
        # which means we have to consume background colors
        if part != 'on':
            ansi_sequence.append(translate_colorname_to_ansi_code(
                part, FOREGROUND_COLOR_OFFSET, colormode))
            # consume the required 'on' keyword after the foreground color
            next(style_parts)

        # next part has to be the background color
        part = next(style_parts)
        ansi_sequence.append(translate_colorname_to_ansi_code(
            part, BACKGROUND_COLOR_OFFSET, colormode))
    except StopIteration:  # we've consumed all parts of the styling string
        pass

    # construct and return ANSI escape code sequence
    return ''.join(ansi_sequence)

def style_string(string, ansi_style, colormode):
    """
    Style the given string according to the given
    ANSI style string.

    :param str string: the string to style
    :param str ansi_style: the styling string returned by ``translate_style``
    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``

    :returns: a string containing proper ANSI sequence
    """
    return '{style}{string}{reset}'.format(
        style=ansi_style,
        string=string,
        reset=resolve_modifier_to_ansi_code('reset', colormode))

class Colorful(object):
    """
    Provides methods to style strings for terminal
    output.

    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``
    """
    def __init__(self, colormode=None):
        if colormode is None:  # try to auto-detect color mode
            colormode = terminal.detect_color_support(env=os.environ)

        #: Holds the color mode to use for this Colorful object.
        self.colormode = colormode

    class ColorfulStyle(object):
        """
        Represents a colorful style
        """
        def __init__(self, style, colormode):
            self.style = style
            self.colormode = colormode

        def __str__(self):
            return self.style

        def __call__(self, string):
            return style_string(string, self.style, self.colormode)

    def __getattr__(self, name):
        # translate the given name into an ANSI escape code sequence
        style = translate_style(name, self.colormode)
        style_wrapper = self.ColorfulStyle(style, self.colormode)
        return style_wrapper
<|endoftext|>


<commit_before>"""

 Summary:
    Contains the HtbdyUnit class.
    This holds all of the data read in from the HTBDY units in the dat file.
    Can be called to load in the data and read and update the contents
    held in the object.

 Author:
     Duncan R.

  Created:
     01 Apr 2016

 Copyright:
     Duncan Runnacles 2016

 TODO:

 Updates:

"""
from __future__ import unicode_literals

from ship.fmp.datunits.isisunit import AUnit
from ship.fmp.datunits import ROW_DATA_TYPES as rdt
from ship.datastructures import dataobject as do
from ship.datastructures.rowdatacollection import RowDataCollection
from ship.utils import utilfunctions as uf
from ship.fmp.headdata import HeadDataItem
from ship.datastructures import DATA_TYPES as dt

import logging
logger = logging.getLogger(__name__)
"""logging references with a __name__ set to this module."""

class HtbdyUnit (AUnit):
    """Concrete implementation of AUnit storing Isis HTBDY Unit data.

    Contains a reference to a rowdatacollection for storing and
    accessing all the row data. i.e. the stage-time data for the section.
    Methods for accessing the data in these objects and adding removing rows
    are available.

    See Also:
        AUnit
    """
    UNIT_TYPE = 'htbdy'
    UNIT_CATEGORY = 'boundary_ds'
    FILE_KEY = 'HTBDY'
    FILE_KEY2 = None

    def __init__(self, **kwargs):
        """Constructor.

        Args:
            fileOrder (int): The location of this unit in the file.
        """
        super(HtbdyUnit, self).__init__(**kwargs)

        self._unit_type = HtbdyUnit.UNIT_TYPE
        self._unit_category = HtbdyUnit.UNIT_CATEGORY
        self._name = 'Htbd'

        time_units = (
            'SECONDS', 'MINUTES', 'HOURS', 'DAYS', 'WEEKS', 'FORTNIGHTS',
            'LUNAR MONTHS', 'MONTHS', 'QUARTERS', 'YEARS', 'DECADES', 'USER SET',
        )
        self.head_data = {
            'comment': HeadDataItem('', '', 0, 1, dtype=dt.STRING),
            'multiplier': HeadDataItem(1.000, '{:>10}', 0, 1, dtype=dt.FLOAT, dps=3),
            'time_units': HeadDataItem('HOURS', '{:>10}', 2, 0, dtype=dt.CONSTANT, choices=time_units),
            'extending_method': HeadDataItem('EXTEND', '{:>10}', 2, 0, dtype=dt.CONSTANT, choices=('EXTEND', 'NOEXTEND', 'REPEAT')),
            'interpolation': HeadDataItem('LINEAR', '{:>10}', 2, 0, dtype=dt.CONSTANT, choices=('LINEAR', 'SPLINE')),
        }

        dobjs = [
            do.FloatData(rdt.ELEVATION, format_str='{:>10}', no_of_dps=3),
            do.FloatData(rdt.TIME, format_str='{:>10}', no_of_dps=3, update_callback=self.checkIncreases),
        ]
        self.row_data['main'] = RowDataCollection.bulkInitCollection(dobjs)
        self.row_data['main'].setDummyRow({rdt.TIME: 0, rdt.ELEVATION: 0})

    def icLabels(self):
        return [self._name]

    def readUnitData(self, unit_data, file_line):
        """Reads the unit data into the geometry objects.

        Args:
            unit_data (list): The part of the isis dat file pertaining to
                this section

        See Also:
            AUnit - readUnitData()
        """
        file_line, rows = self._readHeadData(unit_data, file_line)
        file_line = self._readRowData(unit_data, file_line, rows)
        return file_line - 1

    def _readHeadData(self, unit_data, file_line):
        """Reads the data in the file header section into the class.

        Args:
            unit_data (list): contains data for this unit.
        """
        self.head_data['comment'].value = unit_data[file_line][5:].strip()
        self._name = unit_data[file_line + 1][:12].strip()

        l = unit_data[file_line + 2]
        rows = 1

        # The default in FMP when a new unit is created is:
        # "         1                         HOURS"
        # But this is interpretted as EXTEND, LINEAR, SECONDS??!!
        # If we find this line we need to convert to those explicitely
        if l.strip() == '1                         HOURS':
            self.head_data['time_units'].value = 'SECONDS'
            self.head_data['extending_method'].value = 'EXTEND'
            self.head_data['interpolation'].value = 'LINEAR'
        else:
            if 'LUNAR MONTHS' in l:
                l = l.replace('LUNAR MONTHS', 'LUNAR-MONTHS')
                l = ' '.join(l.split())
                vars = l.split()
                vars[1] = 'LUNAR MONTHS'
            else:
                l = ' '.join(l.split())
                vars = l.split()

            rows = int(vars[0])
            if len(vars) > 1:
                if uf.isNumeric(vars[1]):
                    self.head_data['time_units'].value = 'USER SET'
                    self.head_data['multiplier'].value = vars[1]
                else:
                    self.head_data['time_units'].value = vars[1]

                self.head_data['extending_method'].value = vars[2]
                self.head_data['interpolation'].value = vars[3]

        return file_line + 3, rows

    def _readRowData(self, unit_data, file_line, rows):
        """Reads the units rows into the row collection.

        This is all the geometry data that occurs after the no of rows variable in
        the Spill Units of the dat file.

        Args:
            unit_data: the data pertaining to this unit.
        """
        out_line = file_line + rows
        try:
            # Load the geometry data
            for i in range(file_line, out_line):
                elev = unit_data[i][0:10].strip()
                time = unit_data[i][10:20].strip()

                self.row_data['main'].addRow({
                    rdt.ELEVATION: elev, rdt.TIME: time
                }, no_copy=True)

        except NotImplementedError:
            logger.ERROR('Unable to read Unit Data(dataRowObject creation) - NotImplementedError')
            raise

        return out_line

    def getData(self):
        """Retrieve the data in this unit.

        The String[] returned is formatted for printing in the fashion
        of the .dat file.

        Returns:
            list of output data formated the same as in the .DAT file.
        """
        out_data = self._getHeadData()
        out_data.extend(self._getRowData())
        return out_data

    def _getRowData(self):
        """Get the data in the row collection.

        For all the rows in the spill geometry section get the data from
        the rowdatacollection class.

        Returns:
            list containing the formatted unit rows.
        """
        out_data = []
        for i in range(0, self.row_data['main'].numberOfRows()):
            out_data.append(self.row_data['main'].getPrintableRow(i))

        return out_data

    def _getHeadData(self):
        """Get the header data formatted for printing out.

        This is a bit messy because the formatting changes depending on what
        the setting of 'time_units' is. If it LUNAR MONTHS it takes 12 spaces,
        while all others take 10. It can also be the float value in 'multiplier'
        if 'time_units' is set to USER SET....gaaarrrggghhhhhh.

        Returns:
            list - contining the formatted head data.
        """
        out = ['HTBDY ' + self.head_data['comment'].value]
        out.append('\n' + '{:<12}'.format(self._name))
        out.append('\n' + '{:>10}'.format(self.row_data['main'].numberOfRows()))

        out.append('{:<10}'.format(''))  # There's a weired blank column

        units = self.head_data['time_units'].value
        if units == 'USER SET':
            out.append(self.head_data['multiplier'].format())
        elif units == 'LUNAR MONTHS':
            out.append('{:>12}'.format(self.head_data['time_units'].value))
        else:
            out.append(self.head_data['time_units'].format())
        out.append(self.head_data['extending_method'].format())
        out.append(self.head_data['interpolation'].format())

        final_out = ''.join(out).split('\n')
        return final_out

#     def addDataRow(elevation, time=None, index=None):
    def addRow(self, row_vals, data_key='main', index=None, **kwargs):
        """

        Args:

        Raises:
            IndexError: If the index does not exist.
            ValueError: If the given value is not accepted by the DataObjects.

        See Also:
            ADataObject and subclasses for information on the parameters.
        """
        if not rdt.ELEVATION in row_vals.keys():
            raise AttributeError('row_vals must contain an ELEVATION value')

        elevation = row_vals.get(rdt.ELEVATION, None)
        time = row_vals.get(rdt.TIME, None)

        if index is not None and index < 0:
            raise IndexError('Index value cannot be less than zero')
        if index == 1 and time is None:
            raise ValueError('Cannot determine timestep with only 1 previous value')

        # If it's the same as the record length then we can set index to None
        # type and it will be appended instead of inserted.
        num_rows = self.row_data['main'].numberOfRows()
        orig_index = index
        if index is None or index >= num_rows:
            index = num_rows

        if time is None:
            temp = index
            if index == num_rows:
                temp = index - 1
            t1 = self.row_data['main'].dataValue(rdt.TIME, temp)
            t2 = self.row_data['main'].dataValue(rdt.TIME, temp - 1)
            time = t1 + (t1 - t2)

        if orig_index is None:
            self.row_data['main'].addRow({rdt.TIME: time,
                                          rdt.ELEVATION: elevation}, **kwargs)
        else:
            self.row_data['main'].addRow({rdt.TIME: time, rdt.ELEVATION: elevation},
                                         index, **kwargs)
<commit_msg>[Bugfix] HtbdyUnit default values.<commit_after>"""

 Summary:
    Contains the HtbdyUnit class.
    This holds all of the data read in from the HTBDY units in the dat file.
    Can be called to load in the data and read and update the contents
    held in the object.

 Author:
     Duncan R.

  Created:
     01 Apr 2016

 Copyright:
     Duncan Runnacles 2016

 TODO:

 Updates:

"""
from __future__ import unicode_literals

from ship.fmp.datunits.isisunit import AUnit
from ship.fmp.datunits import ROW_DATA_TYPES as rdt
from ship.datastructures import dataobject as do
from ship.datastructures.rowdatacollection import RowDataCollection
from ship.utils import utilfunctions as uf
from ship.fmp.headdata import HeadDataItem
from ship.datastructures import DATA_TYPES as dt

import logging
logger = logging.getLogger(__name__)
"""logging references with a __name__ set to this module."""

class HtbdyUnit (AUnit):
    """Concrete implementation of AUnit storing Isis HTBDY Unit data.

    Contains a reference to a rowdatacollection for storing and
    accessing all the row data. i.e. the stage-time data for the section.
    Methods for accessing the data in these objects and adding removing rows
    are available.

    See Also:
        AUnit
    """
    UNIT_TYPE = 'htbdy'
    UNIT_CATEGORY = 'boundary_ds'
    FILE_KEY = 'HTBDY'
    FILE_KEY2 = None

    def __init__(self, **kwargs):
        """Constructor.

        Args:
            fileOrder (int): The location of this unit in the file.
        """
        super(HtbdyUnit, self).__init__(**kwargs)

        self._unit_type = HtbdyUnit.UNIT_TYPE
        self._unit_category = HtbdyUnit.UNIT_CATEGORY
        self._name = 'Htbd'

        time_units = (
            'SECONDS', 'MINUTES', 'HOURS', 'DAYS', 'WEEKS', 'FORTNIGHTS',
            'LUNAR MONTHS', 'MONTHS', 'QUARTERS', 'YEARS', 'DECADES', 'USER SET',
        )
        self.head_data = {
            'comment': HeadDataItem('', '', 0, 1, dtype=dt.STRING),
            'multiplier': HeadDataItem(1.000, '{:>10}', 0, 1, dtype=dt.FLOAT, dps=3),
            'time_units': HeadDataItem('HOURS', '{:>10}', 2, 0, dtype=dt.CONSTANT, choices=time_units),
            'extending_method': HeadDataItem('EXTEND', '{:>10}', 2, 0, dtype=dt.CONSTANT, choices=('EXTEND', 'NOEXTEND', 'REPEAT')),
            'interpolation': HeadDataItem('LINEAR', '{:>10}', 2, 0, dtype=dt.CONSTANT, choices=('LINEAR', 'SPLINE')),
        }

        dobjs = [
            do.FloatData(rdt.ELEVATION, format_str='{:>10}', no_of_dps=3),
            do.FloatData(rdt.TIME, format_str='{:>10}', no_of_dps=3, update_callback=self.checkIncreases),
        ]
        self.row_data['main'] = RowDataCollection.bulkInitCollection(dobjs)
        self.row_data['main'].setDummyRow({rdt.TIME: 0, rdt.ELEVATION: 0})

    def icLabels(self):
        return [self._name]

    def readUnitData(self, unit_data, file_line):
        """Reads the unit data into the geometry objects.

        Args:
            unit_data (list): The part of the isis dat file pertaining to
                this section

        See Also:
            AUnit - readUnitData()
        """
        file_line, rows = self._readHeadData(unit_data, file_line)
        file_line = self._readRowData(unit_data, file_line, rows)
        return file_line - 1

    def _readHeadData(self, unit_data, file_line):
        """Reads the data in the file header section into the class.

        Args:
            unit_data (list): contains data for this unit.
        """
        self.head_data['comment'].value = unit_data[file_line][5:].strip()
        self._name = unit_data[file_line + 1][:12].strip()

        l = unit_data[file_line + 2]
        rows = 1

        # The default in FMP when a new unit is created is:
        # "         1                         HOURS"
        # But this is interpretted as EXTEND, LINEAR, SECONDS??!!
        # If we find this line we need to convert to those explicitely
        if l.strip() == '1                         HOURS':
            self.head_data['time_units'].value = 'SECONDS'
            self.head_data['extending_method'].value = 'EXTEND'
            self.head_data['interpolation'].value = 'LINEAR'
        else:
            if 'LUNAR MONTHS' in l:
                l = l.replace('LUNAR MONTHS', 'LUNAR-MONTHS')
                l = ' '.join(l.split())
                vars = l.split()
                vars[1] = 'LUNAR MONTHS'
            else:
                l = ' '.join(l.split())
                vars = l.split()

            rows = int(vars[0])
            if len(vars) > 1:
                if uf.isNumeric(vars[1]):
                    self.head_data['time_units'].value = 'USER SET'
                    self.head_data['multiplier'].value = vars[1]
                else:
                    self.head_data['time_units'].value = vars[1]

                if len(vars) > 2:
                    self.head_data['extending_method'].value = vars[2]
                else:
                    self.head_data['extending_method'].value = 'EXTEND'
                if len(vars) > 3:
                    self.head_data['interpolation'].value = vars[3]
                else:
                    self.head_data['interpolation'].value = 'LINEAR'

        return file_line + 3, rows

    def _readRowData(self, unit_data, file_line, rows):
        """Reads the units rows into the row collection.

        This is all the geometry data that occurs after the no of rows variable in
        the Spill Units of the dat file.

        Args:
            unit_data: the data pertaining to this unit.
        """
        out_line = file_line + rows
        try:
            # Load the geometry data
            for i in range(file_line, out_line):
                elev = unit_data[i][0:10].strip()
                time = unit_data[i][10:20].strip()

                self.row_data['main'].addRow({
                    rdt.ELEVATION: elev, rdt.TIME: time
                }, no_copy=True)

        except NotImplementedError:
            logger.ERROR('Unable to read Unit Data(dataRowObject creation) - NotImplementedError')
            raise

        return out_line

    def getData(self):
        """Retrieve the data in this unit.

        The String[] returned is formatted for printing in the fashion
        of the .dat file.

        Returns:
            list of output data formated the same as in the .DAT file.
        """
        out_data = self._getHeadData()
        out_data.extend(self._getRowData())
        return out_data

    def _getRowData(self):
        """Get the data in the row collection.

        For all the rows in the spill geometry section get the data from
        the rowdatacollection class.

        Returns:
            list containing the formatted unit rows.
        """
        out_data = []
        for i in range(0, self.row_data['main'].numberOfRows()):
            out_data.append(self.row_data['main'].getPrintableRow(i))

        return out_data

    def _getHeadData(self):
        """Get the header data formatted for printing out.

        This is a bit messy because the formatting changes depending on what
        the setting of 'time_units' is. If it LUNAR MONTHS it takes 12 spaces,
        while all others take 10. It can also be the float value in 'multiplier'
        if 'time_units' is set to USER SET....gaaarrrggghhhhhh.

        Returns:
            list - contining the formatted head data.
        """
        out = ['HTBDY ' + self.head_data['comment'].value]
        out.append('\n' + '{:<12}'.format(self._name))
        out.append('\n' + '{:>10}'.format(self.row_data['main'].numberOfRows()))

        out.append('{:<10}'.format(''))  # There's a weired blank column

        units = self.head_data['time_units'].value
        if units == 'USER SET':
            out.append(self.head_data['multiplier'].format())
        elif units == 'LUNAR MONTHS':
            out.append('{:>12}'.format(self.head_data['time_units'].value))
        else:
            out.append(self.head_data['time_units'].format())
        out.append(self.head_data['extending_method'].format())
        out.append(self.head_data['interpolation'].format())

        final_out = ''.join(out).split('\n')
        return final_out

#     def addDataRow(elevation, time=None, index=None):
    def addRow(self, row_vals, data_key='main', index=None, **kwargs):
        """

        Args:

        Raises:
            IndexError: If the index does not exist.
            ValueError: If the given value is not accepted by the DataObjects.

        See Also:
            ADataObject and subclasses for information on the parameters.
        """
        if not rdt.ELEVATION in row_vals.keys():
            raise AttributeError('row_vals must contain an ELEVATION value')

        elevation = row_vals.get(rdt.ELEVATION, None)
        time = row_vals.get(rdt.TIME, None)

        if index is not None and index < 0:
            raise IndexError('Index value cannot be less than zero')
        if index == 1 and time is None:
            raise ValueError('Cannot determine timestep with only 1 previous value')

        # If it's the same as the record length then we can set index to None
        # type and it will be appended instead of inserted.
        num_rows = self.row_data['main'].numberOfRows()
        orig_index = index
        if index is None or index >= num_rows:
            index = num_rows

        if time is None:
            temp = index
            if index == num_rows:
                temp = index - 1
            t1 = self.row_data['main'].dataValue(rdt.TIME, temp)
            t2 = self.row_data['main'].dataValue(rdt.TIME, temp - 1)
            time = t1 + (t1 - t2)

        if orig_index is None:
            self.row_data['main'].addRow({rdt.TIME: time,
                                          rdt.ELEVATION: elevation}, **kwargs)
        else:
            self.row_data['main'].addRow({rdt.TIME: time, rdt.ELEVATION: elevation},
                                         index, **kwargs)
<|endoftext|>


<commit_before>import time, sys, os
import random
import urllib2

from subprocess import Popen, PIPE
from comodit_client.api.exceptions import PythonApiException
from comodit_client.rest.exceptions import ApiException

def create_host(env, name, platform, distribution, applications = []):
    print "Defining host %s" % name
    host = env.hosts().create(name, "", platform['name'], distribution['name'])

    # Configure platform
    context = host.get_platform()
    for key, value in platform['settings'].iteritems():
        context.settings().create(key, value)

    # Configure distribution
    context = host.get_distribution()
    for key, value in distribution['settings'].iteritems():
        context.settings().create(key, value)

    # Install applications
    for app in applications:
        host.install(app['name'], app['settings'])

    return host

def get_short_hostname(hostname):
    parts = hostname.split('.')
    return parts[0]

def get_latest_id(prefix, env):
    names = env.hosts_f
    last_id = -1
    for name in names:
        if name.startswith(prefix):
            i = int(name[len(prefix):].rstrip())
            if i > last_id:
                last_id = i
    return last_id

def exec_cmd(cmd):
    ret = {}
    proc = Popen(cmd,
                 shell=True,
                 stdout=PIPE,
                 stderr=PIPE)
    out, err = proc.communicate()
    ret['cmd'] = cmd
    ret['stdout'] = out
    ret['stderr'] = err
    ret['returncode'] = proc.returncode
    ret['pid'] = proc.pid
    return ret

def exec_cmds(commands, exit_on_fail=True):
    for cmd in commands:
        ret = exec_cmd(cmd)
        if ret["returncode"] > 0:
            print "Error executing command %s: %s", ret["cmd"], ret["stderr"]
            if exit_on_fail:
                return False
    return True

def fork_cmd(cmd):
    Popen(cmd, shell=True)

def download_iso(gpxe_url, file_name, access_key,
                 secret_key, org_name, comodit_host):
    url = gpxe_url + \
          '?access_key=%s' \
          '&secret_key=%s' \
          '&org_name=%s' \
          '&comodit_host=%s' % (access_key, secret_key, org_name, comodit_host)

    file_name += '.iso'
    u = urllib2.urlopen(url)
    f = open(file_name, 'wb')
    meta = u.info()
    file_size = int(meta.getheaders("Content-Length")[0])
    print "Downloading: %s Bytes: %s" % (file_name, file_size)

    file_size_dl = 0
    block_sz = 8192
    while True:
        buffer = u.read(block_sz)
        if not buffer:
            break

        file_size_dl += len(buffer)
        f.write(buffer)
        status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
        status = status + chr(8)*(len(status)+1)
        print status,

    f.close()
    return file_name

def randomMAC(sep=':'):
    mac = [ 0x00, 0x16, 0x3e,
        random.randint(0x00, 0x7f),
        random.randint(0x00, 0xff),
        random.randint(0x00, 0xff) ]
    return sep.join(map(lambda x: "%02x" % x, mac)).upper()

def get_combox_images_directory():
    return os.path.join(os.path.expanduser('~'), '.combox')
<commit_msg>Download gpxe iso in ~/.combox/ folder<commit_after>import time, sys, os
import random
import urllib2

from subprocess import Popen, PIPE
from comodit_client.api.exceptions import PythonApiException
from comodit_client.rest.exceptions import ApiException

def create_host(env, name, platform, distribution, applications = []):
    print "Defining host %s" % name
    host = env.hosts().create(name, "", platform['name'], distribution['name'])

    # Configure platform
    context = host.get_platform()
    for key, value in platform['settings'].iteritems():
        context.settings().create(key, value)

    # Configure distribution
    context = host.get_distribution()
    for key, value in distribution['settings'].iteritems():
        context.settings().create(key, value)

    # Install applications
    for app in applications:
        host.install(app['name'], app['settings'])

    return host

def get_short_hostname(hostname):
    parts = hostname.split('.')
    return parts[0]

def get_latest_id(prefix, env):
    names = env.hosts_f
    last_id = -1
    for name in names:
        if name.startswith(prefix):
            i = int(name[len(prefix):].rstrip())
            if i > last_id:
                last_id = i
    return last_id

def exec_cmd(cmd):
    ret = {}
    proc = Popen(cmd,
                 shell=True,
                 stdout=PIPE,
                 stderr=PIPE)
    out, err = proc.communicate()
    ret['cmd'] = cmd
    ret['stdout'] = out
    ret['stderr'] = err
    ret['returncode'] = proc.returncode
    ret['pid'] = proc.pid
    return ret

def exec_cmds(commands, exit_on_fail=True):
    for cmd in commands:
        ret = exec_cmd(cmd)
        if ret["returncode"] > 0:
            print "Error executing command %s: %s", ret["cmd"], ret["stderr"]
            if exit_on_fail:
                return False
    return True

def fork_cmd(cmd):
    Popen(cmd, shell=True)

def download_iso(gpxe_url, file_name, access_key,
                 secret_key, org_name, comodit_host):
    url = gpxe_url + \
          '?access_key=%s' \
          '&secret_key=%s' \
          '&org_name=%s' \
          '&comodit_host=%s' % (access_key, secret_key, org_name, comodit_host)

    file_name += '.iso'
    file_path = os.path.join(get_combox_images_directory(), file_name)
    u = urllib2.urlopen(url)
    f = open(file_path, 'wb')
    meta = u.info()
    file_size = int(meta.getheaders("Content-Length")[0])
    print "Downloading: %s Bytes: %s" % (file_name, file_size)

    file_size_dl = 0
    block_sz = 8192
    while True:
        buffer = u.read(block_sz)
        if not buffer:
            break

        file_size_dl += len(buffer)
        f.write(buffer)
        status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
        status = status + chr(8)*(len(status)+1)
        print status,

    f.close()
    return file_name

def randomMAC(sep=':'):
    mac = [ 0x00, 0x16, 0x3e,
        random.randint(0x00, 0x7f),
        random.randint(0x00, 0xff),
        random.randint(0x00, 0xff) ]
    return sep.join(map(lambda x: "%02x" % x, mac)).upper()

def get_combox_images_directory():
    return os.path.join(os.path.expanduser('~'), '.combox')
<|endoftext|>


<commit_before>from ..base import ShopifyResource
from .gift_card_adjustment import GiftCardAdjustment

class GiftCard(ShopifyResource):

    def disable(self):
        self._load_attributes_from_response(self.post("disable"))

    @classmethod
    def search(cls, **kwargs):
        """
        Search for gift cards matching supplied query

        Args:
           order: Field and direction to order results by (default: disabled_at DESC)
           query: Text to search for gift cards
           page: Page to show (default: 1)
           limit: Amount of results (default: 50) (maximum: 250)
           fields: comma-separated list of fields to include in the response
        Returns:
           An array of gift cards.
        """
        return cls._build_list(cls.get("search", **kwargs))

    def add_adjustment(self, adjustment):
        """
        Create a new Gift Card Adjustment
        """
        resource = self.post("adjustments", adjustment.encode())
        return GiftCardAdjustment(GiftCard.format.decode(resource.body))
<commit_msg>Corrected use of renamed function `build_collection` that was `build_list` in GiftCard class<commit_after>from ..base import ShopifyResource
from .gift_card_adjustment import GiftCardAdjustment

class GiftCard(ShopifyResource):

    def disable(self):
        self._load_attributes_from_response(self.post("disable"))

    @classmethod
    def search(cls, **kwargs):
        """
        Search for gift cards matching supplied query

        Args:
           order: Field and direction to order results by (default: disabled_at DESC)
           query: Text to search for gift cards
           page: Page to show (default: 1)
           limit: Amount of results (default: 50) (maximum: 250)
           fields: comma-separated list of fields to include in the response
        Returns:
           An array of gift cards.
        """
        return cls._build_collection(cls.get("search", **kwargs))

    def add_adjustment(self, adjustment):
        """
        Create a new Gift Card Adjustment
        """
        resource = self.post("adjustments", adjustment.encode())
        return GiftCardAdjustment(GiftCard.format.decode(resource.body))
<|endoftext|>


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

"""
This is a Python "make" script to generate different output Python scripts
based on "makefile.py" definitions for example for GPL/commercial
differences.
"""
"""n// NOTE
----------------------------------------------------------------------------
pyBuilder
Build Python code for different release targets
----------------------------------------------------------------------------
(C) direct Netware Group - All rights reserved
http://www.direct-netware.de/redirect.php?py;builder

This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------------------------
http://www.direct-netware.de/redirect.php?licenses;mpl2
----------------------------------------------------------------------------
#echo(pyBuilderVersion)#
#echo(__FILEPATH__)#
----------------------------------------------------------------------------
NOTE_END //n"""

from optparse import OptionParser
from py_builder import direct_py_builder
import os, re, sys

sys.path.append(os.getcwd())

try: import makefile
except: pass

_direct_py_builder_make = None

class direct_make(object):
#
	"""
"direct_make" is the main application object.

:author:    direct Netware Group
:copyright: direct Netware Group - All rights reserved
:package:   pyBuilder
:since:     v1.0.0
:license:   http://www.direct-netware.de/redirect.php?licenses;mpl2
            Mozilla Public License, v. 2.0
	"""

	option_parser = None
	"""
OptionParser instance
	"""

	"""
----------------------------------------------------------------------------
Construct the class
----------------------------------------------------------------------------
	"""

	def __init__(self):
	#
		"""
Constructor __init__ (direct_make)

:since: v0.1.00
		"""

		self.option_parser = OptionParser()
		self.option_parser.add_option("--define", action = "store", type = "string", dest = "define")
		self.option_parser.add_option("--filetype", action = "store", type = "string", dest = "filetype")
		self.option_parser.add_option("--include", action = "store", type = "string", dest = "include")
		self.option_parser.add_option("--output_path", action = "store", type = "string", dest = "output_path")
		self.option_parser.add_option("--exclude", action = "store", type = "string", dest = "exclude")
		self.option_parser.add_option("--exclude_dirs", action = "store", type = "string", dest = "exclude_dirs")
		self.option_parser.add_option("--exclude_files", action = "store", type = "string", dest = "exclude_files")
		self.option_parser.add_option("--strip_prefix", action = "store", type = "string", dest = "strip_prefix")
	#

	def run(self):
	#
		"""
Executes registered callbacks for the active application.

:since: v1.0.0
		"""

		( options, invalid_args ) = self.option_parser.parse_args()

		global _direct_pyBuilder_parameters
		targets = [ ]

		if (len(_direct_pyBuilder_parameters) > 0 and type(_direct_pyBuilder_parameters) == list):
		#
			for target in _direct_pyBuilder_parameters:
			#
				if ("make_output_path" in target): targets.append(target)
			#
		#
		elif (options.output_path != None):
		#
			_direct_pyBuilder_parameters.update({ "make_output_path":options.output_path })
			targets.append(_direct_pyBuilder_parameters)
		#

		if (options.filetype == None or options.include == None or len(targets) == 0): self.option_parser.print_help()
		else:
		#
			py_builder = None
			self.option_parser = None

			for target in targets:
			#
				if (options.define != None):
				#
					define_array = options.define.split(",")
					re_define = re.compile("^(\w+)\=(.+?)$")

					for define in define_array:
					#
						result_object = re_define.match(define)

						if (result_object == None):
						#
							define_key = define
							define_value = True
						#
						else:
						#
							define_key = result_object.group(1)
							define_value = result_object.group(2)
						#

						target[define_key] = define_value
					#
				#

				if (py_builder == None): py_builder = direct_py_builder(target, options.include, target['make_output_path'], options.filetype)
				else: py_builder.set_new_target(target, options.include, target['make_output_path'], options.filetype)

				if (options.exclude != None): py_builder.set_exclude(options.exclude)
				if (options.exclude_dirs != None): py_builder.set_exclude_dirs(options.exclude_dirs)
				if (options.exclude_files != None): py_builder.set_exclude_files(options.exclude_files)
				if (options.strip_prefix != None): py_builder.set_strip_prefix(options.strip_prefix)
				py_builder.make_all()
			#
		#
	#
#

print("""
----------------------------------------------------------------------------
pyBuilder #echo(pyBuilderVersion)#
(C) direct Netware Group - All rights reserved
----------------------------------------------------------------------------
""");

try:
#
	if (hasattr(makefile, "direct_makefile_py_set")): _direct_pyBuilder_parameters = makefile.direct_makefile_py_set()
	else: _direct_pyBuilder_parameters = makefile.direct_makefile_set()
#
except: _direct_pyBuilder_parameters = { }

try:
#
	_direct_py_builder_make = direct_make()
	_direct_py_builder_make.run()
#
except:
#
	sys.stderr.write("{0!r}".format(sys.exc_info()))
	sys.exit(1)
#
else: sys.exit(0)

##j## EOF<commit_msg>Added options to set the chmod for files and directories<commit_after># -*- coding: utf-8 -*-
##j## BOF

"""
This is a Python "make" script to generate different output Python scripts
based on "makefile.py" definitions for example for GPL/commercial
differences.
"""
"""n// NOTE
----------------------------------------------------------------------------
pyBuilder
Build Python code for different release targets
----------------------------------------------------------------------------
(C) direct Netware Group - All rights reserved
http://www.direct-netware.de/redirect.php?py;builder

This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------------------------
http://www.direct-netware.de/redirect.php?licenses;mpl2
----------------------------------------------------------------------------
#echo(pyBuilderVersion)#
#echo(__FILEPATH__)#
----------------------------------------------------------------------------
NOTE_END //n"""

from optparse import OptionParser
from py_builder import direct_py_builder
import os, re, sys

sys.path.append(os.getcwd())

try: import makefile
except: pass

_direct_py_builder_make = None

class direct_make(object):
#
	"""
"direct_make" is the main application object.

:author:    direct Netware Group
:copyright: direct Netware Group - All rights reserved
:package:   pyBuilder
:since:     v1.0.0
:license:   http://www.direct-netware.de/redirect.php?licenses;mpl2
            Mozilla Public License, v. 2.0
	"""

	option_parser = None
	"""
OptionParser instance
	"""

	"""
----------------------------------------------------------------------------
Construct the class
----------------------------------------------------------------------------
	"""

	def __init__(self):
	#
		"""
Constructor __init__ (direct_make)

:since: v0.1.00
		"""

		self.option_parser = OptionParser()
		self.option_parser.add_option("--define", action = "store", type = "string", dest = "define")
		self.option_parser.add_option("--filetype", action = "store", type = "string", dest = "filetype")
		self.option_parser.add_option("--include", action = "store", type = "string", dest = "include")
		self.option_parser.add_option("--output_dirs_chmod", action = "store", type = "string", dest = "output_dirs_chmod")
		self.option_parser.add_option("--output_files_chmod", action = "store", type = "string", dest = "output_files_chmod")
		self.option_parser.add_option("--output_path", action = "store", type = "string", dest = "output_path")
		self.option_parser.add_option("--exclude", action = "store", type = "string", dest = "exclude")
		self.option_parser.add_option("--exclude_dirs", action = "store", type = "string", dest = "exclude_dirs")
		self.option_parser.add_option("--exclude_files", action = "store", type = "string", dest = "exclude_files")
		self.option_parser.add_option("--strip_prefix", action = "store", type = "string", dest = "strip_prefix")
	#

	def run(self):
	#
		"""
Executes registered callbacks for the active application.

:since: v1.0.0
		"""

		( options, invalid_args ) = self.option_parser.parse_args()

		global _direct_pyBuilder_parameters
		targets = [ ]

		if (len(_direct_pyBuilder_parameters) > 0 and type(_direct_pyBuilder_parameters) == list):
		#
			for target in _direct_pyBuilder_parameters:
			#
				if ("make_output_path" in target): targets.append(target)
			#
		#
		elif (options.output_path != None):
		#
			_direct_pyBuilder_parameters.update({ "make_output_path":options.output_path })
			targets.append(_direct_pyBuilder_parameters)
		#

		if (options.filetype == None or options.include == None or len(targets) == 0): self.option_parser.print_help()
		else:
		#
			py_builder = None
			self.option_parser = None

			for target in targets:
			#
				if (options.define != None):
				#
					define_array = options.define.split(",")
					re_define = re.compile("^(\w+)\=(.+?)$")

					for define in define_array:
					#
						result_object = re_define.match(define)

						if (result_object == None):
						#
							define_key = define
							define_value = True
						#
						else:
						#
							define_key = result_object.group(1)
							define_value = result_object.group(2)
						#

						target[define_key] = define_value
					#
				#

				if (py_builder == None): py_builder = direct_py_builder(target, options.include, target['make_output_path'], options.filetype, default_chmod_files = options.output_files_chmod, default_chmod_dirs = options.output_dirs_chmod)
				else: py_builder.set_new_target(target, options.include, target['make_output_path'], options.filetype)

				if (options.exclude != None): py_builder.set_exclude(options.exclude)
				if (options.exclude_dirs != None): py_builder.set_exclude_dirs(options.exclude_dirs)
				if (options.exclude_files != None): py_builder.set_exclude_files(options.exclude_files)
				if (options.strip_prefix != None): py_builder.set_strip_prefix(options.strip_prefix)
				py_builder.make_all()
			#
		#
	#
#

print("""
----------------------------------------------------------------------------
pyBuilder #echo(pyBuilderVersion)#
(C) direct Netware Group - All rights reserved
----------------------------------------------------------------------------
""");

try:
#
	if (hasattr(makefile, "direct_makefile_py_set")): _direct_pyBuilder_parameters = makefile.direct_makefile_py_set()
	else: _direct_pyBuilder_parameters = makefile.direct_makefile_set()
#
except: _direct_pyBuilder_parameters = { }

try:
#
	_direct_py_builder_make = direct_make()
	_direct_py_builder_make.run()
#
except:
#
	sys.stderr.write("{0!r}".format(sys.exc_info()))
	sys.exit(1)
#
else: sys.exit(0)

##j## EOF<|endoftext|>


<commit_before>import random
import math

class GenDistribution(object):
    """
    A class to generate variates that satisfied specific distribution
    """
    def __init__(self):
        """
        """
        pass

    def uniform(self, a, b):
        """
        Generate random number that satisfied to uniform distribution

        Parameters:
        -----------
        a: parameters of uniform distribution (U(a, b))
        b: parameters of uniform distribution (U(a, b))

        Return:
        --------
        x: random number that satisfied to uniform distribution
        """
        return a+(b-a)*random.random()

    def gamma(self, alpha, beta):
        """
        Generate random number that satisfied to gamma distribution

        Parameters:
        -----------
        alpha: shape parameter alpha
        beta: inverse scale (rate) parameter beta (beta = 1/theta)

        Return:
        -------
        x: random number that satisfied to gamma distribution
        """
        while 1:
            if (alpha>0) & (alpha<1):
                u1 = random.random()
                b = (math.e + alpha)/math.e
                P = b*u1
                if P>1:
                    Y = -1.0*math.log((b-P)/alpha)
                    u2 = random.random()
                    if u2<= Y**(alpha-1):
                        x = Y
                        break
                    else:
                        continue
                else:
                    Y = P**(1.0/alpha)
                    u2 = random.random()
                    if u2<=math.log(math.e**(-1.0*Y)):
                        x = Y
                        break
                    else:
                        continue
            elif (alpha > 1):
                a = 1.0/math.sqrt(2*alpha-1)
                b = alpha - math.log(4)
                q = alpha + 1.0/alpha
                theta = 4.5
                d = 1 + math.log(theta)
                u1 = random.random()
                u2 = random.random()
                V = a*math.log(u1/(1-u1))
                Y = alpha*(math.e**V)
                Z = u1*u1*u2
                W = b+q*V-Y
                if W+d-theta*Z >= 0:
                    x = Y
                    break
                else:
                    if W >= math.log(Z):
                        x = Y
                        break
                    else:
                        continue
            elif alpha == 1:
                # Exponential distribution
                u = random.random()
                x = -1.0*beta*math.log(u)
                break
            else:
                raise Exception("Wrong range of alpha")
        return x

    def normal(self, mean = 0, std = 1):
        """
        Generate random number that satisfied to gamma distribution

        Parameters:
        -----------
        mean: mean
        std: standard variance

        Return:
        -------
        x1: random number that satisfied to normal distribution
        x2: random number that satisfied to normal distribution
        """
        r1 = random.random()
        r2 = random.random()
        z1 = (-2.0*math.log(r1))**(0.5)*math.cos(2*math.pi*r2)
        z2 = (-2.0*math.log(r2))**(0.5)*math.sin(2*math.pi*r2)
        x1 = mean + std*z1
        x2 = mean + std*z2
        return x1, x2

if __name__ == "__main__":
    sample_number = 10000
    gd_cls = GenDistribution()
    x1 = []
    x2 = []
    for i in range(sample_number):
    # x1~gamma(0.5, 0.5)
        x1.append(gd_cls.gamma(0.5, 0.5))
    # x2~gamma(2, 2)
        x2.append(gd_cls.gamma(2, 2))

<commit_msg>accomplish exponential distribution<commit_after>import random
import math

class GenDistribution(object):
    """
    A class to generate variates that satisfied specific distribution
    """
    def __init__(self):
        """
        """
        pass

    def uniform(self, a, b):
        """
        Generate random number that satisfied to uniform distribution

        Parameters:
        -----------
        a: parameters of uniform distribution (U(a, b))
        b: parameters of uniform distribution (U(a, b))

        Return:
        --------
        x: random number that satisfied to uniform distribution
        """
        return a+(b-a)*random.random()

    def exponential(self, beta):
        """
        Generate random number that satisfied to uniform distribution

        Parameters:
        -----------
        beta: mean

        Return:
        -------
        x: random number that satisfied to exponential distribution
        """
        return -1.0*beta*math.log(random.random())

    def gamma(self, alpha, beta):
        """
        Generate random number that satisfied to gamma distribution

        Parameters:
        -----------
        alpha: shape parameter alpha
        beta: inverse scale (rate) parameter beta (beta = 1/theta)

        Return:
        -------
        x: random number that satisfied to gamma distribution
        """
        while 1:
            if (alpha>0) & (alpha<1):
                u1 = random.random()
                b = (math.e + alpha)/math.e
                P = b*u1
                if P>1:
                    Y = -1.0*math.log((b-P)/alpha)
                    u2 = random.random()
                    if u2<= Y**(alpha-1):
                        x = Y
                        break
                    else:
                        continue
                else:
                    Y = P**(1.0/alpha)
                    u2 = random.random()
                    if u2<=math.log(math.e**(-1.0*Y)):
                        x = Y
                        break
                    else:
                        continue
            elif (alpha > 1):
                a = 1.0/math.sqrt(2*alpha-1)
                b = alpha - math.log(4)
                q = alpha + 1.0/alpha
                theta = 4.5
                d = 1 + math.log(theta)
                u1 = random.random()
                u2 = random.random()
                V = a*math.log(u1/(1-u1))
                Y = alpha*(math.e**V)
                Z = u1*u1*u2
                W = b+q*V-Y
                if W+d-theta*Z >= 0:
                    x = Y
                    break
                else:
                    if W >= math.log(Z):
                        x = Y
                        break
                    else:
                        continue
            elif alpha == 1:
                # Exponential distribution
                x = self.exponential(beta)
                break
            else:
                raise Exception("Wrong range of alpha")
        return x

    def normal(self, mean = 0, std = 1):
        """
        Generate random number that satisfied to gamma distribution

        Parameters:
        -----------
        mean: mean
        std: standard variance

        Return:
        -------
        x1: random number that satisfied to normal distribution
        x2: random number that satisfied to normal distribution
        """
        r1 = random.random()
        r2 = random.random()
        z1 = (-2.0*math.log(r1))**(0.5)*math.cos(2*math.pi*r2)
        z2 = (-2.0*math.log(r2))**(0.5)*math.sin(2*math.pi*r2)
        x1 = mean + std*z1
        x2 = mean + std*z2
        return x1, x2

if __name__ == "__main__":
    sample_number = 10000
    gd_cls = GenDistribution()
    x1 = []
    x2 = []
    for i in range(sample_number):
    # x1~gamma(0.5, 0.5)
        x1.append(gd_cls.gamma(0.5, 0.5))
    # x2~gamma(2, 2)
        x2.append(gd_cls.gamma(2, 2))

<|endoftext|>


<commit_before>from trtools.charting.charting import *
from trtools.charting.imagefile import *
from trtools.charting.styler import *
from trtools.charting.plot_3d import *
from trtools.charting.span import *
<commit_msg>moved over to ts_charting<commit_after>from ts_charting import *
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from usuario import Usuario

class Ambiente(object):

    def __init__(self, dominio, url):
        self._dominio = dominio
        self._url = dominio + url

    @property
    def url(self):
        return self._url

    @property
    def dominio(self):
        return self._dominio

class AmbienteHomologacao(Ambiente):

    def __init__(self):
        super(AmbienteHomologacao, self).__init__(
            'https://apphom.correios.com.br',
            '/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl')

        self._usuario_homologacao = Usuario('sigep', 'n5f9t8',
                                            '34028316000103',  '08082650',
                                            '9912208555', '0057018901')

    @property
    def usuario_homogolacao(self):
        return self._usuario_homologacao

class AmbienteProducao(Ambiente):

    def __init__(self):
        super(AmbienteProducao, self).__init__(
            'https://apps.correios.com.br',
            '/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl')

class FabricaAmbiente(object):

    AMBIENTE_PRODUCAO = 'Producao'
    AMBIENTE_HOMOLOGACAO = 'Homologacao'

    _ambientes = {
        AMBIENTE_PRODUCAO: AmbienteProducao,
        AMBIENTE_HOMOLOGACAO: AmbienteHomologacao,
    }

    @staticmethod
    def get_ambiente(nome_ambiente):
            return FabricaAmbiente._ambientes[nome_ambiente]()
<commit_msg>Ajustes classe Ambiente<commit_after># -*- coding: utf-8 -*-

class Ambiente(object):

    @property
    def url(self):
        pass

class AmbienteHomologacao(Ambiente):

    def __init__(self):
        super(AmbienteHomologacao, self).__init__()

    @property
    def url(self):
        return 'https://apphom.correios.com.br/SigepMasterJPA' \
               '/AtendeClienteService/AtendeCliente?wsdl'

class AmbienteProducao(Ambiente):

    def __init__(self):
        super(AmbienteProducao, self).__init__()

    @property
    def url(self):
        return 'https://apps.correios.com.br/SigepMasterJPA' \
               '/AtendeClienteService/AtendeCliente?wsdl'

class FabricaAmbiente(object):

    AMBIENTE_PRODUCAO = 'Producao'
    AMBIENTE_HOMOLOGACAO = 'Homologacao'

    _ambientes = {
        AMBIENTE_PRODUCAO: AmbienteProducao,
        AMBIENTE_HOMOLOGACAO: AmbienteHomologacao,
    }

    @staticmethod
    def get_ambiente(nome_ambiente):
            return FabricaAmbiente._ambientes[nome_ambiente]()
<|endoftext|>


<commit_before>from CommandTemplate import CommandTemplate
from IrcMessage import IrcMessage

class Command(CommandTemplate):
	triggers = ['join']
	helptext = "Makes me join another channel, if I'm allowed to at least"

	def execute(self, message):
		"""
		:type message: IrcMessage
		"""

		replytext = ""
		if message.messagePartsLength < 1:
			replytext = "Please provide a channel for me to join"
		else:
			channel = message.messageParts[0]
			if channel.replace('#', '') not in message.bot.factory.settings['allowedChannels'] and not message.bot.factory.isUserAdmin(message.user, message.userNickname, message.userAddress):
				replytext = "I'm sorry, I'm not allowed to go there. Please ask my admin(s) for permission"
			else:
				replytext = "All right, I'll go to {}. See you there!".format(channel)
				message.bot.join(channel)

		message.reply(replytext, "say")<commit_msg>[Join] Check if we're already in the channel; Improved parameter parsing<commit_after>from CommandTemplate import CommandTemplate
from IrcMessage import IrcMessage

class Command(CommandTemplate):
	triggers = ['join']
	helptext = "Makes me join another channel, if I'm allowed to at least"

	def execute(self, message):
		"""
		:type message: IrcMessage
		"""

		replytext = ""
		if message.messagePartsLength < 1:
			replytext = "Please provide a channel for me to join"
		else:
			channel = message.messageParts[0].lower()
			if channel.startswith('#'):
				channel = channel.lstrip('#')

			if '#' + channel in message.bot.channelsUserList:
				replytext = "I'm already there, waiting for you. You're welcome!"
			elif channel not in message.bot.factory.settings['allowedChannels'] and not message.bot.factory.isUserAdmin(message.user, message.userNickname, message.userAddress):
				replytext = "I'm sorry, I'm not allowed to go there. Please ask my admin(s) for permission"
			else:
				replytext = "All right, I'll go to #{}. See you there!".format(channel)
				message.bot.join(channel)

		message.reply(replytext, "say")
<|endoftext|>


<commit_before>from google.transit import gtfs_realtime_pb2
import urllib
import datetime
import zipfile
import StringIO
import csv
import sqlite3

class GTFS_Ingestor:
    _endpoint_url = 'http://datamine.mta.info/mta_esi.php'
    _static_data_url = 'http://web.mta.info/developers/data/nyct/subway/google_transit.zip'
    _sqlite_db = 'subway_status.db'

    def __init__(self, key_str, regen_stops = False, regen_trips = False):
        self._key_str = key_str
        if regen_stops:
            self.initialize_stops_table()
        if regen_trips:
            self.initialize_trip_updates_table()

    def initialize_feed(self, feed_id = 2):
        self._load_feed(feed_id)
        self._split_feed()

    def _load_feed(self, feed_id_int):
        payload  = urllib.urlencode({'key': self._key_str, 'feed_id': feed_id_int})
        response = urllib.urlopen('{}?{}'.format(self._endpoint_url, payload))
        self._feed = gtfs_realtime_pb2.FeedMessage()
        self._feed.ParseFromString(response.read())

    def _split_feed(self):
        self._trip_updates = [tu for tu in self._feed.entity if tu.HasField('trip_update')]
        self._vehicles = [tu for tu in self._feed.entity if tu.HasField('vehicle')]
        self._header = self._feed.header

    def _drop_table(self, table_name):
        connection = sqlite3.connect(self._sqlite_db)
        cursor = connection.cursor()
        sql_command = 'DROP TABLE {};'.format(table_name)
        cursor.execute(sql_command)
        connection.commit()
        connection.close()

    def initialize_stops_table(self):
        try:
            self._drop_table('stops')
        except:
            pass
        self._create_stops_table()
        self._populate_stops_table()

    def _create_stops_table(self):
        connection = sqlite3.connect(self._sqlite_db)
        cursor = connection.cursor()
        sql_command = """
        CREATE TABLE stops (
        stop_id TEXT NOT NULL,
        stop_code TEXT,
        stop_name TEXT NOT NULL,
        stop_desc TEXT,
        stop_lat REAL,
        stop_lon REAL,
        zone_id TEXT,
        stop_url TEXT,
        location_type TEXT,
        parent_station TEXT,
        update_ts TEXT NOT NULL);"""
        cursor.execute(sql_command)
        connection.commit()
        connection.close()

    def _populate_stops_table(self):
        url = urllib.urlopen(self._static_data_url)
        f = StringIO.StringIO(url.read())
        reader = csv.DictReader(zipfile.ZipFile(f).open('stops.txt'))
        connection = sqlite3.connect(self._sqlite_db)
        cursor = connection.cursor()
        update_time = datetime.datetime.now()

        def wrap_text(s, q = "'"): return '{}{}{}'.format(q,s,q) if s else 'NULL'

        for row in reader:
            sql_command = """INSERT INTO stops (
            stop_id,
            stop_code,
            stop_name,
            stop_desc,
            stop_lat,
            stop_lon,
            zone_id,
            stop_url,
            location_type,
            parent_station,
            update_ts
            ) VALUES ({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {});""".format(
                wrap_text(row['stop_id']),
                wrap_text(row['stop_code']),
                wrap_text(row['stop_name'], q = '"'),
                wrap_text(row['stop_desc']),
                wrap_text(row['stop_lat'], q = ''),
                wrap_text(row['stop_lon'], q = ''),
                wrap_text(row['zone_id']),
                wrap_text(row['stop_url']),
                wrap_text(row['location_type']),
                wrap_text(row['parent_station']),
                wrap_text(update_time)
            )
            cursor.execute(sql_command)
        connection.commit()
        connection.close()

    def initialize_trip_updates_table(self):
        try:
            self._drop_table('trip_updates')
        except:
            pass
        self._create_trip_updates_table()
        self._populate_trip_updates_table()

    def _create_trip_updates_table(self):
        connection = sqlite3.connect(self._sqlite_db)
        cursor = connection.cursor()
        sql_command = """
        CREATE TABLE trip_updates (
        entity_id INTEGER NOT NULL,
        trip_id TEXT NOT NULL,
        trip_start_date TEXT NOT NULL,
        route_id TEXT NOT NULL,
        stop_id TEXT NOT NULL,
        direction_id TEXT NOT NULL,
        schedule_relationship INTEGER,
        arrival_time INTEGER,
        departure_time INTEGER,
        load_ts TEXT NOT NULL,
        update_ts TEXT NOT NULL);"""
        cursor.execute(sql_command)
        connection.commit()
        connection.close()

    def _populate_trip_updates_table(self):
        self.initialize_feed()
        connection = sqlite3.connect(self._sqlite_db)
        cursor = connection.cursor()
        update_time = datetime.datetime.now()

        def wrap_text(s, q = "'"): return '{}{}{}'.format(q,s,q) if s else 'NULL'

        for entity in self._trip_updates:
            for stu in entity.trip_update.stop_time_update:
                sql_command = """INSERT INTO trip_updates (
                entity_id,
                trip_id,
                trip_start_date,
                route_id,
                stop_id,
                direction_id,
                schedule_relationship,
                arrival_time,
                departure_time,
                load_ts,
                update_ts
                ) VALUES ({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {});""".format(
                    int(entity.id),
                    wrap_text(entity.trip_update.trip.trip_id),
                    wrap_text(datetime.datetime.strptime(entity.trip_update.trip.start_date,'%Y%m%d')),
                    wrap_text(entity.trip_update.trip.route_id),
                    wrap_text(stu.stop_id),
                    wrap_text(stu.stop_id[-1]),
                    wrap_text(stu.schedule_relationship, q = ''),
                    wrap_text(stu.arrival.time, q = ''),
                    wrap_text(stu.departure.time, q = ''),
                    wrap_text(self._header.timestamp, q = ''),
                    wrap_text(update_time)
                )
                cursor.execute(sql_command)

        connection.commit()
        connection.close()

    def update_trip_updates_table(self, replace = False):
        if replace:
            self.initialize_trip_updates_table()
        else:
            self._populate_trip_updates_table()
<commit_msg>fixed schedule relationship<commit_after>from google.transit import gtfs_realtime_pb2
import urllib
import datetime
import zipfile
import StringIO
import csv
import sqlite3

class GTFS_Ingestor:
    _endpoint_url = 'http://datamine.mta.info/mta_esi.php'
    _static_data_url = 'http://web.mta.info/developers/data/nyct/subway/google_transit.zip'
    _sqlite_db = 'subway_status.db'

    def __init__(self, key_str, regen_stops = False, regen_trips = False):
        self._key_str = key_str
        if regen_stops:
            self.initialize_stops_table()
        if regen_trips:
            self.initialize_trip_updates_table()

    def initialize_feed(self, feed_id = 2):
        self._load_feed(feed_id)
        self._split_feed()

    def _load_feed(self, feed_id_int):
        payload  = urllib.urlencode({'key': self._key_str, 'feed_id': feed_id_int})
        response = urllib.urlopen('{}?{}'.format(self._endpoint_url, payload))
        self._feed = gtfs_realtime_pb2.FeedMessage()
        self._feed.ParseFromString(response.read())

    def _split_feed(self):
        self._trip_updates = [tu for tu in self._feed.entity if tu.HasField('trip_update')]
        self._vehicles = [tu for tu in self._feed.entity if tu.HasField('vehicle')]
        self._header = self._feed.header

    def _drop_table(self, table_name):
        connection = sqlite3.connect(self._sqlite_db)
        cursor = connection.cursor()
        sql_command = 'DROP TABLE {};'.format(table_name)
        cursor.execute(sql_command)
        connection.commit()
        connection.close()

    def initialize_stops_table(self):
        try:
            self._drop_table('stops')
        except:
            pass
        self._create_stops_table()
        self._populate_stops_table()

    def _create_stops_table(self):
        connection = sqlite3.connect(self._sqlite_db)
        cursor = connection.cursor()
        sql_command = """
        CREATE TABLE stops (
        stop_id TEXT NOT NULL,
        stop_code TEXT,
        stop_name TEXT NOT NULL,
        stop_desc TEXT,
        stop_lat REAL,
        stop_lon REAL,
        zone_id TEXT,
        stop_url TEXT,
        location_type TEXT,
        parent_station TEXT,
        update_ts TEXT NOT NULL);"""
        cursor.execute(sql_command)
        connection.commit()
        connection.close()

    def _populate_stops_table(self):
        url = urllib.urlopen(self._static_data_url)
        f = StringIO.StringIO(url.read())
        reader = csv.DictReader(zipfile.ZipFile(f).open('stops.txt'))
        connection = sqlite3.connect(self._sqlite_db)
        cursor = connection.cursor()
        update_time = datetime.datetime.now()

        def wrap_text(s, q = "'"): return '{}{}{}'.format(q,s,q) if s else 'NULL'

        for row in reader:
            sql_command = """INSERT INTO stops (
            stop_id,
            stop_code,
            stop_name,
            stop_desc,
            stop_lat,
            stop_lon,
            zone_id,
            stop_url,
            location_type,
            parent_station,
            update_ts
            ) VALUES ({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {});""".format(
                wrap_text(row['stop_id']),
                wrap_text(row['stop_code']),
                wrap_text(row['stop_name'], q = '"'),
                wrap_text(row['stop_desc']),
                wrap_text(row['stop_lat'], q = ''),
                wrap_text(row['stop_lon'], q = ''),
                wrap_text(row['zone_id']),
                wrap_text(row['stop_url']),
                wrap_text(row['location_type']),
                wrap_text(row['parent_station']),
                wrap_text(update_time)
            )
            cursor.execute(sql_command)
        connection.commit()
        connection.close()

    def initialize_trip_updates_table(self):
        try:
            self._drop_table('trip_updates')
        except:
            pass
        self._create_trip_updates_table()
        self._populate_trip_updates_table()

    def _create_trip_updates_table(self):
        connection = sqlite3.connect(self._sqlite_db)
        cursor = connection.cursor()
        sql_command = """
        CREATE TABLE trip_updates (
        entity_id INTEGER NOT NULL,
        trip_id TEXT NOT NULL,
        trip_start_date TEXT NOT NULL,
        route_id TEXT NOT NULL,
        stop_id TEXT NOT NULL,
        direction_id TEXT NOT NULL,
        schedule_relationship INTEGER NOT NULL,
        arrival_time INTEGER,
        departure_time INTEGER,
        load_ts TEXT NOT NULL,
        update_ts TEXT NOT NULL);"""
        cursor.execute(sql_command)
        connection.commit()
        connection.close()

    def _populate_trip_updates_table(self):
        self.initialize_feed()
        connection = sqlite3.connect(self._sqlite_db)
        cursor = connection.cursor()
        update_time = datetime.datetime.now()

        def wrap_text(s, q = "'"): return '{}{}{}'.format(q,s,q) if s else 'NULL'

        for entity in self._trip_updates:
            for stu in entity.trip_update.stop_time_update:
                sql_command = """INSERT INTO trip_updates (
                entity_id,
                trip_id,
                trip_start_date,
                route_id,
                stop_id,
                direction_id,
                schedule_relationship,
                arrival_time,
                departure_time,
                load_ts,
                update_ts
                ) VALUES ({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {});""".format(
                    int(entity.id),
                    wrap_text(entity.trip_update.trip.trip_id),
                    wrap_text(datetime.datetime.strptime(entity.trip_update.trip.start_date,'%Y%m%d')),
                    wrap_text(entity.trip_update.trip.route_id),
                    wrap_text(stu.stop_id),
                    wrap_text(stu.stop_id[-1]),
                    stu.schedule_relationship,
                    wrap_text(stu.arrival.time, q = ''),
                    wrap_text(stu.departure.time, q = ''),
                    wrap_text(self._header.timestamp, q = ''),
                    wrap_text(update_time)
                )
                cursor.execute(sql_command)

        connection.commit()
        connection.close()

    def update_trip_updates_table(self, replace = False):
        if replace:
            self.initialize_trip_updates_table()
        else:
            self._populate_trip_updates_table()
<|endoftext|>