<commit_before>from collections import Counter
from itertools import permutations
from hashlib import md5

wordslist = []
anagram = None
target_md5 = None

with open("./words.txt", "r") as f:
    wordslist = [line.strip("\n") for line in f]

with open("./anagram.txt", "r") as f:
    anagram = f.readline().strip("\n")

with open("./md5.txt", "r") as f:
    target_md5 = [l.strip("\n") for l in f]

anagram_counter = Counter(anagram)
anagram_length = len(anagram)
anagram_letters = set(anagram)

def word_contains(needle, haystack):
    return all(haystack[k] - v >= 0 for k,v in needle.items())

def add_node(node, new_word, new_word_counter):
    (words, length, counter, children) = node
    for c in children:
        add_node(c, new_word, new_word_counter)
    l = len(new_word)
    new_node_length = length + l
    new_node_counter = new_word_counter + counter
    if word_contains(new_node_counter, anagram_counter):
        children.append((words + [new_word], new_node_length, new_node_counter, []))

def nb_leaf_nodes(node, nb_leaves):
    (_, __, ___, children) = node
    if not children:
        return 1
    r = 0
    for c in children:
        r += nb_leaf_nodes(c, nb_leaves)
    return nb_leaves + r

def find_match(node):
    (words, length, _, children) = node
    # we've reached a leaf node that has the right amount of letters
    if not children and length == anagram_length:
        # check for md5 of all permutations with white spaces
        for perm in permutations(words):
            digest = md5(" ".join(perm)).hexdigest()
            if digest in target_md5:
                print "\n\nWINNER!! - %s \n\n" % list(perm)
    for child in children:
        find_match(child)

root = ([], 0, Counter(), [])

print "Looking for %s: %s" % (anagram, anagram_counter)

i = 0
for word in set(wordslist):
    try:
        # If all letters of the word are in the anagram
        word_counter = Counter(word)
        if word_contains(word_counter, anagram_counter):
            add_node(root, word, word_counter)
        i += 1
        if i % 1000 == 0:
            print "done: ", i
            if i % 5000 == 0:
                print "nb-leaves: %d" % nb_leaf_nodes(root, 0)
    except KeyboardInterrupt as e:
        break

print "%s:%s" % (anagram, anagram_length)
print "nb-leaves: %d" % nb_leaf_nodes(root, 0)

find_match(root)
<commit_msg>[ANTS] Add a main 'method'<commit_after>from collections import Counter
from itertools import permutations
from hashlib import md5

wordslist = []
anagram = None
target_md5 = None

with open("./words.txt", "r") as f:
    wordslist = [line.strip("\n") for line in f]

with open("./anagram.txt", "r") as f:
    anagram = f.readline().strip("\n")

with open("./md5.txt", "r") as f:
    target_md5 = [l.strip("\n") for l in f]

anagram_counter = Counter(anagram)
anagram_length = len(anagram)
anagram_letters = set(anagram)

def word_contains(needle, haystack):
    return all(haystack[k] - v >= 0 for k,v in needle.items())

def add_node(node, new_word, new_word_counter):
    (words, length, counter, children) = node
    for c in children:
        add_node(c, new_word, new_word_counter)
    l = len(new_word)
    new_node_length = length + l
    new_node_counter = new_word_counter + counter
    if word_contains(new_node_counter, anagram_counter):
        children.append((words + [new_word], new_node_length, new_node_counter, []))

def nb_leaf_nodes(node, nb_leaves):
    (_, __, ___, children) = node
    if not children:
        return 1
    r = 0
    for c in children:
        r += nb_leaf_nodes(c, nb_leaves)
    return nb_leaves + r

def find_match(node):
    (words, length, _, children) = node
    # we've reached a leaf node that has the right amount of letters
    if not children and length == anagram_length:
        # check for md5 of all permutations with white spaces
        for perm in permutations(words):
            digest = md5(" ".join(perm)).hexdigest()
            if digest in target_md5:
                print "\n\nWINNER!! - %s \n\n" % list(perm)
    for child in children:
        find_match(child)

if __name__ == "__main__":
    root = ([], 0, Counter(), [])

    print "Looking for %s: %s" % (anagram, anagram_counter)

    i = 0
    for word in set(wordslist):
        try:
            # If all letters of the word are in the anagram
            word_counter = Counter(word)
            if word_contains(word_counter, anagram_counter):
                add_node(root, word, word_counter)
            i += 1
            if i % 1000 == 0:
                print "done: ", i
                if i % 5000 == 0:
                    print "nb-leaves: %d" % nb_leaf_nodes(root, 0)
        except KeyboardInterrupt as e:
            break

    print "%s:%s" % (anagram, anagram_length)
    print "nb-leaves: %d" % nb_leaf_nodes(root, 0)

    find_match(root)
<|endoftext|>


<commit_before># Copyright 2013-2021 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *

class RCheckmate(RPackage):
    """Tests and assertions to perform frequent argument checks.
    A substantial part of the package was written in C to
    minimize any worries about execution time overhead."""

    homepage = "https://cloud.r-project.org/package=checkmate"
    url      = "https://cloud.r-project.org/src/contrib/checkmate_1.8.4.tar.gz"
    list_url = "https://cloud.r-project.org/src/contrib/Archive/checkmate"

    version('1.9.4', sha256='faa25754b757fe483b876f5d07b73f76f69a1baa971420892fadec4af4bbad21')
    version('1.8.4', sha256='6f948883e5a885a1c409d997f0c782e754a549227ec3c8eb18318deceb38f8f6')

    depends_on('r@3.0.0:', type=('build', 'run'))
    depends_on('r-backports@1.1.0:', type=('build', 'run'))
<commit_msg>add version 2.0.0 to r-checkmate (#20852)<commit_after># Copyright 2013-2021 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *

class RCheckmate(RPackage):
    """Fast and Versatile Argument Checks

    Tests and assertions to perform frequent argument checks.
    A substantial part of the package was written in C to
    minimize any worries about execution time overhead."""

    homepage = "https://cloud.r-project.org/package=checkmate"
    url      = "https://cloud.r-project.org/src/contrib/checkmate_1.8.4.tar.gz"
    list_url = "https://cloud.r-project.org/src/contrib/Archive/checkmate"

    version('2.0.0', sha256='0dc25b0e20c04836359df1885d099c6e4ad8ae0e585a9e4107f7ea945d9c6fa4')
    version('1.9.4', sha256='faa25754b757fe483b876f5d07b73f76f69a1baa971420892fadec4af4bbad21')
    version('1.8.4', sha256='6f948883e5a885a1c409d997f0c782e754a549227ec3c8eb18318deceb38f8f6')

    depends_on('r@3.0.0:', type=('build', 'run'))
    depends_on('r-backports@1.1.0:', type=('build', 'run'))
<|endoftext|>


<commit_before>import pytest

from libtaxii import messages_10 as tm10
from libtaxii import messages_11 as tm11
from libtaxii.constants import (
    ST_SUCCESS, CB_STIX_XML_111)

from opentaxii.taxii import exceptions

from utils import prepare_headers, as_tm
from fixtures import (
    CUSTOM_CONTENT_BINDING, CONTENT, MESSAGE_ID,
    COLLECTIONS_A, COLLECTIONS_B,
    CONTENT_BINDING_SUBTYPE, INVALID_CONTENT_BINDING,
    COLLECTION_OPEN, COLLECTION_ONLY_STIX)

def make_content(version, content_binding=CUSTOM_CONTENT_BINDING,
                 content=CONTENT, subtype=None):
    if version == 10:
        return tm10.ContentBlock(content_binding, content)

    elif version == 11:
        content_block = tm11.ContentBlock(
            tm11.ContentBinding(content_binding), content)
        if subtype:
            content_block.content_binding.subtype_ids.append(subtype)

        return content_block
    else:
        raise ValueError('Unknown TAXII message version: %s' % version)

def make_inbox_message(version, blocks=None, dest_collection=None):

    if version == 10:
        inbox_message = tm10.InboxMessage(
            message_id=MESSAGE_ID, content_blocks=blocks)

    elif version == 11:
        inbox_message = tm11.InboxMessage(
            message_id=MESSAGE_ID, content_blocks=blocks)
        if dest_collection:
            inbox_message.destination_collection_names.append(dest_collection)
    else:
        raise ValueError('Unknown TAXII message version: %s' % version)

    return inbox_message

@pytest.fixture(autouse=True)
def prepare_server(server, services):
    from opentaxii.persistence.sqldb.models import DataCollection

    coll_mapping = {
        'inbox-A': COLLECTIONS_A,
        'inbox-B': COLLECTIONS_B}

    names = set()
    for service, collections in coll_mapping.items():
        for coll in collections:
            if coll.name not in names:
                coll = server.persistence.create_collection(coll)
                names.add(coll.name)
            else:
                coll = DataCollection.query.filter_by(name=coll.name).one()

            server.persistence.set_collection_services(
                coll.id, service_ids=[service])

@pytest.mark.parametrize("https", [True, False])
@pytest.mark.parametrize("version", [11, 10])
def test_inbox_request_all_content(server, version, https):

    inbox_a = server.get_service('inbox-A')

    headers = prepare_headers(version, https)

    blocks = [
        make_content(
            version,
            content_binding=CUSTOM_CONTENT_BINDING,
            subtype=CONTENT_BINDING_SUBTYPE),
        make_content(
            version,
            content_binding=INVALID_CONTENT_BINDING)
    ]
    inbox_message = make_inbox_message(version, blocks=blocks)

    # "inbox-A" accepts all content
    response = inbox_a.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)

    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    blocks = server.persistence.get_content_blocks(None)
    assert len(blocks) == len(blocks)

@pytest.mark.parametrize("https", [True, False])
def test_inbox_request_destination_collection(server, https):
    version = 11

    inbox_message = make_inbox_message(
        version, blocks=[make_content(version)], dest_collection=None)
    headers = prepare_headers(version, https)

    inbox = server.get_service('inbox-A')
    # destination collection is not required for inbox-A
    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS

    inbox = server.get_service('inbox-B')
    # destination collection is required for inbox-B
    with pytest.raises(exceptions.StatusMessageException):
        response = inbox.process(headers, inbox_message)

@pytest.mark.parametrize("https", [True, False])
@pytest.mark.parametrize("version", [11, 10])
def test_inbox_request_inbox_valid_content_binding(server, version, https):

    inbox = server.get_service('inbox-B')

    blocks = [
        make_content(
            version,
            content_binding=CUSTOM_CONTENT_BINDING,
            subtype=CONTENT_BINDING_SUBTYPE),
        make_content(
            version,
            content_binding=CB_STIX_XML_111)
    ]

    inbox_message = make_inbox_message(
        version, dest_collection=COLLECTION_OPEN, blocks=blocks)
    headers = prepare_headers(version, https)

    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    # all blocks
    blocks = server.persistence.get_content_blocks(collection_id=None)
    assert len(blocks) == len(blocks)

