<commit_before># Implements a simple configuration interface on top of Kconfiglib to
# demonstrate concepts for building a menuconfig-like. Emulates how the
# standard menuconfig prints menu entries.
#
# Always displays the entire Kconfig tree to keep things as simple as possible
# (all symbols, choices, menus, and comments).
#
# Usage:
#
#   $ python(3) Kconfiglib/examples/menuconfig.py <Kconfig file>
#
# A sample Kconfig is available in Kconfiglib/examples/Kmenuconfig.
#
# Here's a notation guide. The notation matches the one used by menuconfig
# (scripts/kconfig/mconf):
#
#   [ ] prompt      - Bool
#   < > prompt      - Tristate
#   {M} prompt      - Tristate selected to m. Can only be set to m or y.
#   -*- prompt      - Bool/tristate selected to y, pinning it
#   -M- prompt      - Tristate selected to m that also has m visibility,
#                     pinning it to m
#   (foo) prompt    - String/int/hex symbol with value "foo"
#   --> prompt      - The selected symbol in a choice in y mode. This
#                     syntax is unique to this example.
#
# When modules are disabled, the .type attribute of TRISTATE symbols and
# choices automatically changes to BOOL. This trick is used by the C
# implementation as well, and gives the expected behavior without having to do
# anything extra here. The original type is available in .orig_type if needed.
#
# The Kconfiglib/examples/Kmenuconfig example uses named choices to be able to
# refer to choices by name. Named choices are supported in the C tools too, but
# I don't think I've ever seen them used in the wild.
#
# Sample session:
#
#   $ python Kconfiglib/examples/menuconfig.py Kconfiglib/examples/Kmenuconfig
#
#   ======== Example Kconfig configuration ========
#
#   [*] Enable loadable module support (MODULES)
#       Bool and tristate symbols
#           [*] Bool symbol (BOOL)
#                   [ ] Dependent bool symbol (BOOL_DEP)
#                   < > Dependent tristate symbol (TRI_DEP)
#                   [ ] First prompt (TWO_MENU_NODES)
#           < > Tristate symbol (TRI)
#           [ ] Second prompt (TWO_MENU_NODES)
#               *** These are selected by TRI_DEP ***
#           < > Tristate selected by TRI_DEP (SELECTED_BY_TRI_DEP)
#           < > Tristate implied by TRI_DEP (IMPLIED_BY_TRI_DEP)
#       String, int, and hex symbols
#           (foo) String symbol (STRING)
#           (747) Int symbol (INT)
#           (0xABC) Hex symbol (HEX)
#       Various choices
#           -*- Bool choice (BOOL_CHOICE)
#                   --> Bool choice sym 1 (BOOL_CHOICE_SYM_1)
#                       Bool choice sym 2 (BOOL_CHOICE_SYM_2)
#           {M} Tristate choice (TRI_CHOICE)
#                   < > Tristate choice sym 1 (TRI_CHOICE_SYM_1)
#                   < > Tristate choice sym 2 (TRI_CHOICE_SYM_2)
#           [ ] Optional bool choice (OPT_BOOL_CHOICE)
#
#   Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): BOOL
#   Value for BOOL (available: n, y): n
#
#   ======== Example Kconfig configuration ========
#
#   [*] Enable loadable module support (MODULES)
#       Bool and tristate symbols
#           [ ] Bool symbol (BOOL)
#           < > Tristate symbol (TRI)
#           [ ] Second prompt (TWO_MENU_NODES)
#               *** These are selected by TRI_DEP ***
#           < > Tristate selected by TRI_DEP (SELECTED_BY_TRI_DEP)
#           < > Tristate implied by TRI_DEP (IMPLIED_BY_TRI_DEP)
#       String, int, and hex symbols
#           (foo) String symbol (STRING)
#           (747) Int symbol (INT)
#           (0xABC) Hex symbol (HEX)
#       Various choices
#           -*- Bool choice (BOOL_CHOICE)
#                   --> Bool choice sym 1 (BOOL_CHOICE_SYM_1)
#                       Bool choice sym 2 (BOOL_CHOICE_SYM_2)
#           {M} Tristate choice (TRI_CHOICE)
#                   < > Tristate choice sym 1 (TRI_CHOICE_SYM_1)
#                   < > Tristate choice sym 2 (TRI_CHOICE_SYM_2)
#          [ ] Optional bool choice (OPT_BOOL_CHOICE)
#
#   Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): MODULES
#   Value for MODULES (available: n, y): n
#
#   ======== Example Kconfig configuration ========
#
#   [ ] Enable loadable module support (MODULES)
#       Bool and tristate symbols
#           [ ] Bool symbol (BOOL)
#           [ ] Tristate symbol (TRI)
#           [ ] Second prompt (TWO_MENU_NODES)
#               *** These are selected by TRI_DEP ***
#           [ ] Tristate selected by TRI_DEP (SELECTED_BY_TRI_DEP)
#           [ ] Tristate implied by TRI_DEP (IMPLIED_BY_TRI_DEP)
#       String, int, and hex symbols
#           (foo) String symbol (STRING)
#           (747) Int symbol (INT)
#           (0xABC) Hex symbol (HEX)
#       Various choices
#           -*- Bool choice (BOOL_CHOICE)
#                   --> Bool choice sym 1 (BOOL_CHOICE_SYM_1)
#                       Bool choice sym 2 (BOOL_CHOICE_SYM_2)
#           -*- Tristate choice (TRI_CHOICE)
#                   --> Tristate choice sym 1 (TRI_CHOICE_SYM_1)
#                       Tristate choice sym 2 (TRI_CHOICE_SYM_2)
#           [ ] Optional bool choice (OPT_BOOL_CHOICE)
#
#   Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): ^D

from kconfiglib import Kconfig, \
                       Symbol, Choice, MENU, COMMENT, \
                       BOOL, TRISTATE, STRING, INT, HEX, UNKNOWN, \
                       expr_value, \
                       TRI_TO_STR, STR_TO_TRI
import readline
import sys

# Python 2/3 compatibility hack
if sys.version_info[0] < 3:
    input = raw_input

def indent_print(s, indent):
    print((" " * indent) + s)

def value_str(sc):
    """
    Returns the value part ("[*]", "<M>", "(foo)" etc.) of a menu entry.

    sc: Symbol or Choice.
    """
    if sc.type in (STRING, INT, HEX):
        return "({})".format(sc.str_value)

    # BOOL or TRISTATE

    # The choice mode acts as an upper bound on the visibility of choice
    # symbols, so we can check the choice symbols' own visibility to see if the
    # choice is in y mode
    if isinstance(sc, Symbol) and sc.choice and sc.visibility == 2:
        # For choices in y mode, print '-->' next to the selected symbol
        if sc.choice.selection is sc:
            return "-->"
        return "   "

    tri_val_str = {0: " ", 1: "M", 2: "*"}[sc.tri_value]

    if len(sc.assignable) == 1:
        # Pinned to a single value
        return "-{}-".format(tri_val_str)

    if sc.type == BOOL:
        return "[{}]".format(tri_val_str)

    if sc.type == TRISTATE:
        if sc.assignable == (1, 2):
            # m and y available
            return "{" + tri_val_str + "}"  # Gets a bit confusing with .format()
        return "<{}>".format(tri_val_str)

def node_str(node):
    """
    Returns the complete menu entry text for a menu node, or "" for invisible
    menu nodes. Invisible menu nodes are those that lack a prompt or don't have
    a satisfied prompt condition.

    Example return value: "[*] Bool symbol (BOOL)"

    The symbol name is printed in parentheses to the right of the prompt. This
    is so that symbols can easily be referred to in the configuration
    interface.
    """
    if not node.prompt:
        return ""

    # Even for menu nodes for symbols and choices, it's wrong to check
    # Symbol.visibility / Choice.visibility here. The reason is that a symbol
    # (and a choice, in theory) can be defined in multiple locations, giving it
    # multiple menu nodes, which do not necessarily all have the same prompt
    # visibility. Symbol.visibility / Choice.visibility is calculated as the OR
    # of the visibility of all the prompts.
    prompt, prompt_cond = node.prompt
    if not expr_value(prompt_cond):
        return ""

    if node.item == MENU:
        return "    " + prompt

    if node.item == COMMENT:
        return "    *** {} ***".format(prompt)

    # Symbol or Choice

    sc = node.item

    if sc.type == UNKNOWN:
        # Skip symbols defined without a type
        return ""

    # {:3} sets the field width to three. Gives nice alignment for empty string
    # values.
    res = "{:3} {}".format(value_str(sc), prompt)

    # Don't print the name for unnamed choices (the normal kind)
    if sc.name is not None:
        res += " ({})".format(sc.name)

    return res

def print_menuconfig_nodes(node, indent):
    """
    Prints a tree with all the menu entries rooted at 'node'. Child menu
    entries are indented.
    """
    while node:
        string = node_str(node)
        if string:
            indent_print(string, indent)

        if node.list:
            print_menuconfig_nodes(node.list, indent + 8)

        node = node.next

