<commit_before>#!/usr/bin/env python
#
# GrovePi Example for using the Grove Chainable RGB LED (http://www.seeedstudio.com/wiki/Grove_-_Chainable_RGB_LED)
#
# The GrovePi connects the Raspberry Pi and Grove sensors.  You can learn more about GrovePi here:  http://www.dexterindustries.com/GrovePi
#
# Have a question about this example?  Ask on the forums here:  http://www.dexterindustries.com/forum/?forum=grovepi
#
# LICENSE:
# These files have been made available online through a [Creative Commons Attribution-ShareAlike 3.0](http://creativecommons.org/licenses/by-sa/3.0/) license.

import time
import grovepi

# Connect first LED in Chainable RGB LED chain to digital port D7
# In: CI,DI,VCC,GND
# Out: CO,DO,VCC,GND
pin = 7

# I have 10 LEDs connected in series with the first connected to the GrovePi and the last not connected
# First LED input socket connected to GrovePi, output socket connected to second LED input and so on
numleds = 10     #If you only plug 1 LED, change 10 to 1

grovepi.pinMode(chain,"OUTPUT")
time.sleep(1)

# Chainable RGB LED methods
# grovepi.storeColor(red, green, blue)
# grovepi.chainableRgbLed_init(pin, numLeds)
# grovepi.chainableRgbLed_test(pin, numLeds, testColor)
# grovepi.chainableRgbLed_pattern(pin, pattern, whichLed)
# grovepi.chainableRgbLed_modulo(pin, offset, divisor)
# grovepi.chainableRgbLed_setLevel(pin, level, reverse)

# test colors used in grovepi.chainableRgbLed_test()
testColorBlack = 0   # 0b000 #000000
testColorBlue = 1    # 0b001 #0000FF
testColorGreen = 2   # 0b010 #00FF00
testColorCyan = 3    # 0b011 #00FFFF
testColorRed = 4     # 0b100 #FF0000
testColorMagenta = 5 # 0b101 #FF00FF
testColorYellow = 6  # 0b110 #FFFF00
testColorWhite = 7   # 0b111 #FFFFFF

# patterns used in grovepi.chainableRgbLed_pattern()
thisLedOnly = 0
allLedsExceptThis = 1
thisLedAndInwards = 2
thisLedAndOutwards = 3

try:

    print "Test 1) Initialise"

    # init chain of leds
    grovepi.chainableRgbLed_init(pin, numleds)
    time.sleep(.5)

    # change color to green
    grovepi.storeColor(0,255,0)
    time.sleep(.5)

    # set led 1 to green
    grovepi.chainableRgbLed_pattern(pin, thisLedOnly, 0)
    time.sleep(.5)

    # change color to red
    grovepi.storeColor(255,0,0)
    time.sleep(.5)

    # set led 10 to red
    grovepi.chainableRgbLed_pattern(pin, thisLedOnly, 9)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 2a) Test Patterns - black"

    # test pattern 0 - black (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(1)

    print "Test 2b) Test Patterns - blue"

    # test pattern 1 blue
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlue)
    time.sleep(1)

    print "Test 2c) Test Patterns - green"

    # test pattern 2 green
    grovepi.chainableRgbLed_test(pin, numleds, testColorGreen)
    time.sleep(1)

    print "Test 2d) Test Patterns - cyan"

    # test pattern 3 cyan
    grovepi.chainableRgbLed_test(pin, numleds, testColorCyan)
    time.sleep(1)

    print "Test 2e) Test Patterns - red"

    # test pattern 4 red
    grovepi.chainableRgbLed_test(pin, numleds, testColorRed)
    time.sleep(1)

    print "Test 2f) Test Patterns - magenta"

    # test pattern 5 magenta
    grovepi.chainableRgbLed_test(pin, numleds, testColorMagenta)
    time.sleep(1)

    print "Test 2g) Test Patterns - yellow"

    # test pattern 6 yellow
    grovepi.chainableRgbLed_test(pin, numleds, testColorYellow)
    time.sleep(1)

    print "Test 2h) Test Patterns - white"

    # test pattern 7 white
    grovepi.chainableRgbLed_test(pin, numleds, testColorWhite)
    time.sleep(1)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 3a) Set using pattern - this led only"

    # change color to red
    grovepi.storeColor(255,0,0)
    time.sleep(.5)

    # set led 3 to red
    grovepi.chainableRgbLed_pattern(pin, thisLedOnly, 2)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 3b) Set using pattern - all leds except this"

    # change color to blue
    grovepi.storeColor(0,0,255)
    time.sleep(.5)

    # set all leds except for 3 to blue
    grovepi.chainableRgbLed_pattern(pin, allLedsExceptThis, 3)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 3c) Set using pattern - this led and inwards"

    # change color to green
    grovepi.storeColor(0,255,0)
    time.sleep(.5)

    # set leds 1-3 to green
    grovepi.chainableRgbLed_pattern(pin, thisLedAndInwards, 2)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 3d) Set using pattern - this led and outwards"

    # change color to green
    grovepi.storeColor(0,255,0)
    time.sleep(.5)

    # set leds 7-10 to green
    grovepi.chainableRgbLed_pattern(pin, thisLedAndOutwards, 6)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 4a) Set using modulo - all leds"

    # change color to black (fully off)
    grovepi.storeColor(0,0,0)
    time.sleep(.5)

    # set all leds black
    # offset 0 means start at first led
    # divisor 1 means every led
    grovepi.chainableRgbLed_modulo(pin, 0, 1)
    time.sleep(.5)

    # change color to white (fully on)
    grovepi.storeColor(255,255,255)
    time.sleep(.5)

    # set all leds white
    grovepi.chainableRgbLed_modulo(pin, 0, 1)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 4b) Set using modulo - every 2"

    # change color to red
    grovepi.storeColor(255,0,0)
    time.sleep(.5)

    # set every 2nd led to red
    grovepi.chainableRgbLed_modulo(pin, 0, 2)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    print "Test 4c) Set using modulo - every 2, offset 1"

    # change color to green
    grovepi.storeColor(0,255,0)
    time.sleep(.5)

    # set every 2nd led to green, offset 1
    grovepi.chainableRgbLed_modulo(pin, 1, 2)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 4d) Set using modulo - every 3, offset 0"

    # change color to red
    grovepi.storeColor(255,0,0)
    time.sleep(.5)

    # set every 3nd led to red
    grovepi.chainableRgbLed_modulo(pin, 0, 3)
    time.sleep(.5)

    # change color to green
    grovepi.storeColor(0,255,0)
    time.sleep(.5)

    # set every 3nd led to green, offset 1
    grovepi.chainableRgbLed_modulo(pin, 1, 3)
    time.sleep(.5)

    # change color to blue
    grovepi.storeColor(0,0,255)
    time.sleep(.5)

    # set every 3nd led to blue, offset 2
    grovepi.chainableRgbLed_modulo(pin, 2, 3)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 4e) Set using modulo - every 3, offset 1"

    # change color to yellow
    grovepi.storeColor(255,255,0)
    time.sleep(.5)

    # set every 4nd led to yellow
    grovepi.chainableRgbLed_modulo(pin, 1, 3)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    print "Test 4f) Set using modulo - every 3, offset 2"

    # change color to magenta
    grovepi.storeColor(255,0,255)
    time.sleep(.5)

    # set every 4nd led to magenta
    grovepi.chainableRgbLed_modulo(pin, 2, 3)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 5a) Set level 6"

    # change color to green
    grovepi.storeColor(0,255,0)
    time.sleep(.5)

    # set leds 1-6 to green
    grovepi.write_i2c_block(0x04,[95,pin,6,0])
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 5b) Set level 7 - reverse"

    # change color to red
    grovepi.storeColor(255,0,0)
    time.sleep(.5)

    # set leds 4-10 to red
    grovepi.write_i2c_block(0x04,[95,pin,7,1])
    time.sleep(.5)

except KeyboardInterrupt:
    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
except IOError:
    print "Error"
<commit_msg>Update grove_chainable_rgb_led.py<commit_after>#!/usr/bin/env python
#
# GrovePi Example for using the Grove Chainable RGB LED (http://www.seeedstudio.com/wiki/Grove_-_Chainable_RGB_LED)
#
# The GrovePi connects the Raspberry Pi and Grove sensors.  You can learn more about GrovePi here:  http://www.dexterindustries.com/GrovePi
#
# Have a question about this example?  Ask on the forums here:  http://www.dexterindustries.com/forum/?forum=grovepi
#
# LICENSE:
# These files have been made available online through a [Creative Commons Attribution-ShareAlike 3.0](http://creativecommons.org/licenses/by-sa/3.0/) license.