@pytest.mark.parametrize("https", [True, False])
@pytest.mark.parametrize("version", [11, 10])
def test_inbox_req_inbox_invalid_inbox_content_binding(server, version, https):

    inbox = server.get_service('inbox-B')

    content = make_content(version, content_binding=INVALID_CONTENT_BINDING)
    inbox_message = make_inbox_message(
        version, dest_collection=COLLECTION_OPEN, blocks=[content])

    headers = prepare_headers(version, https)

    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    blocks = server.persistence.get_content_blocks(None)

    # Content blocks with invalid content should be ignored
    assert len(blocks) == 0

@pytest.mark.parametrize("https", [True, False])
@pytest.mark.parametrize("version", [11, 10])
def test_inbox_req_coll_content_bindings_filtering(server, version, https):

    inbox = server.get_service('inbox-B')
    headers = prepare_headers(version, https)

    blocks = [
        make_content(version, content_binding=CB_STIX_XML_111),
        make_content(version, content_binding=INVALID_CONTENT_BINDING),
    ]

    inbox_message = make_inbox_message(
        version, dest_collection=COLLECTION_ONLY_STIX, blocks=blocks)

    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    blocks = server.persistence.get_content_blocks(None)

    # Content blocks with invalid content should be ignored
    assert len(blocks) == 1
<commit_msg>fix flaky tests (#126)<commit_after>import pytest

from libtaxii import messages_10 as tm10
from libtaxii import messages_11 as tm11
from libtaxii.constants import (
    ST_SUCCESS, CB_STIX_XML_111)

from opentaxii.taxii import exceptions

from utils import prepare_headers, as_tm
from fixtures import (
    CUSTOM_CONTENT_BINDING, CONTENT, MESSAGE_ID,
    COLLECTIONS_A, COLLECTIONS_B,
    CONTENT_BINDING_SUBTYPE, INVALID_CONTENT_BINDING,
    COLLECTION_OPEN, COLLECTION_ONLY_STIX)

def make_content(version, content_binding=CUSTOM_CONTENT_BINDING,
                 content=CONTENT, subtype=None):
    if version == 10:
        return tm10.ContentBlock(content_binding, content)

    elif version == 11:
        content_block = tm11.ContentBlock(
            tm11.ContentBinding(content_binding), content)
        if subtype:
            content_block.content_binding.subtype_ids.append(subtype)

        return content_block
    else:
        raise ValueError('Unknown TAXII message version: %s' % version)

def make_inbox_message(version, blocks=None, dest_collection=None):

    if version == 10:
        inbox_message = tm10.InboxMessage(
            message_id=MESSAGE_ID, content_blocks=blocks)

    elif version == 11:
        inbox_message = tm11.InboxMessage(
            message_id=MESSAGE_ID, content_blocks=blocks)
        if dest_collection:
            inbox_message.destination_collection_names.append(dest_collection)
    else:
        raise ValueError('Unknown TAXII message version: %s' % version)

    return inbox_message

@pytest.fixture(autouse=True)
def prepare_server(server, services):
    from opentaxii.persistence.sqldb.models import DataCollection

    coll_mapping = {
        'inbox-A': COLLECTIONS_A,
        'inbox-B': COLLECTIONS_B}

    names = set()
    for service, collections in coll_mapping.items():
        for coll in collections:
            if coll.name not in names:
                coll = server.persistence.create_collection(coll)
                names.add(coll.name)
                service_ids = [service]
            else:
                coll = DataCollection.query.filter_by(name=coll.name).one()
                service_ids = {s.id for s in coll.services} | {service}

            server.persistence.set_collection_services(
                coll.id, service_ids=service_ids)

@pytest.mark.parametrize("https", [True, False])
@pytest.mark.parametrize("version", [11, 10])
def test_inbox_request_all_content(server, version, https):

    inbox_a = server.get_service('inbox-A')

    headers = prepare_headers(version, https)

    blocks = [
        make_content(
            version,
            content_binding=CUSTOM_CONTENT_BINDING,
            subtype=CONTENT_BINDING_SUBTYPE),
        make_content(
            version,
            content_binding=INVALID_CONTENT_BINDING)
    ]
    inbox_message = make_inbox_message(version, blocks=blocks)

    # "inbox-A" accepts all content
    response = inbox_a.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)

    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    blocks = server.persistence.get_content_blocks(None)
    assert len(blocks) == len(blocks)

@pytest.mark.parametrize("https", [True, False])
def test_inbox_request_destination_collection(server, https):
    version = 11

    inbox_message = make_inbox_message(
        version, blocks=[make_content(version)], dest_collection=None)
    headers = prepare_headers(version, https)

    inbox = server.get_service('inbox-A')
    # destination collection is not required for inbox-A
    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS

    inbox = server.get_service('inbox-B')
    # destination collection is required for inbox-B
    with pytest.raises(exceptions.StatusMessageException):
        response = inbox.process(headers, inbox_message)

@pytest.mark.parametrize("https", [True, False])
@pytest.mark.parametrize("version", [11, 10])
def test_inbox_request_inbox_valid_content_binding(server, version, https):

    inbox = server.get_service('inbox-B')

    blocks = [
        make_content(
            version,
            content_binding=CUSTOM_CONTENT_BINDING,
            subtype=CONTENT_BINDING_SUBTYPE),
        make_content(
            version,
            content_binding=CB_STIX_XML_111)
    ]

    inbox_message = make_inbox_message(
        version, dest_collection=COLLECTION_OPEN, blocks=blocks)
    headers = prepare_headers(version, https)

    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    # all blocks
    blocks = server.persistence.get_content_blocks(collection_id=None)
    assert len(blocks) == len(blocks)

@pytest.mark.parametrize("https", [True, False])
@pytest.mark.parametrize("version", [11, 10])
def test_inbox_req_inbox_invalid_inbox_content_binding(server, version, https):

    inbox = server.get_service('inbox-B')

    content = make_content(version, content_binding=INVALID_CONTENT_BINDING)
    inbox_message = make_inbox_message(
        version, dest_collection=COLLECTION_OPEN, blocks=[content])

    headers = prepare_headers(version, https)

    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    blocks = server.persistence.get_content_blocks(None)

    # Content blocks with invalid content should be ignored
    assert len(blocks) == 0

@pytest.mark.parametrize("https", [True, False])
@pytest.mark.parametrize("version", [11, 10])
def test_inbox_req_coll_content_bindings_filtering(server, version, https):

    inbox = server.get_service('inbox-B')
    headers = prepare_headers(version, https)

    blocks = [
        make_content(version, content_binding=CB_STIX_XML_111),
        make_content(version, content_binding=INVALID_CONTENT_BINDING),
    ]

    inbox_message = make_inbox_message(
        version, dest_collection=COLLECTION_ONLY_STIX, blocks=blocks)

    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    blocks = server.persistence.get_content_blocks(None)

    # Content blocks with invalid content should be ignored
    assert len(blocks) == 1
<|endoftext|>


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

"""BlinkyTape Python communication library.

  This code assumes stock serialLoop() in the firmware.

  Commands are issued in 3-byte blocks, with pixel data
  encoded in RGB triplets in range 0-254, sent sequentially
  and a triplet ending with a 255 causes the accumulated pixel
  data to display (a show command).

  Note that with the stock firmware changing the maximum brightness
  over serial communication is impossible.
"""

import serial
import os.path
import subprocess
import re

# For Python3 support- always run strings through a bytes converter
import sys
if sys.version_info < (3,):
    def encode(x):
        return x
else:
    import codecs
    def encode(x):
        return codecs.latin_1_encode(x)[0]

class BlinkyTape(object):
    linux_port     = '/dev/ttyACM0'
    min_colour_val = 0
    max_colour_val = 254

    def __init__(self, port=None, ledCount=60, buffered=True):
        """Creates a BlinkyTape object and opens the port.

        Parameters:
          port
            Required, port name as accepted by PySerial library:
            http://pyserial.sourceforge.net/pyserial_api.html#serial.Serial
            It is the same port name that is used in Arduino IDE.
            Ex.: COM5 (Windows), /dev/ttyACM0 (Linux).
          ledCount
            Optional, total number of LEDs to work with,
            defaults to 60 LEDs. The limit is enforced and an
            attempt to send more pixel data will throw an exception.
          buffered
            Optional, enabled by default. If enabled, will buffer
            pixel data until a show command is issued. If disabled,
            the data will be sent in byte triplets as expected by firmware,
            with immediate flush of the serial buffers (slower).

        """
        self.port = self.__identify_port(port)
        self.ledCount = ledCount
        self.position = 0
        self.buffered = buffered
        self.buf = ""
        self.serial = serial.Serial(self.port, 115200)
        self.show()  # Flush any incomplete data

    def __identify_port(self, port=None):
        if port is not None:
            return port
        elif os.name == 'posix': # covers all *nix machines, including Linux?
            if not os.path.exists( '/dev/serial') or not os.path.exists('/dev/serial/by-id'):
                raise RuntimeError("Assuming this is a *nix machine, cannot find the device path for BlinkyTape via 'ls -l /dev/serial/by-id'. Is it connected?")
            ls_l_output = subprocess.check_output(['ls', '-l', '/dev/serial/by-id'])
            m = re.search( 'BlinkyTape\S+\s+->\s+\S+(/tty\S+)', ls_l_output)
            if m == None:
                raise RuntimeError("Assuming this is a *nix machine, cannot find the device path for BlinkyTape via 'ls -l /dev/serial/by-id'. Is it connected?")
            device_path = '/dev' + m.group(1)
            return device_path
        elif os.name == 'nt': # covers all windows machines?
            cmd = "wmic path Win32_SerialPort Where 'Caption like \"%" + "BlinkyTape%\"' Get DeviceID" # Clumsy, but can't work out how else to avoid %B going weird
            com_name_output = subprocess.check_output(cmd)
            m = re.search( '(COM\d+)', com_name_output)
            if m == None:
                raise RuntimeError("Assuming this is a Windows machine, cannot find a matching COM port for BlinkyTape. Is it connected?")
            com_name = m.group(1)
            return com_name
        else:
            raise RuntimeError("Cannot establish what kind OS is running, so don't know where to look for the BlinkyTape connection.")

    def send_list(self, colors):
        if len(colors) > self.ledCount:
            raise RuntimeError("Attempting to set pixel outside range!")
        for r, g, b in colors:
            self.sendPixel(r, g, b)
        self.show()

    def colour_chr(self, n):
        if n < self.min_colour_val:
            n = self.min_colour_val
        elif n > self.max_colour_val:
            n = self.max_colour_val
        return chr(n)

    def send_list(self, colors):
        data = ""
        for r, g, b in colors:
            data += self.colour_chr(r) + self.colour_chr(g) + self.colour_chr(b)
        self.serial.write(encode(data))
        self.show()

    def sendPixel(self, r, g, b):
        """Sends the next pixel data triplet in RGB format.

        Values are clamped to 0-254 automatically.

        Throws a RuntimeException if [ledCount] pixels are already set.
        """
        data = self.colour_chr(r) + self.colour_chr(g) + self.colour_chr(b)
        if self.position < self.ledCount:
            if self.buffered:
                self.buf += data
            else:
                self.serial.write(encode(data))
                self.serial.flush()
            self.position += 1
        else:
            raise RuntimeError("Attempting to set pixel outside range!")

    def show(self):
        """Sends the command(s) to display all accumulated pixel data.

        Resets the next pixel position to 0, flushes the serial buffer,
        and discards any accumulated responses from BlinkyTape.
        """
        control = chr(255)
        if self.buffered:
            # Fix an OS X specific bug where sending more than 383 bytes of data at once
            # hangs the BlinkyTape controller. Why this is???
            # TODO: Test me on other platforms
            CHUNK_SIZE = 300

            self.buf += control
            for i in range(0, len(self.buf), CHUNK_SIZE):
                self.serial.write(encode(self.buf[i:i+CHUNK_SIZE]))
                self.serial.flush()

            self.buf = ""
        else:
            self.serial.write(encode(control))
        self.serial.flush()
        self.serial.flushInput()  # Clear responses from BlinkyTape, if any
        self.position = 0

    def displayColor(self, r, g, b):
        """Fills [ledCount] pixels with RGB color and shows it."""
        for i in range(0, self.ledCount):
            self.sendPixel(r, g, b)
        self.show()

    def resetToBootloader(self):
        """Initiates a reset on BlinkyTape.

        Note that it will be disconnected.
        """
        self.serial.setBaudrate(1200)
        self.close()

    def close(self):
        """Safely closes the serial port."""
        self.serial.close()

    def clear_all(self):
        for x in range(self.ledCount):
            self.sendPixel(0, 0, 0)
        self.show()