def print_menuconfig(kconf):
    """
    Prints all menu entries for the configuration.
    """
    # Print the expanded mainmenu text at the top. This is the same as
    # kconf.top_node.prompt[0], but with variable references expanded.
    print("\n======== {} ========\n".format(kconf.mainmenu_text))

    print_menuconfig_nodes(kconf.top_node.list, 0)
    print("")

def get_value_from_user(sc):
    """
    Prompts the user for a value for the symbol or choice 'sc'. For
    bool/tristate symbols and choices, provides a list of all the assignable
    values.
    """
    if not sc.visibility:
        print(sc.name + " is not currently visible")
        return False

    prompt = "Value for {}".format(sc.name)
    if sc.type in (BOOL, TRISTATE):
        prompt += " (available: {})" \
                  .format(", ".join([TRI_TO_STR[val] for val in sc.assignable]))
    prompt += ": "

    val_str = input(prompt).strip()
    if sc.type in (BOOL, TRISTATE):
        if val_str not in STR_TO_TRI:
            print("'{}' is not a valid tristate value".format(val_str))
            return False

        # I was thinking of having set_value() accept "n", "m", "y" as well as
        # a convenience for BOOL / TRISTATE symbols. Consistently using 0, 1, 2
        # makes the format clearer though. That's the best format in all ways
        # except for readability (where it isn't horrible either).
        val = STR_TO_TRI[val_str]
    else:
        val = val_str

    # Automatically add a "0x" prefix for hex symbols, like the menuconfig
    # interface does. This isn't done when loading .config files, hence why
    # set_value() doesn't do it automatically.
    if sc.type == HEX and not val.startswith(("0x", "0X")):
        val = "0x" + val

    # Let Kconfiglib itself print a warning here if the value is invalid. We
    # could also disable warnings temporarily with
    # kconf.disable_warnings() / kconf.enable_warnings() and print our own
    # warning.
    return sc.set_value(val)

if __name__ == "__main__":
    if len(sys.argv) != 2:
        sys.exit("usage: menuconfig.py <Kconfig file>")

    # Load Kconfig configuration files
    kconf = Kconfig(sys.argv[1])

    # Print the initial configuration tree
    print_menuconfig(kconf)

    while True:
        try:
            cmd = input('Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): ') \
                  .strip()
        except EOFError:
            print("")
            break

        if cmd == "load_config":
            config_filename = input(".config file to load: ")

            try:
                kconf.load_config(config_filename)
            except IOError as e:
                # Print the (spammy) error from Kconfiglib itself
                print(e.message + "\n")
            else:
                print("Configuration loaded from " + config_filename)

            print_menuconfig(kconf)
            continue

        if cmd == "write_config":
            config_filename = input("To this file: ")

            try:
                kconf.write_config(config_filename)
            except IOError as e:
                print(e.message)
            else:
                print("Configuration written to " + config_filename)

            continue

        # Assume 'cmd' is the name of a symbol or choice if it isn't one of the
        # commands above, prompt the user for a value for it, and print the new
        # configuration tree

        if cmd in kconf.syms:
            if get_value_from_user(kconf.syms[cmd]):
                print_menuconfig(kconf)

            continue

        if cmd in kconf.named_choices:
            if get_value_from_user(kconf.named_choices[cmd]):
                print_menuconfig(kconf)

            continue

        print("No symbol/choice named '{}' in the configuration"
              .format(cmd))
<commit_msg>Simplify menuconfig.py's value_str() a bit<commit_after># Implements a simple configuration interface on top of Kconfiglib to
# demonstrate concepts for building a menuconfig-like. Emulates how the
# standard menuconfig prints menu entries.
#
# Always displays the entire Kconfig tree to keep things as simple as possible
# (all symbols, choices, menus, and comments).
#
# Usage:
#
#   $ python(3) Kconfiglib/examples/menuconfig.py <Kconfig file>
#
# A sample Kconfig is available in Kconfiglib/examples/Kmenuconfig.
#
# Here's a notation guide. The notation matches the one used by menuconfig
# (scripts/kconfig/mconf):
#
#   [ ] prompt      - Bool
#   < > prompt      - Tristate
#   {M} prompt      - Tristate selected to m. Can only be set to m or y.
#   -*- prompt      - Bool/tristate selected to y, pinning it
#   -M- prompt      - Tristate selected to m that also has m visibility,
#                     pinning it to m
#   (foo) prompt    - String/int/hex symbol with value "foo"
#   --> prompt      - The selected symbol in a choice in y mode. This
#                     syntax is unique to this example.
#
# When modules are disabled, the .type attribute of TRISTATE symbols and
# choices automatically changes to BOOL. This trick is used by the C
# implementation as well, and gives the expected behavior without having to do
# anything extra here. The original type is available in .orig_type if needed.
#
# The Kconfiglib/examples/Kmenuconfig example uses named choices to be able to
# refer to choices by name. Named choices are supported in the C tools too, but
# I don't think I've ever seen them used in the wild.
#
# Sample session:
#
#   $ python Kconfiglib/examples/menuconfig.py Kconfiglib/examples/Kmenuconfig
#
#   ======== Example Kconfig configuration ========
#
#   [*] Enable loadable module support (MODULES)
#       Bool and tristate symbols
#           [*] Bool symbol (BOOL)
#                   [ ] Dependent bool symbol (BOOL_DEP)
#                   < > Dependent tristate symbol (TRI_DEP)
#                   [ ] First prompt (TWO_MENU_NODES)
#           < > Tristate symbol (TRI)
#           [ ] Second prompt (TWO_MENU_NODES)
#               *** These are selected by TRI_DEP ***
#           < > Tristate selected by TRI_DEP (SELECTED_BY_TRI_DEP)
#           < > Tristate implied by TRI_DEP (IMPLIED_BY_TRI_DEP)
#       String, int, and hex symbols
#           (foo) String symbol (STRING)
#           (747) Int symbol (INT)
#           (0xABC) Hex symbol (HEX)
#       Various choices
#           -*- Bool choice (BOOL_CHOICE)
#                   --> Bool choice sym 1 (BOOL_CHOICE_SYM_1)
#                       Bool choice sym 2 (BOOL_CHOICE_SYM_2)
#           {M} Tristate choice (TRI_CHOICE)
#                   < > Tristate choice sym 1 (TRI_CHOICE_SYM_1)
#                   < > Tristate choice sym 2 (TRI_CHOICE_SYM_2)
#           [ ] Optional bool choice (OPT_BOOL_CHOICE)
#
#   Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): BOOL
#   Value for BOOL (available: n, y): n
#
#   ======== Example Kconfig configuration ========
#
#   [*] Enable loadable module support (MODULES)
#       Bool and tristate symbols
#           [ ] Bool symbol (BOOL)
#           < > Tristate symbol (TRI)
#           [ ] Second prompt (TWO_MENU_NODES)
#               *** These are selected by TRI_DEP ***
#           < > Tristate selected by TRI_DEP (SELECTED_BY_TRI_DEP)
#           < > Tristate implied by TRI_DEP (IMPLIED_BY_TRI_DEP)
#       String, int, and hex symbols
#           (foo) String symbol (STRING)
#           (747) Int symbol (INT)
#           (0xABC) Hex symbol (HEX)
#       Various choices
#           -*- Bool choice (BOOL_CHOICE)
#                   --> Bool choice sym 1 (BOOL_CHOICE_SYM_1)
#                       Bool choice sym 2 (BOOL_CHOICE_SYM_2)
#           {M} Tristate choice (TRI_CHOICE)
#                   < > Tristate choice sym 1 (TRI_CHOICE_SYM_1)
#                   < > Tristate choice sym 2 (TRI_CHOICE_SYM_2)
#          [ ] Optional bool choice (OPT_BOOL_CHOICE)
#
#   Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): MODULES
#   Value for MODULES (available: n, y): n
#
#   ======== Example Kconfig configuration ========
#
#   [ ] Enable loadable module support (MODULES)
#       Bool and tristate symbols
#           [ ] Bool symbol (BOOL)
#           [ ] Tristate symbol (TRI)
#           [ ] Second prompt (TWO_MENU_NODES)
#               *** These are selected by TRI_DEP ***
#           [ ] Tristate selected by TRI_DEP (SELECTED_BY_TRI_DEP)
#           [ ] Tristate implied by TRI_DEP (IMPLIED_BY_TRI_DEP)
#       String, int, and hex symbols
#           (foo) String symbol (STRING)
#           (747) Int symbol (INT)
#           (0xABC) Hex symbol (HEX)
#       Various choices
#           -*- Bool choice (BOOL_CHOICE)
#                   --> Bool choice sym 1 (BOOL_CHOICE_SYM_1)
#                       Bool choice sym 2 (BOOL_CHOICE_SYM_2)
#           -*- Tristate choice (TRI_CHOICE)
#                   --> Tristate choice sym 1 (TRI_CHOICE_SYM_1)
#                       Tristate choice sym 2 (TRI_CHOICE_SYM_2)
#           [ ] Optional bool choice (OPT_BOOL_CHOICE)
#
#   Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): ^D

from kconfiglib import Kconfig, \
                       Symbol, Choice, MENU, COMMENT, \
                       BOOL, TRISTATE, STRING, INT, HEX, UNKNOWN, \
                       expr_value, \
                       TRI_TO_STR, STR_TO_TRI
