<commit_before>import os
from distutils.file_util import copy_file
from distutils.dir_util import mkpath

def copy_tree(src, dst, ignores=()):
    """Copy an entire directory tree 'src' to a new location 'dst'.

    Both 'src' and 'dst' must be directory names.  If 'src' is not a
    directory, raise DistutilsFileError.  If 'dst' does not exist, it is
    created with 'mkpath()'.  The end result of the copy is that every
    file in 'src' is copied to 'dst', and directories under 'src' are
    recursively copied to 'dst'.  Return the list of files that were
    copied or might have been copied, using their output name.

    Ignore any file whose name is in the "ignores" iterable.

    This is a forked version of distutils.dir_util.copy_tree, which
    did not have a way to ignore the files I wanted to ignore.
    """
    if not os.path.isdir(src):
        raise DistutilsFileError, "cannot copy tree '%s': not a directory" % src

    try:
        names = os.listdir(src)
    except os.error, (errno, errstr):
        raise DistutilsFileError, "error listing files in '%s': %s" % (src, errstr)

    mkpath(dst)

    outputs = []

    for n in names:
        if n in ignores: continue

        src_name = os.path.join(src, n)
        dst_name = os.path.join(dst, n)

#def copy_tree(src, dst, preserve_mode=1, preserve_times=1,
#              preserve_symlinks=0, update=0, verbose=1, dry_run=0):

        if os.path.islink(src_name):
            continue
        elif os.path.isdir(src_name):
            outputs.extend(copy_tree(src_name, dst_name, ignores))
        else:
            copy_file(src_name, dst_name, verbose=1)
            outputs.append(dst_name)

    return outputs
<commit_msg>need to include the Error if you want to throw it<commit_after>import os
from distutils.file_util import copy_file, DistutilsFileError
from distutils.dir_util import mkpath

def copy_tree(src, dst, ignores=()):
    """Copy an entire directory tree 'src' to a new location 'dst'.

    Both 'src' and 'dst' must be directory names.  If 'src' is not a
    directory, raise DistutilsFileError.  If 'dst' does not exist, it is
    created with 'mkpath()'.  The end result of the copy is that every
    file in 'src' is copied to 'dst', and directories under 'src' are
    recursively copied to 'dst'.  Return the list of files that were
    copied or might have been copied, using their output name.

    Ignore any file whose name is in the "ignores" iterable.

    This is a forked version of distutils.dir_util.copy_tree, which
    did not have a way to ignore the files I wanted to ignore.
    """
    if not os.path.isdir(src):
        raise DistutilsFileError, "cannot copy tree '%s': not a directory" % src

    try:
        names = os.listdir(src)
    except os.error, (errno, errstr):
        raise DistutilsFileError, "error listing files in '%s': %s" % (src, errstr)

    mkpath(dst)

    outputs = []

    for n in names:
        if n in ignores: continue

        src_name = os.path.join(src, n)
        dst_name = os.path.join(dst, n)

#def copy_tree(src, dst, preserve_mode=1, preserve_times=1,
#              preserve_symlinks=0, update=0, verbose=1, dry_run=0):

        if os.path.islink(src_name):
            continue
        elif os.path.isdir(src_name):
            outputs.extend(copy_tree(src_name, dst_name, ignores))
        else:
            copy_file(src_name, dst_name, verbose=1)
            outputs.append(dst_name)

    return outputs
<|endoftext|>


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

"""
	:copyright: (c) 2011 by Tobias Heinzen
	:license: BSD, see LICENSE for more details
"""

from flask import current_app
from lib.config import config

if not config.getboolean('cache','enabled'):
	class DummyCache(object):
		def get(*args, **kwargs):
			pass
		def set(*args, **kwargs):
			pass

	cache = DummyCache()
else:
	if current_app.debug:
		from werkzeug.contrib.cache import SimpleCache
		cache = SimpleCache()
	else:
		from werkzeug.contrib.cache import MemcachedCache
		cache = MemcachedCache([x.strip() for x in config.get('cache','uris').split(",") if not len(x.strip()) == 0])

<commit_msg>adding failsafe when memcache initialization fails<commit_after># -*- coding: utf-8 -*-

"""
	:copyright: (c) 2011 by Tobias Heinzen
	:license: BSD, see LICENSE for more details
"""

from flask import current_app
from lib.config import config

if not config.getboolean('cache','enabled'):
	class DummyCache(object):
		def get(*args, **kwargs):
			pass
		def set(*args, **kwargs):
			pass

	cache = DummyCache()
else:
	if current_app.debug:
		from werkzeug.contrib.cache import SimpleCache
		cache = SimpleCache()
	else:
		try:
			from werkzeug.contrib.cache import MemcachedCache
			cache = MemcachedCache([x.strip() for x in config.get('cache','uris').split(",") if not len(x.strip()) == 0])
		except:
			cache = DummyCache()
			# TODO: create log message

<|endoftext|>


<commit_before>import os, sys
from subprocess import PIPE, Popen