# Example code

if __name__ == "__main__":

    import glob
    import optparse

    parser = optparse.OptionParser()
    parser.add_option("-p", "--port", dest="portname",
                      help="serial port (ex: /dev/ttyUSB0)", default=None)
    parser.add_option("-c", "--ledcount", dest="ledcount",
                      help="number of LEDs attached", type="int", default=60)
    parser.add_option("-b", action="store_true", default="True", dest="buffered")
    parser.add_option("-u", action="store_false", dest="buffered")

    (options, args) = parser.parse_args()

    if options.portname is not None:
        port = options.portname
    else:
        serialPorts = glob.glob("/dev/cu.usbmodem*")
        port = serialPorts[0]

    bt = BlinkyTape(port, options.ledcount, options.buffered)

    while True:
        bt.displayColor(255, 0, 0)
        bt.displayColor(0, 255, 0)
        bt.displayColor(0, 0, 255)
        bt.displayColor(255, 255, 255)
        bt.displayColor(0, 0, 0)
<commit_msg>MODIFIED: refactored __main__ part of BlinkyTape.py<commit_after># -*- coding: utf-8 -*-

"""BlinkyTape Python communication library.

  This code assumes stock serialLoop() in the firmware.

  Commands are issued in 3-byte blocks, with pixel data
  encoded in RGB triplets in range 0-254, sent sequentially
  and a triplet ending with a 255 causes the accumulated pixel
  data to display (a show command).

  Note that with the stock firmware changing the maximum brightness
  over serial communication is impossible.
"""

import serial
import os.path
import subprocess
import re
from time import sleep

# For Python3 support- always run strings through a bytes converter
import sys
if sys.version_info < (3,):
    def encode(x):
        return x
else:
    import codecs
    def encode(x):
        return codecs.latin_1_encode(x)[0]

class BlinkyTape(object):
    min_colour_val = 0
    max_colour_val = 254

    def __init__(self, port=None, ledCount=60, buffered=True):
        """Creates a BlinkyTape object and opens the port.

        Parameters:
          port
            Required, port name as accepted by PySerial library:
            http://pyserial.sourceforge.net/pyserial_api.html#serial.Serial
            It is the same port name that is used in Arduino IDE.
            Ex.: COM5 (Windows), /dev/ttyACM0 (Linux).
          ledCount
            Optional, total number of LEDs to work with,
            defaults to 60 LEDs. The limit is enforced and an
            attempt to send more pixel data will throw an exception.
          buffered
            Optional, enabled by default. If enabled, will buffer
            pixel data until a show command is issued. If disabled,
            the data will be sent in byte triplets as expected by firmware,
            with immediate flush of the serial buffers (slower).

        """
        self.port = self.__identify_port(port)
        print "port=%s" % self.port
        self.ledCount = ledCount
        self.position = 0
        self.buffered = buffered
        self.buf = ""
        self.serial = serial.Serial(self.port, 115200)
        self.show()  # Flush any incomplete data

    def __identify_port(self, port=None):
        if port is not None:
            return port
        elif os.name == 'posix': # covers all *nix machines, including Linux?
            if not os.path.exists( '/dev/serial') or not os.path.exists('/dev/serial/by-id'):
                raise RuntimeError("Assuming this is a *nix machine, cannot find the device path for BlinkyTape via 'ls -l /dev/serial/by-id'. Is it connected?")
            ls_l_output = subprocess.check_output(['ls', '-l', '/dev/serial/by-id'])
            m = re.search( 'BlinkyTape\S+\s+->\s+\S+(/tty\S+)', ls_l_output)
            if m == None:
                raise RuntimeError("Assuming this is a *nix machine, cannot find the device path for BlinkyTape via 'ls -l /dev/serial/by-id'. Is it connected?")
            device_path = '/dev' + m.group(1)
            return device_path
        elif os.name == 'nt': # covers all windows machines?
            cmd = "wmic path Win32_SerialPort Where 'Caption like \"%" + "BlinkyTape%\"' Get DeviceID" # Clumsy, but can't work out how else to avoid %B going weird
            com_name_output = subprocess.check_output(cmd)
            m = re.search( '(COM\d+)', com_name_output)
            if m == None:
                raise RuntimeError("Assuming this is a Windows machine, cannot find a matching COM port for BlinkyTape. Is it connected?")
            com_name = m.group(1)
            return com_name
        else:
            raise RuntimeError("Cannot establish what kind OS is running, so don't know where to look for the BlinkyTape connection.")

    def send_list(self, colors):
        if len(colors) > self.ledCount:
            raise RuntimeError("Attempting to set pixel outside range!")
        for r, g, b in colors:
            self.sendPixel(r, g, b)
        self.show()

    def colour_chr(self, n):
        if n < self.min_colour_val:
            n = self.min_colour_val
        elif n > self.max_colour_val:
            n = self.max_colour_val
        return chr(n)

    def send_list(self, colors):
        data = ""
        for r, g, b in colors:
            data += self.colour_chr(r) + self.colour_chr(g) + self.colour_chr(b)
        self.serial.write(encode(data))
        self.show()

    def sendPixel(self, r, g, b):
        """Sends the next pixel data triplet in RGB format.

        Values are clamped to 0-254 automatically.

        Throws a RuntimeException if [ledCount] pixels are already set.
        """
        data = self.colour_chr(r) + self.colour_chr(g) + self.colour_chr(b)
        if self.position < self.ledCount:
            if self.buffered:
                self.buf += data
            else:
                self.serial.write(encode(data))
                self.serial.flush()
            self.position += 1
        else:
            raise RuntimeError("Attempting to set pixel outside range!")

    def show(self):
        """Sends the command(s) to display all accumulated pixel data.

        Resets the next pixel position to 0, flushes the serial buffer,
        and discards any accumulated responses from BlinkyTape.
        """
        control = chr(255)
        if self.buffered:
            # Fix an OS X specific bug where sending more than 383 bytes of data at once
            # hangs the BlinkyTape controller. Why this is???
            # TODO: Test me on other platforms
            CHUNK_SIZE = 300

            self.buf += control
            for i in range(0, len(self.buf), CHUNK_SIZE):
                self.serial.write(encode(self.buf[i:i+CHUNK_SIZE]))
                self.serial.flush()

            self.buf = ""
        else:
            self.serial.write(encode(control))
        self.serial.flush()
        self.serial.flushInput()  # Clear responses from BlinkyTape, if any
        self.position = 0

    def displayColor(self, r, g, b):
        """Fills [ledCount] pixels with RGB color and shows it."""
        for i in range(0, self.ledCount):
            self.sendPixel(r, g, b)
        self.show()

    def resetToBootloader(self):
        """Initiates a reset on BlinkyTape.

        Note that it will be disconnected.
        """
        self.serial.setBaudrate(1200)
        self.close()

    def close(self):
        """Safely closes the serial port."""
        self.serial.close()

    def clear_all(self):
        for x in range(self.ledCount):
            self.sendPixel(0, 0, 0)
        self.show()

# Example code

if __name__ == "__main__":

    import glob
    import optparse

    parser = optparse.OptionParser()
    parser.add_option("-p", "--port", dest="portname",
                      help="serial port (ex: /dev/ttyUSB0)", default=None)
    parser.add_option("-c", "--ledcount", dest="ledcount",
                      help="number of LEDs attached", type="int", default=60)
    parser.add_option("-b", action="store_true", default="True", dest="buffered")
    parser.add_option("-u", action="store_false", dest="buffered")

    (options, args) = parser.parse_args()

    bt = BlinkyTape(options.portname, options.ledcount, options.buffered)

    while True:
        bt.displayColor(255, 0, 0)
        sleep(0.5)
        bt.displayColor(0, 255, 0)
        sleep(0.5)
        bt.displayColor(0, 0, 255)
        sleep(0.5)
        bt.displayColor(255, 255, 255)
        sleep(0.5)
        bt.displayColor(0, 0, 0)
        sleep(0.5)
<|endoftext|>