import time
import grovepi

# Connect first LED in Chainable RGB LED chain to digital port D7
# In: CI,DI,VCC,GND
# Out: CO,DO,VCC,GND
pin = 7

# I have 10 LEDs connected in series with the first connected to the GrovePi and the last not connected
# First LED input socket connected to GrovePi, output socket connected to second LED input and so on
numleds = 10     #If you only plug 1 LED, change 10 to 1

grovepi.pinMode(pin,"OUTPUT")
time.sleep(1)

# Chainable RGB LED methods
# grovepi.storeColor(red, green, blue)
# grovepi.chainableRgbLed_init(pin, numLeds)
# grovepi.chainableRgbLed_test(pin, numLeds, testColor)
# grovepi.chainableRgbLed_pattern(pin, pattern, whichLed)
# grovepi.chainableRgbLed_modulo(pin, offset, divisor)
# grovepi.chainableRgbLed_setLevel(pin, level, reverse)

# test colors used in grovepi.chainableRgbLed_test()
testColorBlack = 0   # 0b000 #000000
testColorBlue = 1    # 0b001 #0000FF
testColorGreen = 2   # 0b010 #00FF00
testColorCyan = 3    # 0b011 #00FFFF
testColorRed = 4     # 0b100 #FF0000
testColorMagenta = 5 # 0b101 #FF00FF
testColorYellow = 6  # 0b110 #FFFF00
testColorWhite = 7   # 0b111 #FFFFFF

# patterns used in grovepi.chainableRgbLed_pattern()
thisLedOnly = 0
allLedsExceptThis = 1
thisLedAndInwards = 2
thisLedAndOutwards = 3

try:

    print "Test 1) Initialise"

    # init chain of leds
    grovepi.chainableRgbLed_init(pin, numleds)
    time.sleep(.5)

    # change color to green
    grovepi.storeColor(0,255,0)
    time.sleep(.5)

    # set led 1 to green
    grovepi.chainableRgbLed_pattern(pin, thisLedOnly, 0)
    time.sleep(.5)

    # change color to red
    grovepi.storeColor(255,0,0)
    time.sleep(.5)

    # set led 10 to red
    grovepi.chainableRgbLed_pattern(pin, thisLedOnly, 9)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 2a) Test Patterns - black"

    # test pattern 0 - black (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(1)

    print "Test 2b) Test Patterns - blue"

    # test pattern 1 blue
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlue)
    time.sleep(1)

    print "Test 2c) Test Patterns - green"

    # test pattern 2 green
    grovepi.chainableRgbLed_test(pin, numleds, testColorGreen)
    time.sleep(1)

    print "Test 2d) Test Patterns - cyan"

    # test pattern 3 cyan
    grovepi.chainableRgbLed_test(pin, numleds, testColorCyan)
    time.sleep(1)

    print "Test 2e) Test Patterns - red"

    # test pattern 4 red
    grovepi.chainableRgbLed_test(pin, numleds, testColorRed)
    time.sleep(1)

    print "Test 2f) Test Patterns - magenta"

    # test pattern 5 magenta
    grovepi.chainableRgbLed_test(pin, numleds, testColorMagenta)
    time.sleep(1)

    print "Test 2g) Test Patterns - yellow"

    # test pattern 6 yellow
    grovepi.chainableRgbLed_test(pin, numleds, testColorYellow)
    time.sleep(1)

    print "Test 2h) Test Patterns - white"

    # test pattern 7 white
    grovepi.chainableRgbLed_test(pin, numleds, testColorWhite)
    time.sleep(1)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 3a) Set using pattern - this led only"

    # change color to red
    grovepi.storeColor(255,0,0)
    time.sleep(.5)

    # set led 3 to red
    grovepi.chainableRgbLed_pattern(pin, thisLedOnly, 2)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 3b) Set using pattern - all leds except this"

    # change color to blue
    grovepi.storeColor(0,0,255)
    time.sleep(.5)

    # set all leds except for 3 to blue
    grovepi.chainableRgbLed_pattern(pin, allLedsExceptThis, 3)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 3c) Set using pattern - this led and inwards"

    # change color to green
    grovepi.storeColor(0,255,0)
    time.sleep(.5)

    # set leds 1-3 to green
    grovepi.chainableRgbLed_pattern(pin, thisLedAndInwards, 2)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 3d) Set using pattern - this led and outwards"

    # change color to green
    grovepi.storeColor(0,255,0)
    time.sleep(.5)

    # set leds 7-10 to green
    grovepi.chainableRgbLed_pattern(pin, thisLedAndOutwards, 6)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 4a) Set using modulo - all leds"

    # change color to black (fully off)
    grovepi.storeColor(0,0,0)
    time.sleep(.5)

    # set all leds black
    # offset 0 means start at first led
    # divisor 1 means every led
    grovepi.chainableRgbLed_modulo(pin, 0, 1)
    time.sleep(.5)

    # change color to white (fully on)
    grovepi.storeColor(255,255,255)
    time.sleep(.5)

    # set all leds white
    grovepi.chainableRgbLed_modulo(pin, 0, 1)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 4b) Set using modulo - every 2"

    # change color to red
    grovepi.storeColor(255,0,0)
    time.sleep(.5)

    # set every 2nd led to red
    grovepi.chainableRgbLed_modulo(pin, 0, 2)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    print "Test 4c) Set using modulo - every 2, offset 1"

    # change color to green
    grovepi.storeColor(0,255,0)
    time.sleep(.5)

    # set every 2nd led to green, offset 1
    grovepi.chainableRgbLed_modulo(pin, 1, 2)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 4d) Set using modulo - every 3, offset 0"

    # change color to red
    grovepi.storeColor(255,0,0)
    time.sleep(.5)

    # set every 3nd led to red
    grovepi.chainableRgbLed_modulo(pin, 0, 3)
    time.sleep(.5)

    # change color to green
    grovepi.storeColor(0,255,0)
    time.sleep(.5)

    # set every 3nd led to green, offset 1
    grovepi.chainableRgbLed_modulo(pin, 1, 3)
    time.sleep(.5)

    # change color to blue
    grovepi.storeColor(0,0,255)
    time.sleep(.5)

    # set every 3nd led to blue, offset 2
    grovepi.chainableRgbLed_modulo(pin, 2, 3)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 4e) Set using modulo - every 3, offset 1"

    # change color to yellow
    grovepi.storeColor(255,255,0)
    time.sleep(.5)

    # set every 4nd led to yellow
    grovepi.chainableRgbLed_modulo(pin, 1, 3)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    print "Test 4f) Set using modulo - every 3, offset 2"

    # change color to magenta
    grovepi.storeColor(255,0,255)
    time.sleep(.5)

    # set every 4nd led to magenta
    grovepi.chainableRgbLed_modulo(pin, 2, 3)
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 5a) Set level 6"

    # change color to green
    grovepi.storeColor(0,255,0)
    time.sleep(.5)

    # set leds 1-6 to green
    grovepi.write_i2c_block(0x04,[95,pin,6,0])
    time.sleep(.5)

    # pause so you can see what happened
    time.sleep(2)

    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
    time.sleep(.5)

    print "Test 5b) Set level 7 - reverse"

    # change color to red
    grovepi.storeColor(255,0,0)
    time.sleep(.5)

    # set leds 4-10 to red
    grovepi.write_i2c_block(0x04,[95,pin,7,1])
    time.sleep(.5)

except KeyboardInterrupt:
    # reset (all off)
    grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
except IOError:
    print "Error"
<|endoftext|>


<commit_before>import argparse
import contextlib
import functools
import inspect
import time

ANSI_ESCAPE_CODE_RED = "\x1b[1;31m"
ANSI_ESCAPE_CODE_YELLOW = "\x1b[1;33m"
ANSI_ESCAPE_CODE_RESET = "\x1b[1;0m"

class CommandFailedException(Exception):
    def __init__(self, message, hint):
        super().__init__(message)
        self.hint = hint

    def __str__(self):
        return '{}command failed: {}{}{}'.format(
            ANSI_ESCAPE_CODE_RED,
            super().__str__(),
            '\n{}{}'.format(ANSI_ESCAPE_CODE_YELLOW, self.hint)
            if self.hint is not None else '',
            ANSI_ESCAPE_CODE_RESET)

def fail(message: str, hint: str = None) -> None:
    raise CommandFailedException(message, hint)

