<commit_before>import os
import sys
import struct

from ._compat import raw_input, PY2, text_type, string_types, \
     get_best_encoding, colorama, isatty, strip_ansi
from .utils import echo
from .exceptions import Abort, UsageError
from .types import convert_type

# The prompt functions to use.  The doc tools currently override these
# functions to customize how they work.
visible_prompt_func = raw_input

_ansi_colors = ('black', 'red', 'green', 'yellow', 'blue', 'magenta',
                'cyan', 'white', 'reset')
_ansi_reset_all = '\033[0m'

def hidden_prompt_func(prompt):
    import getpass
    return getpass.getpass(prompt)

def _build_prompt(text, suffix, show_default=False, default=None):
    prompt = text
    if default is not None and show_default:
        prompt = '%s [%s]' % (prompt, default)
    return prompt + suffix

def prompt(text, default=None, hide_input=False,
           confirmation_prompt=False, type=None,
           value_proc=None, prompt_suffix=': ', show_default=True):
    """Prompts a user for input.  This is a convenience function that can
    be used to prompt a user for input later.

    If the user aborts the input by sending a interrupt signal this
    function will catch it and raise a :exc:`Abort` exception.

    :param text: the text to show for the prompt.
    :param default: the default value to use if no input happens.  If this
                    is not given it will prompt until it's aborted.
    :param hide_input: if this is set to true then the input value will
                       be hidden.
    :param confirmation_prompt: asks for confirmation for the value.
    :param type: the type to use to check the value against.
    :param value_proc: if this parameter is provided it's a function that
                       is invoked instead of the type conversion to
                       convert a value.
    :param prompt_suffix: a suffix that should be added to the prompt.
    :param show_default: shows or hides the default value in the prompt.
    """
    result = None

    def prompt_func(text):
        f = hide_input and hidden_prompt_func or visible_prompt_func
        try:
            return f(text)
        except (KeyboardInterrupt, EOFError):
            raise Abort()

    if value_proc is None:
        value_proc = convert_type(type, default)

    prompt = _build_prompt(text, prompt_suffix, show_default, default)

    while 1:
        while 1:
            value = prompt_func(prompt)
            if value:
                break
            # If a default is set and used, then the confirmation
            # prompt is always skipped because that's the only thing
            # that really makes sense.
            elif default is not None:
                return default
        try:
            result = value_proc(value)
        except UsageError as e:
            echo('Error: %s' % e.message)
            continue
        if not confirmation_prompt:
            return result
        while 1:
            value2 = prompt_func('Repeat for confirmation: ')
            if value2:
                break
        if value == value2:
            return result
        echo('Error: the two entered values do not match')

def confirm(text, default=False, abort=False, prompt_suffix=': ',
            show_default=True):
    """Prompts for confirmation (yes/no question).

    If the user aborts the input by sending a interrupt signal this
    function will catch it and raise a :exc:`Abort` exception.

    :param text: the question to ask.
    :param default: the default for the prompt.
    :param abort: if this is set to `True` a negative answer aborts the
                  exception by raising :exc:`Abort`.
    :param prompt_suffix: a suffix that should be added to the prompt.
    :param show_default: shows or hides the default value in the prompt.
    """
    prompt = _build_prompt(text, prompt_suffix, show_default,
                           default and 'Y/n' or 'y/N')
    while 1:
        try:
            value = visible_prompt_func(prompt).lower().strip()
        except (KeyboardInterrupt, EOFError):
            raise Abort()
        if value in ('y', 'yes'):
            rv = True
        elif value in ('n', 'no'):
            rv = False
        elif value == '':
            rv = default
        else:
            echo('Error: invalid input')
            continue
        break
    if abort and not rv:
        raise Abort()
    return rv