<commit_before>#! /usr/local/bin/python3
# findLargeFiles.py - given a folder name, walk through its entire hierarchy
#                   - print folders and files within each folder
# python add.trace.statements.to.src.py /Users/iosbuild1/forge/work/code/evernym/sdk/vcx/libvcx/src
# python add.trace.statements.to.src.py /Users/iosbuild1/forge/work/code/evernym/sdk/.macosbuild/vcx-indy-sdk/libnullpay/src
# python add.trace.statements.to.src.py /Users/iosbuild1/forge/work/code/evernym/sdk/.macosbuild/vcx-indy-sdk/libindy/src

import os
import sys

# print(sys.argv)

def recursive_walk(folder, rustLogFunction):
    traceNumber = 0
    for folderName, subfolders, filenames in os.walk(folder):
        if subfolders:
            for subfolder in subfolders:
                recursive_walk(subfolder)
        print('\nFolder: ' + folderName)
        for filename in filenames:
            if (filename.endswith(".newrs")):
                print("Ignoring file: " + folderName + '/' + filename)
                continue

            print("Found file: " + folderName + '/' + filename)
            f = open(folderName + '/' + filename, "r")
            print("Copying to file: " + folderName + '/' + filename + ".newrs")
            copy = open(folderName + '/' + filename + ".newrs", "w")
            fileLineNumber = 0
            previousLine = ""
            previousTrimmedLine = ""
            insideExternCurly = 0
            atTopOfFile = 1
            waitForSemiColon = 0
            eatingCurlys = 0
            eatingLines = 0
            openCurlys = -1
            ignoreEnding = 0
            foundFirstOpeningCurl = 0
            insideUseStatement = 0
            for line in f:
                fileLineNumber += 1
                trimmedLine = line.strip()

                if (foundFirstOpeningCurl == 0 and trimmedLine.count('{') > 0):
                    foundFirstOpeningCurl = 1

                if (line.startswith("use") and not trimmedLine.endswith(';')):
                    insideUseStatement = 1

                if (trimmedLine == "extern {"):
                    insideExternCurly = 1
                if (trimmedLine.endswith(",")):
                    waitForSemiColon = 1
                if (
                    not line.startswith("use") and
                    not line.startswith("extern") and
                    not line.startswith("type ") and
                    waitForSemiColon == 0 and
                    len(trimmedLine) > 0
                ):
                    atTopOfFile = 0

                if (trimmedLine.count("\"") == 1 and trimmedLine.endswith("\"")):
                    eatingLines = 1

                if (line.startswith("pub type ") or line.startswith("pub const ") or line.startswith("const ")):
                    ignoreEnding = 1

                if (trimmedLine.startswith("macro_rules!") or trimmedLine.startswith("impl Node")):
                    eatingCurlys = 1

                if (eatingCurlys == 1):
                    if (openCurlys == -1):
                        openCurlys = trimmedLine.count('{')
                    else:
                        openCurlys += trimmedLine.count('{')
                    openCurlys -= trimmedLine.count('}')

                if (
                    trimmedLine.endswith(";") and
                    waitForSemiColon == 0 and
                    atTopOfFile == 0 and
                    eatingCurlys == 0 and
                    eatingLines == 0 and
                    not filename == "sodium_type.rs" and
                    not trimmedLine == "};" and
                    not trimmedLine == "})?;" and
                    not trimmedLine.startswith(".") and
                    not trimmedLine.startswith("{") and
                    not trimmedLine.startswith(").") and
                    not trimmedLine.startswith("}).") and
                    not trimmedLine.startswith(");") and
                    not trimmedLine.startswith("});") and
                    not trimmedLine.startswith("}));") and
                    not trimmedLine.startswith("static ref") and
                    not trimmedLine.startswith("pub static ref") and
                    not trimmedLine.startswith("pub type ") and
                    not trimmedLine.startswith("type ") and
                    not trimmedLine.startswith("fn ") and
                    not trimmedLine.startswith("extern") and
                    not trimmedLine.startswith("use") and
                    not trimmedLine.startswith("return") and
                    not trimmedLine.startswith("pub const") and
                    not line.startswith("pub mod") and
                    not line.startswith("sodium_type!") and
                    not line.startswith("const ") and
                    not line.startswith("pub static") and
                    not line.startswith("pub struct") and
                    not line.startswith("fn matches") and
                    not line.startswith("//") and
                    not line.startswith("}") and
                    not line.startswith(")") and
                    not line.startswith("/*") and
                    not line.startswith("static") and
                    not line.startswith("mod ") and
                    not line.startswith("#[macro_use") and
                    not previousTrimmedLine.endswith(",") and
                    not previousTrimmedLine.endswith(".") and
                    not previousTrimmedLine.endswith("=") and
                    not previousTrimmedLine.endswith("?") and
                    not previousTrimmedLine.endswith(")") and
                    not previousTrimmedLine.endswith("(") and
                    not previousTrimmedLine.endswith("|") and
                    not previousTrimmedLine.endswith("\\") and
                    not previousTrimmedLine.endswith("}") and
                    not previousTrimmedLine.startswith("retun") and
                    not previousTrimmedLine.startswith("#[cfg") and
                    not previousLine.startswith("#[cfg") and
                    not previousLine.startswith("pub trait") and
                    not previousLine.startswith("impl")
                ):
                    traceNumber += 1
                    copy.write(rustLogFunction + "!(\"TRACE[" + str(traceNumber) + "]: ABOVE LINE[" + str(fileLineNumber) + "]: " + trimmedLine.replace("\\","\\\\").replace("\"","\\\"").replace("{","{{").replace("}","}}") + " -- FILE: " + folderName + "/" + filename + "\");\n")
                    #copy.write("match std::env::var(\"MOBILE_TRACE\") {Ok(val) => {" + rustLogFunction + "!(\"TRACE[" + str(traceNumber) + "]: ABOVE LINE[" + str(fileLineNumber) + "]: " + trimmedLine.replace("\\","\\\\").replace("\"","\\\"").replace("{","{{").replace("}","}}") + " -- FILE: " + folderName + "/" + filename + "\")}, Err(e) => {},}\n")

                copy.write(line)

                if (
                    trimmedLine.endswith(";") and
                    atTopOfFile == 0 and
                    eatingCurlys == 0 and
                    eatingLines == 0 and
                    ignoreEnding == 0 and
                    not filename == "sodium_type.rs" and
                    not trimmedLine == "};" and
                    not trimmedLine == "})?;" and
                    not trimmedLine.startswith("static ref") and
                    not trimmedLine.startswith("pub static ref") and
                    not trimmedLine.startswith("pub type ") and
                    not trimmedLine.startswith("type ") and
                    not trimmedLine.startswith("r#\"{\"") and
                    not trimmedLine.startswith("fn ") and
                    not trimmedLine.startswith("return") and
                    not trimmedLine.startswith("break") and
                    not trimmedLine.startswith("continue") and
                    not trimmedLine.startswith("extern") and
                    not trimmedLine.startswith("use") and
                    not trimmedLine.startswith("pub const") and
                    not line.startswith("pub mod") and
                    not line.startswith("sodium_type!") and
                    not line.startswith("const ") and
                    not line.startswith("pub static") and
                    not line.startswith("pub struct") and
                    not line.startswith("fn matches") and
                    not line.startswith("//") and
                    not line.startswith("/*") and
                    not line.startswith("static") and
                    not line.startswith("mod ") and
                    not line.startswith("});") and
                    not line.startswith("#[macro_use") and
                    insideExternCurly == 0 and
                    not previousTrimmedLine.startswith("return") and
                    not previousTrimmedLine.startswith("break") and
                    not previousTrimmedLine.startswith("continue") and
                    not previousLine.startswith("use logic") and
                    foundFirstOpeningCurl == 1 and
                    insideUseStatement == 0 and
                    not previousLine.startswith("pub trait") and
                    not previousLine.startswith("impl")
                ):
                    traceNumber += 1
                    copy.write(rustLogFunction + "!(\"TRACE[" + str(traceNumber) + "]: BELOW LINE[" + str(fileLineNumber) + "]: " + trimmedLine.replace("\\","\\\\").replace("\"","\\\"").replace("{","{{").replace("}","}}") + " -- FILE: " + folderName + "/" + filename + "\");\n")
                    #copy.write("match std::env::var(\"MOBILE_TRACE\") {Ok(val) => {" + rustLogFunciton + "!(\"TRACE[" + str(traceNumber) + "]: BELOW LINE[" + str(fileLineNumber) + "]: " + trimmedLine.replace("\\","\\\\").replace("\"","\\\"").replace("{","{{").replace("}","}}") + " -- FILE: " + folderName + "/" + filename + "\")}, Err(e) => {},}\n")

                if ( insideExternCurly == 1 and trimmedLine == "}" ):
                    insideExternCurly = 0
                if (trimmedLine.endswith(";")):
                    waitForSemiColon = 0
                    ignoreEnding = 0
                if (openCurlys == 0):
                    eatingCurlys = 0
                    openCurlys = -1
                if (trimmedLine.startswith("\"") and eatingLines == 1):
                    eatingLines = 0
                if (insideUseStatement == 1 and trimmedLine.endswith(';')):
                    insideUseStatement = 0

                previousTrimmedLine = trimmedLine
                previousLine = line
            f.close()
            copy.close()
            os.rename(folderName + '/' + filename + ".newrs", folderName + '/' + filename)

recursive_walk(sys.argv[1], sys.argv[2])

# find vcx/libvcx/src -name "*.rs"|wc -l

# f = open("...", "r")
# copy = open("...", "w")
# for line in f:
#     copy.write(line)
# f.close()
# copy.close()<commit_msg>Fixed the recursive call to the debug statements print logic (#524)<commit_after>#! /usr/local/bin/python3
# findLargeFiles.py - given a folder name, walk through its entire hierarchy
#                   - print folders and files within each folder
# python add.trace.statements.to.src.py /Users/iosbuild1/forge/work/code/evernym/sdk/vcx/libvcx/src
# python add.trace.statements.to.src.py /Users/iosbuild1/forge/work/code/evernym/sdk/.macosbuild/vcx-indy-sdk/libnullpay/src
# python add.trace.statements.to.src.py /Users/iosbuild1/forge/work/code/evernym/sdk/.macosbuild/vcx-indy-sdk/libindy/src

import os
import sys

# print(sys.argv)