def mux_map(desc: str, mapping: dict):
    def configure(command: list, parser: argparse.ArgumentParser):
        parser.set_defaults(argparse_parser=parser)
        subparsers = parser.add_subparsers()

        for component, (inner_desc, inner_configure) in mapping.items():
            inner_parser = subparsers.add_parser(component, description=inner_desc, help=inner_desc)
            inner_configure(command + [component], inner_parser)

    return desc, configure

class Command:
    def __init__(self, func):
        self.func = func
        self.sig = inspect.signature(self.func)
        self._remove_ops_from_sig()
        self._command = None

    def _remove_ops_from_sig(self):
        parameters = list(self.sig.parameters.values())
        if parameters[0].name != 'ops':
            raise ValueError('first argument to command must be ops')
        parameters = parameters[1:]
        self.sig = self.sig.replace(parameters=parameters)

    # so that this can still be called as the original function
    def __call__(self, *args, **kwargs):
        return self.func(*args, **kwargs)

    def operate(self, op, *args, **kwargs):
        "Schedule this command to be run by Operations"
        self.func(op, *args, **kwargs)

    def process_args(self, argparse_args):
        "Process command-line arguments into function arguments"
        cli_args = vars(argparse_args)
        posargs = []
        kwargs = {}
        for name, param in self.sig.parameters.items():
            if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
                if param.default == inspect.Parameter.empty:
                    posargs.append(cli_args[name])
                    continue
                kwargs[name] = cli_args[name]
                continue
            if param.kind == inspect.Parameter.VAR_POSITIONAL:
                posargs.extend(cli_args[name])
                continue
            raise Exception("python argument type not recognized")

        # fail early if arguments do not match function signature
        self.sig.bind(*posargs, **kwargs)

        return posargs, kwargs

    def invoke(self, aargs):
        ops = Operations()
        args, kwargs = self.process_args(aargs)
        self.operate(ops, *args, **kwargs)
        ops()

    def configure(self, command: list, parser: argparse.ArgumentParser):
        parser.set_defaults(argparse_invoke=self.invoke,
                            argparse_parser=parser)

        # convert function signature into argparse configuration
        for name, param in self.sig.parameters.items():
            try:
                if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
                    if param.annotation == bool:
                        if param.default == inspect.Parameter.empty or param.default:
                            raise ValueError("boolean argument must specify default value of false")
                        parser.add_argument('--' + name, action='store_true')
                        continue
                    if param.default == inspect.Parameter.empty:
                        parser.add_argument(name)
                        continue
                    if not (isinstance(param.default, str) or param.default is None):
                        raise ValueError("default for string argument must be string or None")
                    parser.add_argument('--' + name, default=param.default)
                    continue
                if param.kind == inspect.Parameter.VAR_POSITIONAL:
                    parser.add_argument(name, nargs=argparse.REMAINDER)
                    continue
                raise ValueError("python argument kind {} not recognized".format(param.kind))
            except Exception as e:
                raise Exception("command {}: failed to configure argument {}".format(command, name)) from e

        self._command = command

    def command(self, *args, **kwargs):
        "Produce a string representation of this command with the specified arguments"
        if self._command is None:
            return None

        bound = self.sig.bind(*args, **kwargs)
        cl = self._command[:]
        for k, v in bound.arguments.items():
            param = self.sig.parameters[k]
            if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
                if param.default == inspect.Parameter.empty:
                    cl.append(str(v))
                    continue
                if param.annotation == bool:
                    if v:
                        cl.append('--{}'.format(k))
                    continue
                cl.append('--{}={}'.format(k, v))
                continue
            if param.kind == inspect.Parameter.VAR_POSITIONAL:
                cl.extend(str(x) for x in v)
                continue
            raise Exception("python argument type not recognized")
        return ' '.join(cl)

def wrapop(f):
    return functools.update_wrapper(Command(f), f, updated=[])

class Simple(Command):
    def _remove_ops_from_sig(self):
        pass

    def operate(self, op, *args, **kwargs):
        op.add_operation(self.__doc__, lambda: self.func(*args, **kwargs),
                         self.command(*args, **kwargs))

    def invoke(self, aargs):
        args, kwargs = self.process_args(aargs)
        self.func(*args, **kwargs)

def wrap(f):
    return functools.update_wrapper(Simple(f), f, updated=[])

def main_invoke(command):
    parser = argparse.ArgumentParser(description="Administrative toolkit for deploying and maintaining Hyades clusters")
    command.configure(["spire"], parser)
    args = parser.parse_args()
    if "argparse_invoke" in args:
        args.argparse_invoke(args)
    else:
        args.argparse_parser.print_help()
    return 0
<commit_msg>spire: refactor mux_map into Mux class<commit_after>import argparse
import contextlib
import functools
import inspect
import time

ANSI_ESCAPE_CODE_RED = "\x1b[1;31m"
ANSI_ESCAPE_CODE_YELLOW = "\x1b[1;33m"
ANSI_ESCAPE_CODE_RESET = "\x1b[1;0m"

class CommandFailedException(Exception):
    def __init__(self, message, hint):
        super().__init__(message)
        self.hint = hint

    def __str__(self):
        return '{}command failed: {}{}{}'.format(
            ANSI_ESCAPE_CODE_RED,
            super().__str__(),
            '\n{}{}'.format(ANSI_ESCAPE_CODE_YELLOW, self.hint)
            if self.hint is not None else '',
            ANSI_ESCAPE_CODE_RESET)

def fail(message: str, hint: str = None) -> None:
    raise CommandFailedException(message, hint)

class Mux:
    def __init__(self, description, mapping):
        self.__doc__ = description
        self.mapping = mapping

    def configure(self, command: list, parser: argparse.ArgumentParser):
        parser.set_defaults(argparse_parser=parser)
        subparsers = parser.add_subparsers()

        for component, subcommand in self.mapping.items():
            doc = inspect.getdoc(subcommand)
            short_doc = doc.split('\n')[0] if doc else None
            subparser = subparsers.add_parser(
                component,
                description=doc,
                help=short_doc,
                formatter_class=argparse.RawDescriptionHelpFormatter)
            try:
                subcommand.configure(command + [component], subparser)
            except AttributeError as e:
                raise Exception("error configuring subcommand {!r}".format(subcommand)) from e

class Command:
    def __init__(self, func):
        self.func = func
        self.sig = inspect.signature(self.func)
        self._remove_ops_from_sig()
        self._command = None

    def _remove_ops_from_sig(self):
        parameters = list(self.sig.parameters.values())
        if parameters[0].name != 'ops':
            raise ValueError('first argument to command must be ops')
        parameters = parameters[1:]
        self.sig = self.sig.replace(parameters=parameters)

    # so that this can still be called as the original function
    def __call__(self, *args, **kwargs):
        return self.func(*args, **kwargs)

    def operate(self, op, *args, **kwargs):
        "Schedule this command to be run by Operations"
        self.func(op, *args, **kwargs)

    def process_args(self, argparse_args):
        "Process command-line arguments into function arguments"
        cli_args = vars(argparse_args)
        posargs = []
        kwargs = {}
        for name, param in self.sig.parameters.items():
            if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
                if param.default == inspect.Parameter.empty:
                    posargs.append(cli_args[name])
                    continue
                kwargs[name] = cli_args[name]
                continue
            if param.kind == inspect.Parameter.VAR_POSITIONAL:
                posargs.extend(cli_args[name])
                continue
            raise Exception("python argument type not recognized")

        # fail early if arguments do not match function signature
        self.sig.bind(*posargs, **kwargs)

        return posargs, kwargs

    def invoke(self, aargs):
        ops = Operations()
        args, kwargs = self.process_args(aargs)
        self.operate(ops, *args, **kwargs)
        ops()

    def configure(self, command: list, parser: argparse.ArgumentParser):
        parser.set_defaults(argparse_invoke=self.invoke,
                            argparse_parser=parser)

        # convert function signature into argparse configuration
        for name, param in self.sig.parameters.items():
            try:
                if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
                    if param.annotation == bool:
                        if param.default == inspect.Parameter.empty or param.default:
                            raise ValueError("boolean argument must specify default value of false")
                        parser.add_argument('--' + name, action='store_true')
                        continue
                    if param.default == inspect.Parameter.empty:
                        parser.add_argument(name)
                        continue
                    if not (isinstance(param.default, str) or param.default is None):
                        raise ValueError("default for string argument must be string or None")
                    parser.add_argument('--' + name, default=param.default)
                    continue
                if param.kind == inspect.Parameter.VAR_POSITIONAL:
                    parser.add_argument(name, nargs=argparse.REMAINDER)
                    continue
                raise ValueError("python argument kind {} not recognized".format(param.kind))
            except Exception as e:
                raise Exception("command {}: failed to configure argument {}".format(command, name)) from e

        self._command = command

    def command(self, *args, **kwargs):
        "Produce a string representation of this command with the specified arguments"
        if self._command is None:
            return None

        bound = self.sig.bind(*args, **kwargs)
        cl = self._command[:]
        for k, v in bound.arguments.items():
            param = self.sig.parameters[k]
            if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
                if param.default == inspect.Parameter.empty:
                    cl.append(str(v))
                    continue
                if param.annotation == bool:
                    if v:
                        cl.append('--{}'.format(k))
                    continue
                cl.append('--{}={}'.format(k, v))
                continue
            if param.kind == inspect.Parameter.VAR_POSITIONAL:
                cl.extend(str(x) for x in v)
                continue
            raise Exception("python argument type not recognized")
        return ' '.join(cl)