import readline
import sys

# Python 2/3 compatibility hack
if sys.version_info[0] < 3:
    input = raw_input

def indent_print(s, indent):
    print((" " * indent) + s)

def value_str(sc):
    """
    Returns the value part ("[*]", "<M>", "(foo)" etc.) of a menu entry.

    sc: Symbol or Choice.
    """
    if sc.type in (STRING, INT, HEX):
        return "({})".format(sc.str_value)

    # BOOL or TRISTATE

    # The choice mode acts as an upper bound on the visibility of choice
    # symbols, so we can check the choice symbols' own visibility to see if the
    # choice is in y mode
    if isinstance(sc, Symbol) and sc.choice and sc.visibility == 2:
        # For choices in y mode, print '-->' next to the selected symbol
        if sc.choice.selection is sc:
            return "-->"
        return "   "

    tri_val_str = (" ", "M", "*")[sc.tri_value]

    if len(sc.assignable) == 1:
        # Pinned to a single value
        return "-{}-".format(tri_val_str)

    if sc.type == BOOL:
        return "[{}]".format(tri_val_str)

    if sc.type == TRISTATE:
        if sc.assignable == (1, 2):
            # m and y available
            return "{" + tri_val_str + "}"  # Gets a bit confusing with .format()
        return "<{}>".format(tri_val_str)

def node_str(node):
    """
    Returns the complete menu entry text for a menu node, or "" for invisible
    menu nodes. Invisible menu nodes are those that lack a prompt or don't have
    a satisfied prompt condition.

    Example return value: "[*] Bool symbol (BOOL)"

    The symbol name is printed in parentheses to the right of the prompt. This
    is so that symbols can easily be referred to in the configuration
    interface.
    """
    if not node.prompt:
        return ""

    # Even for menu nodes for symbols and choices, it's wrong to check
    # Symbol.visibility / Choice.visibility here. The reason is that a symbol
    # (and a choice, in theory) can be defined in multiple locations, giving it
    # multiple menu nodes, which do not necessarily all have the same prompt
    # visibility. Symbol.visibility / Choice.visibility is calculated as the OR
    # of the visibility of all the prompts.
    prompt, prompt_cond = node.prompt
    if not expr_value(prompt_cond):
        return ""

    if node.item == MENU:
        return "    " + prompt

    if node.item == COMMENT:
        return "    *** {} ***".format(prompt)

    # Symbol or Choice

    sc = node.item

    if sc.type == UNKNOWN:
        # Skip symbols defined without a type
        return ""

    # {:3} sets the field width to three. Gives nice alignment for empty string
    # values.
    res = "{:3} {}".format(value_str(sc), prompt)

    # Don't print the name for unnamed choices (the normal kind)
    if sc.name is not None:
        res += " ({})".format(sc.name)

    return res

def print_menuconfig_nodes(node, indent):
    """
    Prints a tree with all the menu entries rooted at 'node'. Child menu
    entries are indented.
    """
    while node:
        string = node_str(node)
        if string:
            indent_print(string, indent)

        if node.list:
            print_menuconfig_nodes(node.list, indent + 8)

        node = node.next

def print_menuconfig(kconf):
    """
    Prints all menu entries for the configuration.
    """
    # Print the expanded mainmenu text at the top. This is the same as
    # kconf.top_node.prompt[0], but with variable references expanded.
    print("\n======== {} ========\n".format(kconf.mainmenu_text))

    print_menuconfig_nodes(kconf.top_node.list, 0)
    print("")

def get_value_from_user(sc):
    """
    Prompts the user for a value for the symbol or choice 'sc'. For
    bool/tristate symbols and choices, provides a list of all the assignable
    values.
    """
    if not sc.visibility:
        print(sc.name + " is not currently visible")
        return False

    prompt = "Value for {}".format(sc.name)
    if sc.type in (BOOL, TRISTATE):
        prompt += " (available: {})" \
                  .format(", ".join([TRI_TO_STR[val] for val in sc.assignable]))
    prompt += ": "

    val_str = input(prompt).strip()
    if sc.type in (BOOL, TRISTATE):
        if val_str not in STR_TO_TRI:
            print("'{}' is not a valid tristate value".format(val_str))
            return False

        # I was thinking of having set_value() accept "n", "m", "y" as well as
        # a convenience for BOOL / TRISTATE symbols. Consistently using 0, 1, 2
        # makes the format clearer though. That's the best format in all ways
        # except for readability (where it isn't horrible either).
        val = STR_TO_TRI[val_str]
    else:
        val = val_str

    # Automatically add a "0x" prefix for hex symbols, like the menuconfig
    # interface does. This isn't done when loading .config files, hence why
    # set_value() doesn't do it automatically.
    if sc.type == HEX and not val.startswith(("0x", "0X")):
        val = "0x" + val

    # Let Kconfiglib itself print a warning here if the value is invalid. We
    # could also disable warnings temporarily with
    # kconf.disable_warnings() / kconf.enable_warnings() and print our own
    # warning.
    return sc.set_value(val)

if __name__ == "__main__":
    if len(sys.argv) != 2:
        sys.exit("usage: menuconfig.py <Kconfig file>")

    # Load Kconfig configuration files
    kconf = Kconfig(sys.argv[1])

    # Print the initial configuration tree
    print_menuconfig(kconf)

    while True:
        try:
            cmd = input('Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): ') \
                  .strip()
        except EOFError:
            print("")
            break

        if cmd == "load_config":
            config_filename = input(".config file to load: ")

            try:
                kconf.load_config(config_filename)
            except IOError as e:
                # Print the (spammy) error from Kconfiglib itself
                print(e.message + "\n")
            else:
                print("Configuration loaded from " + config_filename)

            print_menuconfig(kconf)
            continue

        if cmd == "write_config":
            config_filename = input("To this file: ")

            try:
                kconf.write_config(config_filename)
            except IOError as e:
                print(e.message)
            else:
                print("Configuration written to " + config_filename)

            continue

        # Assume 'cmd' is the name of a symbol or choice if it isn't one of the
        # commands above, prompt the user for a value for it, and print the new
        # configuration tree

        if cmd in kconf.syms:
            if get_value_from_user(kconf.syms[cmd]):
                print_menuconfig(kconf)

            continue

        if cmd in kconf.named_choices:
            if get_value_from_user(kconf.named_choices[cmd]):
                print_menuconfig(kconf)

            continue

        print("No symbol/choice named '{}' in the configuration"
              .format(cmd))
<|endoftext|>


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

import MySQLdb

from mosql.result import Model

# atch for MySQL, because MySQL uses non-standard syntax by default.
import mosql.mysql

# if you want to use the native escape function
#import mosql.MySQLdb_escape

try:
    import sqlalchemy.pool
    MySQLdb = sqlalchemy.pool.manage(MySQLdb, pool_size=5)
except ImportError:
    pass

class MySQL(Model):

    @classmethod
    def getconn(cls):
        # The ``charset='utf8'`` is just ensure we connect db with safe encoding
        # If ``show variables where variable_name = 'character_set_connection';`` shows ``utf-8``,
        # you can ignore this.
        # The ``use_unicode=False`` is just for the consistency with another example.
        return MySQLdb.connect(user='root', db='mosky', charset='utf8', use_unicode=False)

    @classmethod
    def putconn(cls, conn):
        conn.close()
<commit_msg>fixup! 0e50131<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import MySQLdb

from mosql.result import Model

# patch for MySQL, because MySQL uses non-standard syntax by default.
import mosql.mysql

# if you want to use the native escape function
#import mosql.MySQLdb_escape

try:
    import sqlalchemy.pool
    MySQLdb = sqlalchemy.pool.manage(MySQLdb, pool_size=5)
except ImportError:
    pass

class MySQL(Model):

    @classmethod
    def getconn(cls):
        # The ``charset='utf8'`` is just ensure we connect db with safe encoding
        # If ``show variables where variable_name = 'character_set_connection';`` shows ``utf-8``,
        # you can ignore this.
        # The ``use_unicode=False`` is just for the consistency with another example.
        return MySQLdb.connect(user='root', db='mosky', charset='utf8', use_unicode=False)

    @classmethod
    def putconn(cls, conn):
        conn.close()
<|endoftext|>


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

import sublime
import sys

VERSION = int(sublime.version())

reloader = "sync_settings.reloader"

if VERSION > 3000:
    reloader = 'SyncSettings.' + reloader
    from imp import reload

# Make sure all dependencies are reloaded on upgrade
if reloader in sys.modules:
    reload(sys.modules[reloader])

if VERSION > 3000:
    from .sync_settings import reloader
    from .sync_settings.commands import *
else:
    from sync_settings import reloader
    from sync_settings.commands import *
<commit_msg>set spaces specified on the .editorconfig file<commit_after># -*- coding: utf-8 -*-

import sublime
import sys

VERSION = int(sublime.version())

reloader = "sync_settings.reloader"

if VERSION > 3000:
  reloader = 'SyncSettings.' + reloader
  from imp import reload

# Make sure all dependencies are reloaded on upgrade
if reloader in sys.modules:
  reload(sys.modules[reloader])