def recursive_walk(folder, rustLogFunction):
    traceNumber = 0
    for folderName, subfolders, filenames in os.walk(folder):
        if subfolders:
            for subfolder in subfolders:
                recursive_walk(subfolder, rustLogFunction)
        print('\nFolder: ' + folderName)
        for filename in filenames:
            if (filename.endswith(".newrs")):
                print("Ignoring file: " + folderName + '/' + filename)
                continue

            print("Found file: " + folderName + '/' + filename)
            f = open(folderName + '/' + filename, "r")
            print("Copying to file: " + folderName + '/' + filename + ".newrs")
            copy = open(folderName + '/' + filename + ".newrs", "w")
            fileLineNumber = 0
            previousLine = ""
            previousTrimmedLine = ""
            insideExternCurly = 0
            atTopOfFile = 1
            waitForSemiColon = 0
            eatingCurlys = 0
            eatingLines = 0
            openCurlys = -1
            ignoreEnding = 0
            foundFirstOpeningCurl = 0
            insideUseStatement = 0
            for line in f:
                fileLineNumber += 1
                trimmedLine = line.strip()

                if (foundFirstOpeningCurl == 0 and trimmedLine.count('{') > 0):
                    foundFirstOpeningCurl = 1

                if (line.startswith("use") and not trimmedLine.endswith(';')):
                    insideUseStatement = 1

                if (trimmedLine == "extern {"):
                    insideExternCurly = 1
                if (trimmedLine.endswith(",")):
                    waitForSemiColon = 1
                if (
                    not line.startswith("use") and
                    not line.startswith("extern") and
                    not line.startswith("type ") and
                    waitForSemiColon == 0 and
                    len(trimmedLine) > 0
                ):
                    atTopOfFile = 0

                if (trimmedLine.count("\"") == 1 and trimmedLine.endswith("\"")):
                    eatingLines = 1

                if (line.startswith("pub type ") or line.startswith("pub const ") or line.startswith("const ")):
                    ignoreEnding = 1

                if (trimmedLine.startswith("macro_rules!") or trimmedLine.startswith("impl Node")):
                    eatingCurlys = 1

                if (eatingCurlys == 1):
                    if (openCurlys == -1):
                        openCurlys = trimmedLine.count('{')
                    else:
                        openCurlys += trimmedLine.count('{')
                    openCurlys -= trimmedLine.count('}')

                if (
                    trimmedLine.endswith(";") and
                    waitForSemiColon == 0 and
                    atTopOfFile == 0 and
                    eatingCurlys == 0 and
                    eatingLines == 0 and
                    not filename == "sodium_type.rs" and
                    not trimmedLine == "};" and
                    not trimmedLine == "})?;" and
                    not trimmedLine.startswith(".") and
                    not trimmedLine.startswith("{") and
                    not trimmedLine.startswith(").") and
                    not trimmedLine.startswith("}).") and
                    not trimmedLine.startswith(");") and
                    not trimmedLine.startswith("});") and
                    not trimmedLine.startswith("}));") and
                    not trimmedLine.startswith("static ref") and
                    not trimmedLine.startswith("pub static ref") and
                    not trimmedLine.startswith("pub type ") and
                    not trimmedLine.startswith("type ") and
                    not trimmedLine.startswith("fn ") and
                    not trimmedLine.startswith("extern") and
                    not trimmedLine.startswith("use") and
                    not trimmedLine.startswith("return") and
                    not trimmedLine.startswith("pub const") and
                    not line.startswith("pub mod") and
                    not line.startswith("sodium_type!") and
                    not line.startswith("const ") and
                    not line.startswith("pub static") and
                    not line.startswith("pub struct") and
                    not line.startswith("fn matches") and
                    not line.startswith("//") and
                    not line.startswith("}") and
                    not line.startswith(")") and
                    not line.startswith("/*") and
                    not line.startswith("static") and
                    not line.startswith("mod ") and
                    not line.startswith("#[macro_use") and
                    not previousTrimmedLine.endswith(",") and
                    not previousTrimmedLine.endswith(".") and
                    not previousTrimmedLine.endswith("=") and
                    not previousTrimmedLine.endswith("?") and
                    not previousTrimmedLine.endswith(")") and
                    not previousTrimmedLine.endswith("(") and
                    not previousTrimmedLine.endswith("|") and
                    not previousTrimmedLine.endswith("\\") and
                    not previousTrimmedLine.endswith("}") and
                    not previousTrimmedLine.startswith("retun") and
                    not previousTrimmedLine.startswith("#[cfg") and
                    not previousLine.startswith("#[cfg") and
                    not previousLine.startswith("pub trait") and
                    not previousLine.startswith("impl")
                ):
                    traceNumber += 1
                    copy.write(rustLogFunction + "!(\"TRACE[" + str(traceNumber) + "]: ABOVE LINE[" + str(fileLineNumber) + "]: " + trimmedLine.replace("\\","\\\\").replace("\"","\\\"").replace("{","{{").replace("}","}}") + " -- FILE: " + folderName + "/" + filename + "\");\n")
                    #copy.write("match std::env::var(\"MOBILE_TRACE\") {Ok(val) => {" + rustLogFunction + "!(\"TRACE[" + str(traceNumber) + "]: ABOVE LINE[" + str(fileLineNumber) + "]: " + trimmedLine.replace("\\","\\\\").replace("\"","\\\"").replace("{","{{").replace("}","}}") + " -- FILE: " + folderName + "/" + filename + "\")}, Err(e) => {},}\n")

                copy.write(line)

                if (
                    trimmedLine.endswith(";") and
                    atTopOfFile == 0 and
                    eatingCurlys == 0 and
                    eatingLines == 0 and
                    ignoreEnding == 0 and
                    not filename == "sodium_type.rs" and
                    not trimmedLine == "};" and
                    not trimmedLine == "})?;" and
                    not trimmedLine.startswith("static ref") and
                    not trimmedLine.startswith("pub static ref") and
                    not trimmedLine.startswith("pub type ") and
                    not trimmedLine.startswith("type ") and
                    not trimmedLine.startswith("r#\"{\"") and
                    not trimmedLine.startswith("fn ") and
                    not trimmedLine.startswith("return") and
                    not trimmedLine.startswith("break") and
                    not trimmedLine.startswith("continue") and
                    not trimmedLine.startswith("extern") and
                    not trimmedLine.startswith("use") and
                    not trimmedLine.startswith("pub const") and
                    not line.startswith("pub mod") and
                    not line.startswith("sodium_type!") and
                    not line.startswith("const ") and
                    not line.startswith("pub static") and
                    not line.startswith("pub struct") and
                    not line.startswith("fn matches") and
                    not line.startswith("//") and
                    not line.startswith("/*") and
                    not line.startswith("static") and
                    not line.startswith("mod ") and
                    not line.startswith("});") and
                    not line.startswith("#[macro_use") and
                    insideExternCurly == 0 and
                    not previousTrimmedLine.startswith("return") and
                    not previousTrimmedLine.startswith("break") and
                    not previousTrimmedLine.startswith("continue") and
                    not previousLine.startswith("use logic") and
                    foundFirstOpeningCurl == 1 and
                    insideUseStatement == 0 and
                    not previousLine.startswith("pub trait") and
                    not previousLine.startswith("impl")
                ):
                    traceNumber += 1
                    copy.write(rustLogFunction + "!(\"TRACE[" + str(traceNumber) + "]: BELOW LINE[" + str(fileLineNumber) + "]: " + trimmedLine.replace("\\","\\\\").replace("\"","\\\"").replace("{","{{").replace("}","}}") + " -- FILE: " + folderName + "/" + filename + "\");\n")
                    #copy.write("match std::env::var(\"MOBILE_TRACE\") {Ok(val) => {" + rustLogFunciton + "!(\"TRACE[" + str(traceNumber) + "]: BELOW LINE[" + str(fileLineNumber) + "]: " + trimmedLine.replace("\\","\\\\").replace("\"","\\\"").replace("{","{{").replace("}","}}") + " -- FILE: " + folderName + "/" + filename + "\")}, Err(e) => {},}\n")

                if ( insideExternCurly == 1 and trimmedLine == "}" ):
                    insideExternCurly = 0
                if (trimmedLine.endswith(";")):
                    waitForSemiColon = 0
                    ignoreEnding = 0
                if (openCurlys == 0):
                    eatingCurlys = 0
                    openCurlys = -1
                if (trimmedLine.startswith("\"") and eatingLines == 1):
                    eatingLines = 0
                if (insideUseStatement == 1 and trimmedLine.endswith(';')):
                    insideUseStatement = 0

                previousTrimmedLine = trimmedLine
                previousLine = line
            f.close()
            copy.close()
            os.rename(folderName + '/' + filename + ".newrs", folderName + '/' + filename)

recursive_walk(sys.argv[1], sys.argv[2])

# find vcx/libvcx/src -name "*.rs"|wc -l

# f = open("...", "r")
# copy = open("...", "w")
# for line in f:
#     copy.write(line)
# f.close()
# copy.close()<|endoftext|>


<commit_before>import json

from django.http import HttpResponse
from django.shortcuts import redirect
from django.utils.decorators import method_decorator
from django.utils.encoding import smart_str
from django.views.generic import TemplateView, DetailView, View, FormView, ListView
from django.views.generic.edit import FormMixin
from django.views.decorators.csrf import csrf_exempt

import stripe

from .actions import events, exceptions, customers, subscriptions, sources
from .forms import PlanForm, PaymentMethodForm
from .mixins import LoginRequiredMixin, CustomerMixin, PaymentsContextMixin
from .models import Invoice, Card, Subscription

class InvoiceListView(LoginRequiredMixin, CustomerMixin, ListView):
    model = Invoice
    context_object_name = "invoice_list"
    template_name = "pinax/stripe/invoice_list.html"

    def get_queryset(self):
        return super(InvoiceListView, self).get_queryset().order_by("date")

class PaymentMethodListView(LoginRequiredMixin, CustomerMixin, ListView):
    model = Card
    context_object_name = "payment_method_list"
    template_name = "pinax/stripe/paymentmethod_list.html"

    def get_queryset(self):
        return super(PaymentMethodListView, self).get_queryset().order_by("created_at")

class PaymentMethodCreateView(LoginRequiredMixin, CustomerMixin, PaymentsContextMixin, TemplateView):
    model = Card
    template_name = "pinax/stripe/paymentmethod_create.html"

    def create_card(self, stripe_token):
        sources.create_card(self.customer, token=stripe_token)

    def post(self, request, *args, **kwargs):
        try:
            self.create_card(request.POST.get("stripeToken"))
            return redirect("pinax_stripe_payment_method_list")
        except stripe.CardError as e:
            return self.render_to_response(self.get_context_data(errors=smart_str(e)))

class PaymentMethodDeleteView(LoginRequiredMixin, CustomerMixin, DetailView):
    model = Card
    template_name = "pinax/stripe/paymentmethod_delete.html"

    def delete_card(self, stripe_id):
        sources.delete_card(self.customer, stripe_id)

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        try:
            self.delete_card(self.object.stripe_id)
            return redirect("pinax_stripe_payment_method_list")
        except stripe.CardError as e:
            return self.render_to_response(self.get_context_data(errors=smart_str(e)))