def wrapop(f):
    return functools.update_wrapper(Command(f), f, updated=[])

class Simple(Command):
    def _remove_ops_from_sig(self):
        pass

    def operate(self, op, *args, **kwargs):
        op.add_operation(self.__doc__, lambda: self.func(*args, **kwargs),
                         self.command(*args, **kwargs))

    def invoke(self, aargs):
        args, kwargs = self.process_args(aargs)
        self.func(*args, **kwargs)

def wrap(f):
    return functools.update_wrapper(Simple(f), f, updated=[])

def main_invoke(command):
    parser = argparse.ArgumentParser(description="Administrative toolkit for deploying and maintaining Hyades clusters")
    command.configure(["spire"], parser)
    args = parser.parse_args()
    if "argparse_invoke" in args:
        args.argparse_invoke(args)
    else:
        args.argparse_parser.print_help()
    return 0
<|endoftext|>


<commit_before>print("Hello, world")
<commit_msg>Adding comment<commit_after># Printing hello, world
print("Hello, world")
<|endoftext|>


<commit_before># Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# 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.

# pylint: disable=redefined-builtin, too-many-arguments

from collections import OrderedDict, namedtuple

import click

ConfigOptionClass = namedtuple("ConfigOption", [
    "scope", "name", "type", "multiple", "sysenvvar", "buildenvvar", "oldnames"
])

def ConfigOption(scope,
                 name,
                 type=str,
                 multiple=False,
                 sysenvvar=None,
                 buildenvvar=None,
                 oldnames=None):
    return ConfigOptionClass(scope, name, type, multiple, sysenvvar,
                             buildenvvar, oldnames)

def ConfigPlatformioOption(*args, **kwargs):
    return ConfigOption("platformio", *args, **kwargs)

def ConfigEnvOption(*args, **kwargs):
    return ConfigOption("env", *args, **kwargs)

ProjectOptions = OrderedDict([
    ("%s.%s" % (option.scope, option.name), option) for option in [
        #
        # [platformio]
        #
        ConfigPlatformioOption(name="description"),
        ConfigPlatformioOption(name="default_envs",
                               oldnames=["env_default"],
                               multiple=True,
                               sysenvvar="PLATFORMIO_DEFAULT_ENVS"),
        ConfigPlatformioOption(name="extra_configs", multiple=True),

        # Dirs
        ConfigPlatformioOption(name="core_dir",
                               oldnames=["home_dir"],
                               sysenvvar="PLATFORMIO_CORE_DIR"),
        ConfigPlatformioOption(name="globallib_dir",
                               sysenvvar="PLATFORMIO_GLOBALLIB_DIR"),
        ConfigPlatformioOption(name="platforms_dir",
                               sysenvvar="PLATFORMIO_PLATFORMS_DIR"),
        ConfigPlatformioOption(name="packages_dir",
                               sysenvvar="PLATFORMIO_PACKAGES_DIR"),
        ConfigPlatformioOption(name="cache_dir",
                               sysenvvar="PLATFORMIO_CACHE_DIR"),
        ConfigPlatformioOption(name="workspace_dir",
                               sysenvvar="PLATFORMIO_WORKSPACE_DIR"),
        ConfigPlatformioOption(name="build_dir",
                               sysenvvar="PLATFORMIO_BUILD_DIR"),
        ConfigPlatformioOption(name="libdeps_dir",
                               sysenvvar="PLATFORMIO_LIBDEPS_DIR"),
        ConfigPlatformioOption(name="lib_dir", sysenvvar="PLATFORMIO_LIB_DIR"),
        ConfigPlatformioOption(name="include_dir",
                               sysenvvar="PLATFORMIO_INCLUDE_DIR"),
        ConfigPlatformioOption(name="src_dir", sysenvvar="PLATFORMIO_SRC_DIR"),
        ConfigPlatformioOption(name="test_dir",
                               sysenvvar="PLATFORMIO_TEST_DIR"),
        ConfigPlatformioOption(name="boards_dir",
                               sysenvvar="PLATFORMIO_BOARDS_DIR"),
        ConfigPlatformioOption(name="data_dir",
                               sysenvvar="PLATFORMIO_DATA_DIR"),
        ConfigPlatformioOption(name="shared_dir",
                               sysenvvar="PLATFORMIO_SHARED_DIR"),

        #
        # [env]
        #

        # Generic
        ConfigEnvOption(name="platform", buildenvvar="PIOPLATFORM"),
        ConfigEnvOption(
            name="framework", multiple=True, buildenvvar="PIOFRAMEWORK"),
        ConfigEnvOption(name="targets", multiple=True),

        # Board
        ConfigEnvOption(name="board", buildenvvar="BOARD"),
        ConfigEnvOption(name="board_build.mcu",
                        oldnames=["board_mcu"],
                        buildenvvar="BOARD_MCU"),
        ConfigEnvOption(name="board_build.f_cpu",
                        oldnames=["board_f_cpu"],
                        buildenvvar="BOARD_F_CPU"),
        ConfigEnvOption(name="board_build.f_flash",
                        oldnames=["board_f_flash"],
                        buildenvvar="BOARD_F_FLASH"),
        ConfigEnvOption(name="board_build.flash_mode",
                        oldnames=["board_flash_mode"],
                        buildenvvar="BOARD_FLASH_MODE"),

        # Build
        ConfigEnvOption(name="build_type",
                        type=click.Choice(["release", "debug"])),
        ConfigEnvOption(name="build_flags",
                        multiple=True,
                        sysenvvar="PLATFORMIO_BUILD_FLAGS",
                        buildenvvar="BUILD_FLAGS"),
        ConfigEnvOption(name="src_build_flags",
                        multiple=True,
                        sysenvvar="PLATFORMIO_SRC_BUILD_FLAGS",
                        buildenvvar="SRC_BUILD_FLAGS"),
        ConfigEnvOption(name="build_unflags",
                        multiple=True,
                        sysenvvar="PLATFORMIO_BUILD_UNFLAGS",
                        buildenvvar="BUILD_UNFLAGS"),
        ConfigEnvOption(name="src_filter",
                        multiple=True,
                        sysenvvar="PLATFORMIO_SRC_FILTER",
                        buildenvvar="SRC_FILTER"),

        # Upload
        ConfigEnvOption(name="upload_port",
                        sysenvvar="PLATFORMIO_UPLOAD_PORT",
                        buildenvvar="UPLOAD_PORT"),
        ConfigEnvOption(name="upload_protocol", buildenvvar="UPLOAD_PROTOCOL"),
        ConfigEnvOption(
            name="upload_speed", type=click.INT, buildenvvar="UPLOAD_SPEED"),
        ConfigEnvOption(name="upload_flags",
                        multiple=True,
                        sysenvvar="PLATFORMIO_UPLOAD_FLAGS",
                        buildenvvar="UPLOAD_FLAGS"),
        ConfigEnvOption(name="upload_resetmethod",
                        buildenvvar="UPLOAD_RESETMETHOD"),
        ConfigEnvOption(name="upload_command", buildenvvar="UPLOADCMD"),

        # Monitor
        ConfigEnvOption(name="monitor_port"),
        ConfigEnvOption(name="monitor_speed", oldnames=["monitor_baud"]),
        ConfigEnvOption(name="monitor_rts", type=click.IntRange(0, 1)),
        ConfigEnvOption(name="monitor_dtr", type=click.IntRange(0, 1)),
        ConfigEnvOption(name="monitor_flags", multiple=True),

        # Library
        ConfigEnvOption(name="lib_deps",
                        oldnames=["lib_use", "lib_force", "lib_install"],
                        multiple=True),
        ConfigEnvOption(name="lib_ignore", multiple=True),
        ConfigEnvOption(name="lib_extra_dirs",
                        multiple=True,
                        sysenvvar="PLATFORMIO_LIB_EXTRA_DIRS",
                        type=click.Path(
                            exists=True, file_okay=False, dir_okay=True)),
        ConfigEnvOption(name="lib_ldf_mode",
                        type=click.Choice(
                            ["off", "chain", "deep", "chain+", "deep+"])),
        ConfigEnvOption(name="lib_compat_mode",
                        type=click.Choice(["off", "soft", "strict"])),
        ConfigEnvOption(name="lib_archive", type=click.BOOL),

        # Test
        ConfigEnvOption(name="test_filter", multiple=True),
        ConfigEnvOption(name="test_ignore", multiple=True),
        ConfigEnvOption(name="test_port"),
        ConfigEnvOption(name="test_speed", type=click.INT),
        ConfigEnvOption(name="test_transport"),
        ConfigEnvOption(name="test_build_project_src", type=click.BOOL),

        # Debug
        ConfigEnvOption(name="debug_tool"),
        ConfigEnvOption(name="debug_init_break"),
        ConfigEnvOption(name="debug_init_cmds", multiple=True),
        ConfigEnvOption(name="debug_extra_cmds", multiple=True),
        ConfigEnvOption(name="debug_load_cmds",
                        oldnames=["debug_load_cmd"],
                        multiple=True),
        ConfigEnvOption(name="debug_load_mode",
                        type=click.Choice(["always", "modified", "manual"])),
        ConfigEnvOption(name="debug_server", multiple=True),
        ConfigEnvOption(name="debug_port"),
        ConfigEnvOption(name="debug_svd_path",
                        type=click.Path(
                            exists=True, file_okay=True, dir_okay=False)),

        # Other
        ConfigEnvOption(name="extra_scripts",
                        oldnames=["extra_script"],
                        multiple=True,
                        sysenvvar="PLATFORMIO_EXTRA_SCRIPTS")
    ]
])
<commit_msg>Do not check that lib_extra_dirs exist // Resolve #2624<commit_after># Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# 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.