if VERSION > 3000:
  from .sync_settings import reloader
  from .sync_settings.commands import *
else:
  from sync_settings import reloader
  from sync_settings.commands import *
<|endoftext|>


<commit_before>from simplex.algorithm import NelderMeadSimplex

import numpy as np
import matplotlib.pyplot as plt

from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
## for Palatino and other serif fonts use:
#rc('font',**{'family':'serif','serif':['Palatino']})
rc('text', usetex=True)

# Define objective function
def objective(xs):
    x1, x2 = xs[0], xs[1]
    return x1*x2

# Define callback function
simplices = []
def callback(args):
    simplices.append(args[0])

# Initial simplex
simplex = np.array([[0,0], [1,1], [0,1]], dtype=np.float)

# Initialise NelderMead simplex algorithm
nm = NelderMeadSimplex(objective, simplex, epsilon=1e-6, callback=callback)

# Minimise the objective function
solution = nm.solve()

print("Minimum at {}".format(solution))

# Tabulate objective function
x = np.linspace(-3, 3, 1000)
y = np.linspace(-3, 3, 1000)
X, Y = np.meshgrid(x, y)
Z = X*Y

# Plot function contours together with the evolution of
# the simplices as they approach the minimum
fig = plt.figure()
cs = plt.contour(X, Y, Z, 20)
plt.clabel(cs, inline=1, fontsize=10)

for simplex in simplices:

    lines = []
    for i in range(3):
        for j in range(i, 3):
            if j == i:
                continue

            plt.plot(*zip(simplex[i], simplex[j]), c='black')

plt.xlabel(r"$$x_1$$")
plt.ylabel(r"$$x_2$$")
plt.grid()
plt.savefig('no-minimum.png', bbox_inches=0, dpi=80)
<commit_msg>Modify no minimum example.<commit_after>from simplex.algorithm import NelderMeadSimplex

import numpy as np
import matplotlib.pyplot as plt

from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
## for Palatino and other serif fonts use:
#rc('font',**{'family':'serif','serif':['Palatino']})
rc('text', usetex=True)

# Define objective function
def objective(xs):
    x1, x2 = xs[0], xs[1]
    return x1*x2

# Define callback function
simplices = []
def callback(args):
    simplices.append(args[0])

# Initial simplex
simplex = np.array([[0,0], [1,1], [0,1]], dtype=np.float)

# Initialise NelderMead simplex algorithm
nm = NelderMeadSimplex(objective, simplex, epsilon=1e-6, max_iter=8, callback=callback)

# Minimise the objective function
solution = nm.solve()

print("Minimum at {}".format(solution))

# Tabulate objective function
x = np.linspace(-10, 10, 1000)
y = np.linspace(-10, 10, 1000)
X, Y = np.meshgrid(x, y)
Z = X*Y

# Plot function contours together with the evolution of
# the simplices as they approach the minimum
fig = plt.figure()
cs = plt.contour(X, Y, Z, 20)
plt.clabel(cs, inline=1, fontsize=10)

for simplex in simplices:

    lines = []
    for i in range(3):
        for j in range(i, 3):
            if j == i:
                continue

            plt.plot(*zip(simplex[i], simplex[j]), c='black')

plt.xlabel(r"$$x_1$$")
plt.ylabel(r"$$x_2$$")
plt.grid()
plt.savefig('no-minimum.png', bbox_inches=0, dpi=80)
<|endoftext|>


<commit_before>import Simulation
import ActionAIFramework
import vector

class Blitzy(ActionAIFramework.ActionAIFramework):
    def __init__(self):
        ActionAIFramework.ActionAIFramework.__init__(self)
        self.player_starting_positions = {}
        self.actions = [self.Action_AvoidIncomingBall
                   , self.Action_AttackClosestOpponent
                   , self.Action_CatchIncomingPass
                   , self.Action_GetClosestBall
                   , self.Action_PassBack
                   , self.Action_ReturnToStart
                   ]

    def GetPlayerStats(self, player_number):
        stats = [  None
                 , self.Stats_Blitzer
                 , self.Stats_Blitzer
                 , self.Stats_Blitzer
                 ]
        return stats[player_number]()

    def Update(self, ai_input):
        self.SetupStartingPositionsOnFirstUpdate(ai_input)
        return ActionAIFramework.ActionAIFramework.Update(self, ai_input)

    def Stats_Blitzer(self):
        stats = Simulation.PlayerStats()
        stats.pick = 1
        stats.throw = 1
        stats.run = 3
        stats.stamina = 3
        return stats

    def Stats_Striker(self):
        stats = Simulation.PlayerStats()
        stats.pick = 1
        stats.throw = 3
        stats.run = 3
        stats.stamina = 1
        return stats

    def GetClosestAttackableOpponent(self, player, ai_input):
        target = self.GetClosestObject(player.position, [target for target in ai_input.player_infos if target.team != player.team and target.has_been_hit != True])
        return target

    def GetClosestInFlightThrownBall(self, player, ai_input):
        avoid = self.GetClosestObject(player.position, [ball for ball in ai_input.in_flight_ball_infos if ball.is_throw])
        return avoid

    def SetupStartingPositionsOnFirstUpdate(self, ai_input):
        if len(self.player_starting_positions) == 0:
            for player in ai_input.player_infos:
                self.player_starting_positions[player.number] = player.position

    def BuildDefaultInstruction(self, player):
        instructions = Simulation.PlayerUpdateInstructions(player.number)
        instructions.move_target = player.position
        instructions.is_moving = True # always moving
        return instructions

    def Action_AvoidIncomingBall(self, player, instructions, ai_input):
        ball = self.GetClosestInFlightThrownBall(player, ai_input)
        if ball:
            ball_to_player = player.position - ball.position
            distance = ball_to_player.length
            if distance < 40:
                angel_of_approach = ball_to_player * ball.velocity
                if angel_of_approach > .5:
                    instructions.move_target = player.position + ball_to_player
                    return True
        return False

    def Action_PassBack(self, player, instructions, ai_input):
        if player.has_ball:
            instructions.is_passing = True
            instructions.pass_target = self.player_starting_positions[player.number]
            instructions.move_target = self.player_starting_positions[player.number]
            return True
        return False

    def Action_CatchIncomingPass(self, player, instructions, ai_input):
        if ai_input.in_flight_ball_infos and not player.has_ball:
            ball = self.GetClosestObject(player.position, [ball for ball in ai_input.in_flight_ball_infos if ball.is_throw == False])
            if ball:
                distance = (ball.position - player.position).length
                ball_to_player = player.position - ball.position
                angel_of_approach = ball_to_player * ball.velocity
                if distance < 20.0 and angel_of_approach > .5:
                    ai_input.in_flight_ball_infos.remove(ball)
                    instructions.move_target = ball.position
                    return True
        return False

    def Action_AttackClosestOpponent(self, player, instructions, ai_input):
        #attack
        target = self.GetClosestAttackableOpponent(player, ai_input)
        if player.has_ball and target:
            ai_input.player_infos.remove(target)
            instructions.move_target = target.position
            distance = (target.position - player.position).length
            if distance < 8 * player.throw:
                instructions.is_throwing = True
                instructions.throw_target = target.position
            return True
        return False

    def Action_GetClosestBall(self, player, instructions, ai_input):
        if ai_input.grounded_ball_infos and not player.has_ball:
            ball = self.GetClosestObject(player.position, [ball for ball in ai_input.grounded_ball_infos])
            ai_input.grounded_ball_infos.remove(ball)
            distance = (ball.position - player.position).length
            if distance < 2.0:
                instructions.is_picking = True
            else:
                instructions.move_target = ball.position
            return True
        return False

    def Action_ChargeForward(self, player, instructions, ai_input):
        instructions.move_target = vector.Vector(player.position.x)
        instructions.move_target.y = ai_input.arena_size - self.player_starting_positions[player.number].y
        return True

    def Action_ReturnToStart(self, player, instructions, ai_input):
        instructions.move_target = self.player_starting_positions[player.number]
        return True

    def GetClosestObject(self, origin, objects):
        result = None
        if objects:
            distances = [(origin - obj.position).length for obj in objects]
            result = objects[distances.index(min(distances))]
        return result

    def GetClosest(self, origin, positions):
        result = None
        if positions:
            distances = [(origin - position).length for position in positions]
            result = positions[distances.index(min(distances))]
        return result<commit_msg>Removing unused team subfolder copy<commit_after><|endoftext|>


<commit_before>"""An example of writing an API to scrapper hacker news once, and then enabling usage everywhere"""
import hug
import requests