class PaymentMethodUpdateView(LoginRequiredMixin, CustomerMixin, PaymentsContextMixin, FormMixin, DetailView):
    model = Card
    form_class = PaymentMethodForm
    template_name = "pinax/stripe/paymentmethod_update.html"

    def update_card(self, exp_month, exp_year):
        sources.update_card(self.customer, self.object.stripe_id, exp_month=exp_month, exp_year=exp_year)

    def form_valid(self, form):
        self.object = self.get_object()
        try:
            self.update_card(form.cleaned_data["expMonth"], form.cleaned_data["expYear"])
            return redirect("pinax_stripe_payment_method_list")
        except stripe.CardError as e:
            return self.render_to_response(self.get_context_data(errors=smart_str(e)))

class SubscriptionListView(LoginRequiredMixin, CustomerMixin, ListView):
    model = Subscription
    context_object_name = "subscription_list"
    template_name = "pinax/stripe/subscription_list.html"

    def get_queryset(self):
        return super(SubscriptionListView, self).get_queryset().order_by("created_at")

class SubscriptionCreateView(LoginRequiredMixin, PaymentsContextMixin, CustomerMixin, FormView):
    template_name = "pinax/stripe/subscription_create.html"
    form_class = PlanForm

    def set_customer(self):
        if self.customer is None:
            self._customer = customers.create(self.request.user)

    def subscribe(self, customer, plan, token):
        subscriptions.create(customer, plan, token=token)

    def form_valid(self, form):
        self.set_customer()
        try:
            self.subscribe(self.customer, plan=form.cleaned_data["plan"], token=self.request.POST.get("stripeToken"))
            return redirect("pinax_stripe_subscription_list")
        except stripe.StripeError as e:
            return self.render_to_response(self.get_context_data(form=form, errors=smart_str(e)))

class SubscriptionDeleteView(LoginRequiredMixin, CustomerMixin, DetailView):
    model = Subscription
    template_name = "pinax/stripe/subscription_delete.html"

    def cancel(self):
        subscriptions.cancel(self.object)

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        try:
            self.cancel()
            return redirect("pinax_stripe_subscription_list")
        except stripe.StripeError as e:
            return self.render_to_response(self.get_context_data(errors=smart_str(e)))

class SubscriptionUpdateView(LoginRequiredMixin, CustomerMixin, FormMixin, DetailView):
    model = Subscription
    form_class = PlanForm
    template_name = "pinax/stripe/subscription_update.html"

    @property
    def current_plan(self):
        if not hasattr(self, "_current_plan"):
            self._current_plan = self.object.plan
        return self._current_plan

    def get_context_data(self, **kwargs):
        context = super(SubscriptionUpdateView, self).get_context_data(**kwargs)
        context.update({
            "form": self.get_form(form_class=self.form_class)
        })
        return context

    def update_subscription(self, plan_id):
        subscriptions.update(self.object, plan_id)

    def get_initial(self):
        initial = super(SubscriptionUpdateView, self).get_initial()
        initial.update({
            "plan": self.current_plan
        })
        return initial

    def form_valid(self, form):
        try:
            self.update_subscription(form.cleaned_data["plan"])
            return redirect("pinax_stripe_subscription_list")
        except stripe.StripeError as e:
            return self.render_to_response(self.get_context_data(form=form, errors=smart_str(e)))

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        form = self.get_form(form_class=self.form_class)
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

class Webhook(View):

    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        return super(Webhook, self).dispatch(*args, **kwargs)

    def extract_json(self):
        data = json.loads(smart_str(self.request.body))
        return data

    def post(self, request, *args, **kwargs):
        data = self.extract_json()
        if events.dupe_event_exists(data["id"]):
            exceptions.log_exception(data, "Duplicate event record")
        else:
            events.add_event(
                stripe_id=data["id"],
                kind=data["type"],
                livemode=data["livemode"],
                message=data
            )
        return HttpResponse()
<commit_msg>FormMixin brings no post method<commit_after>import json

from django.http import HttpResponse
from django.shortcuts import redirect
from django.utils.decorators import method_decorator
from django.utils.encoding import smart_str
from django.views.generic import TemplateView, DetailView, View, FormView, ListView
from django.views.generic.edit import FormMixin
from django.views.decorators.csrf import csrf_exempt

import stripe

from .actions import events, exceptions, customers, subscriptions, sources
from .forms import PlanForm, PaymentMethodForm
from .mixins import LoginRequiredMixin, CustomerMixin, PaymentsContextMixin
from .models import Invoice, Card, Subscription

class InvoiceListView(LoginRequiredMixin, CustomerMixin, ListView):
    model = Invoice
    context_object_name = "invoice_list"
    template_name = "pinax/stripe/invoice_list.html"

    def get_queryset(self):
        return super(InvoiceListView, self).get_queryset().order_by("date")

class PaymentMethodListView(LoginRequiredMixin, CustomerMixin, ListView):
    model = Card
    context_object_name = "payment_method_list"
    template_name = "pinax/stripe/paymentmethod_list.html"

    def get_queryset(self):
        return super(PaymentMethodListView, self).get_queryset().order_by("created_at")

class PaymentMethodCreateView(LoginRequiredMixin, CustomerMixin, PaymentsContextMixin, TemplateView):
    model = Card
    template_name = "pinax/stripe/paymentmethod_create.html"

    def create_card(self, stripe_token):
        sources.create_card(self.customer, token=stripe_token)

    def post(self, request, *args, **kwargs):
        try:
            self.create_card(request.POST.get("stripeToken"))
            return redirect("pinax_stripe_payment_method_list")
        except stripe.CardError as e:
            return self.render_to_response(self.get_context_data(errors=smart_str(e)))

class PaymentMethodDeleteView(LoginRequiredMixin, CustomerMixin, DetailView):
    model = Card
    template_name = "pinax/stripe/paymentmethod_delete.html"

    def delete_card(self, stripe_id):
        sources.delete_card(self.customer, stripe_id)

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        try:
            self.delete_card(self.object.stripe_id)
            return redirect("pinax_stripe_payment_method_list")
        except stripe.CardError as e:
            return self.render_to_response(self.get_context_data(errors=smart_str(e)))

class PaymentMethodUpdateView(LoginRequiredMixin, CustomerMixin, PaymentsContextMixin, FormMixin, DetailView):
    model = Card
    form_class = PaymentMethodForm
    template_name = "pinax/stripe/paymentmethod_update.html"

    def update_card(self, exp_month, exp_year):
        sources.update_card(self.customer, self.object.stripe_id, exp_month=exp_month, exp_year=exp_year)

    def form_valid(self, form):
        try:
            self.update_card(form.cleaned_data["expMonth"], form.cleaned_data["expYear"])
            return redirect("pinax_stripe_payment_method_list")
        except stripe.CardError as e:
            return self.render_to_response(self.get_context_data(errors=smart_str(e)))

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        form = self.get_form(form_class=self.form_class)
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

class SubscriptionListView(LoginRequiredMixin, CustomerMixin, ListView):
    model = Subscription
    context_object_name = "subscription_list"
    template_name = "pinax/stripe/subscription_list.html"

    def get_queryset(self):
        return super(SubscriptionListView, self).get_queryset().order_by("created_at")

class SubscriptionCreateView(LoginRequiredMixin, PaymentsContextMixin, CustomerMixin, FormView):
    template_name = "pinax/stripe/subscription_create.html"
    form_class = PlanForm

    def set_customer(self):
        if self.customer is None:
            self._customer = customers.create(self.request.user)

    def subscribe(self, customer, plan, token):
        subscriptions.create(customer, plan, token=token)

    def form_valid(self, form):
        self.set_customer()
        try:
            self.subscribe(self.customer, plan=form.cleaned_data["plan"], token=self.request.POST.get("stripeToken"))
            return redirect("pinax_stripe_subscription_list")
        except stripe.StripeError as e:
            return self.render_to_response(self.get_context_data(form=form, errors=smart_str(e)))

class SubscriptionDeleteView(LoginRequiredMixin, CustomerMixin, DetailView):
    model = Subscription
    template_name = "pinax/stripe/subscription_delete.html"

    def cancel(self):
        subscriptions.cancel(self.object)

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        try:
            self.cancel()
            return redirect("pinax_stripe_subscription_list")
        except stripe.StripeError as e:
            return self.render_to_response(self.get_context_data(errors=smart_str(e)))

class SubscriptionUpdateView(LoginRequiredMixin, CustomerMixin, FormMixin, DetailView):
    model = Subscription
    form_class = PlanForm
    template_name = "pinax/stripe/subscription_update.html"

    @property
    def current_plan(self):
        if not hasattr(self, "_current_plan"):
            self._current_plan = self.object.plan
        return self._current_plan

    def get_context_data(self, **kwargs):
        context = super(SubscriptionUpdateView, self).get_context_data(**kwargs)
        context.update({
            "form": self.get_form(form_class=self.form_class)
        })
        return context

    def update_subscription(self, plan_id):
        subscriptions.update(self.object, plan_id)

    def get_initial(self):
        initial = super(SubscriptionUpdateView, self).get_initial()
        initial.update({
            "plan": self.current_plan
        })
        return initial

    def form_valid(self, form):
        try:
            self.update_subscription(form.cleaned_data["plan"])
            return redirect("pinax_stripe_subscription_list")
        except stripe.StripeError as e:
            return self.render_to_response(self.get_context_data(form=form, errors=smart_str(e)))

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        form = self.get_form(form_class=self.form_class)
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

class Webhook(View):

    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        return super(Webhook, self).dispatch(*args, **kwargs)

    def extract_json(self):
        data = json.loads(smart_str(self.request.body))
        return data

    def post(self, request, *args, **kwargs):
        data = self.extract_json()
        if events.dupe_event_exists(data["id"]):
            exceptions.log_exception(data, "Duplicate event record")
        else:
            events.add_event(
                stripe_id=data["id"],
                kind=data["type"],
                livemode=data["livemode"],
                message=data
            )
        return HttpResponse()
<|endoftext|>


<commit_before>from os import environ

# Memcache addon environment variables.
# See: https://addons.heroku.com/memcache
MEMCACHE_ENV_VARS = (
    'MEMCACHE_PASSWORD',
    'MEMCACHE_SERVERS',
    'MEMCACHE_USERNAME',
)