# pylint: disable=redefined-builtin, too-many-arguments

from collections import OrderedDict, namedtuple

import click

ConfigOptionClass = namedtuple("ConfigOption", [
    "scope", "name", "type", "multiple", "sysenvvar", "buildenvvar", "oldnames"
])

def ConfigOption(scope,
                 name,
                 type=str,
                 multiple=False,
                 sysenvvar=None,
                 buildenvvar=None,
                 oldnames=None):
    return ConfigOptionClass(scope, name, type, multiple, sysenvvar,
                             buildenvvar, oldnames)

def ConfigPlatformioOption(*args, **kwargs):
    return ConfigOption("platformio", *args, **kwargs)

def ConfigEnvOption(*args, **kwargs):
    return ConfigOption("env", *args, **kwargs)

ProjectOptions = OrderedDict([
    ("%s.%s" % (option.scope, option.name), option) for option in [
        #
        # [platformio]
        #
        ConfigPlatformioOption(name="description"),
        ConfigPlatformioOption(name="default_envs",
                               oldnames=["env_default"],
                               multiple=True,
                               sysenvvar="PLATFORMIO_DEFAULT_ENVS"),
        ConfigPlatformioOption(name="extra_configs", multiple=True),

        # Dirs
        ConfigPlatformioOption(name="core_dir",
                               oldnames=["home_dir"],
                               sysenvvar="PLATFORMIO_CORE_DIR"),
        ConfigPlatformioOption(name="globallib_dir",
                               sysenvvar="PLATFORMIO_GLOBALLIB_DIR"),
        ConfigPlatformioOption(name="platforms_dir",
                               sysenvvar="PLATFORMIO_PLATFORMS_DIR"),
        ConfigPlatformioOption(name="packages_dir",
                               sysenvvar="PLATFORMIO_PACKAGES_DIR"),
        ConfigPlatformioOption(name="cache_dir",
                               sysenvvar="PLATFORMIO_CACHE_DIR"),
        ConfigPlatformioOption(name="workspace_dir",
                               sysenvvar="PLATFORMIO_WORKSPACE_DIR"),
        ConfigPlatformioOption(name="build_dir",
                               sysenvvar="PLATFORMIO_BUILD_DIR"),
        ConfigPlatformioOption(name="libdeps_dir",
                               sysenvvar="PLATFORMIO_LIBDEPS_DIR"),
        ConfigPlatformioOption(name="lib_dir", sysenvvar="PLATFORMIO_LIB_DIR"),
        ConfigPlatformioOption(name="include_dir",
                               sysenvvar="PLATFORMIO_INCLUDE_DIR"),
        ConfigPlatformioOption(name="src_dir", sysenvvar="PLATFORMIO_SRC_DIR"),
        ConfigPlatformioOption(name="test_dir",
                               sysenvvar="PLATFORMIO_TEST_DIR"),
        ConfigPlatformioOption(name="boards_dir",
                               sysenvvar="PLATFORMIO_BOARDS_DIR"),
        ConfigPlatformioOption(name="data_dir",
                               sysenvvar="PLATFORMIO_DATA_DIR"),
        ConfigPlatformioOption(name="shared_dir",
                               sysenvvar="PLATFORMIO_SHARED_DIR"),

        #
        # [env]
        #

        # Generic
        ConfigEnvOption(name="platform", buildenvvar="PIOPLATFORM"),
        ConfigEnvOption(
            name="framework", multiple=True, buildenvvar="PIOFRAMEWORK"),
        ConfigEnvOption(name="targets", multiple=True),

        # Board
        ConfigEnvOption(name="board", buildenvvar="BOARD"),
        ConfigEnvOption(name="board_build.mcu",
                        oldnames=["board_mcu"],
                        buildenvvar="BOARD_MCU"),
        ConfigEnvOption(name="board_build.f_cpu",
                        oldnames=["board_f_cpu"],
                        buildenvvar="BOARD_F_CPU"),
        ConfigEnvOption(name="board_build.f_flash",
                        oldnames=["board_f_flash"],
                        buildenvvar="BOARD_F_FLASH"),
        ConfigEnvOption(name="board_build.flash_mode",
                        oldnames=["board_flash_mode"],
                        buildenvvar="BOARD_FLASH_MODE"),

        # Build
        ConfigEnvOption(name="build_type",
                        type=click.Choice(["release", "debug"])),
        ConfigEnvOption(name="build_flags",
                        multiple=True,
                        sysenvvar="PLATFORMIO_BUILD_FLAGS",
                        buildenvvar="BUILD_FLAGS"),
        ConfigEnvOption(name="src_build_flags",
                        multiple=True,
                        sysenvvar="PLATFORMIO_SRC_BUILD_FLAGS",
                        buildenvvar="SRC_BUILD_FLAGS"),
        ConfigEnvOption(name="build_unflags",
                        multiple=True,
                        sysenvvar="PLATFORMIO_BUILD_UNFLAGS",
                        buildenvvar="BUILD_UNFLAGS"),
        ConfigEnvOption(name="src_filter",
                        multiple=True,
                        sysenvvar="PLATFORMIO_SRC_FILTER",
                        buildenvvar="SRC_FILTER"),

        # Upload
        ConfigEnvOption(name="upload_port",
                        sysenvvar="PLATFORMIO_UPLOAD_PORT",
                        buildenvvar="UPLOAD_PORT"),
        ConfigEnvOption(name="upload_protocol", buildenvvar="UPLOAD_PROTOCOL"),
        ConfigEnvOption(
            name="upload_speed", type=click.INT, buildenvvar="UPLOAD_SPEED"),
        ConfigEnvOption(name="upload_flags",
                        multiple=True,
                        sysenvvar="PLATFORMIO_UPLOAD_FLAGS",
                        buildenvvar="UPLOAD_FLAGS"),
        ConfigEnvOption(name="upload_resetmethod",
                        buildenvvar="UPLOAD_RESETMETHOD"),
        ConfigEnvOption(name="upload_command", buildenvvar="UPLOADCMD"),

        # Monitor
        ConfigEnvOption(name="monitor_port"),
        ConfigEnvOption(name="monitor_speed", oldnames=["monitor_baud"]),
        ConfigEnvOption(name="monitor_rts", type=click.IntRange(0, 1)),
        ConfigEnvOption(name="monitor_dtr", type=click.IntRange(0, 1)),
        ConfigEnvOption(name="monitor_flags", multiple=True),

        # Library
        ConfigEnvOption(name="lib_deps",
                        oldnames=["lib_use", "lib_force", "lib_install"],
                        multiple=True),
        ConfigEnvOption(name="lib_ignore", multiple=True),
        ConfigEnvOption(name="lib_extra_dirs",
                        multiple=True,
                        sysenvvar="PLATFORMIO_LIB_EXTRA_DIRS"),
        ConfigEnvOption(name="lib_ldf_mode",
                        type=click.Choice(
                            ["off", "chain", "deep", "chain+", "deep+"])),
        ConfigEnvOption(name="lib_compat_mode",
                        type=click.Choice(["off", "soft", "strict"])),
        ConfigEnvOption(name="lib_archive", type=click.BOOL),

        # Test
        ConfigEnvOption(name="test_filter", multiple=True),
        ConfigEnvOption(name="test_ignore", multiple=True),
        ConfigEnvOption(name="test_port"),
        ConfigEnvOption(name="test_speed", type=click.INT),
        ConfigEnvOption(name="test_transport"),
        ConfigEnvOption(name="test_build_project_src", type=click.BOOL),

        # Debug
        ConfigEnvOption(name="debug_tool"),
        ConfigEnvOption(name="debug_init_break"),
        ConfigEnvOption(name="debug_init_cmds", multiple=True),
        ConfigEnvOption(name="debug_extra_cmds", multiple=True),
        ConfigEnvOption(name="debug_load_cmds",
                        oldnames=["debug_load_cmd"],
                        multiple=True),
        ConfigEnvOption(name="debug_load_mode",
                        type=click.Choice(["always", "modified", "manual"])),
        ConfigEnvOption(name="debug_server", multiple=True),
        ConfigEnvOption(name="debug_port"),
        ConfigEnvOption(name="debug_svd_path",
                        type=click.Path(
                            exists=True, file_okay=True, dir_okay=False)),

        # Other
        ConfigEnvOption(name="extra_scripts",
                        oldnames=["extra_script"],
                        multiple=True,
                        sysenvvar="PLATFORMIO_EXTRA_SCRIPTS")
    ]
])
<|endoftext|>