def get_terminal_size():
    """Returns the current size of the terminal as tuple in the form
    ``(width, height)`` in columns and rows.
    """
    # If shutil has get_terminal_size() (Python 3.3 and later) use that
    if sys.version_info >= (3, 3):
        import shutil
        shutil_get_terminal_size = getattr(shutil, 'get_terminal_size', None)
        if shutil_get_terminal_size:
            sz = shutil_get_terminal_size()
            return sz.columns, sz.lines

    def ioctl_gwinsz(fd):
        try:
            import fcntl
            import termios
            cr = struct.unpack(
                'hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
        except Exception:
            return
        return cr

    cr = ioctl_gwinsz(0) or ioctl_gwinsz(1) or ioctl_gwinsz(2)
    if not cr:
        try:
            fd = os.open(os.ctermid(), os.O_RDONLY)
            try:
                cr = ioctl_gwinsz(fd)
            finally:
                os.close(fd)
        except Exception:
            pass
    if not cr or not cr[0] or not cr[1]:
        cr = (os.environ.get('LINES', 25),
              os.environ.get('COLUMNS', 80))
    return int(cr[1]), int(cr[0])

def echo_via_pager(text):
    """This function takes a text and shows it via an environment specific
    pager on stdout.

    :param text: the text to page.
    """
    if not isinstance(text, string_types):
        text = text_type(text)

    if PY2 and isinstance(text, text_type):
        encoding = get_best_encoding(sys.stdout)
        text = text.encode(encoding, 'replace')

    from ._termui_impl import pager
    return pager(text + '\n')

def progressbar(iterable=None, length=None, label=None, show_eta=True,
                show_percent=None, show_pos=False,
                item_show_func=None, fill_char='#', empty_char='-',
                bar_template='%(label)s  [%(bar)s]  %(info)s',
                info_sep='  ', width=36, file=None):
    """This function creates an iterable context manager that can be used
    to iterate over something while showing a progress bar.  It will
    either iterate over the `iterable` or `length` items (that are counted
    up).  While iteration happens this function will print a rendered
    progress bar to the given `file` (defaults to stdout) and will attempt
    to calculate remaining time and more.  By default this progress bar
    will not be rendered if the file is not a terminal.

    The context manager creates the progress bar.  When the context
    manager is entered the progress bar is already displayed.  With every
    iteration over the progress bar the iterable passed to the bar is
    advanced and the bar is updated.  When the context manager exits
    a newline is printed and the progress bar is finalized on screen.

    No printing must happen or the progress bar will be unintentionally
    destroyed.

    Example usage::

        with progressbar(items) as bar:
            for item in bar:
                do_something_with(item)

    .. versionadded:: 2.0

    :param iterable: an iterable to iterate over.  If not provided the length
                     is required.
    :param length: the number of items to iterate over.  By default the
                   progressbar will attempt to ask the iterator about its
                   length, which might or might not work.  If an iterable is
                   also provided this parameter can be used to override the
                   length.  If an iterable is not provided the progress bar
                   will iterate over a range of that length.
    :param label: the label to show next to the progress bar.
    :param show_eta: enables or disables the estimated time display.  This is
                     automatically disabled if the length cannot be
                     determined.
    :param show_percent: enables or disables the percentage display.  The
                         default is `True` if the iterable has a length or
                         `False` if not.
    :param show_pos: enables or disables the absolute position display.  The
                     default is `False`.
    :param item_show_func: a function called with the current item which
                           can return a string to show the current item
                           next to the progress bar.  Note that the current
                           item can be `None`!
    :param fill_char: the character to use to show the filled part of the
                      progress bar.
    :param empty_char: the character to use to show the non-filled part of
                       the progress bar.
    :param bar_template: the format string to use as template for the bar.
                         The parameters in it are ``label`` for the label,
                         ``bar`` for the progress bar and ``info`` for the
                         info section.
    :param info_sep: the separator between multiple info items (eta etc.)
    :param width: the width of the progress bar in characters.
    :param file: the file to write to.  If this is not a terminal then
                 only the label is printed.
    """
    from ._termui_impl import ProgressBar
    return ProgressBar(iterable=iterable, length=length, show_eta=show_eta,
                       show_percent=show_percent, show_pos=show_pos,
                       item_show_func=item_show_func, fill_char=fill_char,
                       empty_char=empty_char, bar_template=bar_template,
                       info_sep=info_sep, file=file, label=label,
                       width=width)

def clear():
    """Clears the terminal screen.  This will have the effect of clearing
    the whole visible space of the terminal and moving the cursor to the
    top left.  This does not do anything if not connected to a terminal.

    .. versionadded:: 2.0
    """
    if not isatty(sys.stdout):
        return
    # If we're on windows and we don't have colorama available, then we
    # clear the screen by shelling out.  Otherwise we can use an escape
    # sequence.
    if sys.platform.startswith('win') and colorama is None:
        os.system('cls')
    else:
        sys.stdout.write('\033[2J\033[1;1H')

def style(text, fg=None, bg=None, bold=None, dim=None, underline=None,
          blink=None, reverse=None, reset=True):
    """Styles a text with ANSI styles and returns the new string.  By
    default the styling is self contained which means that at the end
    of the string a reset code is issued.  This can be prevented by
    passing ``reset=False``.

    Examples::

        click.echo(click.style('Hello World!', fg='green'))
        click.echo(click.style('ATTENTION!', blink=True))
        click.echo(click.style('Some things', reverse=True, fg='cyan'))

    Supported color names:

    * ``black`` (might be a gray)
    * ``red``
    * ``green``
    * ``yellow`` (might be an orange)
    * ``blue``
    * ``magenta``
    * ``cyan``
    * ``white`` (might be light gray)
    * ``reset`` (reset the color code only)

    .. versionadded:: 2.0

    :param text: the string to style with ansi codes.
    :param fg: if provided this will become the foreground color.
    :param bg: if provided this will become the background color.
    :param bold: if provided this will enable or disable bold mode.
    :param dim: if provided this will enable or disable dim mode.  This is
                badly supported.
    :param underline: if provided this will enable or disable underline.
    :param blink: if provided this will enable or disable blinking.
    :param reverse: if provided this will enable or disable inverse
                    rendering (foreground becomes background and the
                    other way round).
    :param reset: by default a reset-all code is added at the end of the
                  string which means that styles do not carry over.  This
                  can be disabled to compose styles.
    """
    bits = []
    if fg:
        try:
            bits.append('\033[%dm' % (_ansi_colors.index(fg) + 30))
        except ValueError:
            raise TypeError('Unknown color %r' % fg)
    if bg:
        try:
            bits.append('\033[%dm' % (_ansi_colors.index(bg) + 40))
        except ValueError:
            raise TypeError('Unknown color %r' % bg)
    if bold is not None:
        bits.append('\033[%dm' % (1 if bold else 22))
    if dim is not None:
        bits.append('\033[%dm' % (2 if dim else 22))
    if underline is not None:
        bits.append('\033[%dm' % (4 if underline else 24))
    if blink is not None:
        bits.append('\033[%dm' % (5 if blink else 25))
    if reverse is not None:
        bits.append('\033[%dm' % (7 if reverse else 27))
    bits.append(text)
    if reset:
        bits.append(_ansi_reset_all)
    return ''.join(bits)

def unstyle(text):
    """Removes ANSI styling information from a string.  Usually it's not
    necessary to use this function as click's echo function will
    automatically remove styling if necessary.

    .. versionadded:: 2.0

    :param text: the text to remove style information from.
    """
    return strip_ansi(text)

def secho(text, file=None, nl=True, **styles):
    """This function combines :func:`echo` and :func:`style` into one
    call.  As such the following two calls are the same::

        click.secho('Hello World!', fg='green')
        click.echo(click.style('Hello World!', fg='green'))

    All keyword arguments are forwarded to the underlying functions
    depending on which one they go with.

    .. versionadded:: 2.0
    """
    text = style(text, **styles)
    return echo(text, file=file, nl=nl)

def edit(text=None, editor=None, env=None, require_save=True,
         extension='.txt', filename=None):
    r"""Edits the given text in the defined editor.  If an editor is given
    (should be the full path to the executable but the regular operating
    system search path is used for finding the executable) it overrides
    the detected editor.  Optionally some environment variables can be
    used.  If the editor is closed without changes `None` is returned.  In
    case a file is edited directly the return value is always `None` and
    `require_save` and `extension` are ignored.

    If the editor cannot be opened a :exc:`UsageError` is raised.

    Note for Windows: to simplify cross platform usage the newlines are
    automatically converted from posix to windows and reverse.  As such the
    message here will have ``\n`` as newline markers.

    :param text: the text to edit.
    :param editor: optionally the editor to use.  Defaults to automatic
                   detection.
    :param env: environment variables to forward to the editor.
    :param require_save: if this is true, then not saving in the editor
                         will make the return value become `None`.
    :param extension: the extension to tell the editor about.  This defaults
                      to utf-8 but changing this might change syntax
                      highlighting.
    :param filename: if provided it will edit this file instead of the
                     provided text contents.  It will not use a temporary
                     file as an indirection in that case.
    """
    from ._termui_impl import Editor
    editor = Editor(editor=editor, env=env, require_save=require_save,
                    extension=extension)
    if filename is None:
        return editor.edit(text)
    editor.edit_file(filename)

def launch(url, wait=False, locate=False):
    """This function launches the given URL (or filename) in the default
    viewer application for this file type.  If this is an executable it
    might launch the executable in a new session.  The return value is
    the exit code of the launched application.  Usually ``0`` indicates
    success.

    Examples::

        click.open('http://click.pocoo.org/')
        click.open('/my/downloaded/file', locate=True)

    .. versionadded:: 2.0

    :param url: url or filename of the thing to launch.
    :param wait: waits for the program to stop.
    :param locate: if this is set to `True` then instead of launching the
                   application associated with the URL it will attempt to
                   launch a file manager with the file located.  This
                   might have weird effects if the url does not point to
                   the filesystem.
    """
    from _termui_impl import open_url
    return open_url(url, wait=wait, locate=locate)
<commit_msg>Docs: fixed mention to default extension<commit_after>import os
import sys
import struct

from ._compat import raw_input, PY2, text_type, string_types, \
     get_best_encoding, colorama, isatty, strip_ansi
from .utils import echo
from .exceptions import Abort, UsageError
from .types import convert_type

# The prompt functions to use.  The doc tools currently override these
# functions to customize how they work.
visible_prompt_func = raw_input

_ansi_colors = ('black', 'red', 'green', 'yellow', 'blue', 'magenta',
                'cyan', 'white', 'reset')
_ansi_reset_all = '\033[0m'

def hidden_prompt_func(prompt):
    import getpass
    return getpass.getpass(prompt)

def _build_prompt(text, suffix, show_default=False, default=None):
    prompt = text
    if default is not None and show_default:
        prompt = '%s [%s]' % (prompt, default)
    return prompt + suffix

def prompt(text, default=None, hide_input=False,
           confirmation_prompt=False, type=None,
           value_proc=None, prompt_suffix=': ', show_default=True):
    """Prompts a user for input.  This is a convenience function that can
    be used to prompt a user for input later.

    If the user aborts the input by sending a interrupt signal this
    function will catch it and raise a :exc:`Abort` exception.

    :param text: the text to show for the prompt.
    :param default: the default value to use if no input happens.  If this
                    is not given it will prompt until it's aborted.
    :param hide_input: if this is set to true then the input value will
                       be hidden.
    :param confirmation_prompt: asks for confirmation for the value.
    :param type: the type to use to check the value against.
    :param value_proc: if this parameter is provided it's a function that
                       is invoked instead of the type conversion to
                       convert a value.
    :param prompt_suffix: a suffix that should be added to the prompt.
    :param show_default: shows or hides the default value in the prompt.
    """
    result = None

    def prompt_func(text):
        f = hide_input and hidden_prompt_func or visible_prompt_func
        try:
            return f(text)
        except (KeyboardInterrupt, EOFError):
            raise Abort()

    if value_proc is None:
        value_proc = convert_type(type, default)

    prompt = _build_prompt(text, prompt_suffix, show_default, default)

    while 1:
        while 1:
            value = prompt_func(prompt)
            if value:
                break
            # If a default is set and used, then the confirmation
            # prompt is always skipped because that's the only thing
            # that really makes sense.
            elif default is not None:
                return default
        try:
            result = value_proc(value)
        except UsageError as e:
            echo('Error: %s' % e.message)
            continue
        if not confirmation_prompt:
            return result
        while 1:
            value2 = prompt_func('Repeat for confirmation: ')
            if value2:
                break
        if value == value2:
            return result
        echo('Error: the two entered values do not match')

def confirm(text, default=False, abort=False, prompt_suffix=': ',
            show_default=True):
    """Prompts for confirmation (yes/no question).

    If the user aborts the input by sending a interrupt signal this
    function will catch it and raise a :exc:`Abort` exception.

    :param text: the question to ask.
    :param default: the default for the prompt.
    :param abort: if this is set to `True` a negative answer aborts the
                  exception by raising :exc:`Abort`.
    :param prompt_suffix: a suffix that should be added to the prompt.
    :param show_default: shows or hides the default value in the prompt.
    """
    prompt = _build_prompt(text, prompt_suffix, show_default,
                           default and 'Y/n' or 'y/N')
    while 1:
        try:
            value = visible_prompt_func(prompt).lower().strip()
        except (KeyboardInterrupt, EOFError):
            raise Abort()
        if value in ('y', 'yes'):
            rv = True
        elif value in ('n', 'no'):
            rv = False
        elif value == '':
            rv = default
        else:
            echo('Error: invalid input')
            continue
        break
    if abort and not rv:
        raise Abort()
    return rv

def get_terminal_size():
    """Returns the current size of the terminal as tuple in the form
    ``(width, height)`` in columns and rows.
    """
    # If shutil has get_terminal_size() (Python 3.3 and later) use that
    if sys.version_info >= (3, 3):
        import shutil
        shutil_get_terminal_size = getattr(shutil, 'get_terminal_size', None)
        if shutil_get_terminal_size:
            sz = shutil_get_terminal_size()
            return sz.columns, sz.lines

    def ioctl_gwinsz(fd):
        try:
            import fcntl
            import termios
            cr = struct.unpack(
                'hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
        except Exception:
            return
        return cr

    cr = ioctl_gwinsz(0) or ioctl_gwinsz(1) or ioctl_gwinsz(2)
    if not cr:
        try:
            fd = os.open(os.ctermid(), os.O_RDONLY)
            try:
                cr = ioctl_gwinsz(fd)
            finally:
                os.close(fd)
        except Exception:
            pass
    if not cr or not cr[0] or not cr[1]:
        cr = (os.environ.get('LINES', 25),
              os.environ.get('COLUMNS', 80))
    return int(cr[1]), int(cr[0])

def echo_via_pager(text):
    """This function takes a text and shows it via an environment specific
    pager on stdout.

    :param text: the text to page.
    """
    if not isinstance(text, string_types):
        text = text_type(text)

    if PY2 and isinstance(text, text_type):
        encoding = get_best_encoding(sys.stdout)
        text = text.encode(encoding, 'replace')

    from ._termui_impl import pager
    return pager(text + '\n')

def progressbar(iterable=None, length=None, label=None, show_eta=True,
                show_percent=None, show_pos=False,
                item_show_func=None, fill_char='#', empty_char='-',
                bar_template='%(label)s  [%(bar)s]  %(info)s',
                info_sep='  ', width=36, file=None):
    """This function creates an iterable context manager that can be used
    to iterate over something while showing a progress bar.  It will
    either iterate over the `iterable` or `length` items (that are counted
    up).  While iteration happens this function will print a rendered
    progress bar to the given `file` (defaults to stdout) and will attempt
    to calculate remaining time and more.  By default this progress bar
    will not be rendered if the file is not a terminal.

    The context manager creates the progress bar.  When the context
    manager is entered the progress bar is already displayed.  With every
    iteration over the progress bar the iterable passed to the bar is
    advanced and the bar is updated.  When the context manager exits
    a newline is printed and the progress bar is finalized on screen.

    No printing must happen or the progress bar will be unintentionally
    destroyed.

    Example usage::

        with progressbar(items) as bar:
            for item in bar:
                do_something_with(item)

    .. versionadded:: 2.0

    :param iterable: an iterable to iterate over.  If not provided the length
                     is required.
    :param length: the number of items to iterate over.  By default the
                   progressbar will attempt to ask the iterator about its
                   length, which might or might not work.  If an iterable is
                   also provided this parameter can be used to override the
                   length.  If an iterable is not provided the progress bar
                   will iterate over a range of that length.
    :param label: the label to show next to the progress bar.
    :param show_eta: enables or disables the estimated time display.  This is
                     automatically disabled if the length cannot be
                     determined.
    :param show_percent: enables or disables the percentage display.  The
                         default is `True` if the iterable has a length or
                         `False` if not.
    :param show_pos: enables or disables the absolute position display.  The
                     default is `False`.
    :param item_show_func: a function called with the current item which
                           can return a string to show the current item
                           next to the progress bar.  Note that the current
                           item can be `None`!
    :param fill_char: the character to use to show the filled part of the
                      progress bar.
    :param empty_char: the character to use to show the non-filled part of
                       the progress bar.
    :param bar_template: the format string to use as template for the bar.
                         The parameters in it are ``label`` for the label,
                         ``bar`` for the progress bar and ``info`` for the
                         info section.
    :param info_sep: the separator between multiple info items (eta etc.)
    :param width: the width of the progress bar in characters.
    :param file: the file to write to.  If this is not a terminal then
                 only the label is printed.
    """
    from ._termui_impl import ProgressBar
    return ProgressBar(iterable=iterable, length=length, show_eta=show_eta,
                       show_percent=show_percent, show_pos=show_pos,
                       item_show_func=item_show_func, fill_char=fill_char,
                       empty_char=empty_char, bar_template=bar_template,
                       info_sep=info_sep, file=file, label=label,
                       width=width)

def clear():
    """Clears the terminal screen.  This will have the effect of clearing
    the whole visible space of the terminal and moving the cursor to the
    top left.  This does not do anything if not connected to a terminal.

    .. versionadded:: 2.0
    """
    if not isatty(sys.stdout):
        return
    # If we're on windows and we don't have colorama available, then we
    # clear the screen by shelling out.  Otherwise we can use an escape
    # sequence.
    if sys.platform.startswith('win') and colorama is None:
        os.system('cls')
    else:
        sys.stdout.write('\033[2J\033[1;1H')

def style(text, fg=None, bg=None, bold=None, dim=None, underline=None,
          blink=None, reverse=None, reset=True):
    """Styles a text with ANSI styles and returns the new string.  By
    default the styling is self contained which means that at the end
    of the string a reset code is issued.  This can be prevented by
    passing ``reset=False``.

    Examples::

        click.echo(click.style('Hello World!', fg='green'))
        click.echo(click.style('ATTENTION!', blink=True))
        click.echo(click.style('Some things', reverse=True, fg='cyan'))

    Supported color names:

    * ``black`` (might be a gray)
    * ``red``
    * ``green``
    * ``yellow`` (might be an orange)
    * ``blue``
    * ``magenta``
    * ``cyan``
    * ``white`` (might be light gray)
    * ``reset`` (reset the color code only)

    .. versionadded:: 2.0

    :param text: the string to style with ansi codes.
    :param fg: if provided this will become the foreground color.
    :param bg: if provided this will become the background color.
    :param bold: if provided this will enable or disable bold mode.
    :param dim: if provided this will enable or disable dim mode.  This is
                badly supported.
    :param underline: if provided this will enable or disable underline.
    :param blink: if provided this will enable or disable blinking.
    :param reverse: if provided this will enable or disable inverse
                    rendering (foreground becomes background and the
                    other way round).
    :param reset: by default a reset-all code is added at the end of the
                  string which means that styles do not carry over.  This
                  can be disabled to compose styles.
    """
    bits = []
    if fg:
        try:
            bits.append('\033[%dm' % (_ansi_colors.index(fg) + 30))
        except ValueError:
            raise TypeError('Unknown color %r' % fg)
    if bg:
        try:
            bits.append('\033[%dm' % (_ansi_colors.index(bg) + 40))
        except ValueError:
            raise TypeError('Unknown color %r' % bg)
    if bold is not None:
        bits.append('\033[%dm' % (1 if bold else 22))
    if dim is not None:
        bits.append('\033[%dm' % (2 if dim else 22))
    if underline is not None:
        bits.append('\033[%dm' % (4 if underline else 24))
    if blink is not None:
        bits.append('\033[%dm' % (5 if blink else 25))
    if reverse is not None:
        bits.append('\033[%dm' % (7 if reverse else 27))
    bits.append(text)
    if reset:
        bits.append(_ansi_reset_all)
    return ''.join(bits)

def unstyle(text):
    """Removes ANSI styling information from a string.  Usually it's not
    necessary to use this function as click's echo function will
    automatically remove styling if necessary.

    .. versionadded:: 2.0

    :param text: the text to remove style information from.
    """
    return strip_ansi(text)

def secho(text, file=None, nl=True, **styles):
    """This function combines :func:`echo` and :func:`style` into one
    call.  As such the following two calls are the same::

        click.secho('Hello World!', fg='green')
        click.echo(click.style('Hello World!', fg='green'))

    All keyword arguments are forwarded to the underlying functions
    depending on which one they go with.

    .. versionadded:: 2.0
    """
    text = style(text, **styles)
    return echo(text, file=file, nl=nl)

def edit(text=None, editor=None, env=None, require_save=True,
         extension='.txt', filename=None):
    r"""Edits the given text in the defined editor.  If an editor is given
    (should be the full path to the executable but the regular operating
    system search path is used for finding the executable) it overrides
    the detected editor.  Optionally some environment variables can be
    used.  If the editor is closed without changes `None` is returned.  In
    case a file is edited directly the return value is always `None` and
    `require_save` and `extension` are ignored.

    If the editor cannot be opened a :exc:`UsageError` is raised.

    Note for Windows: to simplify cross platform usage the newlines are
    automatically converted from posix to windows and reverse.  As such the
    message here will have ``\n`` as newline markers.

    :param text: the text to edit.
    :param editor: optionally the editor to use.  Defaults to automatic
                   detection.
    :param env: environment variables to forward to the editor.
    :param require_save: if this is true, then not saving in the editor
                         will make the return value become `None`.
    :param extension: the extension to tell the editor about.  This defaults
                      to `.txt` but changing this might change syntax
                      highlighting.
    :param filename: if provided it will edit this file instead of the
                     provided text contents.  It will not use a temporary
                     file as an indirection in that case.
    """
    from ._termui_impl import Editor
    editor = Editor(editor=editor, env=env, require_save=require_save,
                    extension=extension)
    if filename is None:
        return editor.edit(text)
    editor.edit_file(filename)

def launch(url, wait=False, locate=False):
    """This function launches the given URL (or filename) in the default
    viewer application for this file type.  If this is an executable it
    might launch the executable in a new session.  The return value is
    the exit code of the launched application.  Usually ``0`` indicates
    success.

    Examples::

        click.open('http://click.pocoo.org/')
        click.open('/my/downloaded/file', locate=True)

    .. versionadded:: 2.0

    :param url: url or filename of the thing to launch.
    :param wait: waits for the program to stop.
    :param locate: if this is set to `True` then instead of launching the
                   application associated with the URL it will attempt to
                   launch a file manager with the file located.  This
                   might have weird effects if the url does not point to
                   the filesystem.
    """
    from _termui_impl import open_url
    return open_url(url, wait=wait, locate=locate)
<|endoftext|>


<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
"""
vm-mad simulator by
    <riccardo.murri@uzh.ch>
    CHristian Panse <cp@fgcz.ethz.ch>
2011-12-14 @ GC3
2011-12-15 @ KafiSchnaps
2011-12-16 @ FGCZ

aim of the simulations:
    find out how the orchestrator is behaving under 'pressure'

see also:
http://www.ifi.uzh.ch/pax/web/uploads/pdf/publication/1408/thesis.pdf
@mastersthesis{instance1408,
title = {{Investigation of economical and practical aspects of commercial cloud computing for Life Sciences}},
author = {Aleksandar Markovic},
editor = {Burkhard Stiller and Christian Panse},
group = {csg},
year = {2010},
school = {University of Zurich},
month = {March},
Date-Added = {2010-05-06 13:46:02},
Date-Modified = {2010-05-06 13:57:45}
}

pages: 57-60

"""
# Copyright (C) 2011, 2012 ETH Zurich and University of Zurich. All rights reserved.
#
# Authors:
#   Christian Panse <cp@fgcz.ethz.ch>
#   Riccardo Murri <riccardo.murri@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
__docformat__ = 'reStructuredText'
__version__ = '$Revision$'

# stdlib imports
import os
import sys
import time
import random

def signal_handler(signal, frame):
    sys.exit(0)

class Sim(object):
    """
        each integer item in the pending list cor. to the jobs runtime

        a host is idle if the item in the running list is less than zero

        if those hosts keep idle they can be removed.

        note we turn on / halt only one host within  time step
    """

    def __init__(self):
        self.maxVM=10
        self.pending=[ ]
        self.running=[ ]
        self.steps=0

    def startVM(self):
        print "starting vm ..."
        if len(self.running) < self.maxVM:
            self.running.append(-1)
        else:
            print "can not allocate more VMs"

    def stopVM(self, j):
        del self.running[j]

    """
    todo:
        * use real world data; do boot straping
        * random populated queue

    """
    def populate(self):
        for i in range(0,20):
            self.pending.append(random.randint(1800, 3600))

    def printStatus(self):
        vmidleC=0
        runningC=0
        for i in self.running:
            if i < 0:
                vmidleC +=1
            else:
                runningC += 1

        try:
            f = open("main_sim.txt", "a")
        except:
            print ("could not open file")
            sys.exit(1)

        f.write(str(self.steps) + ','
            + str(len(self.pending)) + ','
            + str(runningC) + ','
            + str(vmidleC) + '\n')
        f.close()

        #print "number of pending jobs =", len(self.pending)
        #print "number of running jobs =", (runningC)
        #print "number of idle hosts =", (vmidleC)
        #print self.running

    def run(self):
        # as long as we have things to do
        while (len(self.pending) > 0) or (len(self.running) > 0):
            self.steps+=1
            # time is going on
            for j in range(0, len(self.running)):
                self.running[j] -= 1

            for j in range(0, len(self.running)):
                # check if we run an other job
                if self.running[j] < 0 and len(self.pending) > 0:
                    self.running[j]=self.pending.pop(0)
                    break;
                # in case the pending queue is empty lets check if we can halt stop a VM
                elif self.running[j] < 0:
                    self.stopVM(j)
                    break;

            # print some data to a file for later plotting
            self.printStatus()

            if len(self.pending) / 2  > len(self.running):
                  self.startVM()

            #time.sleep(1)

if "__main__" == __name__:
    mySim=Sim()
    mySim.populate()
    mySim.run()
<commit_msg>Removed `main_sim.py`, as its functionality is now provided by `simul.py`.<commit_after><|endoftext|>


<commit_before>from lib.dialog_utils import VoiceHandler, ResponseBuilder
"""
In this file we specify default event handlers which are then populated into the handler map using metaprogramming
Copyright Anjishnu Kumar 2015

Each VoiceHandler function receives a ResponseBuilder object as input and outputs a Response object
A response object is defined as the output of ResponseBuilder.create_response()
"""

r = ResponseBuilder()

def default_handler(request):
    """ The default handler gets invoked if no handler is set for a request """
    return r.create_response(message="Just ask")

@VoiceHandler(request_type="LaunchRequest")
def launch_request_handler(request):
    return r.create_response(message="Hello Welcome to My Recipes!")

@VoiceHandler(request_type="SessionEndedRequest")
def session_ended_request_handler(request):
    return r.create_response(message="Goodbye!")

@VoiceHandler(intent='GetRecipeIntent')
def get_recipe_intent_handler(request):

    ingredient = request.get_slot_value("Ingredient")
    ingredient = ingredient if ingredient else ""
    card = r.create_card(title="GetRecipeIntent activated",
                         subtitle=None,
                         content="asked alexa to find a recipe using {}"
                         .format(ingredient))

    return r.create_response(message="Finding a recipe with the ingredient {}".format(ingredient),
                             end_session=False,
                             card_obj=card)

"""
@VoiceHandler(intent="NextRecipeIntent")
def call_back_intent_handler(request):
    return r.create_response(message="Getting Next Recipe ...")
"""
<commit_msg>uncommented voice handler<commit_after>from lib.dialog_utils import VoiceHandler, ResponseBuilder
"""
In this file we specify default event handlers which are then populated into the handler map using metaprogramming
Copyright Anjishnu Kumar 2015

Each VoiceHandler function receives a ResponseBuilder object as input and outputs a Response object
A response object is defined as the output of ResponseBuilder.create_response()
"""

r = ResponseBuilder()

def default_handler(request):
    """ The default handler gets invoked if no handler is set for a request """
    return r.create_response(message="Just ask")

@VoiceHandler(request_type="LaunchRequest")
def launch_request_handler(request):
    return r.create_response(message="Hello Welcome to My Recipes!")

@VoiceHandler(request_type="SessionEndedRequest")
def session_ended_request_handler(request):
    return r.create_response(message="Goodbye!")

@VoiceHandler(intent='GetRecipeIntent')
def get_recipe_intent_handler(request):

    ingredient = request.get_slot_value("Ingredient")
    ingredient = ingredient if ingredient else ""
    card = r.create_card(title="GetRecipeIntent activated",
                         subtitle=None,
                         content="asked alexa to find a recipe using {}"
                         .format(ingredient))

    return r.create_response(message="Finding a recipe with the ingredient {}".format(ingredient),
                             end_session=False,
                             card_obj=card)

@VoiceHandler(intent="NextRecipeIntent")
def call_back_intent_handler(request):
    return r.create_response(message="Getting Next Recipe ...")
<|endoftext|>


<commit_before>import json

from django.db import models
from django.contrib.auth import models as auth_models
from django.contrib.auth.signals import user_logged_in
from django.dispatch import receiver
import pytz

TIMEZONE_CHOICES = [(tz, tz.replace('_', ' ')) for tz in pytz.common_timezones]

class User(auth_models.AbstractUser):
    nick = models.CharField("Preferred nick", max_length=100, blank=True)
    timezone = models.CharField(max_length=50, choices=TIMEZONE_CHOICES,
                                blank=True)

    class Meta:
        db_table = 'auth_user'

    def get_quantity_per_membership_kind(self):
        """
        Group the membership per kind and count them
        The result is a list of dict:
            [
                {'kind': u'share', 'quantity': 1},
                {'kind': u'personal', 'quantity': 2}
            ]
        """
        return (
            self.membership_set
                .values("kind")
                .annotate(quantity=models.Count("kind"))
            )

@receiver(user_logged_in)
def set_user_timezone(sender, request, user, **kwargs):
    """Set the session timezone on login"""
    if user.timezone:
        request.session['django_timezone'] = user.timezone

class Membership(models.Model):
    KIND_PERSONAL = "personal"
    KIND_PUBLIC = "public"
    KIND_SPONSOR = "sponsor"
    KIND_CHOICES = (
        (KIND_PERSONAL, KIND_PERSONAL.title()),
        (KIND_PUBLIC, KIND_PUBLIC.title()),
        (KIND_SPONSOR, KIND_SPONSOR.title()),
    )

    user = models.ForeignKey(User)
    channel = models.ForeignKey('bots.Channel')
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)
    is_owner = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)
    kind = models.CharField(
        max_length=30, choices=KIND_CHOICES, default=KIND_PERSONAL)

    class Meta:
        unique_together = ('user', 'channel')

    def to_json(self):
        return json.dumps(
            {
                'id': self.user.id,
                'admin': self.is_admin,
                'email': self.user.email
            })

    def save(self, *args, **kwargs):
        if self.is_owner:
            self.is_admin = True
        return super(Membership, self).save(*args, **kwargs)