# MemCachier addon environment variables.
# See: https://addons.heroku.com/memcachier
MEMCACHIER_ENV_VARS = (
    'MEMCACHIER_PASSWORD',
    'MEMCACHIER_SERVERS',
    'MEMCACHIER_USERNAME',
)

def memcacheify(timeout=500):
    """Return a fully configured Django ``CACHES`` setting. We do this by
    analyzing all environment variables on Heorku, scanning for an available
    memcache addon, and then building the settings dict properly.

    If no memcache servers can be found, we'll revert to building a local
    memory cache.

    Returns a fully configured caches dict.
    """
    caches = {}

    if all((environ.get(e, '') for e in MEMCACHE_ENV_VARS)):
        caches['default'] = {
            'BACKEND': 'django_pylibmc.memcached.PyLibMCCache',
            'BINARY': True,
            'LOCATION': 'localhost:11211',
            'OPTIONS': {
                'ketama': True,
                'tcp_nodelay': True,
            },
            'TIMEOUT': timeout,
        }
    elif all((environ.get(e, '') for e in MEMCACHIER_ENV_VARS)):
        environ['MEMCACHE_SERVERS'] = environ.get('MEMCACHIER_SERVERS').replace(',', ';')
        environ['MEMCACHE_USERNAME'] = environ.get('MEMCACHIER_USERNAME')
        environ['MEMCACHE_PASSWORD'] = environ.get('MEMCACHIER_PASSWORD')
        caches['default'] = {
            'BACKEND': 'django_pylibmc.memcached.PyLibMCCache',
            'BINARY': True,
            'LOCATION': environ.get('MEMCACHIER_SERVERS').replace(',', ';'),
            'OPTIONS': {
                'ketama': True,
                'tcp_nodelay': True,
            },
            'TIMEOUT': timeout,
        }
    else:
        caches['default'] = {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        }

    return caches
<commit_msg>add option to use memcached local without sasl<commit_after>from os import environ

# Memcache addon environment variables.
# See: https://addons.heroku.com/memcache
MEMCACHE_ENV_VARS = (
    'MEMCACHE_PASSWORD',
    'MEMCACHE_SERVERS',
    'MEMCACHE_USERNAME',
)

# MemCachier addon environment variables.
# See: https://addons.heroku.com/memcachier
MEMCACHIER_ENV_VARS = (
    'MEMCACHIER_PASSWORD',
    'MEMCACHIER_SERVERS',
    'MEMCACHIER_USERNAME',
)

def memcacheify(timeout=500):
    """Return a fully configured Django ``CACHES`` setting. We do this by
    analyzing all environment variables on Heorku, scanning for an available
    memcache addon, and then building the settings dict properly.

    If no memcache servers can be found, we'll revert to building a local
    memory cache.

    Returns a fully configured caches dict.
    """
    caches = {}

    if all((environ.get(e, '') for e in MEMCACHE_ENV_VARS)):
        caches['default'] = {
            'BACKEND': 'django_pylibmc.memcached.PyLibMCCache',
            'BINARY': True,
            'LOCATION': 'localhost:11211',
            'OPTIONS': {
                'ketama': True,
                'tcp_nodelay': True,
            },
            'TIMEOUT': timeout,
        }
    elif all((environ.get(e, '') for e in MEMCACHIER_ENV_VARS)):
        environ['MEMCACHE_SERVERS'] = environ.get('MEMCACHIER_SERVERS').replace(',', ';')
        environ['MEMCACHE_USERNAME'] = environ.get('MEMCACHIER_USERNAME')
        environ['MEMCACHE_PASSWORD'] = environ.get('MEMCACHIER_PASSWORD')
        caches['default'] = {
            'BACKEND': 'django_pylibmc.memcached.PyLibMCCache',
            'BINARY': True,
            'LOCATION': environ.get('MEMCACHIER_SERVERS').replace(',', ';'),
            'OPTIONS': {
                'ketama': True,
                'tcp_nodelay': True,
            },
            'TIMEOUT': timeout,
        }
    elif environ.get('MEMCACHEIFY_USE_LOCAL', False):
        caches['default'] = {
            'BACKEND': 'django_pylibmc.memcached.PyLibMCCache',
        }
    else:
        caches['default'] = {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        }

    return caches
<|endoftext|>


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

"""Unit tests for zeroconf._services.types."""

import os
import unittest
import socket
import sys

import zeroconf as r
from zeroconf import Zeroconf, ServiceInfo, ZeroconfServiceTypes

from .. import _clear_cache, has_working_ipv6

class ServiceTypesQuery(unittest.TestCase):
    def test_integration_with_listener(self):

        type_ = "_test-srvc-type._tcp.local."
        name = "xxxyyy"
        registration_name = "%s.%s" % (name, type_)

        zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
        desc = {'path': '/~paulsm/'}
        info = ServiceInfo(
            type_,
            registration_name,
            80,
            0,
            0,
            desc,
            "ash-2.local.",
            addresses=[socket.inet_aton("10.0.1.2")],
        )
        zeroconf_registrar.register_service(info)
        _clear_cache(zeroconf_registrar)
        try:
            service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
            assert type_ in service_types
            _clear_cache(zeroconf_registrar)
            service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
            assert type_ in service_types

        finally:
            zeroconf_registrar.close()

    @unittest.skipIf(not has_working_ipv6(), 'Requires IPv6')
    @unittest.skipIf(os.environ.get('SKIP_IPV6'), 'IPv6 tests disabled')
    def test_integration_with_listener_v6_records(self):

        type_ = "_test-srvc-type._tcp.local."
        name = "xxxyyy"
        registration_name = "%s.%s" % (name, type_)
        addr = "2606:2800:220:1:248:1893:25c8:1946"  # example.com

        zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
        desc = {'path': '/~paulsm/'}
        info = ServiceInfo(
            type_,
            registration_name,
            80,
            0,
            0,
            desc,
            "ash-2.local.",
            addresses=[socket.inet_pton(socket.AF_INET6, addr)],
        )
        zeroconf_registrar.register_service(info)
        _clear_cache(zeroconf_registrar)
        try:
            service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
            assert type_ in service_types
            _clear_cache(zeroconf_registrar)
            service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
            assert type_ in service_types

        finally:
            zeroconf_registrar.close()

    @unittest.skipIf(not has_working_ipv6() or sys.platform == 'win32', 'Requires IPv6')
    @unittest.skipIf(os.environ.get('SKIP_IPV6'), 'IPv6 tests disabled')
    def test_integration_with_listener_ipv6(self):

        type_ = "_test-srvc-type._tcp.local."
        name = "xxxyyy"
        registration_name = "%s.%s" % (name, type_)

        zeroconf_registrar = Zeroconf(ip_version=r.IPVersion.V6Only)
        desc = {'path': '/~paulsm/'}
        info = ServiceInfo(
            type_,
            registration_name,
            80,
            0,
            0,
            desc,
            "ash-2.local.",
            addresses=[socket.inet_aton("10.0.1.2")],
        )
        zeroconf_registrar.register_service(info)
        _clear_cache(zeroconf_registrar)
        try:
            service_types = ZeroconfServiceTypes.find(ip_version=r.IPVersion.V6Only, timeout=0.5)
            assert type_ in service_types
            _clear_cache(zeroconf_registrar)
            service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
            assert type_ in service_types

        finally:
            zeroconf_registrar.close()

    def test_integration_with_subtype_and_listener(self):
        subtype_ = "_subtype._sub"
        type_ = "_type._tcp.local."
        name = "xxxyyy"
        # Note: discovery returns only DNS-SD type not subtype
        discovery_type = "%s.%s" % (subtype_, type_)
        registration_name = "%s.%s" % (name, type_)

        zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
        desc = {'path': '/~paulsm/'}
        info = ServiceInfo(
            discovery_type,
            registration_name,
            80,
            0,
            0,
            desc,
            "ash-2.local.",
            addresses=[socket.inet_aton("10.0.1.2")],
        )
        zeroconf_registrar.register_service(info)
        _clear_cache(zeroconf_registrar)
        try:
            service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
            assert discovery_type in service_types
            _clear_cache(zeroconf_registrar)
            service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
            assert discovery_type in service_types

        finally:
            zeroconf_registrar.close()