<commit_before>from mcex import *
import numpy as np
import theano

#import pydevd
#pydevd.set_pm_excepthook()
np.seterr(invalid = 'raise')

data = np.random.normal(size = (3, 20))

x = FreeVariable('x', (3,1), 'float64')
z = FreeVariable('z', (1,), 'float64')

x_prior = Normal(value = x, mu = .5, tau = 2.**-2) #attach a prior to variable x
ydata = Normal(value = data, mu = x, tau = .75**-2)

z_prior = Beta(value = z, alpha = 10, beta =5.5)

#make a chain with some starting point
chain = {'x' : np.array([[0.2],[.3],[.1]]),
         'z' : np.array([.5])}

hmc_model = ModelView(free_vars = [x,z],
                  logps = [x_prior, ydata , z_prior],
                  derivative_vars = [x,z])

#move the chain to the MAP which should be a good starting point

find_MAP(hmc_model, chain)
hmc_cov = approx_cov( hmc_model, chain) #find a good orientation using the hessian at the MAP

step_method = CompoundStep([hmc.HMCStep(hmc_model, hmc_cov)])

ndraw = 3e3
"""
print chain.values
x = np.linspace(.01, .99, 50)
y = np.array([ hmc_model.function(x = np.array([[0],[0],[0]]), z = np.array([xi]) )[0][()] for xi in x])

import pylab
pylab.figure()
y2 = np.exp(y)/np.sum(np.exp(y))
pylab.plot(x,y2)
pylab.show()
print hmc_cov"""
history = NpHistory(hmc_model, ndraw) # an object that keeps track
print "took :", sample(ndraw, step_method, chain, history)<commit_msg> example bug fix<commit_after>from mcex import *
import numpy as np
import theano

#import pydevd
#pydevd.set_pm_excepthook()
np.seterr(invalid = 'raise')

data = np.random.normal(size = (3, 20))

x = FreeVariable('x', (3,1), 'float64')
z = FreeVariable('z', (1,), 'float64')

x_prior = Normal(value = x, mu = .5, tau = 2.**-2) #attach a prior to variable x
ydata = Normal(value = data, mu = x, tau = .75**-2)

z_prior = Beta(value = z, alpha = 10, beta =5.5)

#make a chain with some starting point
chain = {'x' : np.array([[0.2],[.3],[.1]]),
         'z' : np.array([.5])}

hmc_model = ModelView(free_vars = [x,z],
                  logps = [x_prior, ydata , z_prior],
                  derivative_vars = [x,z])

#move the chain to the MAP which should be a good starting point
chain = find_MAP(hmc_model, chain)
hmc_cov = approx_cov( hmc_model, chain) #find a good orientation using the hessian at the MAP

step_method = CompoundStep([hmc.HMCStep(hmc_model, hmc_cov)])

ndraw = 3e3
"""
print chain.values
x = np.linspace(.01, .99, 50)
y = np.array([ hmc_model.function(x = np.array([[0],[0],[0]]), z = np.array([xi]) )[0][()] for xi in x])

import pylab
pylab.figure()
y2 = np.exp(y)/np.sum(np.exp(y))
pylab.plot(x,y2)
pylab.show()
print hmc_cov"""
history = NpHistory(hmc_model, ndraw) # an object that keeps track
print "took :", sample(ndraw, step_method, chain, history)<|endoftext|>


<commit_before>import sys

from django.conf import settings
from django.core.management.base import BaseCommand

from pillowtop.const import DEFAULT_PROCESSOR_CHUNK_SIZE
from pillowtop.run_pillowtop import start_pillows, start_pillow
from pillowtop.utils import (
    get_all_pillow_instances,
    get_all_pillow_configs,
    get_pillow_config_from_setting,
    get_pillow_by_name
)

class Command(BaseCommand):
    help = "Run pillows pillows listed in settings."

    def add_arguments(self, parser):
        parser.add_argument(
            '--all',
            action='store_true',
            dest='run_all',
            default=False,
            help="Run all pillows from settings - use for local dev",
        )
        parser.add_argument(
            '--list',
            action='store_true',
            dest='list_all',
            default=False,
            help="List pillowtop names",
        )
        parser.add_argument(
            '--list-checkpoints',
            action='store_true',
            dest='list_checkpoints',
            default=False,
            help="Print all pillow doc ids.",
        )
        parser.add_argument(
            '--pillow-key',
            action='store',
            dest='pillow_key',
            default=None,
            help="Run a specific key of a group of pillows from settings.PILLOWTOPS list",
        )
        parser.add_argument(
            '--pillow-name',
            action='store',
            dest='pillow_name',
            default=None,
            help="Run a single specific pillow name from settings.PILLOWTOPS list",
        )
        parser.add_argument(
            '--num-processes',
            action='store',
            dest='num_processes',
            default=1,
            type=int,
            help="The number of processes that are expected to be run for this pillow",
        )
        parser.add_argument(
            '--process-number',
            action='store',
            dest='process_number',
            default=0,
            type=int,
            help="The process number of this pillow process. Should be between 0 and num-processes. "
                 "It's expected that there will only be one process for each number running at once",
        )
        parser.add_argument(
            '--processor-chunk-size',
            action='store',
            dest='processor_chunk_size',
            default=DEFAULT_PROCESSOR_CHUNK_SIZE,
            type=int,
            help="The process number of this pillow process. Should be between 0 and num-processes. "
                 "It's expected that there will only be one process for each number running at once",
        )

    def handle(self, **options):
        run_all = options['run_all']
        list_all = options['list_all']
        list_checkpoints = options['list_checkpoints']
        pillow_name = options['pillow_name']
        pillow_key = options['pillow_key']
        num_processes = options['num_processes']
        process_number = options['process_number']
        processor_chunk_size = options['processor_chunk_size']
        assert 0 <= process_number < num_processes
        assert processor_chunk_size
        if list_all:
            print("\nPillows registered in system:")
            for config in get_all_pillow_configs():
                print('{}: {}'.format(config.section, config.name))

            print("\n\tRun with --pillow-name <name> to run a pillow")
            print("\n\tRun with --pillow-key <key> to run a group of pillows together\n")
            sys.exit()

        if run_all:
            pillows_to_run = get_all_pillow_configs()
        elif not run_all and not pillow_name and pillow_key:
            # get pillows from key
            if pillow_key not in settings.PILLOWTOPS:
                print("\n\tError, key %s is not in settings.PILLOWTOPS, legal keys are: %s" % \
                      (pillow_key, list(settings.PILLOWTOPS)))
                sys.exit()
            else:
                pillows_to_run = [get_pillow_config_from_setting(pillow_key, config)
                                  for config in settings.PILLOWTOPS[pillow_key]]

        elif not run_all and not pillow_key and pillow_name:
            pillow = get_pillow_by_name(pillow_name, num_processes=num_processes, process_num=process_number, processor_chunk_size=processor_chunk_size)
            start_pillow(pillow)
            sys.exit()
        elif list_checkpoints:
            for pillow in get_all_pillow_instances():
                print(pillow.checkpoint.checkpoint_id)
            sys.exit()
        else:
            print("\nNo command set, please see --help for runtime instructions")
            sys.exit()

        start_pillows(pillows=[pillow_config.get_instance() for pillow_config in pillows_to_run])