<commit_msg>Update the logic on `user.get_quantity_per_membership_kind()`<commit_after>import json

from django.db import models
from django.contrib.auth import models as auth_models
from django.contrib.auth.signals import user_logged_in
from django.dispatch import receiver
import pytz

TIMEZONE_CHOICES = [(tz, tz.replace('_', ' ')) for tz in pytz.common_timezones]

class User(auth_models.AbstractUser):
    nick = models.CharField("Preferred nick", max_length=100, blank=True)
    timezone = models.CharField(max_length=50, choices=TIMEZONE_CHOICES,
                                blank=True)

    class Meta:
        db_table = 'auth_user'

    def get_quantity_per_membership_kind(self):
        """
        Group the membership per kind and count them
        The result is a list of dict:
            [
                {'kind': u'public', 'quantity': 1},
                {'kind': u'personal', 'quantity': 2}
            ]
        """
        return (
            self.membership_set
                .filter(channel__is_active=True)
                .filter(
                    models.Q(kind=Membership.KIND_PERSONAL, channel__is_public=False) |
                    models.Q(kind=Membership.KIND_PUBLIC))
                .values("kind")
                .annotate(quantity=models.Count("kind"))
            )

@receiver(user_logged_in)
def set_user_timezone(sender, request, user, **kwargs):
    """Set the session timezone on login"""
    if user.timezone:
        request.session['django_timezone'] = user.timezone