class UtilsMeta(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(UtilsMeta, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class Utils(object):
    __metaclass__ = UtilsMeta

    @staticmethod
    def env_with_default(varname, default):
        return os.getenv(varname, default)

    @staticmethod
    def cmd(command):
        from config import Config
        log     = None if Config.cmd_log() == None else open(Config.cmd_log(), 'a', 0)
        output  = list()
        process = Popen(args=command, stdout=PIPE, stderr=PIPE, shell=True)
        for line in process.stdout:
            if log != None:
                log.write(line)
            output.append(line)
        process.wait()
        if log != None: log.close()
        return { 'ExitCode': process.returncode, 'StdOut': "".join(output), 'StdErr': process.stderr.read() }

    @staticmethod
    def exit_with_cmd_error(file, error):
        from config import Config
        Config.print_help()
        print >>sys.stderr, "{}: {}".format( os.path.basename(file), error)
        exit(102)

    @staticmethod
    def confirm(message):
        from config import Config
        if Config.args().auto_accept == True:
            return True
        response = raw_input("{}\nAre you sure you want to proceed? (y or yes to continue): ".format( message ))
        if response == "yes" or response == "y":
            return True
        return False

    @staticmethod
    def print_result_error(LOG, message, result):
        LOG.error("{} Exit code {}.".format(message, result['ExitCode']))
        LOG.debug(result['StdOut'])
        LOG.debug(result['StdErr'])

    @staticmethod
    def platform():
        return sys.platform

    @staticmethod
    def list_builds(LOG, directory):
        for name in [ name for name in os.listdir(directory) if os.path.isdir(os.path.join(directory, name)) ]:
            print name

    @staticmethod
    def list_releases(LOG, repo_dir, master_branch):
        result = Utils.cmd("cd {} && git tag -l".format(repo_dir))
        if result['ExitCode'] == 0:
            releases = result['StdOut'].split("\n")
            releases.append(master_branch)
            return releases
        else:
            Utils.print_result_error(LOG, "Failed listing releases.", result)
            return []

    @staticmethod
    def list_sources(sources_dir, kind):
        path = "{}/{}/".format(sources_dir, kind)
        for name in os.listdir(path):
            full_path  = os.path.join(path, name)
            git_config = "{}/.git/config".format(full_path)
            if os.path.isfile(git_config):
                file = open(git_config, 'r')
                data = Utils.parse_git_config( file.readlines() )
                file.close()
                if 'remote "origin"' in data:
                    if 'url' in data['remote "origin"']:
                        print "{} in directory {}".format(
                            data['remote "origin"']['url'],
                            full_path )

    ##
    ## GIT RELATED OPERATIONS:
    ##

    @staticmethod
    def parse_git_config(lines):
        response = dict()
        last_section = None
        for line in lines:
            line = line.strip()
            if line.startswith("[") and line.endswith("]"):
                last_section = line[1:-1]
                response[ last_section ] = dict()
            else:
                if last_section != None:
                    prop, value = line.split(" = ")
                    response[ last_section ][ prop.strip() ] = value.strip()
        return response

    @staticmethod
    def ensure_sources(LOG, repo_dir, repo_addr):
        if os.path.isdir("{}/.git".format(repo_dir)):
            LOG.info("Updating sources for {}...".format(repo_addr))
            result = Utils.cmd("cd {} && git fetch origin".format(repo_dir))
            if result['ExitCode'] == 0:
                LOG.info("Done.")
                return True
            else:
                Utils.print_result_error(LOG, "Failed.", result)
                return False
        else:
            LOG.info("No sources for {} found. Cloning...".format(repo_addr))
            result = Utils.cmd("cd {} && git clone {} .".format(repo_dir, repo_addr))
            if result['ExitCode'] == 0:
                LOG.info("Done.")
                return True
            else:
                Utils.print_result_error(LOG, "Failed.", result)
                return False

    @staticmethod
    def exit_if_git_release_not_set(LOG, build_dir, release, master_branch, git_repo):
        LOG.info("Ensuring Git version {}...".format(release))
        result = Utils.cmd("sleep 5 && cd {} && git checkout {}".format(build_dir, release))
        if result['ExitCode'] == 0:
            if release == master_branch:
                LOG.info("Updating source code for {}...".format(release))
                result = Utils.cmd("cd {} && git pull origin {}".format(build_dir, release))
                if result['ExitCode'] != 0:
                    Utils.print_result_error( LOG,
                                              "Git version {} could not be updated from {}.".format( release, git_repo ),
                                              result )
                    exit(201)
                else:
                    LOG.info("Done.")
        else:
            Utils.print_result_error(LOG, "Version {} could not be checked out from {}.".format(
                                          release, git_repo ), result )
            exit(202)

    ##
    ## DOCKER OPERATIONS:
    ##

    @staticmethod
    def is_docker_image(LOG, image_name):
        Utils.exit_if_docker_unavailable(LOG)
        LOG.info("Checking for Docker image...")
        result = Utils.cmd("docker images | grep {}".format(image_name))
        return result['StdOut'] != ""

    @staticmethod
    def exit_if_docker_unavailable(LOG):
        result = Utils.cmd("docker images") # Simply make sure we have docker operational
        if result['ExitCode'] != 0:
            Utils.print_result_error(LOG, "Not able to list Docker images. Is Docker installed and running?", result)
            exit(301)

    @staticmethod
    def exit_if_docker_image_not_built(LOG, templates_dir, item, image_name):
        result = Utils.cmd( "cd {}/{} && docker build --no-cache --force-rm=true -t {} .".format( templates_dir, item, image_name ) )
        if result['ExitCode'] != 0:
            Utils.print_result_error(LOG, "Docker image creation failed. Is Docker installed and running?", result)
            exit(302)
<commit_msg>Grep for complete word to check if image exists (#30)<commit_after>import os, sys
from subprocess import PIPE, Popen

class UtilsMeta(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(UtilsMeta, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class Utils(object):
    __metaclass__ = UtilsMeta

    @staticmethod
    def env_with_default(varname, default):
        return os.getenv(varname, default)

    @staticmethod
    def cmd(command):
        from config import Config
        log     = None if Config.cmd_log() == None else open(Config.cmd_log(), 'a', 0)
        output  = list()
        process = Popen(args=command, stdout=PIPE, stderr=PIPE, shell=True)
        for line in process.stdout:
            if log != None:
                log.write(line)
            output.append(line)
        process.wait()
        if log != None: log.close()
        return { 'ExitCode': process.returncode, 'StdOut': "".join(output), 'StdErr': process.stderr.read() }

    @staticmethod
    def exit_with_cmd_error(file, error):
        from config import Config
        Config.print_help()
        print >>sys.stderr, "{}: {}".format( os.path.basename(file), error)
        exit(102)

    @staticmethod
    def confirm(message):
        from config import Config
        if Config.args().auto_accept == True:
            return True
        response = raw_input("{}\nAre you sure you want to proceed? (y or yes to continue): ".format( message ))
        if response == "yes" or response == "y":
            return True
        return False

    @staticmethod
    def print_result_error(LOG, message, result):
        LOG.error("{} Exit code {}.".format(message, result['ExitCode']))
        LOG.debug(result['StdOut'])
        LOG.debug(result['StdErr'])

    @staticmethod
    def platform():
        return sys.platform

    @staticmethod
    def list_builds(LOG, directory):
        for name in [ name for name in os.listdir(directory) if os.path.isdir(os.path.join(directory, name)) ]:
            print name

    @staticmethod
    def list_releases(LOG, repo_dir, master_branch):
        result = Utils.cmd("cd {} && git tag -l".format(repo_dir))
        if result['ExitCode'] == 0:
            releases = result['StdOut'].split("\n")
            releases.append(master_branch)
            return releases
        else:
            Utils.print_result_error(LOG, "Failed listing releases.", result)
            return []

    @staticmethod
    def list_sources(sources_dir, kind):
        path = "{}/{}/".format(sources_dir, kind)
        for name in os.listdir(path):
            full_path  = os.path.join(path, name)
            git_config = "{}/.git/config".format(full_path)
            if os.path.isfile(git_config):
                file = open(git_config, 'r')
                data = Utils.parse_git_config( file.readlines() )
                file.close()
                if 'remote "origin"' in data:
                    if 'url' in data['remote "origin"']:
                        print "{} in directory {}".format(
                            data['remote "origin"']['url'],
                            full_path )

    ##
    ## GIT RELATED OPERATIONS:
    ##

    @staticmethod
    def parse_git_config(lines):
        response = dict()
        last_section = None
        for line in lines:
            line = line.strip()
            if line.startswith("[") and line.endswith("]"):
                last_section = line[1:-1]
                response[ last_section ] = dict()
            else:
                if last_section != None:
                    prop, value = line.split(" = ")
                    response[ last_section ][ prop.strip() ] = value.strip()
        return response

    @staticmethod
    def ensure_sources(LOG, repo_dir, repo_addr):
        if os.path.isdir("{}/.git".format(repo_dir)):
            LOG.info("Updating sources for {}...".format(repo_addr))
            result = Utils.cmd("cd {} && git fetch origin".format(repo_dir))
            if result['ExitCode'] == 0:
                LOG.info("Done.")
                return True
            else:
                Utils.print_result_error(LOG, "Failed.", result)
                return False
        else:
            LOG.info("No sources for {} found. Cloning...".format(repo_addr))
            result = Utils.cmd("cd {} && git clone {} .".format(repo_dir, repo_addr))
            if result['ExitCode'] == 0:
                LOG.info("Done.")
                return True
            else:
                Utils.print_result_error(LOG, "Failed.", result)
                return False

    @staticmethod
    def exit_if_git_release_not_set(LOG, build_dir, release, master_branch, git_repo):
        LOG.info("Ensuring Git version {}...".format(release))
        result = Utils.cmd("sleep 5 && cd {} && git checkout {}".format(build_dir, release))
        if result['ExitCode'] == 0:
            if release == master_branch:
                LOG.info("Updating source code for {}...".format(release))
                result = Utils.cmd("cd {} && git pull origin {}".format(build_dir, release))
                if result['ExitCode'] != 0:
                    Utils.print_result_error( LOG,
                                              "Git version {} could not be updated from {}.".format( release, git_repo ),
                                              result )
                    exit(201)
                else:
                    LOG.info("Done.")
        else:
            Utils.print_result_error(LOG, "Version {} could not be checked out from {}.".format(
                                          release, git_repo ), result )
            exit(202)

    ##
    ## DOCKER OPERATIONS:
    ##

    @staticmethod
    def is_docker_image(LOG, image_name):
        Utils.exit_if_docker_unavailable(LOG)
        LOG.info("Checking for Docker image...")
        result = Utils.cmd("docker images | grep -w {}".format(image_name))
        return result['StdOut'] != ""

    @staticmethod
    def exit_if_docker_unavailable(LOG):
        result = Utils.cmd("docker images") # Simply make sure we have docker operational
        if result['ExitCode'] != 0:
            Utils.print_result_error(LOG, "Not able to list Docker images. Is Docker installed and running?", result)
            exit(301)

    @staticmethod
    def exit_if_docker_image_not_built(LOG, templates_dir, item, image_name):
        result = Utils.cmd( "cd {}/{} && docker build --no-cache --force-rm=true -t {} .".format( templates_dir, item, image_name ) )
        if result['ExitCode'] != 0:
            Utils.print_result_error(LOG, "Docker image creation failed. Is Docker installed and running?", result)
            exit(302)
<|endoftext|>


<commit_before>import sys, random, time

boardSize = (10,10)
# board = tuple([random.random() < 0.5 for i in range(boardSize[0] * boardSize[1])])
# board = [
#     [random.random() < 0.5 for i in range(boardSize[0])] for j in range(boardSize[1])
# ]

# def print_board():
#     return None if globals().get('board') is None else [
#         (
#             sys.stdout.write('%.2d '%num),
#             [sys.stdout.write('X' if cell else '_') for cell in row],
#             sys.stdout.write('\n')
#         ) for num, row in enumerate(board)
#     ]

# def transform():
#     return [
#         map(
#             lambda z: (z[1] in (2,3) and board[y][z[0]]) or z[1]==3,
#             [
#                 (
#                     x,
#                     sum(
#                         [
#                             int(
#                                 y0 in range(len(board)) and x0 in range(len(board[y0])) and board[y0][x0]
#                             ) for x0,y0 in (
#                                 (x - 1, y - 1),
#                                 (x, y -1),
#                                 (x + 1, y - 1),
#                                 (x - 1, y),
#                                 (x + 1, y),
#                                 (x - 1, y + 1),
#                                 (x, y + 1),
#                                 (x + 1, y + 1)
#                                 )
#                             ]
#                         )
#                 ) for x in range(len(board[y]))]
#             ) for y in range(len(board))
#         ]

while True: foo, bar, baz, globals()['board'] = None if globals().get('board') is None else [
        (
            sys.stdout.write('%.2d '%num),
            [sys.stdout.write('X' if cell else '_') for cell in row],
            sys.stdout.write('\n')
        ) for num, row in enumerate(board)
    ], time.sleep(1), sys.stdout.write('==============\n'), [
            [random.random() < 0.5 for i in range(boardSize[0])] for j in range(boardSize[1])
        ] if 'board' not in globals() else [
            map(
                lambda z: (z[1] in (2,3) and board[y][z[0]]) or z[1]==3,
                [
                    (
                        x,
                        sum(
                            [
                                int(
                                    y0 in range(len(board)) and x0 in range(len(board[y0])) and board[y0][x0]
                                ) for x0,y0 in (
                                    (x - 1, y - 1),
                                    (x, y -1),
                                    (x + 1, y - 1),
                                    (x - 1, y),
                                    (x + 1, y),
                                    (x - 1, y + 1),
                                    (x, y + 1),
                                    (x + 1, y + 1)
                                    )
                                ]
                            )
                    ) for x in range(len(board[y]))]
                ) for y in range(len(board))
            ]
<commit_msg>Remove dead code<commit_after>import sys, random, time

boardSize = (10,10)

while True: foo, bar, baz, globals()['board'] = None if globals().get('board') is None else [
        (
            sys.stdout.write('%.2d '%num),
            [sys.stdout.write('X' if cell else '_') for cell in row],
            sys.stdout.write('\n')
        ) for num, row in enumerate(board)
    ], time.sleep(1), sys.stdout.write('==============\n'), [
            [random.random() < 0.5 for i in range(boardSize[0])] for j in range(boardSize[1])
        ] if 'board' not in globals() else [
            map(
                lambda z: (z[1] in (2,3) and board[y][z[0]]) or z[1]==3,
                [
                    (
                        x,
                        sum(
                            [
                                int(
                                    y0 in range(len(board)) and x0 in range(len(board[y0])) and board[y0][x0]
                                ) for x0,y0 in (
                                    (x - 1, y - 1),
                                    (x, y -1),
                                    (x + 1, y - 1),
                                    (x - 1, y),
                                    (x + 1, y),
                                    (x - 1, y + 1),
                                    (x, y + 1),
                                    (x + 1, y + 1)
                                    )
                                ]
                            )
                    ) for x in range(len(board[y]))]
                ) for y in range(len(board))
            ]
<|endoftext|>


<commit_before>'''
LighteningCorrector
By Giancarlo Grasso
lightning.py
Copyright (c) 2017 Giancarlo Grasso (MIT License)
'''

import praw
from time import sleep
import time
import datetime
import re

text = '''Are you sure you didn't mean lightning? If you are talking about electrostatic discharge or Apple's connector, you mean "lightning".

lightning:

noun:

1. the occurrence of a natural electrical discharge of very short duration and high voltage between a cloud and the ground or within a cloud, accompanied by a bright flash and typically also thunder.
"A tremendous flash of lightning"

adjective:

1. very quick.
"A lightning cure for his hangover"

lighten:

verb:

1. to make lighter in weight.
"I am lightening the load on my truck"

2. to become lighter or less dark; brighten.
"The sky is lightening now that the storm has passed"

P.S. I'm only a bot; I reply to anyone that uses the word "lightening", even if they used it correctly. I apologise if you have used this word correctly.
'''

def foundWord(string):
    match = False
    substring = "lightening"
    false_matches = ["lightening up", "lightening the load"]
    result = re.search(r"\b" + re.escape(substring) + r"\b", string)
    while result:
        for f in false_matches:
            if string[result.start():].find(f) == 0:
                string = string[result.end():]
                result = re.search(r"\b" + re.escape(substring) + r"\b", string)
                break
        else:
            return True
    return False

reddit = praw.Reddit('lightning')
username = reddit.user.me().name
comment_queue = []
then = 0
print("Starting to trawl comments")
for comment in reddit.subreddit('all').stream.comments():
    if username == str(comment.author) and foundWord(comment.body.lower()):
        print("Adding comment",comment,"to queue")
        comment_queue.append(comment)
    if time.time() - then > 60:
        try:
            for c in comment_queue:
                age = datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(c.created_utc)
                if age.seconds < 3600:
                    c.reply(text)
                    comment_queue.remove(c)
                    print("Replied to",comment,"removing from queue")
                    then = time.time() - 40
                else:
                    print(comment,"is too old, removing from queue")
                    comment_queue.remove(c)
        except praw.exceptions.APIException as error:
            then = time.time()
            print("Couldn't comment")
            print(error)
            sleep(1)
<commit_msg>Debug<commit_after>'''
LighteningCorrector
By Giancarlo Grasso
lightning.py
Copyright (c) 2017 Giancarlo Grasso (MIT License)
'''

import praw
from time import sleep
import time
import datetime
import re

text = '''Are you sure you didn't mean lightning? If you are talking about electrostatic discharge or Apple's connector, you mean "lightning".

lightning:

noun:

1. the occurrence of a natural electrical discharge of very short duration and high voltage between a cloud and the ground or within a cloud, accompanied by a bright flash and typically also thunder.
"A tremendous flash of lightning"

adjective:

1. very quick.
"A lightning cure for his hangover"

lighten:

verb:

1. to make lighter in weight.
"I am lightening the load on my truck"

2. to become lighter or less dark; brighten.
"The sky is lightening now that the storm has passed"

P.S. I'm only a bot; I reply to anyone that uses the word "lightening", even if they used it correctly. I apologise if you have used this word correctly.
'''

def foundWord(string):
    match = False
    substring = "lightening"
    false_matches = ["lightening up", "lightening the load"]
    result = re.search(r"\b" + re.escape(substring) + r"\b", string)
    while result:
        for f in false_matches:
            if string[result.start():].find(f) == 0:
                string = string[result.end():]
                result = re.search(r"\b" + re.escape(substring) + r"\b", string)
                break
        else:
            return True
    return False

reddit = praw.Reddit('lightning')
username = reddit.user.me().name
comment_queue = []
then = 0
print("Starting to trawl comments")
for comment in reddit.subreddit('all').stream.comments():
    print(comment.author)
    if username == str(comment.author) and foundWord(comment.body.lower()):
        print("Adding comment",comment,"to queue")
        comment_queue.append(comment)
    if time.time() - then > 60:
        try:
            for c in comment_queue:
                age = datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(c.created_utc)
                if age.seconds < 3600:
                    c.reply(text)
                    comment_queue.remove(c)
                    print("Replied to",comment,"removing from queue")
                    then = time.time() - 40
                else:
                    print(comment,"is too old, removing from queue")
                    comment_queue.remove(c)
        except praw.exceptions.APIException as error:
            then = time.time()
            print("Couldn't comment")
            print(error)
            sleep(1)
<|endoftext|>


<commit_before>import numpy as np
from scipy import sparse as sp
from scipy.sparse import linalg

from sklearn.base import BaseEstimator, ClassifierMixin

#class PrimalSVM(BaseEstimator, ClassifierMixin):
class PrimalSVM():
    '''
    Solves linear SVM in primal, with use of Newton or Conjugate Gradient
    '''

    def __init__(self, l2reg=1.0, newton_iter=20 ):
        self.l2reg = l2reg
        self.newton_iter = newton_iter

    def fit(self, X, y, method=0):
        """Fit the model according to the given training data.
        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training vector, where n_samples in the number of samples and
            n_features is the number of features.
        y : array-like, shape = [n_samples]
            Target vector relative to X
        Returns
        -------
        self : object
            Returns self.
        """

        [n,d] = X.shape
        w = np.zeros(d+1)

        if method==0:
            self._solve_Newton()
        else:
            self._solve_CG()

    def _solve_Newton(self):
        """
        Solve the primal SVM problem with Newton method
        """
        pass

    def _solve_CG(self):
        pass

    def _obj_func(self,w,X,Y,out):
        """
        Computes primal value end gradient
        Parameters
        ----------
        w : {array-like} - hyperplane normal vector
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training vector, where n_samples in the number of samples and
            n_features is the number of features.
        Y : array-like, shape = [n_samples]
            Target vector relative to X
        out: loss function values
        Returns
        -------
        (obj,grad) : tuple, obj - function value, grad - gradient

        """

        l2reg = self.l2reg
        w0 = w
        w0[-1]=0
        obj = np.sum(out**2)/2+l2reg*w0.dot(w0)/2

        grad = l2reg*w0 - np.append( [np.dot(out*Y,X)], [np.sum(out*Y)])

        return (obj,grad)

    def predict(self, X):
        pass

    <commit_msg>write the main loop in Newton function<commit_after>import numpy as np
from scipy import sparse as sp
from scipy.sparse import linalg

from sklearn.base import BaseEstimator, ClassifierMixin

#class PrimalSVM(BaseEstimator, ClassifierMixin):
class PrimalSVM():
    '''
    Solves linear SVM in primal, with use of Newton or Conjugate Gradient
    '''

    def __init__(self, l2reg=1.0, newton_iter=20 ):
        self.l2reg = l2reg
        self.newton_iter = newton_iter

    def fit(self, X, Y, method=0):
        """Fit the model according to the given training data.
        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training vector, where n_samples in the number of samples and
            n_features is the number of features.
        Y : array-like, shape = [n_samples]
            Target vector relative to X
        method: 0 - Newton method (with full Hessian computation), 1 - Conjugate Gradient method
        Returns
        -------
        self : object
            Returns self.
        """

        if method==0:
            self._solve_Newton(X,Y)
        else:
            self._solve_CG()

    def _solve_Newton(self,X,Y):
        """
        Solve the primal SVM problem with Newton method
        """
        [n,d] = X.shape

        #we add one last component, which is b (bias)
        self.w = np.zeros(d+1)
        #helper variable for storing 1-Y*(np.dot(X,w))
        self.out = np.ones((n,1))

        l= self.l2reg
        #the number of alg. iteration
        iter=0

        while True:
            iter=iter+1
            if iter > self.newton_iter:
                print("Maximum {0} of Newton stpes reached, change newton_iter parameter or try larger lambda".format(iter))
                break

            obj, grad = self._obj_func(w,X,Y,out)

            #np.where retunrs a tuple, we take the first dim
            sv = np.where( self.out>0)[0]
            #grab the support vectors
            Xsv = self.X[sv,:]

            #reserve memory for hessian
            hess = np.zeros((d+1,d+1))
            #compute the hessin, add the first part with lambda
            hess= hess+ l* np.diag(np.append(np.ones((d,)), 0))

            #add the second part with dot products between x_i
            hess[0:d,0:d]= Xsv.T.dot(Xsv)
            hess[-1,0:d] = Xsv.sum(axis=0)
            hess[0:d, -1] = Xsv.sum(axis=0)
            hess[-1,-1] = len(sv[0])

            #compute step vector
            step = -hess\grad

    def _solve_CG(self):
        pass

    def _obj_func(self,w,X,Y,out):
        """
        Computes primal value end gradient
        Parameters
        ----------
        w : {array-like} - hyperplane normal vector
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training vector, where n_samples in the number of samples and
            n_features is the number of features.
        Y : array-like, shape = [n_samples]
            Target vector relative to X
        out: loss function values
        Returns
        -------
        (obj,grad) : tuple, obj - function value, grad - gradient

        """

        l2reg = self.l2reg
        w0 = w
        w0[-1]=0
        obj = np.sum(out**2)/2+l2reg*w0.dot(w0)/2

        grad = l2reg*w0 - np.append( [np.dot(out*Y,X)], [np.sum(out*Y)])

        return (obj,grad)

    def predict(self, X):
        pass

    <|endoftext|>


<commit_before>from django.conf import settings  # noqa
from django.utils.translation import ugettext_lazy as _

from appconf import AppConf

class ResponsiveAppConf(AppConf):
    """
    While there are several different items we can query on,
    the ones used for django-responsive2 are min-width and max-width

    min_width -- Rules applied for any device width over the value defined in the query.
    max_width -- Rules applied for any device width under the value defined in the query.

    Usage
    ------
        {% load 'responsive' %}

        {% renderblockif 'small' 'medium' %}
            [...]
        {% endrenderblockif %}

    """

    COOKIE_NAME = 'clientinfo'
    COOKIE_AGE = 365   # days

    # prefix for classes for showing and hiding content by device via media query
    CSS_VISIBLE = 'visible-'
    CSS_HIDDEN = 'hidden-'

    # Borrowed from ZURB Foundation framework.
    # See http://foundation.zurb.com/docs/media-queries.html
    BREAKPOINTS = {
        'small': {
            'verbose_name': _('Small screens'),
            'min_width': None,
            'max_width': 640,
        },
        'medium': {
            'verbose_name': _('Medium screens'),
            'min_width': 641,
            'max_width': 1024,
        },
        'large': {
            'verbose_name': _('Large screens'),
            'min_width': 1025,
            'max_width': 1440,
        },
        'xlarge': {
            'verbose_name': _('XLarge screens'),
            'min_width': 1441,
            'max_width': 1920,
        },
        'xxlarge': {
            'verbose_name': _('XXLarge screens'),
            'min_width': 1921,
            'max_width': None,
        }
    }

    class Meta:
        prefix = 'responsive'
<commit_msg>Update conf.py<commit_after>from django.conf import settings  # noqa
from django.utils.translation import ugettext_lazy as _

from appconf import AppConf

class ResponsiveAppConf(AppConf):
    """
    While there are several different items we can query on,
    the ones used for django-responsive2 are min-width and max-width

    min_width -- Rules applied for any device width over the value defined in the config.
    max_width -- Rules applied for any device width under the value defined in the config.
    pixel_ratio -- Rules applied for any device with devicePixelRatio defined in the config.

    Usage
    ------
        {% load 'responsive' %}

        {% renderblockif 'small' 'medium' %}
            [...]
        {% endrenderblockif %}

    """

    COOKIE_NAME = 'clientinfo'
    COOKIE_AGE = 365  # days

    # prefix for classes for showing and hiding content by device via media query
    CSS_VISIBLE_PREFIX = 'visible-'
    CSS_HIDDEN_PREFIX = 'hidden-'

    # Borrowed from ZURB Foundation framework.
    # See http://foundation.zurb.com/docs/media-queries.html
    MEDIA_QUERIES = {
        'small': {
            'verbose_name': _('Small screens'),
            'min_width': None,
            'max_width': 640,
        },
        'medium': {
            'verbose_name': _('Medium screens'),
            'min_width': 641,
            'max_width': 1024,
        },
        'large': {
            'verbose_name': _('Large screens'),
            'min_width': 1025,
            'max_width': 1440,
        },
        'xlarge': {
            'verbose_name': _('XLarge screens'),
            'min_width': 1441,
            'max_width': 1920,
        },
        'xxlarge': {
            'verbose_name': _('XXLarge screens'),
            'min_width': 1921,
            'max_width': None,
        }
    }

    class Meta:
        prefix = 'responsive'
<|endoftext|>


<commit_before># -*- coding: utf8 -*-
# This file is part of PyBossa-links.
#
# Copyright (C) 2013 Daniel Lombraña González
#
# PyBossa-links is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa-links is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa-links. If not, see <http://www.gnu.org/licenses/>.
import json
from flask import request, Response
from core import app
from utils import crossdomain, save_url
from model import Project, Link

@app.route("/", methods=['GET', 'POST'])
@crossdomain(origin='*')
def index():
    if request.method == 'GET':
        links = Link.query.all()
        projects = Project.query.all()
        return "There are %s stored URLs and %s projects" % (len(links), len(projects))
    else:
        # Check first if the user is allowed to post or if the user has reached the rate limit
        pybossa = dict(endpoint=app.config.get('PYBOSSA_ENDPOINT'),
                       api_key=app.config.get('PYBOSSA_API_KEY'))
        return save_url(request.remote_addr, request.form, pybossa)

@app.route('/project/')
@crossdomain(origin='*')
def project():
    if (request.args.get('slug')):
        project = Project.query.filter_by(slug=request.args.get('slug')).first_or_404()
        return Response(json.dumps(project.dictize()), mimetype="application/json",
                        status=200)
    else:
        projects = Project.query.all()
        return Response(json.dumps([p.dictize() for p in projects]),
                        mimetype='application/json',
                        status=200)

if __name__ == "__main__":
    app.run(host=app.config.get('HOST'))
<commit_msg>Refactor<commit_after># -*- coding: utf8 -*-
# This file is part of PyBossa-links.
#
# Copyright (C) 2013 Daniel Lombraña González
#
# PyBossa-links is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa-links is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa-links. If not, see <http://www.gnu.org/licenses/>.
import json
from flask import request, Response
from core import app
from utils import crossdomain, save_url
from model import Project, Link

@app.route("/", methods=['GET', 'POST'])
@crossdomain(origin='*')
def index():
    if request.method == 'GET':
        links = Link.query.all()
        projects = Project.query.all()
        return "There are %s stored URLs and %s projects" % (len(links), len(projects))
    else:
        # Check first if the user is allowed to post or if the user has reached the rate limit
        pybossa = dict(endpoint=app.config.get('PYBOSSA_ENDPOINT'),
                       api_key=app.config.get('PYBOSSA_API_KEY'))
        return save_url(request.remote_addr, request.form, pybossa,
                        hour=app.config.get('HOUR'),
                        max_hits=app.config.get('MAX_HITS'))

@app.route('/project/')
@crossdomain(origin='*')
def project():
    if (request.args.get('slug')):
        project = Project.query.filter_by(slug=request.args.get('slug')).first_or_404()
        return Response(json.dumps(project.dictize()), mimetype="application/json",
                        status=200)
    else:
        projects = Project.query.all()
        return Response(json.dumps([p.dictize() for p in projects]),
                        mimetype='application/json',
                        status=200)

if __name__ == "__main__":
    app.run(host=app.config.get('HOST'))
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = "Brian Lehman, Scott Hendrickson"

import sys
import re
import codecs

reload(sys)
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
sys.stdin = codecs.getreader('utf-8')(sys.stdin)

import math
import pprint
import json
import fiona
from shapely.geometry import Point, shape, Polygon, box
from collections import defaultdict
import argparse

########################
# functions

def tree(): return defaultdict(tree)

def grid_finder(x,y):
    return (int((math.floor(x)-grid_boundaries[0])/delta)
           ,int((math.floor(y)-grid_boundaries[1])/delta))

def topic_args():
    parser = argparse.ArgumentParser(description="Reverse geo coder returns location info given a set of lon,lat")
    parser.add_argument("file_name"
            , metavar= "file_name"
            , nargs="?"
            , default=[]
            , help="Input file name (optional).")
    parser.add_argument("-b"
            , "--bounding-box"
            , dest="grid_boundaries"
            , default="-185,15,-65,70"
            , help="bounding box for region to include")
    parser.add_argument("-d"
            , "--delta"
            , dest="delta"
            , default=5
            , help="the number of degrees between grid coords (default 5)")
    parser.add_argument("-s"
            , "--shape-file-path"
            , dest="shape_file_path"
            , default="data/tl_2013_us_county.shp"
            , help="shape file path (default: data/tl_2013_us_county.shp)")
    parser.add_argument("-t"
            , "--tweet-input"
            , dest="tweet_input"
            , default=False, action="store_true"
            , help="For input being tweets instead of coordinates")
    return parser

if __name__ == '__main__':
    options = topic_args().parse_args()
    grid_boundaries=[int(item) for item in options.grid_boundaries.split(",")]
    #grid_boundaries=(-185,15,-65,70) # upright edge is plus delta (lower 48 states)
    delta=int(options.delta)
    grid={(i,j):{}
            for i in range((grid_boundaries[2]-grid_boundaries[0])/delta)
            for j in range((grid_boundaries[3]-grid_boundaries[1])/delta) }

    with fiona.open(options.shape_file_path) as fc:
        print >>sys.stderr, fc.driver,"###",fc.schema,"###", len(fc),"###",fc.crs
        print >> sys.stderr,fc.schema
        print >>sys.stderr, "Number of records:", len(fc)
        print >>sys.stderr, "Bounds of all records:", fc.bounds
        print >>sys.stderr, "Bounds applied:",grid_boundaries
        print >> sys.stderr,"######## indexing shapes ########"
        print >> sys.stderr,"shapes complete:"
        c=0
        for feature in fc:
            c+=1
            GEOID=str(feature['properties']['GEOID'])
            NAME=feature['properties']['NAME']
            INTPTLON=float(feature['properties']['INTPTLON'])
            INTPTLAT=float(feature['properties']['INTPTLAT'])
            shp=shape(feature['geometry']) # list of coordinates of geometric shape
            bb=box(*shp.bounds) #box(minx,miny,maxx,maxy)) creates one boxlike shape to rule them all
            for i,j in grid:
                grid_box=box(i*delta+grid_boundaries[0]
                            ,j*delta+grid_boundaries[1]
                            ,(i+1)*delta+grid_boundaries[0]
                            ,(j+1)*delta+grid_boundaries[1] )
                if grid_box.intersects(bb): #http://toblerity.org/shapely/manual.html#object.intersects
                    grid[(i,j)][bb]=(shp,GEOID,NAME,INTPTLON,INTPTLAT) # (county shape, countyID)
            if c%100==0:
                print >> sys.stderr, c
    counter=0
    in_grid_not_in_county=0
    grid_counter=0
    print >> sys.stderr,"######## indexing shapes ########"
    for line in sys.stdin:
        #( lng, lat ) = coord #NOTE:the input file must contain (lng,lat)
        values=line.replace("(","").replace(")","").replace("[","").replace("]","").strip().split(",")
        lng = float(values[0])
        lat = float(values[1])
        point = Point(float(lng), float(lat))
        coords=grid_finder(lng,lat)
        found=False
        if coords not in grid:
            counter+=1
            print >> sys.stderr,"not in grid:{},not in county:{},found{}".format(counter,in_grid_not_in_county,grid_counter)
            print >> sys.stderr,"{},{}: not in grid".format(lng,lat)
            continue
        for box in grid[coords]:
            if box.contains(point):
                if grid[coords][box][0].contains(point):
                    e=tree()
                    found=True
                    grid_counter+=1
                    e["coords"]=(lng,lat)
                    e["GEOID"]=grid[coords][box][1]
                    e["centroid"]=(grid[coords][box][3],grid[coords][box][4])
                    e["county"]=grid[coords][box][2]
                    print json.dumps(e)
                    break #point found, no need to continue searching
        if not found:
            in_grid_not_in_county+=1
    print >> sys.stderr,"######## DONE  ########"
    print >> sys.stderr, "{} points outside of grid".format(counter)
    print >> sys.stderr, "{} points in grid but not in a county".format(in_grid_not_in_county)
    print >> sys.stderr, "{} points in grid and in county".format(grid_counter)

<commit_msg>added -g flag to rev_geo.py to save/use serialized grid<commit_after>#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = "Brian Lehman, Scott Hendrickson"

import sys
import re
import codecs

reload(sys)
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
sys.stdin = codecs.getreader('utf-8')(sys.stdin)

import math
import pprint
import json
import fiona
from shapely.geometry import Point, shape, Polygon, box
from collections import defaultdict
import argparse
import os
import pickle

########################
# functions

def tree(): return defaultdict(tree)

def grid_finder(x,y):
    return (int((math.floor(x)-grid_boundaries[0])/delta)
           ,int((math.floor(y)-grid_boundaries[1])/delta))

def topic_args():
    parser = argparse.ArgumentParser(description="Reverse geo coder returns location info given a set of lon,lat")
    parser.add_argument("file_name"
            , metavar= "file_name"
            , nargs="?"
            , default=[]
            , help="Input file name (optional).")
    parser.add_argument("-b"
            , "--bounding-box"
            , dest="grid_boundaries"
            , default="-185,15,-65,70"
            , help="Set bounding box for region to include (defulat is [-185,15,-65,70])")
    parser.add_argument("-d"
            , "--delta"
            , dest="delta"
            , default=5
            , help="Set the number of degrees between grid coords (default 5)")
    parser.add_argument("-g"
            , "--use-saved-grid"
            , dest="use_saved_grid"
            , default=False
            , action="store_true"
            , help="Save grid or use previously saved version in data/grid.json")
    parser.add_argument("-s"
            , "--shape-file-path"
            , dest="shape_file_path"
            , default="data/tl_2013_us_county.shp"
            , help="Set shapefile path (default: data/tl_2013_us_county.shp)")
    parser.add_argument("-t"
            , "--tweet-input"
            , dest="tweet_input"
            , default=False
            , action="store_true"
            , help="Set input as tweet payload instead of coordinates (in progress)")
    return parser

def build_grid():
    #grid_boundaries=(-185,15,-65,70) # upright edge is plus delta (lower 48 states)
    grid={(i,j):{}
            for i in range((grid_boundaries[2]-grid_boundaries[0])/delta)
            for j in range((grid_boundaries[3]-grid_boundaries[1])/delta) }

    with fiona.open(options.shape_file_path) as fc:
        print >>sys.stderr, fc.driver,"###",fc.schema,"###", len(fc),"###",fc.crs
        print >> sys.stderr,fc.schema
        print >>sys.stderr, "Number of records:", len(fc)
        print >>sys.stderr, "Bounds of all records:", fc.bounds
        print >>sys.stderr, "Bounds applied:",grid_boundaries
        print >> sys.stderr,"######## indexing shapes to grid ########"
        print >> sys.stderr,"shapes complete:"
        c=0
        for feature in fc:
            c+=1
            GEOID=str(feature['properties']['GEOID'])
            NAME=feature['properties']['NAME']
            INTPTLON=float(feature['properties']['INTPTLON'])
            INTPTLAT=float(feature['properties']['INTPTLAT'])
            shp=shape(feature['geometry']) # list of coordinates of geometric shape
            bb=box(*shp.bounds) #box(minx,miny,maxx,maxy)) creates one boxlike shape to rule them all
            for i,j in grid:
                grid_box=box(i*delta+grid_boundaries[0]
                            ,j*delta+grid_boundaries[1]
                            ,(i+1)*delta+grid_boundaries[0]
                            ,(j+1)*delta+grid_boundaries[1] )
                if grid_box.intersects(bb): #http://toblerity.org/shapely/manual.html#object.intersects
                    grid[(i,j)][bb]=(shp,GEOID,NAME,INTPTLON,INTPTLAT) # (county shape, countyID)
            if c%100==0:
                print >> sys.stderr, c
    return grid

if __name__ == '__main__':
    options = topic_args().parse_args()
    grid_boundaries=[int(item) for item in options.grid_boundaries.split(",")]
    delta=int(options.delta)
    if not options.use_saved_grid:
        grid=build_grid()
    else:
        if not os.path.isfile("./data/grid.json"):
            print >>sys.stderr, "file ./data/grid.json does not exist"
            grid=build_grid()
            if not os.path.exists("./data"):
                os.makedirs("./data")
            print >>sys.stderr, "saving file ./data/grid.json"
            with open("./data/grid.json","wb") as g:
                pickle.dump(grid,g)
        else:
            print >>sys.stderr, "using ./data/grid.json"
            grid=pickle.load(open("./data/grid.json"))
    counter=0
    in_grid_not_in_county=0
    grid_counter=0
    print >> sys.stderr,"######## locating geo coords in grid ########"
    for line in sys.stdin:
        #( lng, lat ) = coord #NOTE:the input file must contain (lng,lat)
        values=line.replace("(","").replace(")","").replace("[","").replace("]","").strip().split(",")
        lng = float(values[0])
        lat = float(values[1])
        point = Point(float(lng), float(lat))
        coords=grid_finder(lng,lat)
        found=False
        if coords not in grid:
            counter+=1
            print >> sys.stderr,"not in grid:{},not in county:{},found{}".format(counter,in_grid_not_in_county,grid_counter)
            print >> sys.stderr,"{},{}: not in grid".format(lng,lat)
            continue
        for box in grid[coords]:
            if box.contains(point):
                if grid[coords][box][0].contains(point):
                    e=tree()
                    found=True
                    grid_counter+=1
                    e["coords"]=(lng,lat)
                    e["GEOID"]=grid[coords][box][1]
                    e["centroid"]=(grid[coords][box][3],grid[coords][box][4])
                    e["county"]=grid[coords][box][2]
                    print json.dumps(e)
                    break #point found, no need to continue searching
        if not found:
            in_grid_not_in_county+=1
    print >> sys.stderr,"######## DONE  ########"
    print >> sys.stderr, "{} points outside of grid".format(counter)
    print >> sys.stderr, "{} points in grid but not in a county".format(in_grid_not_in_county)
    print >> sys.stderr, "{} points in grid and in county".format(grid_counter)

<|endoftext|>


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

#
# Copyright (C) 2014, Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import getpass
import json
import logging
import optparse
import sys

import paramiko

def get_from_gerrit(query, gerrit_user, ssh_key, server):
    changes = []

    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    while True:
        try:
            client.connect(server, port=29418,
                           key_filename=ssh_key, username=gerrit_user)
        except paramiko.SSHException:
            # retry with allow_agent=False if initial connect fails.
            client.connect(server, port=29418,
                           key_filename=ssh_key, username=gerrit_user,
                           allow_agent=False)
        cmd = ('gerrit query --all-approvals --patch-sets '
               '--format JSON %s' % query)
        if changes:
            cmd += ' resume_sortkey:%s' % changes[-2]['sortKey']
        stdin, stdout, stderr = client.exec_command(cmd)
        for l in stdout:
            changes += [json.loads(l)]
        if changes[-1]['rowCount'] == 0:
            break

    return changes

def projects_q(projects):
    return ('(' +
            ' OR '.join(['project:' + p for p in projects]) +
            ')')

def get_changes(project, all_from, gerrit_user, ssh_key, server):
    changes = []

    query = '%s status:open reviewer:%s' % (
            ('project:%s' % project) if project else '', gerrit_user)
    changes.extend(get_from_gerrit(query, gerrit_user, ssh_key, server))

    if all_from:
        query = '%s status:open' % (projects_q(all_from))
        changes.extend(get_from_gerrit(query, gerrit_user, ssh_key, server))

    return changes

def patch_set_approved(patch_set):
    approvals = patch_set.get('approvals', [])
    for review in approvals:
        if review['type'] == 'APRV':
            return True
    return False

def print_change(change):
    print ' --> %s [%s %s (%s)]' % (change['subject'], change['project'],
                                    change['branch'], change.get('topic'))
    print ' ------> %s' % change['url']

def print_review_todo(options):
    all_from_projects = options.all_from.split(',')
    if len(all_from_projects) == 1 and all_from_projects[0] == '':
        all_from_projects = []

    changes = get_changes(options.project, all_from_projects, options.user,
                          options.key, options.server)

    backburner = {}
    todo = {}

    for change in changes:
        if 'rowCount' in change:
            continue
        if change['status'] != 'NEW':
            # Filter out WORKINPROGRESS
            continue
        if change['owner']['username'] == options.user:
            # Ignore your own changes
            continue
        latest_patch = change['patchSets'][-1]
        if patch_set_approved(latest_patch):
            # Ignore patches already approved and just waiting to merge
            continue

        reviewed = False
        for patch in change['patchSets']:
            for review in patch.get('approvals', []):
                if review['by'].get('username', 'unknown') == options.user:
                    reviewed = True
                    break
            if reviewed:
                break
        if not reviewed and change['project'] not in all_from_projects:
            # Ignore changes where this user has not actually reviewed it.
            # This catches the case where someone else adds you to a review.
            # Just anyone shouldn't be able to put stuff on your todo list.
            # The exception is projects you have specifically listed to see all
            # changes from that you haven't reviewed.
            continue

        waiting_for_review = True
        approvals = latest_patch.get('approvals', [])
        approvals.sort(key=lambda a: a['grantedOn'])
        already_reviewed_latest = False
        for review in approvals:
            if review['type'] not in ('CRVW', 'VRIF',
                                      'Code-Review', 'Verified'):
                continue
            if review['by']['username'] == options.user:
                already_reviewed_latest = True
                break
            if review['value'] in ('-1', '-2'):
                waiting_for_review = False
                break
        if not waiting_for_review or already_reviewed_latest:
            backburner[change['number']] = change
        else:
            todo[change['number']] = change

    if not todo:
        print 'You\'re caught up on reviews! \o/'
    else:
        print 'Reviews to-do:'
        for change in todo.itervalues():
            print_change(change)

    if options.full:
        print
        print 'Active reviews that do not need attention:'
        for change in backburner.itervalues():
            print_change(change)

def main(argv=None):
    if argv is None:
        argv = sys.argv

    optparser = optparse.OptionParser()
    optparser.add_option(
        '-p', '--project', default='',
        help='Only show reviews for a specific project')
    optparser.add_option(
        '-a', '--all-from', default='',
        help='A comma separated list of projects. List all changes from '
             'these projects that you have not voted on. Useful for '
             'smaller projects you want to see all changes for.')
    optparser.add_option(
        '-u', '--user', default=getpass.getuser(), help='gerrit user')
    optparser.add_option(
        '-k', '--key', default=None, help='ssh key for gerrit')
    optparser.add_option(
        '--server', default='review.openstack.org',
        help='Gerrit server to connect to')
    optparser.add_option(
        '-f', '--full', action='store_true',
        help='Show full output, including list of reviews that do not '
             'currently need any attention.')

    options, args = optparser.parse_args()

    logging.basicConfig(level=logging.ERROR)

    print_review_todo(options)
<commit_msg>Sort todo list by project<commit_after># -*- coding: utf-8 -*-

#
# Copyright (C) 2014, Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import getpass
import json
import logging
import optparse
import sys

import paramiko

def get_from_gerrit(query, gerrit_user, ssh_key, server):
    changes = []

    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    while True:
        try:
            client.connect(server, port=29418,
                           key_filename=ssh_key, username=gerrit_user)
        except paramiko.SSHException:
            # retry with allow_agent=False if initial connect fails.
            client.connect(server, port=29418,
                           key_filename=ssh_key, username=gerrit_user,
                           allow_agent=False)
        cmd = ('gerrit query --all-approvals --patch-sets '
               '--format JSON %s' % query)
        if changes:
            cmd += ' resume_sortkey:%s' % changes[-2]['sortKey']
        stdin, stdout, stderr = client.exec_command(cmd)
        for l in stdout:
            changes += [json.loads(l)]
        if changes[-1]['rowCount'] == 0:
            break

    return changes

def projects_q(projects):
    return ('(' +
            ' OR '.join(['project:' + p for p in projects]) +
            ')')

def get_changes(project, all_from, gerrit_user, ssh_key, server):
    changes = []

    query = '%s status:open reviewer:%s' % (
            ('project:%s' % project) if project else '', gerrit_user)
    changes.extend(get_from_gerrit(query, gerrit_user, ssh_key, server))

    if all_from:
        query = '%s status:open' % (projects_q(all_from))
        changes.extend(get_from_gerrit(query, gerrit_user, ssh_key, server))

    return changes

def patch_set_approved(patch_set):
    approvals = patch_set.get('approvals', [])
    for review in approvals:
        if review['type'] == 'APRV':
            return True
    return False

def print_change(change):
    print ' --> %s [%s (%s)]' % (change['subject'],
                                 change['branch'],
                                 change.get('topic'))
    print ' ------> %s' % change['url']

def print_review_todo(options):
    all_from_projects = options.all_from.split(',')
    if len(all_from_projects) == 1 and all_from_projects[0] == '':
        all_from_projects = []

    changes = get_changes(options.project, all_from_projects, options.user,
                          options.key, options.server)

    backburner = {}
    todo = {}

    for change in changes:
        if 'rowCount' in change:
            continue
        if change['status'] != 'NEW':
            # Filter out WORKINPROGRESS
            continue
        if change['owner']['username'] == options.user:
            # Ignore your own changes
            continue
        latest_patch = change['patchSets'][-1]
        if patch_set_approved(latest_patch):
            # Ignore patches already approved and just waiting to merge
            continue

        reviewed = False
        for patch in change['patchSets']:
            for review in patch.get('approvals', []):
                if review['by'].get('username', 'unknown') == options.user:
                    reviewed = True
                    break
            if reviewed:
                break
        if not reviewed and change['project'] not in all_from_projects:
            # Ignore changes where this user has not actually reviewed it.
            # This catches the case where someone else adds you to a review.
            # Just anyone shouldn't be able to put stuff on your todo list.
            # The exception is projects you have specifically listed to see all
            # changes from that you haven't reviewed.
            continue

        waiting_for_review = True
        approvals = latest_patch.get('approvals', [])
        approvals.sort(key=lambda a: a['grantedOn'])
        already_reviewed_latest = False
        for review in approvals:
            if review['type'] not in ('CRVW', 'VRIF',
                                      'Code-Review', 'Verified'):
                continue
            if review['by']['username'] == options.user:
                already_reviewed_latest = True
                break
            if review['value'] in ('-1', '-2'):
                waiting_for_review = False
                break
        if not waiting_for_review or already_reviewed_latest:
            backburner[change['number']] = change
        else:
            todo.setdefault(change['project'], {})
            todo[change['project']][change['number']] = change

    if not todo:
        print 'You\'re caught up on reviews! \o/'
    else:
        print 'Reviews to-do:\n'
        for project, changes in todo.iteritems():
            print 'Project: %s' % project
            for change in changes.itervalues():
                print_change(change)
            print

    if options.full:
        print
        print 'Active reviews that do not need attention:'
        for change in backburner.itervalues():
            print_change(change)

def main(argv=None):
    if argv is None:
        argv = sys.argv

    optparser = optparse.OptionParser()
    optparser.add_option(
        '-p', '--project', default='',
        help='Only show reviews for a specific project')
    optparser.add_option(
        '-a', '--all-from', default='',
        help='A comma separated list of projects. List all changes from '
             'these projects that you have not voted on. Useful for '
             'smaller projects you want to see all changes for.')
    optparser.add_option(
        '-u', '--user', default=getpass.getuser(), help='gerrit user')
    optparser.add_option(
        '-k', '--key', default=None, help='ssh key for gerrit')
    optparser.add_option(
        '--server', default='review.openstack.org',
        help='Gerrit server to connect to')
    optparser.add_option(
        '-f', '--full', action='store_true',
        help='Show full output, including list of reviews that do not '
             'currently need any attention.')

    options, args = optparser.parse_args()

    logging.basicConfig(level=logging.ERROR)

    print_review_todo(options)
<|endoftext|>


<commit_before>#
# util.py
# Part of SublimeLinter3, a code checking framework for Sublime Text 3
#
# Written by Ryan Hileman and Aparajita Fishman
#
# Project: https://github.com/SublimeLinter/SublimeLinter3
# License: MIT
#

from functools import lru_cache
import os
import re
import shutil
import tempfile
import subprocess

INLINE_OPTIONS_RE = re.compile(r'.*?\[SublimeLinter[ ]+(.+?)\]')
INLINE_OPTION_RE = re.compile(r'([\w\-]+)\s*:\s*(.+?)\s*(?:,|$)')

def merge_user_settings(settings):
    '''Merge the default linter settings with the user's settings.'''
    default = settings.get('default') or {}
    user = settings.get('user') or {}

    if user:
        linters = default.pop('linters', {})
        user_linters = user.get('linters', {})

        for name, data in user_linters.items():
            if name in linters:
                linters[name].update(data)
            else:
                linters[name] = data

        default['linters'] = linters

        user.pop('linters', None)
        default.update(user)

    return default

def climb(top):
    right = True

    while right:
        top, right = os.path.split(top)
        yield top

@lru_cache()
def find(top, name, parent=False):
    for d in climb(top):
        target = os.path.join(d, name)

        if os.path.exists(target):
            if parent:
                return d

            return target

def extract_path(cmd, delim=':'):
    path = popen(cmd, os.environ).communicate()[0].decode()
    path = path.split('__SUBL__', 1)[1].strip('\r\n')
    return ':'.join(path.split(delim))

def find_path(env):
    # find PATH using shell --login
    if 'SHELL' in env:
        shell_path = env['SHELL']
        shell = os.path.basename(shell_path)

        if shell in ('bash', 'zsh'):
            return extract_path(
                (shell_path, '--login', '-c', 'echo __SUBL__$PATH')
            )
        elif shell == 'fish':
            return extract_path(
                (shell_path, '--login', '-c', 'echo __SUBL__; for p in $PATH; echo $p; end'),
                '\n'
            )

    # guess PATH if we haven't returned yet
    split = env['PATH'].split(':')
    p = env['PATH']

    for path in (
        '/usr/bin', '/usr/local/bin',
        '/usr/local/php/bin', '/usr/local/php5/bin'
    ):
        if not path in split:
            p += (':' + path)

    return p

@lru_cache()
def create_environment():
    if os.name == 'posix':
        os.environ['PATH'] = find_path(os.environ)

    return os.environ

def can_exec(fpath):
    return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

@lru_cache()
def which(cmd):
    env = create_environment()

    for base in env.get('PATH', '').split(os.pathsep):
        path = os.path.join(base, cmd)

        if can_exec(path):
            return path

    return None

def touch(path):
    with open(path, 'a'):
        os.utime(path, None)

def inline_options(code):
    options = {}
    m = INLINE_OPTIONS_RE.match(code)

    if m:
        for option in INLINE_OPTION_RE.findall(m.group(1)):
            options[option[0]] = option[1]

    return options

# popen methods

def combine_output(out, sep=''):
    return sep.join((
        (out[0].decode('utf8') or ''),
        (out[1].decode('utf8') or ''),
    ))

def communicate(cmd, code):
    code = code.encode('utf8')
    out = popen(cmd)

    if out is not None:
        out = out.communicate(code)
        return combine_output(out)
    else:
        return ''

def tmpfile(cmd, code, suffix=''):
    if isinstance(cmd, str):
        cmd = cmd,

    f = tempfile.NamedTemporaryFile(suffix=suffix)
    f.write(code.encode('utf8'))
    f.flush()

    cmd = tuple(cmd) + (f.name,)
    out = popen(cmd)

    if out:
        out = out.communicate()
        return combine_output(out)
    else:
        return ''

def tmpdir(cmd, files, filename, code):
    filename = os.path.split(filename)[1]
    d = tempfile.mkdtemp()

    for f in files:
        try:
            os.makedirs(os.path.join(d, os.path.split(f)[0]))
        except:
            pass

        target = os.path.join(d, f)

        if os.path.split(target)[1] == filename:
            # source file hasn't been saved since change, so update it from our live buffer
            f = open(target, 'wb')
            f.write(code)
            f.close()
        else:
            shutil.copyfile(f, target)

    os.chdir(d)
    out = popen(cmd)

    if out:
        out = out.communicate()
        out = combine_output(out, '\n')

        # filter results from build to just this filename
        # no guarantee all languages are as nice about this as Go
        # may need to improve later or just defer to communicate()
        out = '\n'.join([
            line for line in out.split('\n') if filename in line.split(':', 1)[0]
        ])
    else:
        out = ''

    shutil.rmtree(d, True)
    return out

def popen(cmd, env=None):
    import lint.persist as persist

    if isinstance(cmd, str):
        cmd = cmd,

    info = None

    if os.name == 'nt':
        info = subprocess.STARTUPINFO()
        info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        info.wShowWindow = subprocess.SW_HIDE

    if env is None:
        env = create_environment()

    try:
        return subprocess.Popen(
            cmd, stdin=subprocess.PIPE,
            stdout=subprocess.PIPE, stderr=subprocess.PIPE,
            startupinfo=info, env=env)
    except OSError as err:
        persist.debug('error launching', repr(cmd))
        persist.debug('error was:', err.strerror)
        persist.debug('environment:', env)
<commit_msg>Renamed method to better describe its intention, changed import to be relative<commit_after>#
# util.py
# Part of SublimeLinter3, a code checking framework for Sublime Text 3
#
# Written by Ryan Hileman and Aparajita Fishman
#
# Project: https://github.com/SublimeLinter/SublimeLinter3
# License: MIT
#

from functools import lru_cache
import os
import re
import shutil
import tempfile
import subprocess

INLINE_OPTIONS_RE = re.compile(r'.*?\[SublimeLinter[ ]+(.+?)\]')
INLINE_OPTION_RE = re.compile(r'([\w\-]+)\s*:\s*(.+?)\s*(?:,|$)')

def merge_user_settings(settings):
    '''Merge the default linter settings with the user's settings.'''
    default = settings.get('default') or {}
    user = settings.get('user') or {}

    if user:
        linters = default.pop('linters', {})
        user_linters = user.get('linters', {})

        for name, data in user_linters.items():
            if name in linters:
                linters[name].update(data)
            else:
                linters[name] = data

        default['linters'] = linters

        user.pop('linters', None)
        default.update(user)

    return default

def climb(top):
    right = True

    while right:
        top, right = os.path.split(top)
        yield top

@lru_cache()
def find_dir(top, name, parent=False):
    for d in climb(top):
        target = os.path.join(d, name)

        if os.path.exists(target):
            if parent:
                return d

            return target

def extract_path(cmd, delim=':'):
    path = popen(cmd, os.environ).communicate()[0].decode()
    path = path.split('__SUBL__', 1)[1].strip('\r\n')
    return ':'.join(path.split(delim))

def find_path(env):
    # find PATH using shell --login
    if 'SHELL' in env:
        shell_path = env['SHELL']
        shell = os.path.basename(shell_path)

        if shell in ('bash', 'zsh'):
            return extract_path(
                (shell_path, '--login', '-c', 'echo __SUBL__$PATH')
            )
        elif shell == 'fish':
            return extract_path(
                (shell_path, '--login', '-c', 'echo __SUBL__; for p in $PATH; echo $p; end'),
                '\n'
            )

    # guess PATH if we haven't returned yet
    split = env['PATH'].split(':')
    p = env['PATH']

    for path in (
        '/usr/bin', '/usr/local/bin',
        '/usr/local/php/bin', '/usr/local/php5/bin'
    ):
        if not path in split:
            p += (':' + path)

    return p

@lru_cache()
def create_environment():
    if os.name == 'posix':
        os.environ['PATH'] = find_path(os.environ)

    return os.environ

def can_exec(fpath):
    return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

@lru_cache()
def which(cmd):
    env = create_environment()

    for base in env.get('PATH', '').split(os.pathsep):
        path = os.path.join(base, cmd)

        if can_exec(path):
            return path

    return None

def touch(path):
    with open(path, 'a'):
        os.utime(path, None)

def inline_options(code):
    options = {}
    m = INLINE_OPTIONS_RE.match(code)

    if m:
        for option in INLINE_OPTION_RE.findall(m.group(1)):
            options[option[0]] = option[1]

    return options

# popen methods

def combine_output(out, sep=''):
    return sep.join((
        (out[0].decode('utf8') or ''),
        (out[1].decode('utf8') or ''),
    ))

def communicate(cmd, code):
    code = code.encode('utf8')
    out = popen(cmd)

    if out is not None:
        out = out.communicate(code)
        return combine_output(out)
    else:
        return ''

def tmpfile(cmd, code, suffix=''):
    if isinstance(cmd, str):
        cmd = cmd,

    f = tempfile.NamedTemporaryFile(suffix=suffix)
    f.write(code.encode('utf8'))
    f.flush()

    cmd = tuple(cmd) + (f.name,)
    out = popen(cmd)

    if out:
        out = out.communicate()
        return combine_output(out)
    else:
        return ''

def tmpdir(cmd, files, filename, code):
    filename = os.path.split(filename)[1]
    d = tempfile.mkdtemp()

    for f in files:
        try:
            os.makedirs(os.path.join(d, os.path.split(f)[0]))
        except:
            pass

        target = os.path.join(d, f)

        if os.path.split(target)[1] == filename:
            # source file hasn't been saved since change, so update it from our live buffer
            f = open(target, 'wb')
            f.write(code)
            f.close()
        else:
            shutil.copyfile(f, target)

    os.chdir(d)
    out = popen(cmd)

    if out:
        out = out.communicate()
        out = combine_output(out, '\n')

        # filter results from build to just this filename
        # no guarantee all languages are as nice about this as Go
        # may need to improve later or just defer to communicate()
        out = '\n'.join([
            line for line in out.split('\n') if filename in line.split(':', 1)[0]
        ])
    else:
        out = ''

    shutil.rmtree(d, True)
    return out

def popen(cmd, env=None):
    from . import persist

    if isinstance(cmd, str):
        cmd = cmd,

    info = None

    if os.name == 'nt':
        info = subprocess.STARTUPINFO()
        info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        info.wShowWindow = subprocess.SW_HIDE

    if env is None:
        env = create_environment()

    try:
        return subprocess.Popen(
            cmd, stdin=subprocess.PIPE,
            stdout=subprocess.PIPE, stderr=subprocess.PIPE,
            startupinfo=info, env=env)
    except OSError as err:
        persist.debug('error launching', repr(cmd))
        persist.debug('error was:', err.strerror)
        persist.debug('environment:', env)
<|endoftext|>


<commit_before>try:
    # micropython
    from utime import sleep_ms
except:
    from time import sleep
    def sleep_ms(t):
        sleep(t/1000)

try:
    from ustruct import pack
except:
    from struct import pack

from rfb.clientmsgs import dispatch_msgs
from rfb.servermsgs import ServerSetPixelFormat

class RfbSession():

    # on fail raise; to prevent invalid session at parent
    def __init__(self, conn, w, h, name):
        self.conn, self.addr = conn
        self.w = w
        self.h = h
        self.bpp = 32
        self.depth = 24
        self.big = True
        self.true = True
        channel_mask = 2**(self.depth//3)-1
        self.masks = (channel_mask, channel_mask, channel_mask)
        self.shifts = (16,8,0)
        self.name = name
        self._security = 1 # None/No Security
        self.encodings = [] # sent post init by client

        # HandShake
        self.send( b'RFB 003.003\n' )
        if self.recv(True) != b'RFB 003.003\n':
            raise RfbSessionRejected('version proposal')

        # Security
        self.send( pack('>L', self.security) )
        # ignore instruction to disconnect other clients
        if self.recv(True)[0] not in (0,1):
            raise RfbSessionRejected('no security')

        # ServerInit
        self.send(
            pack('>2H', w, h) \
            + ServerSetPixelFormat(
                self.bpp, self.depth,
                self.big, self.true,
                self.masks, self.shifts
            ) \
            + pack('>L', len(name)) \
            + name
        )

        # we *may* be sent encodings and pixel format
        # we *must* process these messages before
        # sending any rectangles, otherwise we don't
        # know what encodings or pixel format client
        # accepts
        self.service_msg_queue()

        # send colourmap (not currently supported)
        # must be sent after receiving pixel format
        # any sent earlier ignorred by client

    @property
    def security(self):
        return self._security

    def recv(self, blocking=False):
        sleep_ms(100) #init fails at peer without this delay???
        while blocking:
            r = self.conn.recv(1024)
            if r is not None:
                return r
        try:
            return self.conn.recv(1024)
        except:
            pass

    def send(self, b):
        if b: # None and b'' are False
            self.conn.send(b)

    def service_msg_queue(self):
        msg = self.recv()

        if msg == b'': #closed by peer
            return False

        elif msg is not None:

            dispatch_msgs(self, msg)

        return True

class RfbSessionRejected(Exception):
    pass
<commit_msg>move delay into while loop for blocking recv<commit_after>try:
    # micropython
    from utime import sleep_ms
except:
    from time import sleep
    def sleep_ms(t):
        sleep(t/1000)

try:
    from ustruct import pack
except:
    from struct import pack

from rfb.clientmsgs import dispatch_msgs
from rfb.servermsgs import ServerSetPixelFormat

class RfbSession():

    # on fail raise; to prevent invalid session at parent
    def __init__(self, conn, w, h, name):
        self.conn, self.addr = conn
        self.w = w
        self.h = h
        self.bpp = 32
        self.depth = 24
        self.big = True
        self.true = True
        channel_mask = 2**(self.depth//3)-1
        self.masks = (channel_mask, channel_mask, channel_mask)
        self.shifts = (16,8,0)
        self.name = name
        self._security = 1 # None/No Security
        self.encodings = [] # sent post init by client

        # HandShake
        self.send( b'RFB 003.003\n' )
        if self.recv(True) != b'RFB 003.003\n':
            raise RfbSessionRejected('version proposal')

        # Security
        self.send( pack('>L', self.security) )
        # ignore instruction to disconnect other clients
        if self.recv(True)[0] not in (0,1):
            raise RfbSessionRejected('no security')

        # ServerInit
        self.send(
            pack('>2H', w, h) \
            + ServerSetPixelFormat(
                self.bpp, self.depth,
                self.big, self.true,
                self.masks, self.shifts
            ) \
            + pack('>L', len(name)) \
            + name
        )

        # we *may* be sent encodings and pixel format
        # we *must* process these messages before
        # sending any rectangles, otherwise we don't
        # know what encodings or pixel format client
        # accepts
        self.service_msg_queue()

        # send colourmap (not currently supported)
        # must be sent after receiving pixel format
        # any sent earlier ignorred by client

    @property
    def security(self):
        return self._security

    def recv(self, blocking=False):
        while blocking:
            sleep_ms(100) #init fails at peer without this delay
            r = self.conn.recv(1024)
            if r is not None:
                return r
        try:
            return self.conn.recv(1024)
        except:
            pass

    def send(self, b):
        if b: # None and b'' are False
            self.conn.send(b)

    def service_msg_queue(self):
        msg = self.recv()

        if msg == b'': #closed by peer
            return False

        elif msg is not None:

            dispatch_msgs(self, msg)

        return True

class RfbSessionRejected(Exception):
    pass
<|endoftext|>


<commit_before>import gpiozero
import time
from datetime import datetime

def now():
    return datetime.now().time()

class auto:
    def __init__(self):
        # auto bevat alle componenten van de auto/robot
        # de individuele wielen (links_voor, rechts_voor, etc)
        # de groepen wielen (links, rechts en alle)
        # en de snelheid (stadaard 100%)

        self.links_voor     = gpiozero.Motor(forward=6, backward=13)
        self.rechts_voor    = gpiozero.Motor(forward=12, backward=16)
        self.links_achter   = gpiozero.Motor(forward=19, backward=26)
        self.rechts_achter  = gpiozero.Motor(forward=20, backward=21)

        self.alle_motors = (self.links_voor, self.links_achter, self.rechts_voor, self.rechts_achter)
        self.links_motors = (self.links_voor, self.links_achter)
        self.rechts_motors = (self.rechts_voor, self.rechts_achter)
#        self.voor = (links_voor, rechts_voor)
#        self.achter = (links_achter, rechts_achter)

        self.vaart = 0

    def vooruit(self, duur = 0.5):
        print(now(), 'vooruit - snelheid', self.vaart, ';', duur, 'sec')
        for m in self.alle_motors:
            m.forward(self.vaart)
        time.sleep(duur)

    def stop(self):
        print(now(),'stop')
        for m in self.alle_motors:
            m.stop()

    def achteruit(self, duur = 0.5):
        print(now(), 'achteruit - snelheid', self.vaart, ';', duur, 'sec')
        for m in self.alle_motors:
            m.backward(self.vaart)
        time.sleep(duur)

    def links(self, duur = 0.5):
        print(now(), 'links - snelheid', self.vaart, ';', duur, 'sec')
        for m in self.links_motors:
            m.forward(self.vaart)
        time.sleep(duur)

    def rechts(self, duur = 0.5):
        print(now(), 'rechts - snelheid', self.vaart, ';', duur, 'sec')
        for m in self.rechts_motors:
            m.forward(self.vaart)
        time.sleep(duur)

    def rechts_as(self, duur = 0.5):
        print(now(), 'rechts om de as - snelheid', self.vaart, ';', duur, 'sec')
        for m in self.rechts_motors:
            m.forward(self.vaart)
        for m in self.links_motors:
            m.backward(self.vaart)
        time.sleep(duur)

    def links_as(self, duur = 0.5):
        print(now(), 'links om de as - snelheid', self.vaart, ';', duur, 'sec')
        for m in self.links_motors:
            m.forward(self.vaart)
        for m in self.rechts_motors:
            m.backward(self.vaart)
        time.sleep(duur)

if __name__ == "__main__":
    robot = auto()
    robot.vaart = 1

    while True:
        key = input('?')
        if key == 'w':
            robot.vooruit()
        elif key == 'a':
            robot.links_as()
        elif key == 's':
            robot.achteruit()
        elif key == 'd':
            robot.rechts_as()
        else:
            robot.stop()

import gpiozero
import time
from datetime import datetime

def now():
    return datetime.now().time()

class auto:

    def __init__(self):

        self.links_voor     = gpiozero.Motor(forward=6, backward=13)
        self.rechts_voor    = gpiozero.Motor(forward=12, backward=16)
        self.links_achter   = gpiozero.Motor(forward=19, backward=26)
        self.rechts_achter  = gpiozero.Motor(forward=20, backward=21)

        self.alle_motors = (self.links_voor, self.links_achter, self.rechts_voor, self.rechts_achter)
        self.links_motors = (self.links_voor, self.links_achter)
        self.rechts_motors = (self.rechts_voor, self.rechts_achter)
#        self.voor = (links_voor, rechts_voor)
#        self.achter = (links_achter, rechts_achter)

        self.vaart = 0

    def vooruit(self, duur = 0.5):
        print(now(), 'vooruit - snelheid', self.vaart, ';', duur, 'sec')
        for m in self.alle_motors:
            m.forward(self.vaart)
        time.sleep(duur)

    def stop(self):
        print(now(),'stop')
        for m in self.alle_motors:
            m.stop()

    def achteruit(self, duur = 0.5):
        print(now(), 'achteruit - snelheid', self.vaart, ';', duur, 'sec')
        for m in self.alle_motors:
            m.backward(self.vaart)
        time.sleep(duur)

    def links(self, duur = 0.5):
        print(now(), 'links - snelheid', self.vaart, ';', duur, 'sec')
        for m in self.links_motors:
            m.forward(self.vaart)
        time.sleep(duur)

    def rechts(self, duur = 0.5):
        print(now(), 'rechts - snelheid', self.vaart, ';', duur, 'sec')
        for m in self.rechts_motors:
            m.forward(self.vaart)
        time.sleep(duur)

    def rechts_as(self, duur = 0.5):
        print(now(), 'rechts om de as - snelheid', self.vaart, ';', duur, 'sec')
        for m in self.rechts_motors:
            m.forward(self.vaart)
        for m in self.links_motors:
            m.backward(self.vaart)
        time.sleep(duur)

    def links_as(self, duur = 0.5):
        print(now(), 'links om de as - snelheid', self.vaart, ';', duur, 'sec')
        for m in self.links_motors:
            m.forward(self.vaart)
        for m in self.rechts_motors:
            m.backward(self.vaart)
        time.sleep(duur)

if __name__ == "__main__":
    print('bestuur d auto met 'w = vooruit, a = links, s = achteruit, d = rechts')
    robot = auto()
    robot.vaart = 1

    while True:
        key = input('?')
        if key == 'w':
            robot.vooruit()
        elif key == 'a':
            robot.links_as()
        elif key == 's':
            robot.achteruit()
        elif key == 'd':
            robot.rechts_as()
        else:
            robot.stop()

<commit_msg>Delete rijden_keyboard.py<commit_after><|endoftext|>


<commit_before>"""
Wrapper module around a Linux PTY which can be used to start an underlying shell
"""

import os
import select
import subprocess
import struct
import signal

try:
    import fcntl
    import termios
except ImportError:
    pass

from . import utils

class LinuxPty():
    """
    Linux PTY class that starts an underlying and provides methods for
    communicating with it
    """
    def __init__(self, cmd, cwd):
        self._cmd = cmd
        self._env = os.environ.copy()
        self._env["TERM"] = "linux"
        (self._pty, self._pts) = os.openpty()
        self._process = subprocess.Popen(self._cmd, stdin=self._pts,
                                         stdout=self._pts, stderr=self._pts, shell=False,
                                         env=self._env, close_fds=True, start_new_session=True,
                                         cwd=cwd)

    def __del__(self):
        utils.ConsoleLogger.log("Linux PTY instance deleted")

    def stop(self):
        """
        Stop the shell
        """
        if self.is_running():
            self._process.kill()
        self._process = None
        os.close(self._pts)
        os.close(self._pty)

    def receive_output(self, max_read_size, timeout=0):
        """
        Poll the shell output
        """
        if not self.is_running():
            return None

        (ready, _, _) = select.select([self._pty], [], [], timeout)
        if not ready:
            return None

        return os.read(self._pty, max_read_size)

    def update_screen_size(self, lines, columns):
        """
        Notify the shell of a terminal screen resize
        """
        if self.is_running:
            # Note, assume ws_xpixel and ws_ypixel are zero.
            tiocswinsz = getattr(termios, 'TIOCSWINSZ', -2146929561)
            size_update = struct.pack('HHHH', lines, columns, 0, 0)
            fcntl.ioctl(self._pts, tiocswinsz, size_update)
            os.kill(self._process.pid, signal.SIGWINCH)

    def is_running(self):
        """
        Check if the shell is running
        """
        return self._process is not None and self._process.poll() is None

    def send_keypress(self, key, ctrl=False, alt=False, shift=False, meta=False):
        """
        Send keypress to the shell
        """
        if ctrl:
            keycode = self._get_ctrl_combination_key_code(key)
        elif alt:
            keycode = self._get_alt_combination_key_code(key)
        else:
            keycode = self._get_key_code(key)

        self.send_string(keycode)

    def send_string(self, string):
        if self.is_running():
            os.write(self._pty, string.encode('UTF-8'))

    def _get_ctrl_combination_key_code(self, key):
        key = key.lower()
        if key in _LINUX_CTRL_KEY_MAP:
            return _LINUX_CTRL_KEY_MAP[key]
        elif len(key) == 1:
            unicode = ord(key)
            if (unicode >= 97) and (unicode <= 122):
                unicode = unicode - ord('a') + 1
                return chr(unicode)
            return self._get_key_code(key)

        return self._get_key_code(key)

    def _get_alt_combination_key_code(self, key):
        key = key.lower()
        if key in _LINUX_ALT_KEY_MAP:
            return _LINUX_ALT_KEY_MAP[key]

        code = self._get_key_code(key)
        return "\x1b" + code

    def _get_key_code(self, key):
        if key in _LINUX_KEY_MAP:
            return _LINUX_KEY_MAP[key]
        return key

_LINUX_KEY_MAP = {
    "enter": "\r",
    "backspace": "\x7f",
    "tab": "\t",
    "space": " ",
    "escape": "\x1b",
    "down": "\x1b[B",
    "up": "\x1b[A",
    "right": "\x1b[C",
    "left": "\x1b[D",
    "home": "\x1b[1~",
    "end": "\x1b[4~",
    "pageup": "\x1b[5~",
    "pagedown": "\x1b[6~",
    "delete": "\x1b[3~",
    "insert": "\x1b[2~",
    "f1": "\x1bOP",
    "f2": "\x1bOQ",
    "f3": "\x1bOR",
    "f4": "\x1bOS",
    "f5": "\x1b[15~",
    "f6": "\x1b[17~",
    "f7": "\x1b[18~",
    "f8": "\x1b[19~",
    "f9": "\x1b[20~",
    "f10": "\x1b[21~",
    "f12": "\x1b[24~",
    "bracketed_paste_mode_start": "\x1b[200~",
    "bracketed_paste_mode_end": "\x1b[201~",
}

_LINUX_CTRL_KEY_MAP = {
    "up": "\x1b[1;5A",
    "down": "\x1b[1;5B",
    "right": "\x1b[1;5C",
    "left": "\x1b[1;5D",
    "@": "\x00",
    "`": "\x00",
    "[": "\x1b",
    "{": "\x1b",
    "\\": "\x1c",
    "|": "\x1c",
    "]": "\x1d",
    "}": "\x1d",
    "^": "\x1e",
    "~": "\x1e",
    "_": "\x1f",
    "?": "\x7f",
}

_LINUX_ALT_KEY_MAP = {
    "up": "\x1b[1;3A",
    "down": "\x1b[1;3B",
    "right": "\x1b[1;3C",
    "left": "\x1b[1;3D",
}
<commit_msg>Revert "send SIGWINCH to the process when resizing"<commit_after>"""
Wrapper module around a Linux PTY which can be used to start an underlying shell
"""

import os
import select
import subprocess
import struct

try:
    import fcntl
    import termios
except ImportError:
    pass

from . import utils

class LinuxPty():
    """
    Linux PTY class that starts an underlying and provides methods for
    communicating with it
    """
    def __init__(self, cmd, cwd):
        self._cmd = cmd
        self._env = os.environ.copy()
        self._env["TERM"] = "linux"
        (self._pty, self._pts) = os.openpty()
        self._process = subprocess.Popen(self._cmd, stdin=self._pts,
                                         stdout=self._pts, stderr=self._pts, shell=False,
                                         env=self._env, close_fds=True, start_new_session=True,
                                         cwd=cwd)

    def __del__(self):
        utils.ConsoleLogger.log("Linux PTY instance deleted")

    def stop(self):
        """
        Stop the shell
        """
        if self.is_running():
            self._process.kill()
        self._process = None
        os.close(self._pts)
        os.close(self._pty)

    def receive_output(self, max_read_size, timeout=0):
        """
        Poll the shell output
        """
        if not self.is_running():
            return None

        (ready, _, _) = select.select([self._pty], [], [], timeout)
        if not ready:
            return None

        return os.read(self._pty, max_read_size)

    def update_screen_size(self, lines, columns):
        """
        Notify the shell of a terminal screen resize
        """
        if self.is_running:
            # Note, assume ws_xpixel and ws_ypixel are zero.
            tiocswinsz = getattr(termios, 'TIOCSWINSZ', -2146929561)
            size_update = struct.pack('HHHH', lines, columns, 0, 0)
            fcntl.ioctl(self._pts, tiocswinsz, size_update)

    def is_running(self):
        """
        Check if the shell is running
        """
        return self._process is not None and self._process.poll() is None

    def send_keypress(self, key, ctrl=False, alt=False, shift=False, meta=False):
        """
        Send keypress to the shell
        """
        if ctrl:
            keycode = self._get_ctrl_combination_key_code(key)
        elif alt:
            keycode = self._get_alt_combination_key_code(key)
        else:
            keycode = self._get_key_code(key)

        self.send_string(keycode)

    def send_string(self, string):
        if self.is_running():
            os.write(self._pty, string.encode('UTF-8'))

    def _get_ctrl_combination_key_code(self, key):
        key = key.lower()
        if key in _LINUX_CTRL_KEY_MAP:
            return _LINUX_CTRL_KEY_MAP[key]
        elif len(key) == 1:
            unicode = ord(key)
            if (unicode >= 97) and (unicode <= 122):
                unicode = unicode - ord('a') + 1
                return chr(unicode)
            return self._get_key_code(key)

        return self._get_key_code(key)

    def _get_alt_combination_key_code(self, key):
        key = key.lower()
        if key in _LINUX_ALT_KEY_MAP:
            return _LINUX_ALT_KEY_MAP[key]

        code = self._get_key_code(key)
        return "\x1b" + code

    def _get_key_code(self, key):
        if key in _LINUX_KEY_MAP:
            return _LINUX_KEY_MAP[key]
        return key

_LINUX_KEY_MAP = {
    "enter": "\r",
    "backspace": "\x7f",
    "tab": "\t",
    "space": " ",
    "escape": "\x1b",
    "down": "\x1b[B",
    "up": "\x1b[A",
    "right": "\x1b[C",
    "left": "\x1b[D",
    "home": "\x1b[1~",
    "end": "\x1b[4~",
    "pageup": "\x1b[5~",
    "pagedown": "\x1b[6~",
    "delete": "\x1b[3~",
    "insert": "\x1b[2~",
    "f1": "\x1bOP",
    "f2": "\x1bOQ",
    "f3": "\x1bOR",
    "f4": "\x1bOS",
    "f5": "\x1b[15~",
    "f6": "\x1b[17~",
    "f7": "\x1b[18~",
    "f8": "\x1b[19~",
    "f9": "\x1b[20~",
    "f10": "\x1b[21~",
    "f12": "\x1b[24~",
    "bracketed_paste_mode_start": "\x1b[200~",
    "bracketed_paste_mode_end": "\x1b[201~",
}

_LINUX_CTRL_KEY_MAP = {
    "up": "\x1b[1;5A",
    "down": "\x1b[1;5B",
    "right": "\x1b[1;5C",
    "left": "\x1b[1;5D",
    "@": "\x00",
    "`": "\x00",
    "[": "\x1b",
    "{": "\x1b",
    "\\": "\x1c",
    "|": "\x1c",
    "]": "\x1d",
    "}": "\x1d",
    "^": "\x1e",
    "~": "\x1e",
    "_": "\x1f",
    "?": "\x7f",
}

_LINUX_ALT_KEY_MAP = {
    "up": "\x1b[1;3A",
    "down": "\x1b[1;3B",
    "right": "\x1b[1;3C",
    "left": "\x1b[1;3D",
}
<|endoftext|>