<commit_msg>Update run_ptop.py<commit_after>import sys

from django.conf import settings
from django.core.management.base import BaseCommand

from pillowtop.const import DEFAULT_PROCESSOR_CHUNK_SIZE
from pillowtop.run_pillowtop import start_pillows, start_pillow
from pillowtop.utils import (
    get_all_pillow_instances,
    get_all_pillow_configs,
    get_pillow_config_from_setting,
    get_pillow_by_name
)

class Command(BaseCommand):
    help = "Run pillows pillows listed in settings."

    def add_arguments(self, parser):
        parser.add_argument(
            '--all',
            action='store_true',
            dest='run_all',
            default=False,
            help="Run all pillows from settings - use for local dev",
        )
        parser.add_argument(
            '--list',
            action='store_true',
            dest='list_all',
            default=False,
            help="List pillowtop names",
        )
        parser.add_argument(
            '--list-checkpoints',
            action='store_true',
            dest='list_checkpoints',
            default=False,
            help="Print all pillow doc ids.",
        )
        parser.add_argument(
            '--pillow-key',
            action='store',
            dest='pillow_key',
            default=None,
            help="Run a specific key of a group of pillows from settings.PILLOWTOPS list",
        )
        parser.add_argument(
            '--pillow-name',
            action='store',
            dest='pillow_name',
            default=None,
            help="Run a single specific pillow name from settings.PILLOWTOPS list",
        )
        parser.add_argument(
            '--num-processes',
            action='store',
            dest='num_processes',
            default=1,
            type=int,
            help="The number of processes that are expected to be run for this pillow",
        )
        parser.add_argument(
            '--process-number',
            action='store',
            dest='process_number',
            default=0,
            type=int,
            help="The process number of this pillow process. Should be between 0 and num-processes. "
                 "It's expected that there will only be one process for each number running at once",
        )
        parser.add_argument(
            '--processor-chunk-size',
            action='store',
            dest='processor_chunk_size',
            default=DEFAULT_PROCESSOR_CHUNK_SIZE,
            type=int,
            help="The batch size for this pillow. Some pillows process changes in bulk, "
            "setting this value to 1 will process each change as it comes in.",
        )

    def handle(self, **options):
        run_all = options['run_all']
        list_all = options['list_all']
        list_checkpoints = options['list_checkpoints']
        pillow_name = options['pillow_name']
        pillow_key = options['pillow_key']
        num_processes = options['num_processes']
        process_number = options['process_number']
        processor_chunk_size = options['processor_chunk_size']
        assert 0 <= process_number < num_processes
        assert processor_chunk_size
        if list_all:
            print("\nPillows registered in system:")
            for config in get_all_pillow_configs():
                print('{}: {}'.format(config.section, config.name))

            print("\n\tRun with --pillow-name <name> to run a pillow")
            print("\n\tRun with --pillow-key <key> to run a group of pillows together\n")
            sys.exit()

        if run_all:
            pillows_to_run = get_all_pillow_configs()
        elif not run_all and not pillow_name and pillow_key:
            # get pillows from key
            if pillow_key not in settings.PILLOWTOPS:
                print("\n\tError, key %s is not in settings.PILLOWTOPS, legal keys are: %s" % \
                      (pillow_key, list(settings.PILLOWTOPS)))
                sys.exit()
            else:
                pillows_to_run = [get_pillow_config_from_setting(pillow_key, config)
                                  for config in settings.PILLOWTOPS[pillow_key]]

        elif not run_all and not pillow_key and pillow_name:
            pillow = get_pillow_by_name(pillow_name, num_processes=num_processes, process_num=process_number, processor_chunk_size=processor_chunk_size)
            start_pillow(pillow)
            sys.exit()
        elif list_checkpoints:
            for pillow in get_all_pillow_instances():
                print(pillow.checkpoint.checkpoint_id)
            sys.exit()
        else:
            print("\nNo command set, please see --help for runtime instructions")
            sys.exit()

        start_pillows(pillows=[pillow_config.get_instance() for pillow_config in pillows_to_run])
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# Copyright (c) 2014-2017 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test mempool persistence.

By default, bitcoind will dump mempool on shutdown and
then reload it on startup. This can be overridden with
the -persistmempool=0 command line option.

Test is as follows:

  - start node0, node1 and node2. node1 has -persistmempool=0
  - create 5 transactions on node2 to its own address. Note that these
    are not sent to node0 or node1 addresses because we don't want
    them to be saved in the wallet.
  - check that node0 and node1 have 5 transactions in their mempools
  - shutdown all nodes.
  - startup node0. Verify that it still has 5 transactions
    in its mempool. Shutdown node0. This tests that by default the
    mempool is persistent.
  - startup node1. Verify that its mempool is empty. Shutdown node1.
    This tests that with -persistmempool=0, the mempool is not
    dumped to disk when the node is shut down.
  - Restart node0 with -persistmempool=0. Verify that its mempool is
    empty. Shutdown node0. This tests that with -persistmempool=0,
    the mempool is not loaded from disk on start up.
  - Restart node0 with -persistmempool. Verify that it has 5
    transactions in its mempool. This tests that -persistmempool=0
    does not overwrite a previously valid mempool stored on disk.
  - Remove node0 mempool.dat and verify savemempool RPC recreates it
    and verify that node1 can load it and has 5 transactions in its
    mempool.
  - Verify that savemempool throws when the RPC is called if
    node1 can't write to disk.