@hug.local()
@hug.cli()
@hug.get()
def top_post(section:hug.types.one_of(('news', 'newest', 'show'))='news'):
    """Returns the top post from the provided section"""
    content = requests.get('https://news.ycombinator.com/{0}'.format(section)).content
    text = content.decode('utf-8')
    return text.split('
')[1].split("")[1].split("<")[0]
<commit_msg>Improve documentation<commit_after>"""An example of writing an API to scrape hacker news once, and then enabling usage everywhere"""
import hug
import requests

@hug.local()
@hug.cli()
@hug.get()
def top_post(section:hug.types.one_of(('news', 'newest', 'show'))='news'):
    """Returns the top post from the provided section"""
    content = requests.get('https://news.ycombinator.com/{0}'.format(section)).content
    text = content.decode('utf-8')
    return text.split('
')[1].split("")[1].split("<")[0]
<|endoftext|>


<commit_before>from decimal import Decimal as D

from django.views.generic import RedirectView
from django.conf import settings
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.db.models import get_model

from oscar.apps.checkout.views import PaymentDetailsView
from oscar.apps.payment.exceptions import PaymentError
from oscar.apps.payment.models import SourceType, Source

from paypal.express.facade import get_paypal_url, fetch_transaction_details, complete
from paypal.express import PayPalError

ShippingAddress = get_model('order', 'ShippingAddress')
Country = get_model('address', 'Country')

class RedirectView(RedirectView):
    """
    Initiate the transaction with Paypal and redirect the user
    to PayPal's Express Checkout to perform the transaction.
    """
    permanent = False

    def get_redirect_url(self, **kwargs):
        if settings.DEBUG:
            # Determine the localserver's hostname to use when
            # in testing mode
            return get_paypal_url(self.request.basket,
                                  host=self.request.META['HTTP_HOST'],
                                  scheme='http')
        return get_paypal_url(self.request.basket)

class CancelResponseView(RedirectView):
    def get_redirect_url(self, **kwargs):
        messages.error(self.request, "PayPal transaction cancelled")
        return reverse('basket:summary')

class SuccessResponseView(PaymentDetailsView):
    template_name_preview = 'paypal/preview.html'

    def get(self, request, *args, **kwargs):
        """
        Fetch details about the successful transaction from
        PayPal.  We use these details to show a preview of
        the order with a 'submit' button to place it.
        """
        try:
            payer_id = request.GET['PayerID']
            token = request.GET['token']
        except KeyError:
            # Manipulation - redirect to basket page with warning message
            messages.error(self.request, "Unable to determine PayPal transaction details")
            return HttpResponseRedirect(reverse('basket:summary'))
        self.fetch_paypal_data(payer_id, token)

        try:
            self.txn = fetch_transaction_details(self.token)
        except PayPalError:
            messages.error(self.request, "A problem occurred communicating with PayPal - please try again later")
            return HttpResponseRedirect(reverse('basket:summary'))
        return super(SuccessResponseView, self).get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        """
        Place an order.

        We fetch the txn details again and then proceed with oscar's standard
        payment details view for placing the order.
        """
        try:
            payer_id = request.POST['payer_id']
            token = request.POST['token']
        except KeyError:
            # Probably suspicious manipulation if we get here
            messages.error(self.request, "A problem occurred communicating with PayPal - please try again later")
            return HttpResponseRedirect(reverse('basket:summary'))
        try:
            self.fetch_paypal_data(payer_id, token)
        except PayPalError:
            # Unable to fetch txn details from PayPal - we have to bail out
            messages.error(self.request, "A problem occurred communicating with PayPal - please try again later")
            return HttpResponseRedirect(reverse('basket:summary'))
        return super(SuccessResponseView, self).post(request, *args, **kwargs)

    def fetch_paypal_data(self, payer_id, token):
        self.payer_id = payer_id
        self.token = token
        self.txn = fetch_transaction_details(token)

    def get_error_response(self):
        # We bypass the normal session checks for shipping address and shipping
        # method as they don't apply here.

        # We do check that the order total is still the same as when we redirected
        # off to PayPal
        assert False

    def get_txn_value(self, key):
        if not hasattr(self, 'txn_ctx'):
            self.txn_ctx = self.txn.context
        if key not in self.txn_ctx:
            return None
        return self.txn_ctx[key][0]

    def get_context_data(self, **kwargs):
        if not hasattr(self, 'payer_id'):
            return {}
        ctx = {
            'payer_id': self.payer_id,
            'token': self.token,
            'paypal_user_email': self.get_txn_value('EMAIL'),
            'paypal_amount': D(self.get_txn_value('AMT')),
        }
        # We convert the PayPal response values into those that match Oscar's normal
        # context so we can re-use the preview template as is
        shipping_address_fields = [
            self.get_txn_value('SHIPTONAME'),
            self.get_txn_value('SHIPTOSTREET'),
            self.get_txn_value('SHIPTOCITY'),
            self.get_txn_value('SHIPTOSTATE'),
            self.get_txn_value('SHIPTOZIP'),
            self.get_txn_value('SHIPTOCOUNTRYNAME'),
        ]
        ctx['shipping_address'] = {
            'active_address_fields': filter(bool, shipping_address_fields)
        }
        shipping_charge = D(self.get_txn_value('SHIPPINGAMT'))
        ctx['shipping_method'] = {
            'name': 'PayPal delivery',
            'description': 'Some description here',
            'basket_charge_incl_tax': shipping_charge,
        }
        ctx['order_total_incl_tax'] = self.request.basket.total_incl_tax + shipping_charge

        return ctx

    def handle_payment(self, order_number, total_incl_tax, **kwargs):
        """
        Complete payment with PayPal - this calls the 'DoExpressCheckout'
        method to capture the money from the initial transaction.
        """
        try:
            payer_id = self.request.POST['payer_id']
            token = self.request.POST['token']
        except KeyError:
            raise PaymentError("Unable to determine PayPal transaction details")
        txn = complete(payer_id, token)

        # Record payment source
        source_type, is_created = SourceType.objects.get_or_create(name='PayPal')
        source = Source(source_type=source_type,
                        currency=txn.currency,
                        amount_allocated=txn.amount,
                        amount_debited=txn.amount)
        self.add_payment_source(source)

    def create_shipping_address(self):
        """
        Return a created shipping address instance, created using
        the data returned by PayPal.
        """
        # Determine names - PayPal uses a single field
        ship_to_name = self.get_txn_value('SHIPTONAME')
        first_name = last_name = None
        parts = ship_to_name.split()
        if len(parts) == 1:
            last_name = ship_to_name
        elif len(parts) > 1:
            first_name = parts[0]
            last_name = " ".join(parts[1:])

        return ShippingAddress.objects.create(
            first_name=first_name,
            last_name=last_name,
            line1=self.get_txn_value('SHIPTOSTREET'),
            line4=self.get_txn_value('SHIPTOCITY'),
            state=self.get_txn_value('SHIPTOSTATE'),
            postcode=self.get_txn_value('SHIPTOZIP'),
            country=Country.objects.get(iso_3166_1_a2=self.get_txn_value('COUNTRYCODE'))
        )
<commit_msg>Checkout views now check to see if the basket total has been manipulated<commit_after>from decimal import Decimal as D

from django.views.generic import RedirectView
from django.conf import settings
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.db.models import get_model

from oscar.apps.checkout.views import PaymentDetailsView
from oscar.apps.payment.exceptions import PaymentError
from oscar.apps.payment.models import SourceType, Source

from paypal.express.facade import get_paypal_url, fetch_transaction_details, complete
from paypal.express import PayPalError

ShippingAddress = get_model('order', 'ShippingAddress')
Country = get_model('address', 'Country')

class RedirectView(RedirectView):
    """
    Initiate the transaction with Paypal and redirect the user
    to PayPal's Express Checkout to perform the transaction.
    """
    permanent = False

    def get_redirect_url(self, **kwargs):
        if settings.DEBUG:
            # Determine the localserver's hostname to use when
            # in testing mode
            return get_paypal_url(self.request.basket,
                                  host=self.request.META['HTTP_HOST'],
                                  scheme='http')
        return get_paypal_url(self.request.basket)

class CancelResponseView(RedirectView):
    def get_redirect_url(self, **kwargs):
        messages.error(self.request, "PayPal transaction cancelled")
        return reverse('basket:summary')

class SuccessResponseView(PaymentDetailsView):
    template_name_preview = 'paypal/preview.html'

    def get(self, request, *args, **kwargs):
        """
        Fetch details about the successful transaction from
        PayPal.  We use these details to show a preview of
        the order with a 'submit' button to place it.
        """
        try:
            payer_id = request.GET['PayerID']
            token = request.GET['token']
        except KeyError:
            # Manipulation - redirect to basket page with warning message
            messages.error(self.request, "Unable to determine PayPal transaction details")
            return HttpResponseRedirect(reverse('basket:summary'))
        self.fetch_paypal_data(payer_id, token)

        try:
            self.txn = fetch_transaction_details(self.token)
        except PayPalError:
            messages.error(self.request, "A problem occurred communicating with PayPal - please try again later")
            return HttpResponseRedirect(reverse('basket:summary'))
        return super(SuccessResponseView, self).get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        """
        Place an order.

        We fetch the txn details again and then proceed with oscar's standard
        payment details view for placing the order.
        """
        try:
            payer_id = request.POST['payer_id']
            token = request.POST['token']
        except KeyError:
            # Probably suspicious manipulation if we get here
            messages.error(self.request, "A problem occurred communicating with PayPal - please try again later")
            return HttpResponseRedirect(reverse('basket:summary'))
        try:
            self.fetch_paypal_data(payer_id, token)
        except PayPalError:
            # Unable to fetch txn details from PayPal - we have to bail out
            messages.error(self.request, "A problem occurred communicating with PayPal - please try again later")
            return HttpResponseRedirect(reverse('basket:summary'))
        return super(SuccessResponseView, self).post(request, *args, **kwargs)

    def fetch_paypal_data(self, payer_id, token):
        self.payer_id = payer_id
        self.token = token
        self.txn = fetch_transaction_details(token)

    def get_error_response(self):
        # We bypass the normal session checks for shipping address and shipping
        # method as they don't apply here.

        # We do check that the order total is still the same as when we redirected
        # off to PayPal
        txn_amount = self.txn.amount
        order_total = self.request.basket.total_incl_tax

        if txn_amount != order_total:
            messages.error(self.request,
                           "Your order total (%s) differs from the total PayPal authorised (%s) - aborting" % (
                               order_total, txn_amount))
            return HttpResponseRedirect(reverse('basket:summary'))

    def get_txn_value(self, key):
        if not hasattr(self, 'txn_ctx'):
            self.txn_ctx = self.txn.context
        if key not in self.txn_ctx:
            return None
        return self.txn_ctx[key][0]

    def get_context_data(self, **kwargs):
        if not hasattr(self, 'payer_id'):
            return {}
        ctx = {
            'payer_id': self.payer_id,
            'token': self.token,
            'paypal_user_email': self.get_txn_value('EMAIL'),
            'paypal_amount': D(self.get_txn_value('AMT')),
        }
        # We convert the PayPal response values into those that match Oscar's normal
        # context so we can re-use the preview template as is
        shipping_address_fields = [
            self.get_txn_value('SHIPTONAME'),
            self.get_txn_value('SHIPTOSTREET'),
            self.get_txn_value('SHIPTOCITY'),
            self.get_txn_value('SHIPTOSTATE'),
            self.get_txn_value('SHIPTOZIP'),
            self.get_txn_value('SHIPTOCOUNTRYNAME'),
        ]
        ctx['shipping_address'] = {
            'active_address_fields': filter(bool, shipping_address_fields)
        }
        shipping_charge = D(self.get_txn_value('SHIPPINGAMT'))
        ctx['shipping_method'] = {
            'name': 'PayPal delivery',
            'description': 'Some description here',
            'basket_charge_incl_tax': shipping_charge,
        }
        ctx['order_total_incl_tax'] = self.request.basket.total_incl_tax + shipping_charge

        return ctx

    def handle_payment(self, order_number, total_incl_tax, **kwargs):
        """
        Complete payment with PayPal - this calls the 'DoExpressCheckout'
        method to capture the money from the initial transaction.
        """
        try:
            payer_id = self.request.POST['payer_id']
            token = self.request.POST['token']
        except KeyError:
            raise PaymentError("Unable to determine PayPal transaction details")
        txn = complete(payer_id, token)

        # Record payment source
        source_type, is_created = SourceType.objects.get_or_create(name='PayPal')
        source = Source(source_type=source_type,
                        currency=txn.currency,
                        amount_allocated=txn.amount,
                        amount_debited=txn.amount)
        self.add_payment_source(source)

    def create_shipping_address(self):
        """
        Return a created shipping address instance, created using
        the data returned by PayPal.
        """
        # Determine names - PayPal uses a single field
        ship_to_name = self.get_txn_value('SHIPTONAME')
        first_name = last_name = None
        parts = ship_to_name.split()
        if len(parts) == 1:
            last_name = ship_to_name
        elif len(parts) > 1:
            first_name = parts[0]
            last_name = " ".join(parts[1:])

        return ShippingAddress.objects.create(
            first_name=first_name,
            last_name=last_name,
            line1=self.get_txn_value('SHIPTOSTREET'),
            line4=self.get_txn_value('SHIPTOCITY'),
            state=self.get_txn_value('SHIPTOSTATE'),
            postcode=self.get_txn_value('SHIPTOZIP'),
            country=Country.objects.get(iso_3166_1_a2=self.get_txn_value('COUNTRYCODE'))
        )
<|endoftext|>


<commit_before>from django.conf import settings
from django.conf.urls import include, url
from django.urls import path

from peering_manager.admin import admin_site
from peering_manager.api.views import (
    APIRootView,
    SpectacularAPIView,
    SpectacularRedocView,
    SpectacularSwaggerView,
    StatusView,
)
from peering_manager.views import Home, SearchView, handle_500, trigger_500
from users.views import LoginView, LogoutView

handler500 = handle_500

__patterns = [
    # Home
    path("", Home.as_view(), name="home"),
    # Global search
    path("search/", SearchView.as_view(), name="search"),
    # Login/Logout
    path("login/", LoginView.as_view(), name="login"),
    path("logout/", LogoutView.as_view(), name="logout"),
    # Apps
    path("", include("bgp.urls")),
    path("", include("devices.urls")),
    path("", include("extras.urls")),
    path("", include("messaging.urls")),
    path("", include("net.urls")),
    path("", include("peering.urls")),
    path("", include("peeringdb.urls")),
    path("", include("utils.urls")),
    path("user/", include("users.urls")),
    # API
    path("api/", APIRootView.as_view(), name="api-root"),
    path("api/bgp/", include("bgp.api.urls")),
    path("api/devices/", include("devices.api.urls")),
    path("api/extras/", include("extras.api.urls")),
    path("api/messaging/", include("messaging.api.urls")),
    path("api/net/", include("net.api.urls")),
    path("api/peering/", include("peering.api.urls")),
    path("api/peeringdb/", include("peeringdb.api.urls")),
    path("api/users/", include("users.api.urls")),
    path("api/utils/", include("utils.api.urls")),
    path("api/status/", StatusView.as_view(), name="api-status"),
    path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
    path(
        "api/docs/",
        SpectacularSwaggerView.as_view(url_name="schema"),
        name="swagger-ui",
    ),
    path("api/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc"),
    # Admin
    path("admin/background-tasks/", include("django_rq.urls")),
    path("admin/", admin_site.urls),
    # Error triggering
    path("error500/", trigger_500),
]

# Add debug_toolbar in debug mode
if settings.DEBUG:
    import debug_toolbar

    __patterns += [path("__debug__/", include(debug_toolbar.urls))]

if settings.METRICS_ENABLED:
    __patterns += [path("", include("django_prometheus.urls"))]

# Prepend BASE_PATH
urlpatterns = [path(f"{settings.BASE_PATH}", include(__patterns))]
<commit_msg>Remove useless and dreprecated import<commit_after>from django.conf import settings
from django.conf.urls import include
from django.urls import path

from peering_manager.admin import admin_site
from peering_manager.api.views import (
    APIRootView,
    SpectacularAPIView,
    SpectacularRedocView,
    SpectacularSwaggerView,
    StatusView,
)
from peering_manager.views import Home, SearchView, handle_500, trigger_500
from users.views import LoginView, LogoutView

handler500 = handle_500

__patterns = [
    # Home
    path("", Home.as_view(), name="home"),
    # Global search
    path("search/", SearchView.as_view(), name="search"),
    # Login/Logout
    path("login/", LoginView.as_view(), name="login"),
    path("logout/", LogoutView.as_view(), name="logout"),
    # Apps
    path("", include("bgp.urls")),
    path("", include("devices.urls")),
    path("", include("extras.urls")),
    path("", include("messaging.urls")),
    path("", include("net.urls")),
    path("", include("peering.urls")),
    path("", include("peeringdb.urls")),
    path("", include("utils.urls")),
    path("user/", include("users.urls")),
    # API
    path("api/", APIRootView.as_view(), name="api-root"),
    path("api/bgp/", include("bgp.api.urls")),
    path("api/devices/", include("devices.api.urls")),
    path("api/extras/", include("extras.api.urls")),
    path("api/messaging/", include("messaging.api.urls")),
    path("api/net/", include("net.api.urls")),
    path("api/peering/", include("peering.api.urls")),
    path("api/peeringdb/", include("peeringdb.api.urls")),
    path("api/users/", include("users.api.urls")),
    path("api/utils/", include("utils.api.urls")),
    path("api/status/", StatusView.as_view(), name="api-status"),
    path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
    path(
        "api/docs/",
        SpectacularSwaggerView.as_view(url_name="schema"),
        name="swagger-ui",
    ),
    path("api/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc"),
    # Admin
    path("admin/background-tasks/", include("django_rq.urls")),
    path("admin/", admin_site.urls),
    # Error triggering
    path("error500/", trigger_500),
]

# Add debug_toolbar in debug mode
if settings.DEBUG:
    import debug_toolbar

    __patterns += [path("__debug__/", include(debug_toolbar.urls))]

if settings.METRICS_ENABLED:
    __patterns += [path("", include("django_prometheus.urls"))]

# Prepend BASE_PATH
urlpatterns = [path(f"{settings.BASE_PATH}", include(__patterns))]
<|endoftext|>


<commit_before>"""
This module provides the foundation of job elements.

 .. moduleauthor:: Michael Markert <markert.michael@googlemail.com>

 :copyright: PenchY Developers 2011-2012, see AUTHORS
 :license: MIT License, see LICENSE
"""
import logging
from collections import defaultdict

from penchy.compat import path
from penchy.jobs.dependency import Pipeline
from penchy.jobs.typecheck import Types

log = logging.getLogger(__name__)

class PipelineElement(object):
    """
    This class is the base class for all objects participating in the
    transformation pipeline.

    A PipelineElement must have the following attributes:

    - ``out``, a dictionary that maps logical names for output to actual.
    - ``inputs`` a :class:`~penchy.jobs.typecheck.Types` that describes the
                 necessary inputs and their types for the element
    - ``outputs`` a :class:`~penchy.jobs.typecheck.Types` that describes the
                  output with a logical name and its types

    A PipelineElement must have the following methods:

    - ``_run(**kwargs)``, to run the element on kwargs, kwargs has to have the
                          types that ``input`` describes

    A :class:`PipelineElement` must call ``PipelineElement.__init__`` on its
    initialization.
    """
    DEPENDENCIES = set()
    inputs = Types()
    outputs = Types()

    def __init__(self):
        self.reset()

        self.hooks = []

    def run(self, **kwargs):
        """
        Run element with hooks.
        """
        self.inputs.check_input(kwargs)
        for hook in self.hooks:
            hook.setup()

        self._run(**kwargs)

        for hook in self.hooks:
            hook.teardown()

    def reset(self):
        """
        Reset state of element.

        Resets
          - element.out
        """
        self.out = defaultdict(list)

    def __rshift__(self, other):
        p = Pipeline(self)
        return p >> other

    def _run(self, **kwargs):  # pragma: no cover
        """
        Run the actual Element on the arguments.
        """
        raise NotImplementedError("PipelineElements must implement this")

    @property
    def _output_names(self):
        """
        Return the set of output names

        :returns: the output names
        :rtype: set
        """
        return self.outputs.names

    def __repr__(self):
        return self.__class__.__name__

class NotRunnable(object):
    """
    This represents a pipeline element that can't be run.
    """

    def run(self):
        msg = "{0} can't be run!".format(self.__class__.__name__)
        log.error(msg)
        raise ValueError(msg)

class Filter(PipelineElement):
    """
    This represents a Filter of the pipeline.

    A Filter receives and processes data.
    """
    pass

class SystemFilter(Filter):
    """
    This represents a Filter of the pipeline that needs access to the system.

    Additionally to :class:`Filter` it receives additionally an input named
    ``:environment:`` that describes the execution environment.
    """
    pass

class Tool(NotRunnable, PipelineElement):
    """
    This represents a Tool of the pipeline.

    A Tool modifies the JVM on which it runs, so that data about that run is
    gathered. Hprof, for example, is a Tool.
    """
    def __init__(self, name=None):
        """
        :param name: descriptive name of this tool
        :type name: str
        """
        super(Tool, self).__init__()
        self.name = name

    @property
    def arguments(self):  # pragma: no cover
        """
        The arguments the jvm has to include to use the tool.
        """
        raise NotImplementedError("Tools must implement this")

    def __str__(self):  # pragma: no cover
        return self.name

class Workload(NotRunnable, PipelineElement):
    """
    This represents a Workload of the pipeline.

    A Workload is code that the JVM should execute. Typically it provides the
    classpath (via its dependencies) and the complete commandline arguments to
    call it correctly. The DaCapo benchmark suite is a workload (with a
    benchmark specified).

    A workload has at least three exported values:

    - `stdout`, the path to the file that contains the output on stdout
    - `stderr`, the path to the file that contains the output on stderr
    - `exit_code`, the exitcode as int
    """
    outputs = Types(('stdout', list, path),
                    ('stderr', list, path),
                    ('exit_code', list, int))

    def __init__(self, timeout=0, name=None):
        """
        :param timeout: timeout (in seconds) after which this workload should
                        be terminated
        :type timeout: int
        :param name: descriptive name of this workload
        :type name: str
        """
        super(Workload, self).__init__()
        self.timeout = timeout
        self.name = name

    @property
    def arguments(self):  # pragma: no cover
        """
        The arguments the jvm has to include to execute the workloads.
        """
        raise NotImplementedError("Workloads must implement this")
<commit_msg>Workload: implemented str method<commit_after>"""
This module provides the foundation of job elements.

 .. moduleauthor:: Michael Markert <markert.michael@googlemail.com>

 :copyright: PenchY Developers 2011-2012, see AUTHORS
 :license: MIT License, see LICENSE
"""
import logging
from collections import defaultdict

from penchy.compat import path
from penchy.jobs.dependency import Pipeline
from penchy.jobs.typecheck import Types

log = logging.getLogger(__name__)

class PipelineElement(object):
    """
    This class is the base class for all objects participating in the
    transformation pipeline.

    A PipelineElement must have the following attributes:

    - ``out``, a dictionary that maps logical names for output to actual.
    - ``inputs`` a :class:`~penchy.jobs.typecheck.Types` that describes the
                 necessary inputs and their types for the element
    - ``outputs`` a :class:`~penchy.jobs.typecheck.Types` that describes the
                  output with a logical name and its types

    A PipelineElement must have the following methods:

    - ``_run(**kwargs)``, to run the element on kwargs, kwargs has to have the
                          types that ``input`` describes

    A :class:`PipelineElement` must call ``PipelineElement.__init__`` on its
    initialization.
    """
    DEPENDENCIES = set()
    inputs = Types()
    outputs = Types()

    def __init__(self):
        self.reset()

        self.hooks = []

    def run(self, **kwargs):
        """
        Run element with hooks.
        """
        self.inputs.check_input(kwargs)
        for hook in self.hooks:
            hook.setup()

        self._run(**kwargs)

        for hook in self.hooks:
            hook.teardown()

    def reset(self):
        """
        Reset state of element.

        Resets
          - element.out
        """
        self.out = defaultdict(list)

    def __rshift__(self, other):
        p = Pipeline(self)
        return p >> other

    def _run(self, **kwargs):  # pragma: no cover
        """
        Run the actual Element on the arguments.
        """
        raise NotImplementedError("PipelineElements must implement this")

    @property
    def _output_names(self):
        """
        Return the set of output names

        :returns: the output names
        :rtype: set
        """
        return self.outputs.names

    def __repr__(self):
        return self.__class__.__name__

class NotRunnable(object):
    """
    This represents a pipeline element that can't be run.
    """

    def run(self):
        msg = "{0} can't be run!".format(self.__class__.__name__)
        log.error(msg)
        raise ValueError(msg)

class Filter(PipelineElement):
    """
    This represents a Filter of the pipeline.

    A Filter receives and processes data.
    """
    pass

class SystemFilter(Filter):
    """
    This represents a Filter of the pipeline that needs access to the system.

    Additionally to :class:`Filter` it receives additionally an input named
    ``:environment:`` that describes the execution environment.
    """
    pass

class Tool(NotRunnable, PipelineElement):
    """
    This represents a Tool of the pipeline.

    A Tool modifies the JVM on which it runs, so that data about that run is
    gathered. Hprof, for example, is a Tool.
    """
    def __init__(self, name=None):
        """
        :param name: descriptive name of this tool
        :type name: str
        """
        super(Tool, self).__init__()
        self.name = name

    @property
    def arguments(self):  # pragma: no cover
        """
        The arguments the jvm has to include to use the tool.
        """
        raise NotImplementedError("Tools must implement this")

    def __str__(self):  # pragma: no cover
        return self.name

class Workload(NotRunnable, PipelineElement):
    """
    This represents a Workload of the pipeline.

    A Workload is code that the JVM should execute. Typically it provides the
    classpath (via its dependencies) and the complete commandline arguments to
    call it correctly. The DaCapo benchmark suite is a workload (with a
    benchmark specified).

    A workload has at least three exported values:

    - `stdout`, the path to the file that contains the output on stdout
    - `stderr`, the path to the file that contains the output on stderr
    - `exit_code`, the exitcode as int
    """
    outputs = Types(('stdout', list, path),
                    ('stderr', list, path),
                    ('exit_code', list, int))

    def __init__(self, timeout=0, name=None):
        """
        :param timeout: timeout (in seconds) after which this workload should
                        be terminated
        :type timeout: int
        :param name: descriptive name of this workload
        :type name: str
        """
        super(Workload, self).__init__()
        self.timeout = timeout
        self.name = name

    def __str__(self):  # pragma: no cover
        return self.name

    @property
    def arguments(self):  # pragma: no cover
        """
        The arguments the jvm has to include to execute the workloads.
        """
        raise NotImplementedError("Workloads must implement this")
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# Copyright (c) 2015-2020 The Dash Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

import time

from test_framework.test_framework import DashTestFramework
from test_framework.util import *

'''
llmq-simplepose.py

Checks simple PoSe system based on LLMQ commitments

'''

class LLMQSimplePoSeTest(DashTestFramework):
    def set_test_params(self):
        self.set_dash_test_params(6, 5, fast_dip3_enforcement=True)
        self.set_dash_llmq_test_params(5, 3)

    def run_test(self):

        self.nodes[0].spork("SPORK_17_QUORUM_DKG_ENABLED", 0)
        self.wait_for_sporks_same()

        # check if mining quorums with all nodes being online succeeds without punishment/banning
        self.test_no_banning()

        # Now lets isolate MNs one by one and verify that punishment/banning happens
        def isolate_mn(mn):
            mn.node.setnetworkactive(False)
            wait_until(lambda: mn.node.getconnectioncount() == 0)
        self.test_banning(isolate_mn, True)

        self.repair_masternodes(False)

        self.nodes[0].spork("SPORK_21_QUORUM_ALL_CONNECTED", 0)
        self.wait_for_sporks_same()

        self.reset_probe_timeouts()

        # Make sure no banning happens with spork21 enabled
        self.test_no_banning(expected_connections=4)

        # Lets restart masternodes with closed ports and verify that they get banned even though they are connected to other MNs (via outbound connections)
        def close_mn_port(mn):
            self.stop_node(mn.node.index)
            self.start_masternode(mn, ["-listen=0"])
            connect_nodes(mn.node, 0)
            # Make sure the to-be-banned node is still connected well via outbound connections
            for mn2 in self.mninfo:
                if mn2 is not mn:
                    connect_nodes(mn.node, mn2.node.index)
            self.reset_probe_timeouts()
        self.test_banning(close_mn_port, False)

        self.repair_masternodes(True)
        self.reset_probe_timeouts()

        def force_old_mn_proto(mn):
            self.stop_node(mn.node.index)
            self.start_masternode(mn, ["-pushversion=70216"])
            connect_nodes(mn.node, 0)
            self.reset_probe_timeouts()
        self.test_banning(force_old_mn_proto, False)

    def test_no_banning(self, expected_connections=1):
        for i in range(3):
            self.mine_quorum(expected_connections=expected_connections)
        for mn in self.mninfo:
            assert(not self.check_punished(mn) and not self.check_banned(mn))

    def test_banning(self, invalidate_proc, expect_contribution_to_fail):
        online_mninfos = self.mninfo.copy()
        for i in range(2):
            mn = online_mninfos[len(online_mninfos) - 1]
            online_mninfos.remove(mn)
            invalidate_proc(mn)

            t = time.time()
            while (not self.check_punished(mn) or not self.check_banned(mn)) and (time.time() - t) < 120:
                expected_contributors = len(online_mninfos) + 1
                if expect_contribution_to_fail:
                    expected_contributors -= 1
                # Make sure we do fresh probes
                self.bump_mocktime(60 * 60)
                self.mine_quorum(expected_connections=1, expected_members=len(online_mninfos), expected_contributions=expected_contributors, expected_complaints=expected_contributors-1, expected_commitments=expected_contributors, mninfos=online_mninfos)

            assert(self.check_punished(mn) and self.check_banned(mn))

    def repair_masternodes(self, restart):
        # Repair all nodes
        for mn in self.mninfo:
            if self.check_banned(mn) or self.check_punished(mn):
                addr = self.nodes[0].getnewaddress()
                self.nodes[0].sendtoaddress(addr, 0.1)
                self.nodes[0].protx('update_service', mn.proTxHash, '127.0.0.1:%d' % p2p_port(mn.node.index), mn.keyOperator, "", addr)
                self.nodes[0].generate(1)
                assert(not self.check_banned(mn))

                if restart:
                    self.stop_node(mn.node.index)
                    self.start_masternode(mn)
                else:
                    mn.node.setnetworkactive(True)
            connect_nodes(mn.node, 0)
        self.sync_all()

        # Isolate and re-connect all MNs (otherwise there might be open connections with no MNAUTH for MNs which were banned before)
        for mn in self.mninfo:
            mn.node.setnetworkactive(False)
            wait_until(lambda: mn.node.getconnectioncount() == 0)
            mn.node.setnetworkactive(True)
            connect_nodes(mn.node, 0)

    def reset_probe_timeouts(self):
        # Make sure all masternodes will reconnect/re-probe
        self.bump_mocktime(60 * 60 + 1)
        self.sync_all()

    def check_punished(self, mn):
        info = self.nodes[0].protx('info', mn.proTxHash)
        if info['state']['PoSePenalty'] > 0:
            return True
        return False

    def check_banned(self, mn):
        info = self.nodes[0].protx('info', mn.proTxHash)
        if info['state']['PoSeBanHeight'] != -1:
            return True
        return False

if __name__ == '__main__':
    LLMQSimplePoSeTest().main()
<commit_msg>add extra args named param<commit_after>#!/usr/bin/env python3
# Copyright (c) 2015-2020 The Dash Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

import time

from test_framework.test_framework import DashTestFramework
from test_framework.util import *

'''
llmq-simplepose.py

Checks simple PoSe system based on LLMQ commitments

'''

class LLMQSimplePoSeTest(DashTestFramework):
    def set_test_params(self):
        self.set_dash_test_params(6, 5, fast_dip3_enforcement=True)
        self.set_dash_llmq_test_params(5, 3)

    def run_test(self):

        self.nodes[0].spork("SPORK_17_QUORUM_DKG_ENABLED", 0)
        self.wait_for_sporks_same()

        # check if mining quorums with all nodes being online succeeds without punishment/banning
        self.test_no_banning()

        # Now lets isolate MNs one by one and verify that punishment/banning happens
        def isolate_mn(mn):
            mn.node.setnetworkactive(False)
            wait_until(lambda: mn.node.getconnectioncount() == 0)
        self.test_banning(isolate_mn, True)

        self.repair_masternodes(False)

        self.nodes[0].spork("SPORK_21_QUORUM_ALL_CONNECTED", 0)
        self.wait_for_sporks_same()

        self.reset_probe_timeouts()

        # Make sure no banning happens with spork21 enabled
        self.test_no_banning(expected_connections=4)

        # Lets restart masternodes with closed ports and verify that they get banned even though they are connected to other MNs (via outbound connections)
        def close_mn_port(mn):
            self.stop_node(mn.node.index)
            self.start_masternode(mn, extra_args=["-listen=0"])
            connect_nodes(mn.node, 0)
            # Make sure the to-be-banned node is still connected well via outbound connections
            for mn2 in self.mninfo:
                if mn2 is not mn:
                    connect_nodes(mn.node, mn2.node.index)
            self.reset_probe_timeouts()
        self.test_banning(close_mn_port, False)

        self.repair_masternodes(True)
        self.reset_probe_timeouts()

        def force_old_mn_proto(mn):
            self.stop_node(mn.node.index)
            self.start_masternode(mn, extra_args=["-pushversion=70216"])
            connect_nodes(mn.node, 0)
            self.reset_probe_timeouts()
        self.test_banning(force_old_mn_proto, False)

    def test_no_banning(self, expected_connections=1):
        for i in range(3):
            self.mine_quorum(expected_connections=expected_connections)
        for mn in self.mninfo:
            assert(not self.check_punished(mn) and not self.check_banned(mn))

    def test_banning(self, invalidate_proc, expect_contribution_to_fail):
        online_mninfos = self.mninfo.copy()
        for i in range(2):
            mn = online_mninfos[len(online_mninfos) - 1]
            online_mninfos.remove(mn)
            invalidate_proc(mn)

            t = time.time()
            while (not self.check_punished(mn) or not self.check_banned(mn)) and (time.time() - t) < 120:
                expected_contributors = len(online_mninfos) + 1
                if expect_contribution_to_fail:
                    expected_contributors -= 1
                # Make sure we do fresh probes
                self.bump_mocktime(60 * 60)
                self.mine_quorum(expected_connections=1, expected_members=len(online_mninfos), expected_contributions=expected_contributors, expected_complaints=expected_contributors-1, expected_commitments=expected_contributors, mninfos=online_mninfos)

            assert(self.check_punished(mn) and self.check_banned(mn))

    def repair_masternodes(self, restart):
        # Repair all nodes
        for mn in self.mninfo:
            if self.check_banned(mn) or self.check_punished(mn):
                addr = self.nodes[0].getnewaddress()
                self.nodes[0].sendtoaddress(addr, 0.1)
                self.nodes[0].protx('update_service', mn.proTxHash, '127.0.0.1:%d' % p2p_port(mn.node.index), mn.keyOperator, "", addr)
                self.nodes[0].generate(1)
                assert(not self.check_banned(mn))

                if restart:
                    self.stop_node(mn.node.index)
                    self.start_masternode(mn)
                else:
                    mn.node.setnetworkactive(True)
            connect_nodes(mn.node, 0)
        self.sync_all()

        # Isolate and re-connect all MNs (otherwise there might be open connections with no MNAUTH for MNs which were banned before)
        for mn in self.mninfo:
            mn.node.setnetworkactive(False)
            wait_until(lambda: mn.node.getconnectioncount() == 0)
            mn.node.setnetworkactive(True)
            connect_nodes(mn.node, 0)

    def reset_probe_timeouts(self):
        # Make sure all masternodes will reconnect/re-probe
        self.bump_mocktime(60 * 60 + 1)
        self.sync_all()

    def check_punished(self, mn):
        info = self.nodes[0].protx('info', mn.proTxHash)
        if info['state']['PoSePenalty'] > 0:
            return True
        return False

    def check_banned(self, mn):
        info = self.nodes[0].protx('info', mn.proTxHash)
        if info['state']['PoSeBanHeight'] != -1:
            return True
        return False

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