<commit_msg>Fix flakey ZeroconfServiceTypes types test (#662)<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Unit tests for zeroconf._services.types."""

import os
import unittest
import socket
import sys
import time

import zeroconf as r
from zeroconf import Zeroconf, ServiceInfo, ZeroconfServiceTypes

from .. import _clear_cache, has_working_ipv6

class ServiceTypesQuery(unittest.TestCase):
    def test_integration_with_listener(self):

        type_ = "_test-srvc-type._tcp.local."
        name = "xxxyyy"
        registration_name = "%s.%s" % (name, type_)

        zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
        desc = {'path': '/~paulsm/'}
        info = ServiceInfo(
            type_,
            registration_name,
            80,
            0,
            0,
            desc,
            "ash-2.local.",
            addresses=[socket.inet_aton("10.0.1.2")],
        )
        zeroconf_registrar.register_service(info)
        # Ensure we do not clear the cache until after the last broadcast is processed
        time.sleep(0.2)
        _clear_cache(zeroconf_registrar)
        try:
            service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
            assert type_ in service_types
            _clear_cache(zeroconf_registrar)
            service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
            assert type_ in service_types

        finally:
            zeroconf_registrar.close()

    @unittest.skipIf(not has_working_ipv6(), 'Requires IPv6')
    @unittest.skipIf(os.environ.get('SKIP_IPV6'), 'IPv6 tests disabled')
    def test_integration_with_listener_v6_records(self):

        type_ = "_test-srvc-type._tcp.local."
        name = "xxxyyy"
        registration_name = "%s.%s" % (name, type_)
        addr = "2606:2800:220:1:248:1893:25c8:1946"  # example.com

        zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
        desc = {'path': '/~paulsm/'}
        info = ServiceInfo(
            type_,
            registration_name,
            80,
            0,
            0,
            desc,
            "ash-2.local.",
            addresses=[socket.inet_pton(socket.AF_INET6, addr)],
        )
        zeroconf_registrar.register_service(info)
        # Ensure we do not clear the cache until after the last broadcast is processed
        time.sleep(0.2)
        _clear_cache(zeroconf_registrar)
        try:
            service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
            assert type_ in service_types
            _clear_cache(zeroconf_registrar)
            service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
            assert type_ in service_types

        finally:
            zeroconf_registrar.close()

    @unittest.skipIf(not has_working_ipv6() or sys.platform == 'win32', 'Requires IPv6')
    @unittest.skipIf(os.environ.get('SKIP_IPV6'), 'IPv6 tests disabled')
    def test_integration_with_listener_ipv6(self):

        type_ = "_test-srvc-type._tcp.local."
        name = "xxxyyy"
        registration_name = "%s.%s" % (name, type_)

        zeroconf_registrar = Zeroconf(ip_version=r.IPVersion.V6Only)
        desc = {'path': '/~paulsm/'}
        info = ServiceInfo(
            type_,
            registration_name,
            80,
            0,
            0,
            desc,
            "ash-2.local.",
            addresses=[socket.inet_aton("10.0.1.2")],
        )
        zeroconf_registrar.register_service(info)
        # Ensure we do not clear the cache until after the last broadcast is processed
        time.sleep(0.2)
        _clear_cache(zeroconf_registrar)
        try:
            service_types = ZeroconfServiceTypes.find(ip_version=r.IPVersion.V6Only, timeout=0.5)
            assert type_ in service_types
            _clear_cache(zeroconf_registrar)
            service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
            assert type_ in service_types

        finally:
            zeroconf_registrar.close()

    def test_integration_with_subtype_and_listener(self):
        subtype_ = "_subtype._sub"
        type_ = "_type._tcp.local."
        name = "xxxyyy"
        # Note: discovery returns only DNS-SD type not subtype
        discovery_type = "%s.%s" % (subtype_, type_)
        registration_name = "%s.%s" % (name, type_)

        zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
        desc = {'path': '/~paulsm/'}
        info = ServiceInfo(
            discovery_type,
            registration_name,
            80,
            0,
            0,
            desc,
            "ash-2.local.",
            addresses=[socket.inet_aton("10.0.1.2")],
        )
        zeroconf_registrar.register_service(info)
        # Ensure we do not clear the cache until after the last broadcast is processed
        time.sleep(0.2)
        _clear_cache(zeroconf_registrar)
        try:
            service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
            assert discovery_type in service_types
            _clear_cache(zeroconf_registrar)
            service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
            assert discovery_type in service_types

        finally:
            zeroconf_registrar.close()
<|endoftext|>


<commit_before>"""
This module provides an abstraction for working with XModuleDescriptors
that are stored in a database an accessible using their Location as an identifier
"""

import re
from .exceptions import InvalidLocationError

URL_RE = re.compile("""
    (?P<tag>[^:]+)://
    (?P<org>[^/]+)/
    (?P<course>[^/]+)/
    (?P<category>[^/]+)/
    (?P<name>[^/]+)
    (/(?P<revision>[^/]+))?
    """, re.VERBOSE)

class Location(object):
    '''
    Encodes a location.

    Locations representations of URLs of the
    form {tag}://{org}/{course}/{category}/{name}[/{revision}]

    However, they can also be represented a dictionaries (specifying each component),
    tuples or list (specified in order), or as strings of the url
    '''
    def __init__(self, location):
        """
        Create a new location that is a clone of the specifed one.

        location - Can be any of the following types:
            string: should be of the form {tag}://{org}/{course}/{category}/{name}[/{revision}]
            list: should be of the form [tag, org, course, category, name, revision]
            dict: should be of the form {
                'tag': tag,
                'org': org,
                'course': course,
                'category': category,
                'name': name,
                'revision': revision,
            }
            Location: another Location object

        In both the dict and list forms, the revision is optional, and can be ommitted.

        None of the components of a location may contain the '/' character

        Components may be set to None, which may be interpreted by some contexts to mean
        wildcard selection
        """
        self.update(location)

    def update(self, location):
        """
        Update this instance with data from another Location object.

        location: can take the same forms as specified by `__init__`
        """
        self.tag = self.org = self.course = self.category = self.name = self.revision = None

        if isinstance(location, basestring):
            match = URL_RE.match(location)
            if match is None:
                raise InvalidLocationError(location)
            else:
                self.update(match.groupdict())
        elif isinstance(location, list):
            if len(location) not in (5, 6):
                raise InvalidLocationError(location)

            (self.tag, self.org, self.course, self.category, self.name) = location[0:5]
            self.revision = location[5] if len(location) == 6 else None
        elif isinstance(location, dict):
            try:
                self.tag = location['tag']
                self.org = location['org']
                self.course = location['course']
                self.category = location['category']
                self.name = location['name']
            except KeyError:
                raise InvalidLocationError(location)
            self.revision = location.get('revision')
        elif isinstance(location, Location):
            self.update(location.list())
        else:
            raise InvalidLocationError(location)

        for val in self.list():
            if val is not None and '/' in val:
                raise InvalidLocationError(location)

    def __str__(self):
        return self.url()

    def url(self):
        """
        Return a string containing the URL for this location
        """
        url = "{tag}://{org}/{course}/{category}/{name}".format(**self.dict())
        if self.revision:
            url += "/" + self.revision
        return url

    def list(self):
        """
        Return a list representing this location
        """
        return [self.tag, self.org, self.course, self.category, self.name, self.revision]

    def dict(self):
        """
        Return a dictionary representing this location
        """
        return {'tag': self.tag,
                'org': self.org,
                'course': self.course,
                'category': self.category,
                'name': self.name,
                'revision': self.revision}

class ModuleStore(object):
    """
    An abstract interface for a database backend that stores XModuleDescriptor instances
    """
    def get_item(self, location, default_class=None):
        """
        Returns an XModuleDescriptor instance for the item at location.
        If location.revision is None, returns the item with the most
        recent revision

        If any segment of the location is None except revision, raises
            keystore.exceptions.InsufficientSpecificationError
        If no object is found at that location, raises keystore.exceptions.ItemNotFoundError

        location: Something that can be passed to Location
        default_class: An XModuleDescriptor subclass to use if no plugin matching the
            location is found
        """
        raise NotImplementedError

    # TODO (cpennington): Replace with clone_item
    def create_item(self, location, editor):
        raise NotImplementedError

    def update_item(self, location, data):
        """
        Set the data in the item specified by the location to
        data

        location: Something that can be passed to Location
        data: A nested dictionary of problem data
        """
        raise NotImplementedError

    def update_children(self, location, children):
        """
        Set the children for the item specified by the location to
        data

        location: Something that can be passed to Location
        children: A list of child item identifiers
        """
        raise NotImplementedError
<commit_msg>Restrict the set of characters allowed in locations further<commit_after>"""
This module provides an abstraction for working with XModuleDescriptors
that are stored in a database an accessible using their Location as an identifier
"""

import re
from .exceptions import InvalidLocationError

URL_RE = re.compile("""
    (?P<tag>[^:]+)://
    (?P<org>[^/]+)/
    (?P<course>[^/]+)/
    (?P<category>[^/]+)/
    (?P<name>[^/]+)
    (/(?P<revision>[^/]+))?
    """, re.VERBOSE)

INVALID_CHARS = re.compile(r"[^\w-]")

class Location(object):
    '''
    Encodes a location.

    Locations representations of URLs of the
    form {tag}://{org}/{course}/{category}/{name}[/{revision}]

    However, they can also be represented a dictionaries (specifying each component),
    tuples or list (specified in order), or as strings of the url
    '''

    @classmethod
    def clean(cls, value):
        """
        Return value, made into a form legal for locations
        """
        return re.sub('_+', '_', INVALID_CHARS.sub('_', value))

    def __init__(self, location):
        """
        Create a new location that is a clone of the specifed one.

        location - Can be any of the following types:
            string: should be of the form {tag}://{org}/{course}/{category}/{name}[/{revision}]
            list: should be of the form [tag, org, course, category, name, revision]
            dict: should be of the form {
                'tag': tag,
                'org': org,
                'course': course,
                'category': category,
                'name': name,
                'revision': revision,
            }
            Location: another Location object

        In both the dict and list forms, the revision is optional, and can be ommitted.

        Components must be composed of alphanumeric characters, or the characters _, and -

        Components may be set to None, which may be interpreted by some contexts to mean
        wildcard selection
        """
        self.update(location)

    def update(self, location):
        """
        Update this instance with data from another Location object.

        location: can take the same forms as specified by `__init__`
        """
        self.tag = self.org = self.course = self.category = self.name = self.revision = None

        if isinstance(location, basestring):
            match = URL_RE.match(location)
            if match is None:
                raise InvalidLocationError(location)
            else:
                self.update(match.groupdict())
        elif isinstance(location, list):
            if len(location) not in (5, 6):
                raise InvalidLocationError(location)

            (self.tag, self.org, self.course, self.category, self.name) = location[0:5]
            self.revision = location[5] if len(location) == 6 else None
        elif isinstance(location, dict):
            try:
                self.tag = location['tag']
                self.org = location['org']
                self.course = location['course']
                self.category = location['category']
                self.name = location['name']
            except KeyError:
                raise InvalidLocationError(location)
            self.revision = location.get('revision')
        elif isinstance(location, Location):
            self.update(location.list())
        else:
            raise InvalidLocationError(location)

        for val in self.list():
            if val is not None and INVALID_CHARS.search(val) is not None:
                raise InvalidLocationError(location)

    def __str__(self):
        return self.url()

    def url(self):
        """
        Return a string containing the URL for this location
        """
        url = "{tag}://{org}/{course}/{category}/{name}".format(**self.dict())
        if self.revision:
            url += "/" + self.revision
        return url

    def list(self):
        """
        Return a list representing this location
        """
        return [self.tag, self.org, self.course, self.category, self.name, self.revision]

    def dict(self):
        """
        Return a dictionary representing this location
        """
        return {'tag': self.tag,
                'org': self.org,
                'course': self.course,
                'category': self.category,
                'name': self.name,
                'revision': self.revision}

class ModuleStore(object):
    """
    An abstract interface for a database backend that stores XModuleDescriptor instances
    """
    def get_item(self, location, default_class=None):
        """
        Returns an XModuleDescriptor instance for the item at location.
        If location.revision is None, returns the item with the most
        recent revision

        If any segment of the location is None except revision, raises
            keystore.exceptions.InsufficientSpecificationError
        If no object is found at that location, raises keystore.exceptions.ItemNotFoundError

        location: Something that can be passed to Location
        default_class: An XModuleDescriptor subclass to use if no plugin matching the
            location is found
        """
        raise NotImplementedError

    # TODO (cpennington): Replace with clone_item
    def create_item(self, location, editor):
        raise NotImplementedError

    def update_item(self, location, data):
        """
        Set the data in the item specified by the location to
        data

        location: Something that can be passed to Location
        data: A nested dictionary of problem data
        """
        raise NotImplementedError

    def update_children(self, location, children):
        """
        Set the children for the item specified by the location to
        data

        location: Something that can be passed to Location
        children: A list of child item identifiers
        """
        raise NotImplementedError
<|endoftext|>