"""
from decimal import Decimal
import os

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
    assert_equal,
    assert_raises_rpc_error,
    wait_until
)

class MempoolPersistTest(BitcoinTestFramework):
    def set_test_params(self):
        self.num_nodes = 3
        self.extra_args = [[], ["-persistmempool=0"], []]

    def skip_test_if_missing_module(self):
        self.skip_if_no_wallet()

    def run_test(self):
        chain_height = self.nodes[0].getblockcount()
        assert_equal(chain_height, 200)

        self.log.debug("Mine a single block to get out of IBD")
        self.nodes[0].generate(1)
        self.sync_all()

        self.log.debug("Send 5 transactions from node2 (to its own address)")
        for i in range(5):
            self.nodes[2].sendtoaddress(
                self.nodes[2].getnewaddress(), Decimal("10"))
        node2_balance = self.nodes[2].getbalance()
        self.sync_all()

        self.log.debug(
            "Verify that node0 and node1 have 5 transactions in their mempools")
        assert_equal(len(self.nodes[0].getrawmempool()), 5)
        assert_equal(len(self.nodes[1].getrawmempool()), 5)

        self.log.debug("Stop-start the nodes. Verify that node0 has the "
                       "transactions in its mempool and node1 does not. "
                       "Verify that node2 calculates its balance correctly "
                       "after loading wallet transactions.")
        self.stop_nodes()
        # Give this one a head-start, so we can be "extra-sure" that it didn't
        # load anything later
        # Also don't store the mempool, to keep the datadir clean
        self.start_node(1, extra_args=["-persistmempool=0"])
        self.start_node(0)
        self.start_node(2)
        wait_until(lambda: self.nodes[0].getmempoolinfo()["loaded"], timeout=1)
        wait_until(lambda: self.nodes[2].getmempoolinfo()["loaded"], timeout=1)
        assert_equal(len(self.nodes[0].getrawmempool()), 5)
        assert_equal(len(self.nodes[2].getrawmempool()), 5)
        # The others have loaded their mempool. If node_1 loaded anything, we'd
        # probably notice by now:
        assert_equal(len(self.nodes[1].getrawmempool()), 0)

        # Verify accounting of mempool transactions after restart is correct
        # Flush mempool to wallet
        self.nodes[2].syncwithvalidationinterfacequeue()
        assert_equal(node2_balance, self.nodes[2].getbalance())

        self.log.debug(
            "Stop-start node0 with -persistmempool=0. Verify that it doesn't load its mempool.dat file.")
        self.stop_nodes()
        self.start_node(0, extra_args=["-persistmempool=0"])
        wait_until(lambda: self.nodes[0].getmempoolinfo()["loaded"])
        assert_equal(len(self.nodes[0].getrawmempool()), 0)

        self.log.debug(
            "Stop-start node0. Verify that it has the transactions in its mempool.")
        self.stop_nodes()
        self.start_node(0)
        wait_until(lambda: self.nodes[0].getmempoolinfo()["loaded"])
        assert_equal(len(self.nodes[0].getrawmempool()), 5)

        mempooldat0 = os.path.join(
            self.nodes[0].datadir, 'regtest', 'mempool.dat')
        mempooldat1 = os.path.join(
            self.nodes[1].datadir, 'regtest', 'mempool.dat')
        self.log.debug(
            "Remove the mempool.dat file. Verify that savemempool to disk via RPC re-creates it")
        os.remove(mempooldat0)
        self.nodes[0].savemempool()
        assert os.path.isfile(mempooldat0)

        self.log.debug(
            "Stop nodes, make node1 use mempool.dat from node0. Verify it has 5 transactions")
        os.rename(mempooldat0, mempooldat1)
        self.stop_nodes()
        self.start_node(1, extra_args=[])
        wait_until(lambda: self.nodes[1].getmempoolinfo()["loaded"])
        assert_equal(len(self.nodes[1].getrawmempool()), 5)

        self.log.debug(
            "Prevent bitcoind from writing mempool.dat to disk. Verify that `savemempool` fails")
        # to test the exception we are creating a tmp folder called mempool.dat.new
        # which is an implementation detail that could change and break this
        # test
        mempooldotnew1 = mempooldat1 + '.new'
        os.mkdir(mempooldotnew1)
        assert_raises_rpc_error(-1, "Unable to dump mempool to disk",
                                self.nodes[1].savemempool)
        os.rmdir(mempooldotnew1)

if __name__ == '__main__':
    MempoolPersistTest().main()
<commit_msg>[backport#14931] test: mempool_persist: Verify prioritization is dumped correctly<commit_after>#!/usr/bin/env python3
# Copyright (c) 2014-2017 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test mempool persistence.

By default, bitcoind will dump mempool on shutdown and
then reload it on startup. This can be overridden with
the -persistmempool=0 command line option.

Test is as follows:

  - start node0, node1 and node2. node1 has -persistmempool=0
  - create 5 transactions on node2 to its own address. Note that these
    are not sent to node0 or node1 addresses because we don't want
    them to be saved in the wallet.
  - check that node0 and node1 have 5 transactions in their mempools
  - shutdown all nodes.
  - startup node0. Verify that it still has 5 transactions
    in its mempool. Shutdown node0. This tests that by default the
    mempool is persistent.
  - startup node1. Verify that its mempool is empty. Shutdown node1.
    This tests that with -persistmempool=0, the mempool is not
    dumped to disk when the node is shut down.
  - Restart node0 with -persistmempool=0. Verify that its mempool is
    empty. Shutdown node0. This tests that with -persistmempool=0,
    the mempool is not loaded from disk on start up.
  - Restart node0 with -persistmempool. Verify that it has 5
    transactions in its mempool. This tests that -persistmempool=0
    does not overwrite a previously valid mempool stored on disk.
  - Remove node0 mempool.dat and verify savemempool RPC recreates it
    and verify that node1 can load it and has 5 transactions in its
    mempool.
  - Verify that savemempool throws when the RPC is called if
    node1 can't write to disk.

"""
from decimal import Decimal
import os

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
    assert_equal,
    assert_raises_rpc_error,
    wait_until
)

class MempoolPersistTest(BitcoinTestFramework):
    def set_test_params(self):
        self.num_nodes = 3
        self.extra_args = [[], ["-persistmempool=0"], []]

    def skip_test_if_missing_module(self):
        self.skip_if_no_wallet()

    def run_test(self):
        chain_height = self.nodes[0].getblockcount()
        assert_equal(chain_height, 200)

        self.log.debug("Mine a single block to get out of IBD")
        self.nodes[0].generate(1)
        self.sync_all()

        self.log.debug("Send 5 transactions from node2 (to its own address)")
        for i in range(5):
            last_txid = self.nodes[2].sendtoaddress(
                self.nodes[2].getnewaddress(), Decimal("10"))
        node2_balance = self.nodes[2].getbalance()
        self.sync_all()

        self.log.debug(
            "Verify that node0 and node1 have 5 transactions in their mempools")
        assert_equal(len(self.nodes[0].getrawmempool()), 5)
        assert_equal(len(self.nodes[1].getrawmempool()), 5)

        self.log.debug("Prioritize a transaction on node0")
        fees = self.nodes[0].getmempoolentry(txid=last_txid)['fees']
        assert_equal(fees['base'], fees['modified'])
        self.nodes[0].prioritisetransaction(txid=last_txid, fee_delta=1000)
        fees = self.nodes[0].getmempoolentry(txid=last_txid)['fees']
        assert_equal(fees['base'] + Decimal('0.00001000'), fees['modified'])

        self.log.debug("Stop-start the nodes. Verify that node0 has the "
                       "transactions in its mempool and node1 does not. "
                       "Verify that node2 calculates its balance correctly "
                       "after loading wallet transactions.")
        self.stop_nodes()
        # Give this one a head-start, so we can be "extra-sure" that it didn't
        # load anything later
        # Also don't store the mempool, to keep the datadir clean
        self.start_node(1, extra_args=["-persistmempool=0"])
        self.start_node(0)
        self.start_node(2)
        wait_until(lambda: self.nodes[0].getmempoolinfo()["loaded"], timeout=1)
        wait_until(lambda: self.nodes[2].getmempoolinfo()["loaded"], timeout=1)
        assert_equal(len(self.nodes[0].getrawmempool()), 5)
        assert_equal(len(self.nodes[2].getrawmempool()), 5)
        # The others have loaded their mempool. If node_1 loaded anything, we'd
        # probably notice by now:
        assert_equal(len(self.nodes[1].getrawmempool()), 0)

        self.log.debug('Verify prioritization is loaded correctly')
        fees = self.nodes[0].getmempoolentry(txid=last_txid)['fees']
        assert_equal(fees['base'] + Decimal('0.00001000'), fees['modified'])

        # Verify accounting of mempool transactions after restart is correct
        # Flush mempool to wallet
        self.nodes[2].syncwithvalidationinterfacequeue()
        assert_equal(node2_balance, self.nodes[2].getbalance())

        self.log.debug(
            "Stop-start node0 with -persistmempool=0. Verify that it doesn't load its mempool.dat file.")
        self.stop_nodes()
        self.start_node(0, extra_args=["-persistmempool=0"])
        wait_until(lambda: self.nodes[0].getmempoolinfo()["loaded"])
        assert_equal(len(self.nodes[0].getrawmempool()), 0)

        self.log.debug(
            "Stop-start node0. Verify that it has the transactions in its mempool.")
        self.stop_nodes()
        self.start_node(0)
        wait_until(lambda: self.nodes[0].getmempoolinfo()["loaded"])
        assert_equal(len(self.nodes[0].getrawmempool()), 5)

        mempooldat0 = os.path.join(
            self.nodes[0].datadir, 'regtest', 'mempool.dat')
        mempooldat1 = os.path.join(
            self.nodes[1].datadir, 'regtest', 'mempool.dat')
        self.log.debug(
            "Remove the mempool.dat file. Verify that savemempool to disk via RPC re-creates it")
        os.remove(mempooldat0)
        self.nodes[0].savemempool()
        assert os.path.isfile(mempooldat0)

        self.log.debug(
            "Stop nodes, make node1 use mempool.dat from node0. Verify it has 5 transactions")
        os.rename(mempooldat0, mempooldat1)
        self.stop_nodes()
        self.start_node(1, extra_args=[])
        wait_until(lambda: self.nodes[1].getmempoolinfo()["loaded"])
        assert_equal(len(self.nodes[1].getrawmempool()), 5)

        self.log.debug(
            "Prevent bitcoind from writing mempool.dat to disk. Verify that `savemempool` fails")
        # to test the exception we are creating a tmp folder called mempool.dat.new
        # which is an implementation detail that could change and break this
        # test
        mempooldotnew1 = mempooldat1 + '.new'
        os.mkdir(mempooldotnew1)
        assert_raises_rpc_error(-1, "Unable to dump mempool to disk",
                                self.nodes[1].savemempool)
        os.rmdir(mempooldotnew1)

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