class Membership(models.Model):
    KIND_PERSONAL = "personal"
    KIND_PUBLIC = "public"
    KIND_SPONSOR = "sponsor"
    KIND_CHOICES = (
        (KIND_PERSONAL, KIND_PERSONAL.title()),
        (KIND_PUBLIC, KIND_PUBLIC.title()),
        (KIND_SPONSOR, KIND_SPONSOR.title()),
    )

    user = models.ForeignKey(User)
    channel = models.ForeignKey('bots.Channel')
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)
    is_owner = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)
    kind = models.CharField(
        max_length=30, choices=KIND_CHOICES, default=KIND_PERSONAL)

    class Meta:
        unique_together = ('user', 'channel')

    def to_json(self):
        return json.dumps(
            {
                'id': self.user.id,
                'admin': self.is_admin,
                'email': self.user.email
            })

    def save(self, *args, **kwargs):
        if self.is_owner:
            self.is_admin = True
        return super(Membership, self).save(*args, **kwargs)
<|endoftext|>


<commit_before>#!/usr/bin/env python

# Copyright (c) 2006-2014 sqlmap developers (http://sqlmap.org/)
# See the file 'doc/COPYING' for copying permission

import codecs
import inspect
import os
import re
import smtplib
import subprocess
import sys
import time
import traceback

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

sys.path.append(os.path.normpath("%s/../../" % os.path.dirname(inspect.getfile(inspect.currentframe()))))

from lib.core.revision import getRevisionNumber

START_TIME = time.strftime("%H:%M:%S %d-%m-%Y", time.gmtime())
SQLMAP_HOME = "/opt/sqlmap"
REVISION = getRevisionNumber()

SMTP_SERVER = "127.0.0.1"
SMTP_PORT = 25
SMTP_TIMEOUT = 30
FROM = "regressiontest@sqlmap.org"
#TO = "dev@sqlmap.org"
TO = ["bernardo.damele@gmail.com", "miroslav.stampar@gmail.com"]
SUBJECT = "regression test started on %s using revision %s" % (START_TIME, REVISION)

def prepare_email(content):
    global FROM
    global TO
    global SUBJECT

    msg = MIMEMultipart()
    msg["Subject"] = SUBJECT
    msg["From"] = FROM
    msg["To"] = TO if isinstance(TO, basestring) else ",".join(TO)

    msg.attach(MIMEText(content))

    return msg

def send_email(msg):
    global SMTP_SERVER
    global SMTP_PORT
    global SMTP_TIMEOUT

    try:
        s = smtplib.SMTP(host=SMTP_SERVER, port=SMTP_PORT, timeout=SMTP_TIMEOUT)
        s.sendmail(FROM, TO, msg.as_string())
        s.quit()
    # Catch all for SMTP exceptions
    except smtplib.SMTPException, e:
        print "Failure to send email: %s" % str(e)

def failure_email(msg):
    msg = prepare_email(msg)
    send_email(msg)
    sys.exit(1)

def main():
    global SUBJECT

    content = ""
    test_counts = []
    attachments = {}

    updateproc = subprocess.Popen("cd /opt/sqlmap/ ; python /opt/sqlmap/sqlmap.py --update", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = updateproc.communicate()

    if stderr:
        failure_email("Update of sqlmap failed with error:\n\n%s" % stderr)

    regressionproc = subprocess.Popen("python /opt/sqlmap/sqlmap.py --live-test --run-case 'MySQL UNION query multi-threaded enumeration'", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
    stdout, stderr = regressionproc.communicate()

    if stderr:
        failure_email("Execution of regression test failed with error:\n\n%s" % stderr)

    failed_tests = re.findall("running live test case: (.+?) \((\d+)\/\d+\)[\r]*\n.+test failed (at parsing items: (.+))?\s*\- scan folder: (\/.+) \- traceback: (.*?)( - SQL injection not detected)?[\r]*\n", stdout, re.M)

    for failed_test in failed_tests:
        title = failed_test[0]
        test_count = int(failed_test[1])
        parse = failed_test[3] if failed_test[3] else None
        output_folder = failed_test[4]
        traceback = False if failed_test[5] == "False" else bool(failed_test[5])
        detected = False if failed_test[6] else True

        test_counts.append(test_count)

        console_output_file = os.path.join(output_folder, "console_output")
        log_file = os.path.join(output_folder, "debiandev", "log")
        traceback_file = os.path.join(output_folder, "traceback")

        if os.path.exists(console_output_file):
            console_output_fd = codecs.open(console_output_file, "rb", "utf8")
            console_output = console_output_fd.read()
            console_output_fd.close()
            attachments[test_count] = str(console_output)

        if os.path.exists(log_file):
            log_fd = codecs.open(log_file, "rb", "utf8")
            log = log_fd.read()
            log_fd.close()

        if os.path.exists(traceback_file):
            traceback_fd = codecs.open(traceback_file, "rb", "utf8")
            traceback = traceback_fd.read()
            traceback_fd.close()

        content += "Failed test case '%s' (#%d)" % (title, test_count)

        if parse:
            content += " at parsing: %s:\n\n" % parse
            content += "### Log file:\n\n"
            content += "%s\n\n" % log
        elif not detected:
            content += " - SQL injection not detected\n\n"
        else:
            content += "\n\n"

        if traceback:
            content += "### Traceback:\n\n"
            content += "%s\n\n" % str(traceback)

        content += "#######################################################################\n\n"

    end_string = "Regression test finished at %s" % time.strftime("%H:%M:%S %d-%m-%Y", time.gmtime())

    if content:
        content += end_string
        SUBJECT = "Failed %s (%s)" % (SUBJECT, ", ".join("#%d" % count for count in test_counts))

        msg = prepare_email(content)

        for test_count, attachment in attachments.items():
            attachment = MIMEText(attachment)
            attachment.add_header("Content-Disposition", "attachment", filename="test_case_%d_console_output.txt" % test_count)
            msg.attach(attachment)

        send_email(msg)
    else:
        SUBJECT = "Successful %s" % SUBJECT
        msg = prepare_email("All test cases were successful\n\n%s" % end_string)
        send_email(msg)

if __name__ == "__main__":
    log_fd = open("/tmp/sqlmapregressiontest.log", "wb")
    log_fd.write("Regression test started at %s\n" % START_TIME)

    try:
        main()
    except Exception, e:
        log_fd.write("An exception has occurred:\n%s" % str(traceback.format_exc()))

    log_fd.write("Regression test finished at %s\n\n" % time.strftime("%H:%M:%S %d-%m-%Y", time.gmtime()))
    log_fd.close()
<commit_msg>adapted<commit_after>#!/usr/bin/env python

# Copyright (c) 2006-2014 sqlmap developers (http://sqlmap.org/)
# See the file 'doc/COPYING' for copying permission

import codecs
import inspect
import os
import re
import smtplib
import subprocess
import sys
import time
import traceback

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

sys.path.append(os.path.normpath("%s/../../" % os.path.dirname(inspect.getfile(inspect.currentframe()))))

from lib.core.revision import getRevisionNumber

START_TIME = time.strftime("%H:%M:%S %d-%m-%Y", time.gmtime())
SQLMAP_HOME = "/opt/sqlmap"
REVISION = getRevisionNumber()

SMTP_SERVER = "127.0.0.1"
SMTP_PORT = 25
SMTP_TIMEOUT = 30
FROM = "regressiontest@sqlmap.org"
#TO = "dev@sqlmap.org"
TO = ["bernardo.damele@gmail.com", "miroslav.stampar@gmail.com"]
SUBJECT = "regression test started on %s using revision %s" % (START_TIME, REVISION)
TARGET = "debian"

def prepare_email(content):
    global FROM
    global TO
    global SUBJECT

    msg = MIMEMultipart()
    msg["Subject"] = SUBJECT
    msg["From"] = FROM
    msg["To"] = TO if isinstance(TO, basestring) else ",".join(TO)

    msg.attach(MIMEText(content))

    return msg

def send_email(msg):
    global SMTP_SERVER
    global SMTP_PORT
    global SMTP_TIMEOUT

    try:
        s = smtplib.SMTP(host=SMTP_SERVER, port=SMTP_PORT, timeout=SMTP_TIMEOUT)
        s.sendmail(FROM, TO, msg.as_string())
        s.quit()
    # Catch all for SMTP exceptions
    except smtplib.SMTPException, e:
        print "Failure to send email: %s" % str(e)

def failure_email(msg):
    msg = prepare_email(msg)
    send_email(msg)
    sys.exit(1)

def main():
    global SUBJECT

    content = ""
    test_counts = []
    attachments = {}

    updateproc = subprocess.Popen("cd /opt/sqlmap/ ; python /opt/sqlmap/sqlmap.py --update", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = updateproc.communicate()

    if stderr:
        failure_email("Update of sqlmap failed with error:\n\n%s" % stderr)

    regressionproc = subprocess.Popen("python /opt/sqlmap/sqlmap.py --live-test --run-case 'MySQL UNION query multi-threaded enumeration'", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
    stdout, stderr = regressionproc.communicate()

    if stderr:
        failure_email("Execution of regression test failed with error:\n\n%s" % stderr)

    failed_tests = re.findall("running live test case: (.+?) \((\d+)\/\d+\)[\r]*\n.+test failed (at parsing items: (.+))?\s*\- scan folder: (\/.+) \- traceback: (.*?)( - SQL injection not detected)?[\r]*\n", stdout, re.M)

    for failed_test in failed_tests:
        title = failed_test[0]
        test_count = int(failed_test[1])
        parse = failed_test[3] if failed_test[3] else None
        output_folder = failed_test[4]
        traceback = False if failed_test[5] == "False" else bool(failed_test[5])
        detected = False if failed_test[6] else True

        test_counts.append(test_count)

        console_output_file = os.path.join(output_folder, "console_output")
        log_file = os.path.join(output_folder, TARGET, "log")
        traceback_file = os.path.join(output_folder, "traceback")

        if os.path.exists(console_output_file):
            console_output_fd = codecs.open(console_output_file, "rb", "utf8")
            console_output = console_output_fd.read()
            console_output_fd.close()
            attachments[test_count] = str(console_output)

        if os.path.exists(log_file):
            log_fd = codecs.open(log_file, "rb", "utf8")
            log = log_fd.read()
            log_fd.close()

        if os.path.exists(traceback_file):
            traceback_fd = codecs.open(traceback_file, "rb", "utf8")
            traceback = traceback_fd.read()
            traceback_fd.close()

        content += "Failed test case '%s' (#%d)" % (title, test_count)

        if parse:
            content += " at parsing: %s:\n\n" % parse
            content += "### Log file:\n\n"
            content += "%s\n\n" % log
        elif not detected:
            content += " - SQL injection not detected\n\n"
        else:
            content += "\n\n"

        if traceback:
            content += "### Traceback:\n\n"
            content += "%s\n\n" % str(traceback)

        content += "#######################################################################\n\n"

    end_string = "Regression test finished at %s" % time.strftime("%H:%M:%S %d-%m-%Y", time.gmtime())

    if content:
        content += end_string
        SUBJECT = "Failed %s (%s)" % (SUBJECT, ", ".join("#%d" % count for count in test_counts))

        msg = prepare_email(content)

        for test_count, attachment in attachments.items():
            attachment = MIMEText(attachment)
            attachment.add_header("Content-Disposition", "attachment", filename="test_case_%d_console_output.txt" % test_count)
            msg.attach(attachment)

        send_email(msg)
    else:
        SUBJECT = "Successful %s" % SUBJECT
        msg = prepare_email("All test cases were successful\n\n%s" % end_string)
        send_email(msg)

if __name__ == "__main__":
    log_fd = open("/tmp/sqlmapregressiontest.log", "wb")
    log_fd.write("Regression test started at %s\n" % START_TIME)

    try:
        main()
    except Exception, e:
        log_fd.write("An exception has occurred:\n%s" % str(traceback.format_exc()))

    log_fd.write("Regression test finished at %s\n\n" % time.strftime("%H:%M:%S %d-%m-%Y", time.gmtime()))
    log_fd.close()
<|endoftext|>


<commit_before>#!/bin/env python

import numpy as np
from void_basics import *
from pylab import *

if __name__ == '__main__':
    # get the lensing data HDU
    hdu = get_lensing_data()

    # apply selection
    selection = np.ones(len(hdu[1].data), dtype="bool") # all voids
    lrgs = get_LRG_sample(hdu[1].data)
    selection &= lrgs
    print sum(selection), "voids selected"

    # define binning
    rbins = np.linspace(0.15, 2.4, 12)

    # get bootstrap profiles
    Emode, e_Emode, Bmode, e_Bmode, r_mean = stack_EBR(hdu[1].data, selection, rbins, rebinned=True, samples=5000, mean_method=kappa_sigma)

    # compute chi^2 and likelihood ratio wrt to void model
    model = void_model(rbins)
    K = likelihood_ratio(Emode, e_Emode,  model)
    Chi2 = chi2(Emode, e_Emode,  model)

    # plot data and model
    figure()
    ax = subplot(111)
    ax.plot([0.05, 3], [0,0], 'k:')
    ax.errorbar(r_mean, Emode, yerr=e_Emode, c='k', marker='o', label='E-mode', zorder=1000)
    ax.errorbar(r_mean, Bmode, yerr=e_Bmode, c='r', marker='d', label='B-mode', zorder=999)
    # plot model: due to plotting bug, needs one extra entry to disply fully
    ax.plot(rbins, np.concatenate((model, [nan])), 'b-', drawstyle='steps-post', label='model', zorder=998)
    ax.text(0.98, 0.19, r'$N_v=' + '%d' % sum(selection) + '$', ha='right', va='center', transform=ax.transAxes)
    ax.text(0.98, 0.12, r'$N_g=' + '%.2f' % (hdu[1].data['npair'][selection].sum()/1e8) + '\cdot 10^8$', ha='right', va='center', transform=ax.transAxes)
    ax.text(0.98, 0.05, r'$\chi^2_m=%1.2f' % Chi2 + ',\ K=' + '%1.2f' % K + '$', ha='right', va='center', transform=ax.transAxes)

    # make legend with "model" label at the bottom
    handles, labels = ax.get_legend_handles_labels()
    handles.append(handles[0])
    del handles[0]
    labels.append(labels[0])
    del labels[0]
    ax.legend(handles, labels, loc='upper right', numpoints=1, frameon=True, scatterpoints=0)

    # data ranges, labels ...
    ax.set_ylim(-0.2, 0.2)
    ax.set_xlim(0.01, 2.6)
    ax.set_ylabel(r'$\Delta\Sigma/R_v\ \mathrm{[} 10^{12} h^2\, \mathrm{M_\odot Mpc^{-3}]}$')
    ax.set_xlabel(r'$r/R_v$')
    subplots_adjust(bottom=0.12, left=0.15, right=0.98, top=0.92)

    # make inset to show radius/z distribution of selection
    z = hdu[1].data["z"]
    radius = hdu[1].data["radius"]
    axi = axes([0.26, 0.65, 0.25, 0.25], axisbg='None')
    axi.scatter(z, radius, c='r', s=5,label='all voids', linewidths=0)
    axi.scatter(z[selection], radius[selection], c='k', s=10, label='selection', linewidths=0)
    axi.set_ylim(0,120)
    axi.set_xlim(0.025, 0.425)
    axi.set_xticks([0.1, 0.2, 0.3, 0.4])
    axi.set_yticks([0, 50, 100])
    axi.set_xlabel('$z$')
    axi.set_ylabel('$R_v$')

    show()
    hdu.close()
<commit_msg>simpler selection<commit_after>#!/bin/env python

import numpy as np
from void_basics import *
from pylab import *

if __name__ == '__main__':
    # get the lensing data HDU
    hdu = get_lensing_data()

    # apply selection
    lrgs = get_LRG_sample(hdu[1].data)
    selection = (lrgs == False) # main sample
    print sum(selection), "voids selected"

    # define binning
    rbins = np.linspace(0.15, 2.4, 12)

    # get bootstrap profiles
    Emode, e_Emode, Bmode, e_Bmode, r_mean = stack_EBR(hdu[1].data, selection, rbins, rebinned=True, samples=5000, mean_method=kappa_sigma)

    # compute chi^2 and likelihood ratio wrt to void model
    model = void_model(rbins)
    K = likelihood_ratio(Emode, e_Emode,  model)
    Chi2 = chi2(Emode, e_Emode,  model)

    # plot data and model
    figure()
    ax = subplot(111)
    ax.plot([0.05, 3], [0,0], 'k:')
    ax.errorbar(r_mean, Emode, yerr=e_Emode, c='k', marker='o', label='E-mode', zorder=1000)
    ax.errorbar(r_mean, Bmode, yerr=e_Bmode, c='r', marker='d', label='B-mode', zorder=999)
    # plot model: due to plotting bug, needs one extra entry to disply fully
    ax.plot(rbins, np.concatenate((model, [nan])), 'b-', drawstyle='steps-post', label='model', zorder=998)
    ax.text(0.98, 0.19, r'$N_v=' + '%d' % sum(selection) + '$', ha='right', va='center', transform=ax.transAxes)
    ax.text(0.98, 0.12, r'$N_g=' + '%.2f' % (hdu[1].data['npair'][selection].sum()/1e8) + '\cdot 10^8$', ha='right', va='center', transform=ax.transAxes)
    ax.text(0.98, 0.05, r'$\chi^2_m=%1.2f' % Chi2 + ',\ K=' + '%1.2f' % K + '$', ha='right', va='center', transform=ax.transAxes)

    # make legend with "model" label at the bottom
    handles, labels = ax.get_legend_handles_labels()
    handles.append(handles[0])
    del handles[0]
    labels.append(labels[0])
    del labels[0]
    ax.legend(handles, labels, loc='upper right', numpoints=1, frameon=True, scatterpoints=0)

    # data ranges, labels ...
    ax.set_ylim(-0.2, 0.2)
    ax.set_xlim(0.01, 2.6)
    ax.set_ylabel(r'$\Delta\Sigma/R_v\ \mathrm{[} 10^{12} h^2\, \mathrm{M_\odot Mpc^{-3}]}$')
    ax.set_xlabel(r'$r/R_v$')
    subplots_adjust(bottom=0.12, left=0.15, right=0.98, top=0.92)

    # make inset to show radius/z distribution of selection
    z = hdu[1].data["z"]
    radius = hdu[1].data["radius"]
    axi = axes([0.26, 0.65, 0.25, 0.25], axisbg='None')
    axi.scatter(z, radius, c='r', s=5,label='all voids', linewidths=0)
    axi.scatter(z[selection], radius[selection], c='k', s=10, label='selection', linewidths=0)
    axi.set_ylim(0,120)
    axi.set_xlim(0.025, 0.425)
    axi.set_xticks([0.1, 0.2, 0.3, 0.4])
    axi.set_yticks([0, 50, 100])
    axi.set_xlabel('$z$')
    axi.set_ylabel('$R_v$')

    show()
    hdu.close()
<|endoftext|>


<commit_before># Module offering inventory class for registering BoxTypes
#
#
from __future__ import absolute_import
from collections import defaultdict

from .paramDatGUI import ParamDatGUI

class BoxType(object):
    """
    A BoxType is a type of Box. A BoxType is to a Box what a
    parameterized class is to a parameterized instance.
    """

    def __init__(self, typeobj, nodetype='LabelledNode',
                 untyped=[], hidden=[], display_fn = None):
        self.typeobj = typeobj
        self.nodetype = nodetype
        self.display_fn = display_fn if display_fn else lambda x: {}

        self.name = typeobj.name

        self.untyped = set(getattr(typeobj, 'untyped', [])) | set(untyped)
        self.hidden = set(getattr(typeobj, 'hidden', [])) | set(hidden)

    def mode(self, name):
        """
        Return the port mode of the given parameter by name.
        """
        if name in self.untyped:
            return 'untyped'
        return 'hidden' if name in self.hidden else 'normal'

    def __call__(self, inventory, *args, **kwargs):
        return Box(self, inventory, *args, **kwargs)

class Box(object):
    """
    A Box is an instance of a BoxType. A Box is to a BoxType what a
    parameterized instance is to a parameterized class.
    """
    def __init__(self, boxtype, inventory, *args, **kwargs):
        self.boxtype = boxtype
        self.inventory = inventory

        self.instance = boxtype.typeobj(*args, **kwargs)
        self.name = self.instance.name

    def display(self):
        return self.boxtype.display_fn(self.instance)

    def propagate(self):
        return (self.instance.propagate()
                if hasattr(self.instance, 'propagate') else self.instance)

    def set_param(self, *args, **kwargs):
        self.instance.set_param(*args, **kwargs)

    def params(self):
        return self.instance.params()

    def __getitem__(self, name):
        return getattr(self.instance, name)

class Inventory(object):
    """
    The Inventory holds all available BoxType definitions and offers the
    means to serialize these definitions to JSON.
    """

    definitions = {}

    guis = {'datgui': ParamDatGUI }

    @classmethod
    def _boxlist(cls, blist):
        return [el if isinstance(el, BoxType) else BoxType(el) for el in blist]

    @classmethod
    def add(cls, group, definition):

        definition = definition if isinstance(definition, list) else [definition]
        boxtypes = cls._boxlist(definition)

        if group not in cls.definitions:
            cls.definitions[group] = defaultdict(list)

        for boxtype in boxtypes:
            cls.definitions[group][boxtype.nodetype].append(boxtype)

    @classmethod
    def lookup_boxtype(cls, name):
        """
        Find the appropriate BoxType associated with the given name.
        """
        for spec in cls.definitions.values():
            for boxlist in spec.values():
                for boxtype in boxlist:
                    if boxtype.name == name:
                        return boxtype

    @classmethod
    def json(cls, excluded, gui):
        """
        Generate a JSON-serializable parameter definitions for the given
        parameterized objects.
        """
        json_obj = {}
        for group, defs in cls.definitions.items():
            for nodetype, boxlist in defs.items():
                for boxtype in boxlist:
                    inputs = cls._json_params(boxtype, excluded, gui)
                    json_obj[boxtype.name] = {'inputs'  : inputs,
                                              'outputs' : [ {'name':'', 'lims':[],
                                                             'mode':'untyped-port'}],
                                              'nodetype': nodetype,
                                              'group'   : group }
        return json_obj

    @classmethod
    def _json_params(cls, boxtype, excluded, gui, min_precedence=0):
        """
        Return JSON-serializable list of parameter definitions using the
        chosen gui interface.
        """
        paramlist = cls.guis[gui].paramlist(boxtype.typeobj, min_precedence)
        json_params= [cls._json_param(name, p, boxtype.mode(name), gui=gui)
                      for (name, p) in paramlist if name not in excluded ]
        return [el for el in json_params if el]

    @classmethod
    def _json_param(cls, name, p, mode, gui):
        """
        Return the parameter mode string and None if parameter unsupported.
        """
        if mode != 'untyped' and not cls.guis[gui].supported(p):
            return None

        return {'name': name,
                'mode':  mode,
                'value': cls.guis[gui].param_default(p),
                'lims':  cls.guis[gui].param_lims(p),
                'step':  cls.guis[gui].param_step(p)}
<commit_msg>Extended docstring for Box class<commit_after># Module offering inventory class for registering BoxTypes
#
#
from __future__ import absolute_import
from collections import defaultdict

from .paramDatGUI import ParamDatGUI

class BoxType(object):
    """
    A BoxType is a type of Box. A BoxType is to a Box what a
    parameterized class is to a parameterized instance.
    """

    def __init__(self, typeobj, nodetype='LabelledNode',
                 untyped=[], hidden=[], display_fn = None):
        self.typeobj = typeobj
        self.nodetype = nodetype
        self.display_fn = display_fn if display_fn else lambda x: {}

        self.name = typeobj.name

        self.untyped = set(getattr(typeobj, 'untyped', [])) | set(untyped)
        self.hidden = set(getattr(typeobj, 'hidden', [])) | set(hidden)

    def mode(self, name):
        """
        Return the port mode of the given parameter by name.
        """
        if name in self.untyped:
            return 'untyped'
        return 'hidden' if name in self.hidden else 'normal'

    def __call__(self, inventory, *args, **kwargs):
        return Box(self, inventory, *args, **kwargs)

class Box(object):
    """
    A Box is an instance of a BoxType. A Box is to a BoxType what a
    parameterized instance is to a parameterized class.

    The purpose of a Box is to define the necessary portion of the param
    API needed from parameterized instance and to allow additional
    processing when setting parameters.
    """
    def __init__(self, boxtype, inventory, *args, **kwargs):
        self.boxtype = boxtype
        self.inventory = inventory

        self.instance = boxtype.typeobj(*args, **kwargs)
        self.name = self.instance.name

    def display(self):
        return self.boxtype.display_fn(self.instance)

    def propagate(self):
        return (self.instance.propagate()
                if hasattr(self.instance, 'propagate') else self.instance)

    def set_param(self, *args, **kwargs):
        self.instance.set_param(*args, **kwargs)

    def params(self):
        return self.instance.params()

    def __getitem__(self, name):
        return getattr(self.instance, name)

class Inventory(object):
    """
    The Inventory holds all available BoxType definitions and offers the
    means to serialize these definitions to JSON.
    """

    definitions = {}

    guis = {'datgui': ParamDatGUI }

    @classmethod
    def _boxlist(cls, blist):
        return [el if isinstance(el, BoxType) else BoxType(el) for el in blist]

    @classmethod
    def add(cls, group, definition):

        definition = definition if isinstance(definition, list) else [definition]
        boxtypes = cls._boxlist(definition)

        if group not in cls.definitions:
            cls.definitions[group] = defaultdict(list)

        for boxtype in boxtypes:
            cls.definitions[group][boxtype.nodetype].append(boxtype)

    @classmethod
    def lookup_boxtype(cls, name):
        """
        Find the appropriate BoxType associated with the given name.
        """
        for spec in cls.definitions.values():
            for boxlist in spec.values():
                for boxtype in boxlist:
                    if boxtype.name == name:
                        return boxtype

    @classmethod
    def json(cls, excluded, gui):
        """
        Generate a JSON-serializable parameter definitions for the given
        parameterized objects.
        """
        json_obj = {}
        for group, defs in cls.definitions.items():
            for nodetype, boxlist in defs.items():
                for boxtype in boxlist:
                    inputs = cls._json_params(boxtype, excluded, gui)
                    json_obj[boxtype.name] = {'inputs'  : inputs,
                                              'outputs' : [ {'name':'', 'lims':[],
                                                             'mode':'untyped-port'}],
                                              'nodetype': nodetype,
                                              'group'   : group }
        return json_obj

    @classmethod
    def _json_params(cls, boxtype, excluded, gui, min_precedence=0):
        """
        Return JSON-serializable list of parameter definitions using the
        chosen gui interface.
        """
        paramlist = cls.guis[gui].paramlist(boxtype.typeobj, min_precedence)
        json_params= [cls._json_param(name, p, boxtype.mode(name), gui=gui)
                      for (name, p) in paramlist if name not in excluded ]
        return [el for el in json_params if el]

    @classmethod
    def _json_param(cls, name, p, mode, gui):
        """
        Return the parameter mode string and None if parameter unsupported.
        """
        if mode != 'untyped' and not cls.guis[gui].supported(p):
            return None

        return {'name': name,
                'mode':  mode,
                'value': cls.guis[gui].param_default(p),
                'lims':  cls.guis[gui].param_lims(p),
                'step':  cls.guis[gui].param_step(p)}
<|endoftext|>


<commit_before>"""
Created on Mar 7, 2014

@author: tjoneslo
"""
import unittest
import re
import sys

sys.path.append('../PyRoute')
from Star import Star
from Galaxy import Sector, Galaxy
from TradeCalculation import TradeCalculation

class TestStar(unittest.TestCase):

    def setUp(self):
        pass

    def testParseIrkigkhan(self):
        sector = Sector(' Core', ' 0, 0')
        star1 = Star.parse_line_into_star(
            "0103 Irkigkhan            C9C4733-9 Fl                   { 0 }  (E69+0) [4726] B     - - 123 8  Im M2 V           ",
            sector, 'fixed', 'fixed')

        self.assertTrue(star1.sector.name == 'Core', star1.sector.name)
        self.assertTrue(star1.position == '0103')
        self.assertTrue(star1.q == 0 and star1.r == 2, "%s, %s" % (star1.q, star1.r))
        self.assertTrue(star1.name == 'Irkigkhan')
        self.assertTrue(star1.uwp == 'C9C4733-9')
        self.assertTrue(star1.alg == 'Im')
        self.assertTrue(star1.population == 10, "Population %s" % star1.population)
        self.assertTrue(star1.wtn == 9, "wtn %s" % star1.wtn)
        self.assertFalse(star1.tradeCode.industrial)
        self.assertFalse(star1.tradeCode.agricultural)
        self.assertFalse(star1.tradeCode.poor)
        self.assertFalse(star1.tradeCode.rich)
        self.assertTrue(star1.ggCount == 3)
        self.assertEqual(star1.star_list, ['M2 V'])

    def testParseIrkigkhanRUCollapse(self):
        sector = Sector('Core', ' 0, 0')
        star1 = Star.parse_line_into_star(
            "0103 Irkigkhan            C9C4733-9 Fl                   { 0 }  (E69+0) [4726] B     - - 123 8  Im M2 V           ",
            sector, 'fixed', 'fixed')

        expected = 756
        actual = star1.ru

        self.assertEqual(expected, actual)

    def testParseIrkigkhanRUScaled(self):
        sector = Sector('Core', ' 0, 0')
        star1 = Star.parse_line_into_star(
            "0103 Irkigkhan            C9C4733-9 Fl                   { 0 }  (E69+0) [4726] B     - - 123 8  Im M2 V           ",
            sector, 'fixed', 'fixed')

        expected = 756
        actual = star1.ru

        self.assertEqual(expected, actual)

    def testParseShanaMa(self):
        sector = Sector('Core', ' 0, 0')
        star1 = Star.parse_line_into_star(
            "0104 Shana Ma             E551112-7 Lo Po                { -3 } (301-3) [1113] B     - - 913 9  Im K2 IV M7 V     ",
            sector, 'fixed', 'fixed')

        self.assertTrue(star1.sector.name == 'Core', star1.sector.name)
        self.assertTrue(star1.position == '0104')
        self.assertTrue(star1.q == 0 and star1.r == 3, "%s, %s" % (star1.q, star1.r))
        self.assertTrue(star1.name == 'Shana Ma')
        self.assertTrue(star1.uwp == 'E551112-7')
        self.assertTrue(star1.alg == 'Im')
        self.assertTrue(star1.population == 0, "Population %s" % star1.population)
        self.assertTrue(star1.wtn == 2, "wtn %s" % star1.wtn)
        self.assertFalse(star1.tradeCode.industrial)
        self.assertFalse(star1.tradeCode.agricultural)
        self.assertTrue(star1.tradeCode.poor)
        self.assertFalse(star1.tradeCode.rich)
        self.assertTrue(star1.ggCount == 3)
        self.assertEqual(len(star1.star_list), 2)
        self.assertEqual(star1.star_list, ['K2 IV', 'M7 V'])

    def testParseSyss(self):
        sector = Sector('Core', ' 0, 0')
        star1 = Star.parse_line_into_star(
            "2323 Syss                 C400746-8 Na Va Pi                   { -1 } (A67-2) [6647] BD   S  - 510 5  ImDv M9 III D M5 V",
            sector, 'fixed', 'fixed')

        self.assertTrue(star1.sector.name == 'Core', star1.sector.name)
        self.assertEqual(star1.position, '2323')
        self.assertEqual(star1.name, 'Syss')
        self.assertEqual(star1.uwp, 'C400746-8')
        self.assertEqual(star1.stars, 'M9 III D M5 V')
        self.assertEqual(star1.star_list, ['M9 III', 'D', 'M5 V'])

    def testAPortModifier(self):
        # cwtn =[3,4,4,5,6,7,7,8,9,10,10,11,12,13,14,15]
        cwtn = [3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 13, 13, 14]
        for uwtn in range(15):
            wtn = int(round(max(0, (uwtn * 3 + 13) / 4)))
            self.assertTrue(wtn == cwtn[uwtn], "at %s: %s vs %s" % (uwtn, wtn, cwtn[uwtn]))

    def testBPortModifier(self):
        # cwtn =[2,3,4,5,5,6,7,8,8,9,10,11,11,12,12,13]
        cwtn = [2, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14]
        for uwtn in range(15):
            wtn = int(round(max(0, (uwtn * 3 + 11) / 4)))
            self.assertTrue(wtn == cwtn[uwtn], "at %s: %s vs %s" % (uwtn, wtn, cwtn[uwtn]))

    def testCPortModifier(self):
        cwtn = [2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 10, 10, 11, 11, 12]
        for uwtn in range(15):
            if (uwtn > 9):
                wtn = int(round(max(0, (uwtn + 9) / 2)))
            else:
                wtn = int(round(max(0, (uwtn * 3 + 9) / 4)))
            self.assertTrue(wtn == cwtn[uwtn], "at %s: %s vs %s" % (uwtn, wtn, cwtn[uwtn]))

    def testDPortModifier(self):
        cwtn = [1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11]
        for uwtn in range(15):
            if (uwtn > 7):
                wtn = int(round(max(0, (uwtn + 7) / 2)))
            else:
                wtn = int(round(max(0, (uwtn * 3 + 7) / 4)))
            self.assertTrue(wtn == cwtn[uwtn], "at %s: %s vs %s" % (uwtn, wtn, cwtn[uwtn]))

    def testEPortModifier(self):
        cwtn = [1, 2, 2, 3, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10]
        for uwtn in range(15):
            if (uwtn > 5):
                wtn = int(round(max(0, (uwtn + 5) / 2)))
            else:
                wtn = int(round(max(0, (uwtn * 3 + 5) / 4)))
            self.assertTrue(wtn == cwtn[uwtn], "at %s: %s vs %s" % (uwtn, wtn, cwtn[uwtn]))

    def testXPortModifier(self):
        # cwtn =[0,1,2,3,0,0,0,1,1,2,2,3,3,4,4,5]
        cwtn = [0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5]
        for uwtn in range(15):
            wtn = int(round(max(0, (uwtn - 5) / 2)))
            self.assertTrue(wtn == cwtn[uwtn], "at %s: %s vs %s" % (uwtn, wtn, cwtn[uwtn]))

    def testCalcTrade(self):
        self.assertEqual(TradeCalculation.calc_trade(0), 1)
        self.assertEqual(TradeCalculation.calc_trade(1), 5)
        self.assertEqual(TradeCalculation.calc_trade(2), 10)
        self.assertEqual(TradeCalculation.calc_trade(3), 50)
        self.assertEqual(TradeCalculation.calc_trade(4), 100)
        self.assertEqual(TradeCalculation.calc_trade(5), 500)
        self.assertEqual(TradeCalculation.calc_trade(6), 1000)

    def testPassengerBTN(self):
        self.assertEqual(TradeCalculation.calc_passengers(10), 0)
        self.assertEqual(TradeCalculation.calc_passengers(11), 5)
        self.assertEqual(TradeCalculation.calc_passengers(12), 10)
        self.assertEqual(TradeCalculation.calc_passengers(13), 50)
        self.assertEqual(TradeCalculation.calc_passengers(14), 100)
        self.assertEqual(TradeCalculation.calc_passengers(15), 500)

    def testHashValueSameAfterCaching(self):
        star1 = Star.parse_line_into_star(
            "0103 Irkigkhan            C9C4733-9 Fl                   { 0 }  (E69+0) [4726] B     - - 123 8  Im M2 V  ",
            Sector('Core', ' 0, 0'), 'fixed', 'fixed')

        # Grabbing hash value twice, once to seed Star._hash, second to dig it out of that cache
        oldHash = star1.__hash__()
        newHash = star1.__hash__()
        self.assertEqual(oldHash, newHash)

    def testEquals(self):
        star1 = Star.parse_line_into_star(
            "0103 Irkigkhan            C9C4733-9 Fl                   { 0 }  (E69+0) [4726] B     - - 123 8  Im M2 V           ",
            Sector('Core', ' 0, 0'), 'fixed', 'fixed')
        star2 = Star.parse_line_into_star(
            "0103 Irkigkhan            C9C4733-9 Fl                   { 0 }  (E69+0) [4726] B     - - 123 8  Im M2 V           ",
            Sector('Core', ' 0, 0'), 'fixed', 'fixed')
        star3 = Star.parse_line_into_star(
            "0104 Shana Ma             E551112-7 Lo Po                { -3 } (301-3) [1113] B     - - 913 9  Im K2 IV M7 V     ",
            Sector('Core', ' 0, 0'), 'fixed', 'fixed')

        self.assertEqual(star1, star2)
        self.assertNotEqual(star1, star3)
        self.assertNotEqual(star2, star3)

    def testStarSize(self):
        star1 = Star.parse_line_into_star(
            "0104 Shana Ma             E551112-7 Lo Po                { -3 } (301-3) [1113] B     - - 913 9  Im K2 IV M7 V     ",
            Sector(' Core', ' 0, 0'), 'fixed', 'fixed')

if __name__ == "__main__":
    # import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
<commit_msg>Fix errors in tests<commit_after>"""
Created on Mar 7, 2014

@author: tjoneslo
"""
import unittest
import re
import sys

sys.path.append('../PyRoute')
from Star import Star
from Galaxy import Sector, Galaxy
from TradeCalculation import TradeCalculation

class TestStar(unittest.TestCase):

    def setUp(self):
        pass

    def testParseIrkigkhan(self):
        sector = Sector(' Core', ' 0, 0')
        star1 = Star.parse_line_into_star(
            "0103 Irkigkhan            C9C4733-9 Fl                   { 0 }  (E69+0) [4726] B     - - 123 8  Im M2 V           ",
            sector, 'fixed', 'fixed')

        self.assertTrue(star1.sector.name == 'Core', star1.sector.name)
        self.assertTrue(star1.position == '0103')
        self.assertTrue(star1.q == 0 and star1.r == 2, "%s, %s" % (star1.q, star1.r))
        self.assertTrue(star1.name == 'Irkigkhan')
        self.assertTrue(star1.uwp == 'C9C4733-9')
        self.assertTrue(star1.alg_code == 'Im')
        self.assertTrue(star1.population == 10, "Population %s" % star1.population)
        self.assertTrue(star1.wtn == 9, "wtn %s" % star1.wtn)
        self.assertFalse(star1.tradeCode.industrial)
        self.assertFalse(star1.tradeCode.agricultural)
        self.assertFalse(star1.tradeCode.poor)
        self.assertFalse(star1.tradeCode.rich)
        self.assertTrue(star1.ggCount == 3)
        self.assertEqual(star1.star_list, ['M2 V'])

    def testParseIrkigkhanRUCollapse(self):
        sector = Sector(' Core', ' 0, 0')
        star1 = Star.parse_line_into_star(
            "0103 Irkigkhan            C9C4733-9 Fl                   { 0 }  (E69+0) [4726] B     - - 123 8  Im M2 V           ",
            sector, 'fixed', 'fixed')

        expected = 756
        actual = star1.ru

        self.assertEqual(expected, actual)

    def testParseIrkigkhanRUScaled(self):
        sector = Sector(' Core', ' 0, 0')
        star1 = Star.parse_line_into_star(
            "0103 Irkigkhan            C9C4733-9 Fl                   { 0 }  (E69+0) [4726] B     - - 123 8  Im M2 V           ",
            sector, 'fixed', 'fixed')

        expected = 756
        actual = star1.ru

        self.assertEqual(expected, actual)

    def testParseShanaMa(self):
        sector = Sector(' Core', ' 0, 0')
        star1 = Star.parse_line_into_star(
            "0104 Shana Ma             E551112-7 Lo Po                { -3 } (301-3) [1113] B     - - 913 9  Im K2 IV M7 V     ",
            sector, 'fixed', 'fixed')

        self.assertTrue(star1.sector.name == 'Core', star1.sector.name)
        self.assertTrue(star1.position == '0104')
        self.assertTrue(star1.q == 0 and star1.r == 3, "%s, %s" % (star1.q, star1.r))
        self.assertTrue(star1.name == 'Shana Ma')
        self.assertTrue(star1.uwp == 'E551112-7')
        self.assertTrue(star1.alg_code == 'Im')
        self.assertTrue(star1.population == 0, "Population %s" % star1.population)
        self.assertTrue(star1.wtn == 2, "wtn %s" % star1.wtn)
        self.assertFalse(star1.tradeCode.industrial)
        self.assertFalse(star1.tradeCode.agricultural)
        self.assertTrue(star1.tradeCode.poor)
        self.assertFalse(star1.tradeCode.rich)
        self.assertTrue(star1.ggCount == 3)
        self.assertEqual(len(star1.star_list), 2)
        self.assertEqual(star1.star_list, ['K2 IV', 'M7 V'])

    def testParseSyss(self):
        sector = Sector(' Core', ' 0, 0')
        star1 = Star.parse_line_into_star(
            "2323 Syss                 C400746-8 Na Va Pi                   { -1 } (A67-2) [6647] BD   S  - 510 5  ImDv M9 III D M5 V",
            sector, 'fixed', 'fixed')

        self.assertTrue(star1.sector.name == 'Core', star1.sector.name)
        self.assertEqual(star1.position, '2323')
        self.assertEqual(star1.name, 'Syss')
        self.assertEqual(star1.uwp, 'C400746-8')
        self.assertEqual(star1.stars, 'M9 III D M5 V')
        self.assertEqual(star1.star_list, ['M9 III', 'D', 'M5 V'])

    def testAPortModifier(self):
        # cwtn =[3,4,4,5,6,7,7,8,9,10,10,11,12,13,14,15]
        cwtn = [3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 13, 13, 14]
        for uwtn in range(15):
            wtn = int(round(max(0, (uwtn * 3 + 13) / 4)))
            self.assertTrue(wtn == cwtn[uwtn], "at %s: %s vs %s" % (uwtn, wtn, cwtn[uwtn]))

    def testBPortModifier(self):
        # cwtn =[2,3,4,5,5,6,7,8,8,9,10,11,11,12,12,13]
        cwtn = [2, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14]
        for uwtn in range(15):
            wtn = int(round(max(0, (uwtn * 3 + 11) / 4)))
            self.assertTrue(wtn == cwtn[uwtn], "at %s: %s vs %s" % (uwtn, wtn, cwtn[uwtn]))

    def testCPortModifier(self):
        cwtn = [2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 10, 10, 11, 11, 12]
        for uwtn in range(15):
            if (uwtn > 9):
                wtn = int(round(max(0, (uwtn + 9) / 2)))
            else:
                wtn = int(round(max(0, (uwtn * 3 + 9) / 4)))
            self.assertTrue(wtn == cwtn[uwtn], "at %s: %s vs %s" % (uwtn, wtn, cwtn[uwtn]))

    def testDPortModifier(self):
        cwtn = [1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11]
        for uwtn in range(15):
            if (uwtn > 7):
                wtn = int(round(max(0, (uwtn + 7) / 2)))
            else:
                wtn = int(round(max(0, (uwtn * 3 + 7) / 4)))
            self.assertTrue(wtn == cwtn[uwtn], "at %s: %s vs %s" % (uwtn, wtn, cwtn[uwtn]))

    def testEPortModifier(self):
        cwtn = [1, 2, 2, 3, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10]
        for uwtn in range(15):
            if (uwtn > 5):
                wtn = int(round(max(0, (uwtn + 5) / 2)))
            else:
                wtn = int(round(max(0, (uwtn * 3 + 5) / 4)))
            self.assertTrue(wtn == cwtn[uwtn], "at %s: %s vs %s" % (uwtn, wtn, cwtn[uwtn]))

    def testXPortModifier(self):
        # cwtn =[0,1,2,3,0,0,0,1,1,2,2,3,3,4,4,5]
        cwtn = [0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5]
        for uwtn in range(15):
            wtn = int(round(max(0, (uwtn - 5) / 2)))
            self.assertTrue(wtn == cwtn[uwtn], "at %s: %s vs %s" % (uwtn, wtn, cwtn[uwtn]))

    def testCalcTrade(self):
        self.assertEqual(TradeCalculation.calc_trade(0), 1)
        self.assertEqual(TradeCalculation.calc_trade(1), 5)
        self.assertEqual(TradeCalculation.calc_trade(2), 10)
        self.assertEqual(TradeCalculation.calc_trade(3), 50)
        self.assertEqual(TradeCalculation.calc_trade(4), 100)
        self.assertEqual(TradeCalculation.calc_trade(5), 500)
        self.assertEqual(TradeCalculation.calc_trade(6), 1000)

    def testPassengerBTN(self):
        self.assertEqual(TradeCalculation.calc_passengers(10), 0)
        self.assertEqual(TradeCalculation.calc_passengers(11), 5)
        self.assertEqual(TradeCalculation.calc_passengers(12), 10)
        self.assertEqual(TradeCalculation.calc_passengers(13), 50)
        self.assertEqual(TradeCalculation.calc_passengers(14), 100)
        self.assertEqual(TradeCalculation.calc_passengers(15), 500)

    def testHashValueSameAfterCaching(self):
        star1 = Star.parse_line_into_star(
            "0103 Irkigkhan            C9C4733-9 Fl                   { 0 }  (E69+0) [4726] B     - - 123 8  Im M2 V  ",
            Sector(' Core', ' 0, 0'), 'fixed', 'fixed')

        # Grabbing hash value twice, once to seed Star._hash, second to dig it out of that cache
        oldHash = star1.__hash__()
        newHash = star1.__hash__()
        self.assertEqual(oldHash, newHash)

    def testEquals(self):
        star1 = Star.parse_line_into_star(
            "0103 Irkigkhan            C9C4733-9 Fl                   { 0 }  (E69+0) [4726] B     - - 123 8  Im M2 V           ",
            Sector(' Core', ' 0, 0'), 'fixed', 'fixed')
        star2 = Star.parse_line_into_star(
            "0103 Irkigkhan            C9C4733-9 Fl                   { 0 }  (E69+0) [4726] B     - - 123 8  Im M2 V           ",
            Sector(' Core', ' 0, 0'), 'fixed', 'fixed')
        star3 = Star.parse_line_into_star(
            "0104 Shana Ma             E551112-7 Lo Po                { -3 } (301-3) [1113] B     - - 913 9  Im K2 IV M7 V     ",
            Sector(' Core', ' 0, 0'), 'fixed', 'fixed')

        self.assertEqual(star1, star2)
        self.assertNotEqual(star1, star3)
        self.assertNotEqual(star2, star3)

    def testStarSize(self):
        star1 = Star.parse_line_into_star(
            "0104 Shana Ma             E551112-7 Lo Po                { -3 } (301-3) [1113] B     - - 913 9  Im K2 IV M7 V     ",
            Sector(' Core', ' 0, 0'), 'fixed', 'fixed')

if __name__ == "__main__":
    # import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
<|endoftext|>


<commit_before>from shfi_otf_pipeline import make_apex_cubes
from os.path import join
from astropy import log
import time
import subprocess

root = '/scratch/aginsbur/apex/'
rawpath = join(root,'raw/')
reducedpath = join(root,'reduced/')
make_apex_cubes.june2013datapath = rawpath
make_apex_cubes.june2013path = join(reducedpath,'june2013/')
#make_apex_cubes.april2014path = join(reducedpath,'april2014/')
make_apex_cubes.april2014path = rawpath
make_apex_cubes.h2copath = join(reducedpath, 'h2co_cubes/')
make_apex_cubes.mergepath = join(reducedpath, 'merged_datasets/')
make_apex_cubes.aorawpath = rawpath
make_apex_cubes.aopath = join(reducedpath, '2010_reduced/')
make_apex_cubes.diagplotdir = join(root,'diagnostic_plots/')

try:
    label = "_v"+subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip()
except CalledProcessError:
    label = ""

logfile = ("".join([time.strftime("apexcmzpipeline{0}_%y_%m_%d_%H:%M:%S"),".log"])).format(label)

#with log.log_to_file(logfile.replace(".log","_do_everything_default.log")):
#   make_apex_cubes.do_everything()

# with log.log_to_file(logfile.replace(".log","_default.log")):
#    make_apex_cubes.make_high_mergecube(mergefile2='APEX_H2CO_merge_high_default')
# with log.log_to_file(logfile.replace(".log","_nopca.log")):
#    make_apex_cubes.make_high_mergecube(pca_clean=False, mergefile2='APEX_H2CO_merge_high_nopca')
# with log.log_to_file(logfile.replace(".log","_timepca.log")):
#    make_apex_cubes.make_high_mergecube(timewise_pca=True, mergefile2='APEX_H2CO_merge_high_timepca')
# with log.log_to_file(logfile.replace(".log","_unclean.log")):
#    make_apex_cubes.make_high_mergecube(pca_clean={'2014':False, '2013':False, 'ao':False},
#                                        scanblsub={'2014':False, '2013':False, 'ao':False},
#                                        timewise_pca={'2014':False, '2013':False, 'ao':False},
#                                        mergefile2='APEX_H2CO_merge_high_nopca_noscanblsub')

#/scratch/aginsbur/apex/reduced/april2014/M-093.F-0009-2014-2014-04/M-093.F-0009-2014-2014-05-08.apex
#/scratch/aginsbur/apex/APEX_CMZ_H2CO/raw_data/M-093.F-0009-2014-2014-05-08.apex
#/scratch/aginsbur/apex/raw/M-093.F-0009-2014-2014-05-08.apex
#/scratch/aginsbur/apex/raw/M-093.F-0009-2014-2014-04/M-093.F-0009-2014-2014-05-08.apex
<commit_msg>add another option: 2014 can be cleaned?<commit_after>from shfi_otf_pipeline import make_apex_cubes
from os.path import join
from astropy import log
import time
import subprocess

root = '/scratch/aginsbur/apex/'
rawpath = join(root,'raw/')
reducedpath = join(root,'reduced/')
make_apex_cubes.june2013datapath = rawpath
make_apex_cubes.june2013path = join(reducedpath,'june2013/')
#make_apex_cubes.april2014path = join(reducedpath,'april2014/')
make_apex_cubes.april2014path = rawpath
make_apex_cubes.h2copath = join(reducedpath, 'h2co_cubes/')
make_apex_cubes.mergepath = join(reducedpath, 'merged_datasets/')
make_apex_cubes.aorawpath = rawpath
make_apex_cubes.aopath = join(reducedpath, '2010_reduced/')
make_apex_cubes.diagplotdir = join(root,'diagnostic_plots/')

try:
    label = "_v"+subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip()
except CalledProcessError:
    label = ""

logfile = ("".join([time.strftime("apexcmzpipeline{0}_%y_%m_%d_%H:%M:%S"),".log"])).format(label)

#with log.log_to_file(logfile.replace(".log","_do_everything_default.log")):
#   make_apex_cubes.do_everything()

# with log.log_to_file(logfile.replace(".log","_default.log")):
#    make_apex_cubes.make_high_mergecube(mergefile2='APEX_H2CO_merge_high_default')
# with log.log_to_file(logfile.replace(".log","_nopca.log")):
#    make_apex_cubes.make_high_mergecube(pca_clean=False, mergefile2='APEX_H2CO_merge_high_nopca')
# with log.log_to_file(logfile.replace(".log","_timepca.log")):
#    make_apex_cubes.make_high_mergecube(timewise_pca=True, mergefile2='APEX_H2CO_merge_high_timepca')
# with log.log_to_file(logfile.replace(".log","_unclean.log")):
#    make_apex_cubes.make_high_mergecube(pca_clean={'2014':False, '2013':False, 'ao':False},
#                                        scanblsub={'2014':False, '2013':False, 'ao':False},
#                                        timewise_pca={'2014':False, '2013':False, 'ao':False},
#                                        mergefile2='APEX_H2CO_merge_high_nopca_noscanblsub')
# with log.log_to_file(logfile.replace(".log","_only_clean_2014.log")):
#    make_apex_cubes.make_high_mergecube(pca_clean={'2014':True, '2013':False, 'ao':False},
#                                        scanblsub={'2014':False, '2013':False, 'ao':False},
#                                        timewise_pca={'2014':True, '2013':False, 'ao':False},
#                                        mergefile2='APEX_H2CO_merge_high_pca2014only')
#

#/scratch/aginsbur/apex/reduced/april2014/M-093.F-0009-2014-2014-04/M-093.F-0009-2014-2014-05-08.apex
#/scratch/aginsbur/apex/APEX_CMZ_H2CO/raw_data/M-093.F-0009-2014-2014-05-08.apex
#/scratch/aginsbur/apex/raw/M-093.F-0009-2014-2014-05-08.apex
#/scratch/aginsbur/apex/raw/M-093.F-0009-2014-2014-04/M-093.F-0009-2014-2014-05-08.apex
<|endoftext|>