<commit_before>"""
Generate a colorscheme using imagemagick.
"""
import re
import shutil
import subprocess

from pywal.settings import CACHE_DIR, COLOR_COUNT
from pywal import util

def imagemagick(color_count, img):
    """Call Imagemagick to generate a scheme."""
    colors = subprocess.Popen(["convert", img, "+dither", "-colors",
                               str(color_count), "-unique-colors", "txt:-"],
                              stdout=subprocess.PIPE)

    return colors.stdout.readlines()

def gen_colors(img):
    """Generate a color palette using imagemagick."""
    # Check if the user has Imagemagick installed.
    if not shutil.which("convert"):
        print("error: imagemagick not found, exiting...\n"
              "error: wal requires imagemagick to function.")
        exit(1)

    # Generate initial scheme.
    raw_colors = imagemagick(COLOR_COUNT, img)

    # If imagemagick finds less than 16 colors, use a larger source number
    # of colors.
    index = 0
    while len(raw_colors) - 1 < COLOR_COUNT:
        index += 1
        raw_colors = imagemagick(COLOR_COUNT + index, img)

        print("colors: Imagemagick couldn't generate a", COLOR_COUNT,
              "color palette, trying a larger palette size",
              COLOR_COUNT + index)

    # Remove the first element, which isn't a color.
    del raw_colors[0]

    # Create a list of hex colors.
    return [re.search("#.{6}", str(col)).group(0) for col in raw_colors]

def get_colors(img, quiet):
    """Generate a colorscheme using imagemagick."""
    # Cache the wallpaper name.
    util.save_file(img, CACHE_DIR / "wal")

    # Cache the sequences file.
    cache_file = CACHE_DIR / "schemes" / img.replace("/", "_")
    cache_file = cache_file.with_suffix(".json")

    if cache_file.is_file():
        colors = util.read_file_json(cache_file)
        print("colors: Found cached colorscheme.")

    else:
        print("colors: Generating a colorscheme...")
        if not quiet:
            util.disown("notify-send", "wal: Generating a colorscheme...")

        # Generate the colors.
        colors = gen_colors(img)
        colors = sort_colors(colors)

        # Cache the colorscheme.
        util.save_file_json(colors, cache_file)

        print("colors: Generated colorscheme")
        if not quiet:
            util.disown("notify-send", "wal: Generation complete.")

    return colors

def sort_colors(colors):
    """Sort the generated colors and store them in a dict that
       we will later save in json format."""
    raw_colors = colors[:1] + colors[9:] + colors[8:]

    # Special colors.
    colors_special = {}
    colors_special.update({"background": raw_colors[0]})
    colors_special.update({"foreground": raw_colors[15]})
    colors_special.update({"cursor": raw_colors[15]})

    # Colors 0-15
    colors_hex = {}
    [colors_hex.update({f"color{index}": color})  # pylint: disable=W0106
     for index, color in enumerate(raw_colors)]

    # Color 8
    colors_hex["color8"] = util.set_grey(raw_colors)

    # Add the colors to a dict.
    colors = {}
    colors["special"] = colors_special
    colors["colors"] = colors_hex

    return colors
<commit_msg>magic: Update docs<commit_after>"""
Generate a colorscheme using imagemagick.
"""
import re
import shutil
import subprocess

from pywal.settings import CACHE_DIR, COLOR_COUNT
from pywal import util

def imagemagick(color_count, img):
    """Call Imagemagick to generate a scheme."""
    colors = subprocess.Popen(["convert", img, "+dither", "-colors",
                               str(color_count), "-unique-colors", "txt:-"],
                              stdout=subprocess.PIPE)

    return colors.stdout.readlines()

def gen_colors(img):
    """Format the output from imagemagick into a list
       of hex colors."""
    # Check if the user has Imagemagick installed.
    if not shutil.which("convert"):
        print("error: imagemagick not found, exiting...\n"
              "error: wal requires imagemagick to function.")
        exit(1)

    # Generate initial scheme.
    raw_colors = imagemagick(COLOR_COUNT, img)

    # If imagemagick finds less than 16 colors, use a larger source number
    # of colors.
    index = 0
    while len(raw_colors) - 1 < COLOR_COUNT:
        index += 1
        raw_colors = imagemagick(COLOR_COUNT + index, img)

        print("colors: Imagemagick couldn't generate a", COLOR_COUNT,
              "color palette, trying a larger palette size",
              COLOR_COUNT + index)

    # Remove the first element, which isn't a color.
    del raw_colors[0]

    # Create a list of hex colors.
    return [re.search("#.{6}", str(col)).group(0) for col in raw_colors]

def get_colors(img, quiet):
    """Get the colorscheme."""
    # Cache the wallpaper name.
    util.save_file(img, CACHE_DIR / "wal")

    # Cache the sequences file.
    cache_file = CACHE_DIR / "schemes" / img.replace("/", "_")
    cache_file = cache_file.with_suffix(".json")

    if cache_file.is_file():
        colors = util.read_file_json(cache_file)
        print("colors: Found cached colorscheme.")

    else:
        print("colors: Generating a colorscheme...")
        if not quiet:
            util.disown("notify-send", "wal: Generating a colorscheme...")

        # Generate the colors.
        colors = gen_colors(img)
        colors = sort_colors(colors)

        # Cache the colorscheme.
        util.save_file_json(colors, cache_file)

        print("colors: Generated colorscheme")
        if not quiet:
            util.disown("notify-send", "wal: Generation complete.")

    return colors

def sort_colors(colors):
    """Sort the generated colors and store them in a dict that
       we will later save in json format."""
    raw_colors = colors[:1] + colors[9:] + colors[8:]

    # Special colors.
    colors_special = {}
    colors_special.update({"background": raw_colors[0]})
    colors_special.update({"foreground": raw_colors[15]})
    colors_special.update({"cursor": raw_colors[15]})

    # Colors 0-15
    colors_hex = {}
    [colors_hex.update({f"color{index}": color})  # pylint: disable=W0106
     for index, color in enumerate(raw_colors)]

    # Color 8
    colors_hex["color8"] = util.set_grey(raw_colors)

    # Add the colors to a dict.
    colors = {}
    colors["special"] = colors_special
    colors["colors"] = colors_hex

    return colors
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# vim: set ts=4

# Copyright 2016 Rémi Duraffort
# This file is part of ReactOBus.
#
# ReactOBus 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.
#
# ReactOBus 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 ReactOBus.  If not, see <http://www.gnu.org/licenses/>

import logging
from setproctitle import setproctitle
import multiprocessing
import re
import subprocess
import threading
import zmq
from zmq.utils.strtypes import b, u

LOG = logging.getLogger("ROB.lib.reactor")

class Matcher(object):
    def __init__(self, rule):
        self.name = rule["name"]
        self.field = rule["match"]["field"]
        self.pattern = re.compile(rule["match"]["pattern"])
        self.bin = rule["exec"]["path"]
        self.timeout = rule["exec"]["timeout"]
        self.args = rule["exec"]["args"]

    def match(self, variables):
        return self.pattern.match(variables[self.field]) is not None

    def run(self, topic, uuid, datetime, username, data):
        variables = {"topic": topic,
                     "uuid": uuid,
                     "datetime": datetime,
                     "username": username}
        args = [self.bin]
        for arg in self.args:
            if arg[0] == '$':
                # TODO: check for errors
                args.append(variables[arg[1:]])
            else:
                args.append(arg)

        LOG.debug("Running: %s", args)
        try:
            out = subprocess.check_output(args, stderr=subprocess.STDOUT,
                                          timeout=self.timeout)
        except OSError as err:
            LOG.error("Unable to run %s (%s)", args, err)
        except subprocess.TimeoutExpired:
            LOG.error("Timeout when running %s", args)
        else:
            LOG.debug("=> %s", out)

class Worker(threading.Thread):
    def __init__(self, matchers):
        super().__init__()
        self.matchers = matchers

    def run(self):
        self.context = zmq.Context.instance()
        self.sock = self.context.socket(zmq.DEALER)
        self.sock.connect("inproc://workers")
        while True:
            msg = self.sock.recv_multipart()
            matcher_index = int(msg[0])
            (topic, uuid, dt, username, data) = msg[1:]
            LOG.debug("Running matcher n°%d on %s", matcher_index, self.name)
            self.matchers[matcher_index].run(topic, uuid, dt, username, data)

class Reactor(multiprocessing.Process):
    def __init__(self, options, outbound):
        super().__init__()
        self.options = options
        self.matchers = []
        self.outbound = outbound
        self.workers = []

    def setup(self):
        setproctitle("ReactOBus [reactor]")
        # Connect to the stream
        self.context = zmq.Context.instance()
        self.sub = self.context.socket(zmq.SUB)
        self.sub.setsockopt(zmq.SUBSCRIBE, b"")
        LOG.debug("Connecting to outbound (%s)", self.outbound)
        self.sub.connect(self.outbound)

        # Loop on all rules
        for rule in self.options["rules"]:
            m = Matcher(rule)
            self.matchers.append(m)

        # Create the workers
        self.ctrl = self.context.socket(zmq.DEALER)
        self.ctrl.bind("inproc://workers")

        LOG.debug("Starting %d workers", self.options["workers"])
        for _ in range(0, self.options["workers"]):
            w = Worker(self.matchers)
            w.start()
            self.workers.append(w)

    def run(self):
        self.setup()
        while True:
            msg = self.sub.recv_multipart()
            try:
                (topic, uuid, datetime, username, data) = (u(m) for m in msg)
            except (IndexError, ValueError):
                LOG.error("Invalid message: %s", msg)
                continue

            variables = {"topic": topic,
                         "uuid": uuid,
                         "datetime": datetime,
                         "username": username}
            for (i, m) in enumerate(self.matchers):
                if m.match(variables):
                    LOG.debug("%s matching %s", msg, m.name)
                    self.ctrl.send_multipart([b(str(i)),
                                              b(topic),
                                              b(uuid),
                                              b(datetime),
                                              b(username),
                                              b(data)])
<commit_msg>Deal with when the match field is not in the message<commit_after># -*- coding: utf-8 -*-
# vim: set ts=4

# Copyright 2016 Rémi Duraffort
# This file is part of ReactOBus.
#
# ReactOBus 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.
#
# ReactOBus 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 ReactOBus.  If not, see <http://www.gnu.org/licenses/>

import logging
from setproctitle import setproctitle
import multiprocessing
import re
import subprocess
import threading
import zmq
from zmq.utils.strtypes import b, u

LOG = logging.getLogger("ROB.lib.reactor")

class Matcher(object):
    def __init__(self, rule):
        self.name = rule["name"]
        self.field = rule["match"]["field"]
        self.pattern = re.compile(rule["match"]["pattern"])
        self.bin = rule["exec"]["path"]
        self.timeout = rule["exec"]["timeout"]
        self.args = rule["exec"]["args"]

    def match(self, variables):
        if self.field:
            return self.pattern.match(variables[self.field]) is not None
        return False

    def run(self, topic, uuid, datetime, username, data):
        variables = {"topic": topic,
                     "uuid": uuid,
                     "datetime": datetime,
                     "username": username}
        args = [self.bin]
        for arg in self.args:
            if arg[0] == '$':
                # TODO: check for errors
                args.append(variables[arg[1:]])
            else:
                args.append(arg)

        LOG.debug("Running: %s", args)
        try:
            out = subprocess.check_output(args, stderr=subprocess.STDOUT,
                                          timeout=self.timeout)
        except OSError as err:
            LOG.error("Unable to run %s (%s)", args, err)
        except subprocess.TimeoutExpired:
            LOG.error("Timeout when running %s", args)
        else:
            LOG.debug("=> %s", out)

class Worker(threading.Thread):
    def __init__(self, matchers):
        super().__init__()
        self.matchers = matchers

    def run(self):
        self.context = zmq.Context.instance()
        self.sock = self.context.socket(zmq.DEALER)
        self.sock.connect("inproc://workers")
        while True:
            msg = self.sock.recv_multipart()
            matcher_index = int(msg[0])
            (topic, uuid, dt, username, data) = msg[1:]
            LOG.debug("Running matcher n°%d on %s", matcher_index, self.name)
            self.matchers[matcher_index].run(topic, uuid, dt, username, data)

class Reactor(multiprocessing.Process):
    def __init__(self, options, outbound):
        super().__init__()
        self.options = options
        self.matchers = []
        self.outbound = outbound
        self.workers = []

    def setup(self):
        setproctitle("ReactOBus [reactor]")
        # Connect to the stream
        self.context = zmq.Context.instance()
        self.sub = self.context.socket(zmq.SUB)
        self.sub.setsockopt(zmq.SUBSCRIBE, b"")
        LOG.debug("Connecting to outbound (%s)", self.outbound)
        self.sub.connect(self.outbound)

        # Loop on all rules
        for rule in self.options["rules"]:
            m = Matcher(rule)
            self.matchers.append(m)

        # Create the workers
        self.ctrl = self.context.socket(zmq.DEALER)
        self.ctrl.bind("inproc://workers")

        LOG.debug("Starting %d workers", self.options["workers"])
        for _ in range(0, self.options["workers"]):
            w = Worker(self.matchers)
            w.start()
            self.workers.append(w)

    def run(self):
        self.setup()
        while True:
            msg = self.sub.recv_multipart()
            try:
                (topic, uuid, datetime, username, data) = (u(m) for m in msg)
            except (IndexError, ValueError):
                LOG.error("Invalid message: %s", msg)
                continue

            variables = {"topic": topic,
                         "uuid": uuid,
                         "datetime": datetime,
                         "username": username}
            for (i, m) in enumerate(self.matchers):
                if m.match(variables):
                    LOG.debug("%s matching %s", msg, m.name)
                    self.ctrl.send_multipart([b(str(i)),
                                              b(topic),
                                              b(uuid),
                                              b(datetime),
                                              b(username),
                                              b(data)])
<|endoftext|>


<commit_before>from .get_app import get_app
from .mplwidget import Mpl_Image, Mpl_Image_Plus_Slider, Mpl_Plot, Slider_and_Text
from .ButtonMsg import *
from .Rectangle import Rectangle
<commit_msg>Formatting<commit_after>from .get_app import get_app                                                         # noqa
from .mplwidget import Mpl_Image, Mpl_Image_Plus_Slider, Mpl_Plot, Slider_and_Text   # noqa
from .ButtonMsg import *                                                             # noqa
from .Rectangle import Rectangle                                                     # noqa
<|endoftext|>


<commit_before>#CHANGE FTP VERIFY TO MORE MODULAR THING, THE PWRSHELL SCRIPT NEED DPENEDNCIES BUT IT DOESNT NEED OT BE RUN
	#PROBABLY A FOR LOOP FOR LENGTH OF NEW LIST THAT I WILL CREATE
	#
	#
	#verifies file integrity
import time
import sys
import getpass
import os
from ftplib import FTP
import subprocess as sp
class Class01:
	def __init__(self):
		#try:
		self.ftp = FTP('192.168.1.9')
		self.ftp.login('tech')
		self.user = getpass.getuser()
		#except:
		#print('Could not connect to FTP server')

	def ftpverify(self,dl,directory): #verifies download and runs app/scripts
		count = 0
		while count <= len(dl):
			size = self.ftp.size(dl[count])
			self.ftp.retrbinary('RETR ' + dl[count], open(dl[count], 'wb').write)
			if os.path.exists(directory+'\\' +dl[count]):
				while os.path.getsize(directory+'\\'+dl[count]) != size: #checks to make sure file size is correct, if it is not correct this would indicate a a bad download
					continue

				sp.Popen(['powershell.exe'],''+directory+'\\'+dl[count],1) #figure out bufsize

			else:
				print('Download did not start')
				self.main()
			count = count + 1
	#keep track of data in list
	def listcheck(self,UserAns,directory,scripts):
		if UserAns.lower() == 'new user':
			dl = [scripts[0],scripts[1]]
			self.ftpverify(dl,directory)

	#Checks user input
	def check(self,UserAns,scripts,directory):
		if UserAns.lower() == 'new user':
			self.NewUsr(scripts,directory,UserAns)
		elif UserAns.lower() == 'exit' or 'quit':
			sys.exit()
	#Begins program
	def main(self):
		directory = 'C:\\Users\\'+self.user+'\\desktop'
		scripts = ['pcsetup.ps1','PSWindowsUpdate']
		UserAns =input("Hello, what do you need? > ")
		self.check(UserAns,scripts,directory)
	#for new user
	def NewUsr(self,scripts,directory,UserAns):
		#self.ftp.cwd('scripts')
		os.chdir(directory)
		self.listcheck(UserAns,directory,scripts)

if __name__ == '__main__':
	quick = Class01()
	quick.main()

<commit_msg>Fixed popen implementation, working on checks for downloads.<commit_after>#CHANGE FTP VERIFY TO MORE MODULAR THING, THE PWRSHELL SCRIPT NEED DPENEDNCIES BUT IT DOESNT NEED OT BE RUN
	#PROBABLY A FOR LOOP FOR LENGTH OF NEW LIST THAT I WILL CREATE
	#
	#
	#verifies file integrity
import time
import sys
import getpass
import os
from ftplib import FTP
import subprocess as sp
class Class01:
	def __init__(self):
		#try:
		self.ftp = FTP('192.168.1.9')
		self.ftp.login('tech')
		self.user = getpass.getuser()
		#except:
		#print('Could not connect to FTP server')

	def ftpverify(self,dl,directory): #verifies download and runs app/scripts
		count = 0
		while count <= len(dl):
			size = self.ftp.size(dl[count])
			self.ftp.retrbinary('RETR ' + dl[count], open(dl[count], 'wb').write)
			if os.path.exists(directory+'\\' +dl[count]):
				while os.path.getsize(directory+'\\'+dl[count]) != size: #checks to make sure file size is correct, if it is not correct this would indicate a a bad download
					continue

				sp.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe','-ExecutionPolicy','Unrestricted',''+directory+'\\'+dl[count]]) #works now

			else:
				print('Download did not start')
				self.main()
			count = count + 1
	#keep track of data in list
	def listcheck(self,UserAns,directory,scripts):
		if UserAns.lower() == 'new user':
			dl = [scripts[0]]
			self.ftpverify(dl,directory)

	#Checks user input
	def check(self,UserAns,scripts,directory):
		if UserAns.lower() == 'new user':
			self.NewUsr(scripts,directory,UserAns)
		elif UserAns.lower() == 'exit' or 'quit':
			sys.exit()
	#Begins program
	def main(self):
		directory = 'C:\\Users\\'+self.user+'\\desktop'
		scripts = ['pcsetup.ps1','PSWindowsUpdate']
		UserAns =input("Hello, what do you need? > ")
		self.check(UserAns,scripts,directory)
	#for new user
	def NewUsr(self,scripts,directory,UserAns):
		#self.ftp.cwd('scripts')
		os.chdir(directory)
		self.listcheck(UserAns,directory,scripts)

if __name__ == '__main__':
	quick = Class01()
	quick.main()

<|endoftext|>


<commit_before># vim: fileencoding=utf-8 et sw=4 ts=4 tw=80:

# python-quilt - A Python implementation of the quilt patch system
#
# Copyright (C) 2012  Björn Ricks <bjoern.ricks@googlemail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

import os
import os.path

from quilt.utils import Process, Directory, File, FileParam, SubprocessError

class Patch(object):

    """ Wrapper arround the patch util """

    def __init__(self, patch_name, strip=1, reverse=False):
        self.patch_name = patch_name
        self.strip = strip
        self.reverse = reverse

    def run(self, cwd, patch_dir=None, backup=False, prefix=None,
            reverse=False, work_dir=None, force=False, dry_run=False,
            no_backup_if_mismatch=False, remove_empty_files=False, quiet=False):
        cmd = ["patch"]
        cmd.append("-p" + str(self.strip))

        if backup:
            cmd.append("--backup")

        if prefix:
            cmd.append("--prefix")
            if not prefix[-1] == os.sep:
                prefix += os.sep
            cmd.append(prefix)

        reverse = reverse != self.reverse
        if reverse:
            cmd.append("-R")

        if work_dir:
            cmd.append("-d")
            cmd.append(work_dir)

        if no_backup_if_mismatch:
            cmd.append("--no-backup-if-mismatch")

        if remove_empty_files:
            cmd.append("--remove-empty-files")

        if force:
            cmd.append("-f")

        cmd.append("-i")
        if patch_dir:
            name = os.path.join(patch_dir, self.get_name())
        else:
            name = self.get_name()
        cmd.append(name)

        if quiet:
            cmd.append("-s")

        if dry_run:
            cmd.append("--dry-run")

        Process(cmd).run(cwd=cwd)

    def get_name(self):
        return self.patch_name

    def __eq__(self, other):
        return (isinstance(other, Patch) and self.get_name() == \
                other.get_name())

    def __hash__(self):
        return hash(self.get_name())

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

    def __repr__(self):
        return "<Patch(%r, %r, %r) id=0x%0x>" % (self.get_name(), self.strip,
                                                 self.reverse, id(self))

class RollbackPatch(object):

    def __init__(self, cwd, backup_dir):
        self.cwd = Directory(cwd)
        self.backup_dir = Directory(backup_dir)

    def rollback(self, keep=False):
        (dirs, files) = self.backup_dir.content()

        for dir in dirs:
            newdir = self.cwd + dir
            if not newdir.exists():
                newdir.create()

        for file in files:
            file = File(file)
            backup_file = self.backup_dir + file
            rollback_file = self.cwd + file

            if not keep:
                rollback_file.delete_if_exists()
            if not backup_file.is_empty():
                backup_file.copy(rollback_file)

    def delete_backup(self):
        self.backup_dir.delete()

class Diff(object):
    """ Wrapper arround the diff util
    """

    @FileParam(["left", "right"])
    def __init__(self, left, right):
        """ left points to the first file and right to the second file
        """
        self.left = left
        if not self.left.exists():
            self.left = File("/dev/null")

        self.right = right
        if not self.right.exists():
            self.right = File("/dev/null")

    def run(self, cwd, left_label=None, right_label=None, unified=True,
            fd=None):
        cmd = ["diff"]

        if unified:
            cmd.append("-u")

        if left_label:
            cmd.append("--label")
            cmd.append(left_label)

        if right_label:
            if not left_label:
                cmd.append("--label")
                cmd.append(self.right.get_name())
            cmd.append("--label")
            cmd.append(right_label)

        cmd.append(self.left.get_name())
        cmd.append(self.right.get_name())

        try:
            Process(cmd).run(cwd=cwd, stdout=fd)
        except SubprocessError, e:
            if e.get_returncode() > 1:
                raise e

    def equal(self, cwd):
        """ Returns True if left and right are equal
        """
        cmd = ["diff"]
        cmd.append("-q")
        cmd.append(self.left.get_name())
        cmd.append(self.right.get_name())

        try:
            Process(cmd).run(cwd=cwd, suppress_output=True)
        except SubprocessError, e:
            if e.get_returncode() == 1:
                return False
            else:
                raise e
        return True
<commit_msg>Add Patch get_header method<commit_after># vim: fileencoding=utf-8 et sw=4 ts=4 tw=80:

# python-quilt - A Python implementation of the quilt patch system
#
# Copyright (C) 2012  Björn Ricks <bjoern.ricks@googlemail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

import os
import os.path

from quilt.utils import Process, Directory, DirectoryParam, File, FileParam, \
                        SubprocessError

class Patch(object):

    """ Wrapper arround the patch util """

    def __init__(self, patch_name, strip=1, reverse=False):
        self.patch_name = patch_name
        self.strip = strip
        self.reverse = reverse

    def run(self, cwd, patch_dir=None, backup=False, prefix=None,
            reverse=False, work_dir=None, force=False, dry_run=False,
            no_backup_if_mismatch=False, remove_empty_files=False, quiet=False):
        cmd = ["patch"]
        cmd.append("-p" + str(self.strip))

        if backup:
            cmd.append("--backup")

        if prefix:
            cmd.append("--prefix")
            if not prefix[-1] == os.sep:
                prefix += os.sep
            cmd.append(prefix)

        reverse = reverse != self.reverse
        if reverse:
            cmd.append("-R")

        if work_dir:
            cmd.append("-d")
            cmd.append(work_dir)

        if no_backup_if_mismatch:
            cmd.append("--no-backup-if-mismatch")

        if remove_empty_files:
            cmd.append("--remove-empty-files")

        if force:
            cmd.append("-f")

        cmd.append("-i")
        if patch_dir:
            name = os.path.join(patch_dir, self.get_name())
        else:
            name = self.get_name()
        cmd.append(name)

        if quiet:
            cmd.append("-s")

        if dry_run:
            cmd.append("--dry-run")

        Process(cmd).run(cwd=cwd)

    def get_name(self):
        return self.patch_name

    @DirectoryParam(["patch_dir"])
    def get_header(self, patch_dir=None):
        lines = []

        if patch_dir:
            file = patch_dir + File(self.get_name())
            name = file.get_name()
        else:
            name = self.get_name()
        with open(name, "r") as f:
            for line in f:
                if line.startswith("---") or line.startswith("Index:"):
                    break
                lines.append(line)

        return "".join(lines)

    def __eq__(self, other):
        return (isinstance(other, Patch) and self.get_name() == \
                other.get_name())

    def __hash__(self):
        return hash(self.get_name())

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

    def __repr__(self):
        return "<Patch(%r, %r, %r) id=0x%0x>" % (self.get_name(), self.strip,
                                                 self.reverse, id(self))

class RollbackPatch(object):

    def __init__(self, cwd, backup_dir):
        self.cwd = Directory(cwd)
        self.backup_dir = Directory(backup_dir)

    def rollback(self, keep=False):
        (dirs, files) = self.backup_dir.content()

        for dir in dirs:
            newdir = self.cwd + dir
            if not newdir.exists():
                newdir.create()

        for file in files:
            file = File(file)
            backup_file = self.backup_dir + file
            rollback_file = self.cwd + file

            if not keep:
                rollback_file.delete_if_exists()
            if not backup_file.is_empty():
                backup_file.copy(rollback_file)

    def delete_backup(self):
        self.backup_dir.delete()

class Diff(object):
    """ Wrapper arround the diff util
    """

    @FileParam(["left", "right"])
    def __init__(self, left, right):
        """ left points to the first file and right to the second file
        """
        self.left = left
        if not self.left.exists():
            self.left = File("/dev/null")

        self.right = right
        if not self.right.exists():
            self.right = File("/dev/null")

    def run(self, cwd, left_label=None, right_label=None, unified=True,
            fd=None):
        cmd = ["diff"]

        if unified:
            cmd.append("-u")

        if left_label:
            cmd.append("--label")
            cmd.append(left_label)

        if right_label:
            if not left_label:
                cmd.append("--label")
                cmd.append(self.right.get_name())
            cmd.append("--label")
            cmd.append(right_label)

        cmd.append(self.left.get_name())
        cmd.append(self.right.get_name())

        try:
            Process(cmd).run(cwd=cwd, stdout=fd)
        except SubprocessError, e:
            if e.get_returncode() > 1:
                raise e

    def equal(self, cwd):
        """ Returns True if left and right are equal
        """
        cmd = ["diff"]
        cmd.append("-q")
        cmd.append(self.left.get_name())
        cmd.append(self.right.get_name())

        try:
            Process(cmd).run(cwd=cwd, suppress_output=True)
        except SubprocessError, e:
            if e.get_returncode() == 1:
                return False
            else:
                raise e
        return True
<|endoftext|>


<commit_before>from raco import pyra
import sampledb

V1 = pyra.scan("R", sampledb.__dict__)

result = pyra.select(lambda t: ((t.predicate == "knows") or (t.predicate == "holdsAccount")), V1)

pyra.dump(result)
<commit_msg>raco: remove unused result.py<commit_after><|endoftext|>


<commit_before>import tweepy
from pymongo import MongoClient

class RaffleSlave:
    "The class responsbile for a single raffle, a new instance for each individual raffle"

    Params = None

    api = None

    alive = True

    def __init__(self, hashtag, max, id, owner ):
        self.Params = {}
        self.Params['max'] = max
        self.Params['hashtag'] = hashtag
        self.Params[ '_id' ] = id
        self.Params[ 'owner' ] = owner

        auth = tweepy.OAuthHandler( '5Xr8HX71XetZYmGV86AmcEgVo', '85ql1GsrOLTRre0AqqprX9Xtm5SkMOWzJk9OVJPRiLM8bm72JA' )
        auth.set_access_token( '832250876551110658-MLGfJUjJH6Ktwlf51AQQlSO9QPcp3ew', 'UvCcyNqwH3X7u2KfRWeYvlOWxN2k1ONfjrlpxRK1Shj33' )

        self.api = tweepy.API( auth )

    def update(self):
        public_tweets = self.api.search( '@'+self.Params['owner']+' #'+self.Params['hashtag'] )
        client = MongoClient()
        db = client.raftl

        tweetcollection = db.tweets
        followers = self.api.followers_ids(self.Params['owner'])

        existingTweets = tweetcollection.find()

        for tweet in public_tweets:

            val = 0
            for checker in tweetcollection.find():
                if( tweet.author.id == checker['user_id'] ):
                    val+=1
                    if( val >= self.Params['max'] ):
                        break

            if( val < self.Params['max'] && tweet.author.screen_name != self.Params['owner'] ):
                tweetcollection.update_one( {'_id':tweet.id}, {'$set': {'_id':tweet.id, 'user_id':tweet.author.id, 'following':tweet.author.id in followers,'raffle_id':self.Params['_id'], 'body':tweet.text, 'username':tweet.author.screen_name, 'profile_img':tweet.author.profile_image_url_https } }, True )

    def getParams(self):
        return self.Params

    def checkAlive(self):
        return self.alive
<commit_msg>Fixed unary operatory<commit_after>import tweepy
from pymongo import MongoClient

class RaffleSlave:
    "The class responsbile for a single raffle, a new instance for each individual raffle"

    Params = None

    api = None

    alive = True

    def __init__(self, hashtag, max, id, owner ):
        self.Params = {}
        self.Params['max'] = max
        self.Params['hashtag'] = hashtag
        self.Params[ '_id' ] = id
        self.Params[ 'owner' ] = owner

        auth = tweepy.OAuthHandler( '5Xr8HX71XetZYmGV86AmcEgVo', '85ql1GsrOLTRre0AqqprX9Xtm5SkMOWzJk9OVJPRiLM8bm72JA' )
        auth.set_access_token( '832250876551110658-MLGfJUjJH6Ktwlf51AQQlSO9QPcp3ew', 'UvCcyNqwH3X7u2KfRWeYvlOWxN2k1ONfjrlpxRK1Shj33' )

        self.api = tweepy.API( auth )

    def update(self):
        public_tweets = self.api.search( '@'+self.Params['owner']+' #'+self.Params['hashtag'] )
        client = MongoClient()
        db = client.raftl

        tweetcollection = db.tweets
        followers = self.api.followers_ids(self.Params['owner'])

        existingTweets = tweetcollection.find()

        for tweet in public_tweets:

            val = 0
            for checker in tweetcollection.find():
                if( tweet.author.id == checker['user_id'] ):
                    val+=1
                    if( val >= self.Params['max'] ):
                        break

            if( val < self.Params['max'] and tweet.author.screen_name != self.Params['owner'] ):
                tweetcollection.update_one( {'_id':tweet.id}, {'$set': {'_id':tweet.id, 'user_id':tweet.author.id, 'following':tweet.author.id in followers,'raffle_id':self.Params['_id'], 'body':tweet.text, 'username':tweet.author.screen_name, 'profile_img':tweet.author.profile_image_url_https } }, True )

    def getParams(self):
        return self.Params

    def checkAlive(self):
        return self.alive
<|endoftext|>


<commit_before>from pytmx import TiledImageLayer, TiledTileLayer, TiledObjectGroup
from pytmx.util_pyglet import load_pyglet

from lib.pyglet.gfx import Gfx

class TiledMap(object):
    def __init__(self, filename):
        tm = load_pyglet(filename)
        self.size = tm.width * tm.tilewidth, tm.height * tm.tileheight
        self.tmx_data = tm
        # self.sprites = []  # container for tiles
        # self.generate_sprites()

    def draw_rect(self, color, rect, width):
        pass

    def draw_lines(self, color, closed, points, width):
        pass

    def generate_sprites(self):
        tw = self.tmx_data.tilewidth
        th = self.tmx_data.tileheight
        mw = self.tmx_data.width
        mh = self.tmx_data.height - 1
        pixel_height = (mh + 1) * th
        draw_rect = self.draw_rect
        draw_lines = self.draw_lines

        rect_color = (255, 0, 0)
        poly_color = (0, 255, 0)

        for layer in self.tmx_data.visible_layers:
            # draw map tile layers
            if isinstance(layer, TiledTileLayer):

                # iterate over the tiles in the layer
                for x, y, image in layer.tiles():
                    y = mh - y
                    x = x * tw
                    y = y * th
                    # TODO

            # draw object layers
            elif isinstance(layer, TiledObjectGroup):

                # iterate over all the objects in the layer
                for obj in layer:
                    # logger.info(obj)

                    # objects with points are polygons or lines
                    if hasattr(obj, 'points'):
                        draw_lines(poly_color, obj.closed, obj.points, 3)

                    # some object have an image
                    elif obj.image:
                        # obj.image.blit(0,0)
                        if obj.x >= 0 and obj.x < 600 and obj.y >= -0 and obj.y < 600:
                            obj.image.blit(obj.x, Gfx.screen_height - obj.y - obj.height)

                    # draw a rect for everything else
                    else:
                        draw_rect(rect_color,
                                  (obj.x, obj.y, obj.width, obj.height), 3)

            # draw image layers
            elif isinstance(layer, TiledImageLayer):
                if layer.image:
                    x = mw // 2  # centers image
                    y = mh // 2
                    # TODO

    def draw(self):
        self.generate_sprites()
<commit_msg>Class renamed<commit_after>from pytmx import TiledImageLayer, TiledTileLayer, TiledObjectGroup
from pytmx.util_pyglet import load_pyglet

from lib.pyglet.gfx import Gfx

class TMXMap(object):
    def __init__(self, filename):
        tm = load_pyglet(filename)
        self.size = tm.width * tm.tilewidth, tm.height * tm.tileheight
        self.tmx_data = tm
        # self.sprites = []  # container for tiles
        # self.generate_sprites()

    def draw_rect(self, color, rect, width):
        pass

    def draw_lines(self, color, closed, points, width):
        pass

    def generate_sprites(self):
        tw = self.tmx_data.tilewidth
        th = self.tmx_data.tileheight
        mw = self.tmx_data.width
        mh = self.tmx_data.height - 1
        pixel_height = (mh + 1) * th
        draw_rect = self.draw_rect
        draw_lines = self.draw_lines

        rect_color = (255, 0, 0)
        poly_color = (0, 255, 0)

        for layer in self.tmx_data.visible_layers:
            # draw map tile layers
            if isinstance(layer, TiledTileLayer):

                # iterate over the tiles in the layer
                for x, y, image in layer.tiles():
                    y = mh - y
                    x = x * tw
                    y = y * th
                    # TODO

            # draw object layers
            elif isinstance(layer, TiledObjectGroup):

                # iterate over all the objects in the layer
                for obj in layer:
                    # logger.info(obj)

                    # objects with points are polygons or lines
                    if hasattr(obj, 'points'):
                        draw_lines(poly_color, obj.closed, obj.points, 3)

                    # some object have an image
                    elif obj.image:
                        # obj.image.blit(0,0)
                        if obj.x >= 0 and obj.x < 600 and obj.y >= -0 and obj.y < 600:
                            obj.image.blit(obj.x, Gfx.screen_height - obj.y - obj.height)

                    # draw a rect for everything else
                    else:
                        draw_rect(rect_color,
                                  (obj.x, obj.y, obj.width, obj.height), 3)

            # draw image layers
            elif isinstance(layer, TiledImageLayer):
                if layer.image:
                    x = mw // 2  # centers image
                    y = mh // 2
                    # TODO

    def draw(self):
        self.generate_sprites()
<|endoftext|>


<commit_before>PACKAGE_VERSION = '3.1.7'     # version of the client package
PROTOCOL_VERSION = '1.2'     # protocol version requested

# The hash of the mnemonic seed must begin with this
SEED_PREFIX      = '01'      # Standard wallet

def seed_prefix(seed_type):
    assert seed_type == 'standard'
    return SEED_PREFIX
<commit_msg>change 3.1.7 to 3.2<commit_after>PACKAGE_VERSION = '3.2'     # version of the client package
PROTOCOL_VERSION = '1.2'     # protocol version requested

# The hash of the mnemonic seed must begin with this
SEED_PREFIX      = '01'      # Standard wallet

def seed_prefix(seed_type):
    assert seed_type == 'standard'
    return SEED_PREFIX
<|endoftext|>


<commit_before>ELECTRUM_VERSION = "1.7.4"  # version of the client package
PROTOCOL_VERSION = '0.6'    # protocol version requested
SEED_VERSION     = 4        # bump this every time the seed generation is modified
TRANSLATION_ID   = 4073     # version of the wiki page
<commit_msg>version 1.8<commit_after>ELECTRUM_VERSION = "1.8"    # version of the client package
PROTOCOL_VERSION = '0.6'    # protocol version requested
SEED_VERSION     = 4        # bump this every time the seed generation is modified
TRANSLATION_ID   = 4073     # version of the wiki page
<|endoftext|>


<commit_before>import socket
import select
import sys
from data import hexdump
import ansi

class Sock(object):

    """
    wrapper for a socket that's smart
    functions for pretty printing the dialog
    read_until_string
    read_all (for slow connections)
    """
    def __init__(self, host, port, family=socket.AF_INET, stype=socket.SOCK_STREAM, timeout=.1, verbose=True, newline='', color=True):
        self.socket = socket.socket(family, stype)
        self.socket.settimeout(timeout)
        self.socket.connect((host,port))
        self._timeout = timeout
        self._family = family
        self._stype = stype
        self._host = host
        self._port = port
        self._tail = ""
        self._recv_color = ansi.ANSI_F_CYAN if color else ''
        self._send_color = ansi.ANSI_F_MAGENTA if color else ''
        self.history = []
        self.verbose = verbose
        self.newline = newline

    def recv(self, count=None, timeout=None):
        """Alias for read()"""
        return self.read(count)

    def read(self, count=None, timeout=None):
        """read up to count bytes, if specified, otherwise read all data available"""
        if timeout:
            # set timeout for this request
            self.socket.settimeout(timeout)
        data = self._tail
        if count:
            data = self._read(count)
        else:
            data = self._readall()
        if self.verbose:
            sys.stdout.write(self._recv_color + data + ansi.ANSI_ENDC)
            #print hexdump(data)

        # reset timeout, append to history, truncate tail
        self.socket.settimeout(self._timeout)
        self.history.append(('>',data))
        self._tail = ''

        return data

    def _read(self, count=4096):
        data = ''
        try:
            data = self.socket.recv(count)

        except socket.timeout:
            # timeout doesn't necesarily mean the connection is closed
            pass

        return data

    def _readall(self):
        # read all data from sock
        data = ""
        chunk = ""
        flag = True
        while flag:
            try:
                chunk = self.socket.recv(1024)
            except socket.timeout:
                chunk = ""
            finally:
                if len(chunk) > 0:
                    data += chunk
                    chunk = ""
                else:
                    flag = False

        return data

    def recvuntil(self, premark, postmark="\n"):
        """
        Read until premark in mesg. Return string between premark and postmark.
        TODO: add support for regex
        """
        # self._tail caches data read but not yet returned. May be part of next read
        while not premark in self._tail:
            self._tail += self._readall()

        start = self._tail.find(premark) + len(premark)
        end = self._tail.find(postmark, start)
        sub = self._tail[start:end]
        self._tail = self._tail[end:]

        return sub

    def send(self, data):
        """alias for write()"""
        self.write(data)

    def sendline(self, data):
        """write(data + "\n")"""
        self.write(data, newline="\n")

    def sr(self, data=''):
        """send(data) && return recv()"""
        if data:
            self.write(data)
        return self.read()

    def write(self, data, newline=''):
        newline = newline or self.newline
        try:
            data += newline
            self.socket.send(data)
            self.history.append(('<',data))

            if self.verbose:
                sys.stdout.write(self._send_color + data + ansi.ANSI_ENDC)
                #print hexdump(data)

            return data

        except Exception as e:
            self.socket.close()
            self._closed = True

    def close(self):
        self.socket.close()

    def reconnect(self):
        self.socket.close()
        self.socket = socket.socket(self._family, self._stype)
        self.socket.settimeout(2)
        self.socket.connect((self._host,self._port))

    def interact(self, newline="\n"):

        while True:
            try:
                # It would be nice to have a prompt, but it's tricky to add
                #sys.stdout.write('$> ')
                #sys.stdout.flush()

                # Wait for input from stdin & socket
                input_ready,output_ready,except_ready = select.select([sys.stdin, self.socket], [],[])

                # loop over the file handles with input
                for i in input_ready:
                    if i == sys.stdin:
                        data = sys.stdin.readline()
                        # patch up newlines
                        if not data.endswith(newline):
                            data = data.strip('\n') + newline

                        if data:
                            self.history.append(('<', data))
                            self.socket.send(data)
                    elif i == self.socket:
                        data = self.socket.recv(1024)
                        if data:
                            self.history.append(('>', data))
                            sys.stdout.write(data)
                            sys.stdout.flush()

            except KeyboardInterrupt:
                break

            except socket.error as msg:
                print msg
                break

<commit_msg>Adding ipv6 support to Sock<commit_after>import socket
import select
import sys
from data import hexdump
import ansi

class Sock(object):

    """
    wrapper for a socket that's smart
    functions for pretty printing the dialog
    read_until_string
    read_all (for slow connections)
    """
    def __init__(self, host, port, verbose=True, timeout=.1, newline='', color=True):
        socket.setdefaulttimeout(timeout)
	# supports ipv4 and ipv6
        self.socket = socket.create_connection((host,port))
        self._timeout = timeout
        self._host = host
        self._port = port
        self._tail = ""
        self._recv_color = ansi.ANSI_F_CYAN if color else ''
        self._send_color = ansi.ANSI_F_MAGENTA if color else ''
        self.history = []
        self.verbose = verbose
        self.newline = newline

    def recv(self, count=None, timeout=None):
        """Alias for read()"""
        return self.read(count)

    def read(self, count=None, timeout=None):
        """read up to count bytes, if specified, otherwise read all data available"""
        if timeout:
            # set timeout for this request
            self.socket.settimeout(timeout)
        data = self._tail
        if count:
            data = self._read(count)
        else:
            data = self._readall()
        if self.verbose:
            sys.stdout.write(self._recv_color + data + ansi.ANSI_ENDC)
            #print hexdump(data)

        # reset timeout, append to history, truncate tail
        self.socket.settimeout(self._timeout)
        self.history.append(('>',data))
        self._tail = ''

        return data

    def _read(self, count=4096):
        data = ''
        try:
            data = self.socket.recv(count)

        except socket.timeout:
            # timeout doesn't necesarily mean the connection is closed
            pass

        return data

    def _readall(self):
        # read all data from sock
        data = ""
        chunk = ""
        flag = True
        while flag:
            try:
                chunk = self.socket.recv(1024)
            except socket.timeout:
                chunk = ""
            finally:
                if len(chunk) > 0:
                    data += chunk
                    chunk = ""
                else:
                    flag = False

        return data

    def recvuntil(self, premark, postmark="\n"):
        """
        Read until premark in mesg. Return string between premark and postmark.
        TODO: add support for regex
        """
        # self._tail caches data read but not yet returned. May be part of next read
        while not premark in self._tail:
            self._tail += self._readall()

        start = self._tail.find(premark) + len(premark)
        end = self._tail.find(postmark, start)
        sub = self._tail[start:end]
        self._tail = self._tail[end:]

        return sub

    def send(self, data):
        """alias for write()"""
        self.write(data)

    def sendline(self, data):
        """write(data + "\n")"""
        self.write(data, newline="\n")

    def sr(self, data=''):
        """send(data) && return recv()"""
        if data:
            self.write(data)
        return self.read()

    def write(self, data, newline=''):
        newline = newline or self.newline
        try:
            data += newline
            self.socket.send(data)
            self.history.append(('<',data))

            if self.verbose:
                sys.stdout.write(self._send_color + data + ansi.ANSI_ENDC)
                #print hexdump(data)

            return data

        except Exception as e:
            self.socket.close()
            self._closed = True

    def close(self):
        self.socket.close()

    def reconnect(self):
        self.socket.close()
        self.socket = socket.socket(self._family, self._stype)
        self.socket.settimeout(2)
        self.socket.connect((self._host,self._port))

    def interact(self, newline="\n"):

        while True:
            try:
                # It would be nice to have a prompt, but it's tricky to add
                #sys.stdout.write('$> ')
                #sys.stdout.flush()

                # Wait for input from stdin & socket
                input_ready,output_ready,except_ready = select.select([sys.stdin, self.socket], [],[])

                # loop over the file handles with input
                for i in input_ready:
                    if i == sys.stdin:
                        data = sys.stdin.readline()
                        # patch up newlines
                        if not data.endswith(newline):
                            data = data.strip('\n') + newline

                        if data:
                            self.history.append(('<', data))
                            self.socket.send(data)
                    elif i == self.socket:
                        data = self.socket.recv(1024)
                        if data:
                            self.history.append(('>', data))
                            sys.stdout.write(data)
                            sys.stdout.flush()

            except KeyboardInterrupt:
                break

            except socket.error as msg:
                print msg
                break

<|endoftext|>


<commit_before>from keywords import var_cache, keyword

@keyword('get_view')
def get_view(view_id):
    """

    :param view_id:
    :return:
    """
    return var_cache['proxy'].get_view(view_id)

@keyword('click_on_text')
def click_on_text(text):
    """

    :param text:
    :return:
    """
    return var_cache['proxy'].click_on_text(text)

@keyword('click_on_view')
def click_on_view(view):
    """

    :param view:
    :return:
    """
    return var_cache['proxy'].click_on_view(view)

@keyword('launch_app')
def launch_app(package_name):
    """

    :param package_name:
    :return:
    """
    return var_cache['proxy'].launch_app(package_name)

@keyword('wait_for_text')
def wait_for_text(text):
    """
    wait for text
    :param text:
    :return:
    """
    return var_cache['proxy'].wait_for_text(text)

@keyword('get_list_item')
def get_list_item(view, index):
    """
    get item from listView by index
    :param view:
    :param index:
    :return:
    """
    return var_cache['proxy'].get_list_item(view, index)

@keyword('load_more')
def load_more(view):
    """
    load more
    :param view:
    :return:
    """
    return var_cache['proxy'].load_more(view)

@keyword('refresh_content')
def refresh_content(view):
    """
    refresh content for listView
    :param view:
    :return:
    """
    return var_cache['proxy'].refresh_content(view)

@keyword('find_view_by_id')
def find_view_by_id(parent_view, view_id):
    """
    get view by id
    :param parent_view:
    :param view_id:
    :return:
    """
    return var_cache['proxy'].find_view_by_id(parent_view, view_id)

@keyword('switch_to_tab')
def switch_to_tab(view, index):
    """
    switch to tab
    :param view:
    :param index:
    :return:
    """
    return var_cache['proxy'].switch_to_tab(view, index)

@keyword('enter_text')
def enter_text(view, text):
    """
    enter text into TextView
    :param view:
    :param text:
    :return:
    """
    return var_cache['proxy'].enter_text(view, text)

@keyword('get_list_count')
def get_list_count(view):
    """
    get listView's count
    :param view:
    :return:
    """
    return var_cache['proxy'].get_list_count(view)
<commit_msg>update keyword "get_view" -> "get_list_view" on version=1<commit_after>from keywords import var_cache, keyword

@keyword('get_list_view')
def get_view(view_id):
    """

    :param view_id:
    :return:
    """
    return var_cache['proxy'].get_view(view_id)

@keyword('click_on_text')
def click_on_text(text):
    """

    :param text:
    :return:
    """
    return var_cache['proxy'].click_on_text(text)

@keyword('click_on_view')
def click_on_view(view):
    """

    :param view:
    :return:
    """
    return var_cache['proxy'].click_on_view(view)

@keyword('launch_app')
def launch_app(package_name):
    """

    :param package_name:
    :return:
    """
    return var_cache['proxy'].launch_app(package_name)

@keyword('wait_for_text')
def wait_for_text(text):
    """
    wait for text
    :param text:
    :return:
    """
    return var_cache['proxy'].wait_for_text(text)

@keyword('get_list_item')
def get_list_item(view, index):
    """
    get item from listView by index
    :param view:
    :param index:
    :return:
    """
    return var_cache['proxy'].get_list_item(view, index)

@keyword('load_more')
def load_more(view):
    """
    load more
    :param view:
    :return:
    """
    return var_cache['proxy'].load_more(view)

@keyword('refresh_content')
def refresh_content(view):
    """
    refresh content for listView
    :param view:
    :return:
    """
    return var_cache['proxy'].refresh_content(view)

@keyword('find_view_by_id')
def find_view_by_id(parent_view, view_id):
    """
    get view by id
    :param parent_view:
    :param view_id:
    :return:
    """
    return var_cache['proxy'].find_view_by_id(parent_view, view_id)

@keyword('switch_to_tab')
def switch_to_tab(view, index):
    """
    switch to tab
    :param view:
    :param index:
    :return:
    """
    return var_cache['proxy'].switch_to_tab(view, index)

@keyword('enter_text')
def enter_text(view, text):
    """
    enter text into TextView
    :param view:
    :param text:
    :return:
    """
    return var_cache['proxy'].enter_text(view, text)

@keyword('get_list_count')
def get_list_count(view):
    """
    get listView's count
    :param view:
    :return:
    """
    return var_cache['proxy'].get_list_count(view)
<|endoftext|>


<commit_before>#!/usr/bin/env python
from __future__ import print_function
from __future__ import unicode_literals

class Node(object):

    def __init__(self, value):
        self._val = value
        self._next = None

    @property
    def next(self):
        return self._next

    @next.setter
    def next(self, value):
        self._next = value

    @property
    def val(self):
        return self._val

class LinkedList(object):

    def __init__(self):
        self._head = None

    def __repr__(self):
        z = 1
        a = self._head
        node_list = "("
        while z:
            if not a:
                z = 0
                node_list += ")"
            elif isinstance(a.val, str) or isinstance(a.val, unicode):
                if not a.next:
                    z = 0
                    node_list += "'{}')".format(a.val)
                else:
                    node_list += "'{}', ".format(a.val)
                    a = a.next
            else:
                if not a.next:
                    z = 0
                    node_list += "{})".format(a.val)
                else:
                    node_list += "{}, ".format(a.val)
                    a = a.next
        return node_list

    @property
    def head(self):
        return self._head

    def insert(self, val):
        if not self._head:
            self._head = Node(val)
            self._head.next = None
        else:
            b = self._head
            self._head = Node(val)
            self._head.next = b

    def pop(self):
        val = self._head.val
        self._head = self._head.next
        return val

    def size(self):
        i = 0
        if not self._head:
            return i
        else:
            i = 1
            if not self._head.next:
                return i
            else:
                i = 2
                z = 1
                a = self._head.next
                while z:
                    if not a.next:
                        z = 0
                        return i
                    else:
                        a = a.next
                        i += 1

    def search(self, value):
        z = 1
        a = self._head
        while z:
            if a.val == value:
                z = 0
                return a
            elif not a.next:
                z = 0
                return None
            else:
                a = a.next

    def remove(self, node):
        z = 1
        a = self._head
        while z:
            if self._head == node:
                z = 0
                self._head = self._head.next
            elif not a.next:
                z = 0
            elif a.next == node:
                z = 0
                a.next = node.next
            else:
                a = a.next

    def display(self):
        print(repr(self))
<commit_msg>Constantine: fixed search to handle empty list, v2<commit_after>#!/usr/bin/env python
from __future__ import print_function
from __future__ import unicode_literals

class Node(object):

    def __init__(self, value):
        self._val = value
        self._next = None

    @property
    def next(self):
        return self._next

    @next.setter
    def next(self, value):
        self._next = value

    @property
    def val(self):
        return self._val

class LinkedList(object):

    def __init__(self):
        self._head = None

    def __repr__(self):
        z = 1
        a = self._head
        node_list = "("
        while z:
            if not a:
                z = 0
                node_list += ")"
            elif isinstance(a.val, str) or isinstance(a.val, unicode):
                if not a.next:
                    z = 0
                    node_list += "'{}')".format(a.val)
                else:
                    node_list += "'{}', ".format(a.val)
                    a = a.next
            else:
                if not a.next:
                    z = 0
                    node_list += "{})".format(a.val)
                else:
                    node_list += "{}, ".format(a.val)
                    a = a.next
        return node_list

    @property
    def head(self):
        return self._head

    def insert(self, val):
        if not self._head:
            self._head = Node(val)
            self._head.next = None
        else:
            b = self._head
            self._head = Node(val)
            self._head.next = b

    def pop(self):
        val = self._head.val
        self._head = self._head.next
        return val

    def size(self):
        i = 0
        if not self._head:
            return i
        else:
            i = 1
            if not self._head.next:
                return i
            else:
                i = 2
                z = 1
                a = self._head.next
                while z:
                    if not a.next:
                        z = 0
                        return i
                    else:
                        a = a.next
                        i += 1

    def search(self, value):
        z = 1
        a = self._head
        while z:
            if not a:
                z = 0
                return None
            elif a.val == value:
                z = 0
                return a
            elif not a.next:
                z = 0
                return None
            else:
                a = a.next

    def remove(self, node):
        z = 1
        a = self._head
        while z:
            if self._head == node:
                z = 0
                self._head = self._head.next
            elif not a.next:
                z = 0
            elif a.next == node:
                z = 0
                a.next = node.next
            else:
                a = a.next

    def display(self):
        print(repr(self))
<|endoftext|>


<commit_before>#!/usr/bin/env python
from __future__ import print_function
from __future__ import unicode_literals

class Node(object):

    def __init__(self, value=None, next=None):
        self.val = value
        self.next = next

class LinkedList(object):

    def __init__(self):
        self.head = None

    def insert(self, val):
        if not self._head:
            self._head = Node(val)
            self._head.next = None
        else:
            b = self._head
            self._head = Node(val)
            self._head.next = b

    def pop(self):
        if not self._head:
            return None
        else:
            val = self._head.val
            self._head = self._head.next
            return val

    def size(self):
        i = 0
        if not self._head:
            return i
        else:
            i = 1
            if not self._head.next:
                return i
            else:
                i = 2
                z = 1
                a = self._head.next
                while z:
                    if not a.next:
                        z = 0
                        return i
                    else:
                        a = a.next
                        i += 1

    def search(self, value):
        z = 1
        a = self._head
        while z:
            if not a:
                z = 0
                return None
            elif a.val == value:
                z = 0
                return a
            elif not a.next:
                z = 0
                return None
            else:
                a = a.next

    def remove(self, node):
        z = 1
        a = self._head
        while z:
            if self._head == node:
                z = 0
                self._head = self._head.next
            elif not a.next:
                z = 0
            elif a.next == node:
                z = 0
                a.next = node.next
            else:
                a = a.next

    def display(self):
        print(repr(self))

    def __repr__(self):
        z = 1
        a = self._head
        node_list = "("
        while z:
            if not a:
                z = 0
                node_list += ")"
            elif isinstance(a.val, str) or isinstance(a.val, unicode):
                if not a.next:
                    z = 0
                    node_list += "'{}')".format(a.val)
                else:
                    node_list += "'{}', ".format(a.val)
                    a = a.next
            else:
                if not a.next:
                    z = 0
                    node_list += "{})".format(a.val)
                else:
                    node_list += "{}, ".format(a.val)
                    a = a.next
        return node_list
<commit_msg>remove redundant assignment of None to next.<commit_after>#!/usr/bin/env python
from __future__ import print_function
from __future__ import unicode_literals

class Node(object):

    def __init__(self, value=None, next=None):
        self.val = value
        self.next = next

class LinkedList(object):

    def __init__(self):
        self.head = None

    def insert(self, val):
        if not self._head:
            self._head = Node(val)
        else:
            b = self._head
            self._head = Node(val)
            self._head.next = b

    def pop(self):
        if not self._head:
            return None
        else:
            val = self._head.val
            self._head = self._head.next
            return val

    def size(self):
        i = 0
        if not self._head:
            return i
        else:
            i = 1
            if not self._head.next:
                return i
            else:
                i = 2
                z = 1
                a = self._head.next
                while z:
                    if not a.next:
                        z = 0
                        return i
                    else:
                        a = a.next
                        i += 1

    def search(self, value):
        z = 1
        a = self._head
        while z:
            if not a:
                z = 0
                return None
            elif a.val == value:
                z = 0
                return a
            elif not a.next:
                z = 0
                return None
            else:
                a = a.next

    def remove(self, node):
        z = 1
        a = self._head
        while z:
            if self._head == node:
                z = 0
                self._head = self._head.next
            elif not a.next:
                z = 0
            elif a.next == node:
                z = 0
                a.next = node.next
            else:
                a = a.next

    def display(self):
        print(repr(self))

    def __repr__(self):
        z = 1
        a = self._head
        node_list = "("
        while z:
            if not a:
                z = 0
                node_list += ")"
            elif isinstance(a.val, str) or isinstance(a.val, unicode):
                if not a.next:
                    z = 0
                    node_list += "'{}')".format(a.val)
                else:
                    node_list += "'{}', ".format(a.val)
                    a = a.next
            else:
                if not a.next:
                    z = 0
                    node_list += "{})".format(a.val)
                else:
                    node_list += "{}, ".format(a.val)
                    a = a.next
        return node_list
<|endoftext|>


<commit_before>import argparse
import logging

from linkins import link, util

log = logging.getLogger(__name__)

def parse_args():
    parser = argparse.ArgumentParser(
        description=(
            'Link a directory structure, execute user-defined '
            'scripts and safely backup existing files'
        )
    )
    parser.add_argument(
        'srcdir',
        metavar='TARGET_DIR',
        type=str,
        help='path to the directory structure to be linked',
    )
    parser.add_argument(
        'linkdir',
        metavar='LINK_DIR',
        type=str,
        help='path to the directory where the links are to be created',
    )
    parser.add_argument(
        '-s',
        '--script',
        default='linkins-script',
        type=str,
        help=(
            'name of the script that can be executed at each level '
            'of the target directory hierarchy. Scripts are never '
            'linked (default: %(default)s)'
            ),
        )
    parser.add_argument(
        '-r',
        '--run',
        action='store_true',
        default=False,
        help=(
            'run the script (defined by --script) at each level of the '
            'target directory hierarchy it is found (default: '
            '%(default)s)'
            ),
        )
    parser.add_argument(
        '-p',
        '--replace',
        action='store_true',
        default=False,
        help='replace existing links (default: %(default)s)',
        )
    parser.add_argument(
        '-c',
        '--clean',
        action='store_true',
        default=False,
        help=(
            'remove existing links (and their empty parent '
            'directories). Supersedes all linking and '
            'script options (default: %(default)s)'
            ),
        )
    parser.add_argument(
        '-m',
        '--multiprocess',
        action='store_true',
        default=False,
        help='run scripts as subprocesses (default: %(default)s)',
        )
    loggroup = parser.add_mutually_exclusive_group()
    loggroup.add_argument(
        '-v',
        '--verbose',
        action='store_true',
        default=False,
        help='output DEBUG logging statements (default: %(default)s)',
        )
    loggroup.add_argument(
        '-q',
        '--quiet',
        action='store_true',
        default=False,
        help=(
            'only output ERROR and FATAL logging statements '
            '(default: %(default)s)'
        )
    )
    args = parser.parse_args()
    return args

def main():
    args = parse_args()
    level = logging.INFO
    # Only one of verbose and quiet will be set
    if args.verbose:
        level = logging.DEBUG
    if args.quiet:
        level = logging.ERROR
    logging.basicConfig(
        level=level,
        format='%(name)s: %(levelname)s: %(message)s',
        )

    srcdir = util.abs_path(args.srcdir)
    linkdir = util.abs_path(args.linkdir)
    log.debug(
        'Processing links from "{srcdir}" to "{linkdir}"...'.format(
            srcdir=srcdir,
            linkdir=linkdir,
        )
    )
    link.make(
        srcdir=srcdir,
        linkdir=linkdir,
        scriptname=args.script,
        runscript=args.run,
        replace=args.replace,
        clean=args.clean,
        multiprocess=args.multiprocess,
        )
<commit_msg>Better description for clean cli option<commit_after>import argparse
import logging

from linkins import link, util

log = logging.getLogger(__name__)

def parse_args():
    parser = argparse.ArgumentParser(
        description=(
            'Link a directory structure, execute user-defined '
            'scripts and safely backup existing files'
        )
    )
    parser.add_argument(
        'srcdir',
        metavar='TARGET_DIR',
        type=str,
        help='path to the directory structure to be linked',
    )
    parser.add_argument(
        'linkdir',
        metavar='LINK_DIR',
        type=str,
        help='path to the directory where the links are to be created',
    )
    parser.add_argument(
        '-s',
        '--script',
        default='linkins-script',
        type=str,
        help=(
            'name of the script that can be executed at each level '
            'of the target directory hierarchy. Scripts are never '
            'linked (default: %(default)s)'
            ),
        )
    parser.add_argument(
        '-r',
        '--run',
        action='store_true',
        default=False,
        help=(
            'run the script (defined by --script) at each level of the '
            'target directory hierarchy it is found (default: '
            '%(default)s)'
            ),
        )
    parser.add_argument(
        '-p',
        '--replace',
        action='store_true',
        default=False,
        help='replace existing links (default: %(default)s)',
        )
    parser.add_argument(
        '-c',
        '--clean',
        action='store_true',
        default=False,
        help=(
            'remove existing links (and their empty parent '
            'directories). Supersedes --replace and --run'
            '(default: %(default)s)'
            ),
        )
    parser.add_argument(
        '-m',
        '--multiprocess',
        action='store_true',
        default=False,
        help='run scripts as subprocesses (default: %(default)s)',
        )
    loggroup = parser.add_mutually_exclusive_group()
    loggroup.add_argument(
        '-v',
        '--verbose',
        action='store_true',
        default=False,
        help='output DEBUG logging statements (default: %(default)s)',
        )
    loggroup.add_argument(
        '-q',
        '--quiet',
        action='store_true',
        default=False,
        help=(
            'only output ERROR and FATAL logging statements '
            '(default: %(default)s)'
        )
    )
    args = parser.parse_args()
    return args

def main():
    args = parse_args()
    level = logging.INFO
    # Only one of verbose and quiet will be set
    if args.verbose:
        level = logging.DEBUG
    if args.quiet:
        level = logging.ERROR
    logging.basicConfig(
        level=level,
        format='%(name)s: %(levelname)s: %(message)s',
        )

    srcdir = util.abs_path(args.srcdir)
    linkdir = util.abs_path(args.linkdir)
    log.debug(
        'Processing links from "{srcdir}" to "{linkdir}"...'.format(
            srcdir=srcdir,
            linkdir=linkdir,
        )
    )
    link.make(
        srcdir=srcdir,
        linkdir=linkdir,
        scriptname=args.script,
        runscript=args.run,
        replace=args.replace,
        clean=args.clean,
        multiprocess=args.multiprocess,
        )
<|endoftext|>


<commit_before>from django.contrib import admin
from django.contrib.contenttypes import generic
from django.contrib import messages

from links.models import ObjectLink, ExternalLink, ExternalSite, LinkType

# imports for FK search box
from django.db.models import ForeignKey
#from arkestra_utilities.widgets.widgets import ForeignKeySearchInput, GenericForeignKeySearchInput
#from arkestra_utilities.views import search
from arkestra_utilities.admin import SupplyRequestMixin

from widgetry import fk_lookup
from widgetry.views import search

from django import forms

#LINK_SCHEMA = getattr(settings, 'LINK_SCHEMA', {})

from links import schema
from urlparse import urlparse
from urllib import urlopen

class LinkAdmin(admin.ModelAdmin):
    related_search_fields = ['destination_content_type']

    def formfield_for_dbfield(self, db_field, **kwargs):
        """
        Overrides the default widget for Foreignkey fields if they are
        specified in the related_search_fields class attribute.
        """
        if isinstance(db_field, ForeignKey) and \
                db_field.name in self.related_search_fields:
            kwargs['widget'] = fk_lookup.FkLookup(db_field.rel.to)
        return super(LinkAdmin, self).formfield_for_dbfield(db_field, **kwargs)

class ObjectLinkInlineForm(forms.ModelForm):
    class Meta:
        model=ObjectLink

    def __init__(self, *args, **kwargs):
        super(ObjectLinkInlineForm, self).__init__(*args, **kwargs)
        if self.instance.pk is not None:
            if self.instance.destination_content_type:
                destination_content_type = self.instance.destination_content_type.model_class()
        else:
            destination_content_type = None
        #self.fields['destination_object_id'].widget = GenericForeignKeySearchInput(LINK_SCHEMA, 'id_%s-destination_content_type' % self.prefix, destination_content_type)
        self.fields['destination_object_id'].widget = fk_lookup.GenericFkLookup('id_%s-destination_content_type' % self.prefix, destination_content_type)
        self.fields['destination_content_type'].widget.choices = schema.content_type_choices()

class ObjectLinkAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('destination_content_type', 'destination_object_id',),
            }),
        ('Additional options', {
            'fields': ('text_override', 'description_override', 'html_title_attribute',),
            'classes': ('collapse',),
            }),
        )

class ObjectLinkInline(generic.GenericStackedInline):
    model = ObjectLink
    form = ObjectLinkInlineForm
    extra = 3
    fieldsets = (
        (None, {
            'fields': (
                'destination_content_type', 'destination_object_id',
                ('include_description'),
            ),
        }),
        ('Overrides', {
            'fields': ('metadata_override', 'heading_override', 'text_override','description_override', 'html_title_attribute',
            ),
            'classes': ('collapse',),
        })
    )

def get_or_create_external_link(request, input_url, external_url, title, description=""):
    """
    When provided with candidate attributes for an ExternalLink object, will:
    * return the URL of an ExternalLink that matches
    * create an ExternalLink if there's no match, then return its URL
    """
    if input_url or external_url:
        # run checks - doesn't return anything
        check_urls(request, input_url or external_url.url, ["https", "http"])

    if input_url:
        # get or create the external_link based on the url
        external_url, created = ExternalLink.objects.get_or_create(url=input_url, defaults = {
            "url": input_url,
            "title": title,
            "description": description,
        })

        if created:
            message = "A link for this item has been added to the External Links database: %s." % external_url.url
            messages.add_message(request, messages.INFO, message)
        else:
            message = "Using existing External Link: %s." % external_url.url
            messages.add_message(request, messages.INFO, message)

    elif external_url:
        message = "This is an external item: %s." % external_url.url
        messages.add_message(request, messages.INFO, message)

    return external_url

def check_urls(request, url, allowed_schemes = None):
    """
    Checks and reports on a URL that might end up in the database
    """
    allowed_schemes = allowed_schemes or [kind.scheme for kind in LinkType.objects.all()]
    # parse the url and get some attributes
    purl = urlparse(url)
    scheme = purl.scheme

    # make sure it's a kind we allow before anything else
    if not scheme in allowed_schemes:
        permitted_schemes = (", ".join(allowed_schemes[:-1]) + " and " + allowed_schemes[-1]) if len(allowed_schemes) > 1 else allowed_schemes[0]
        if scheme:
            message = "Sorry, link type %s is not permitted. Permitted types are %s." % (scheme, permitted_schemes)
        else:
            message = "Please provide a complete URL of the form http://example.com/. Permitted schemes are %s." % permitted_schemes

        raise forms.ValidationError(message)

    # for hypertext types only
    if str(scheme) == "http" or scheme == "https":
        # can we reach the domain?
        try:
            url_test = urlopen(url)
        except IOError:
            message = "Hostname " + purl.netloc + " not found. Please check that it is correct."
            raise forms.ValidationError(message)

        # check for a 404 (needs python 2.6)
        if url_test.getcode() == 404:
            message = "Warning: the link %s appears not to work. Please check that it is correct." %url
            messages.add_message(request, messages.WARNING, message)

        # check for a redirect
        if url_test.geturl() != url:
            message = "Warning: your URL " + url + " doesn't match the site's, which is: " + url_test.geturl()
            messages.add_message(request, messages.WARNING, message)

    # for mailto types only
    elif str(scheme) == "mailto":
        message = "Warning: this email address hasn't been checked. I hope it's correct."
        messages.add_message(request, messages.WARNING, message)

class ExternalLinkForm(forms.ModelForm):
    class Meta:
        model = ExternalLink

    def clean(self):
        url = self.cleaned_data.get('url', "")
        title = self.cleaned_data.get('title', "")

        # check if the url is a duplicate
        # if the url exists, and this would be a new instance in the database, it's a duplicate
        print "ExternalLinkForm self.instance.pk", self.instance
        if self.Meta.model.objects.filter(url=url) and not self.instance.pk:
            message = "Sorry, this link appears to exist already"
            raise forms.ValidationError(message)

        # warn if the title is a duplicate
        # if the title exists, and this would be a new instance in the database, it's a duplicate
        if self.Meta.model.objects.filter(title=title) and not self.instance.pk:
            message = "Warning: the link title %s already exists in the database - consider changing it." %title
            messages.add_message(self.request, messages.WARNING, message)

        return self.cleaned_data

class ExternalLinkAdmin(SupplyRequestMixin, admin.ModelAdmin):
    readonly_fields = ('external_site', 'kind',)
    search_fields = ('title', 'external_site__site','description', 'url')
    list_display = ('title', 'url', )
    form = ExternalLinkForm

    def save_model(self, request, obj, form, change):
        return super(ExternalLinkAdmin, self).save_model(request, obj, form, change)

class LinkTypeAdmin(admin.ModelAdmin):
    pass

class ExternalSiteForm(forms.ModelForm):
    class Meta:
        model = ExternalSite

    def clean(self):
        # if the site isn't named, use the domain name
        site = self.cleaned_data.get("site", None)
        domain = self.cleaned_data.get("domain", None)
        if not site:
            self.cleaned_data["site"] = domain
        return self.cleaned_data

class ExternalSiteAdmin(admin.ModelAdmin):
    readonly_fields = ('parent',)
    form = ExternalSiteForm

    def changelist_view(self, request, extra_context=None):
        extra_context = extra_context or {}
        extra_context.update({
                'root_domains': ExternalSite.objects.filter(level = 0),
                # 'has_add_permission': request.user.has_perm('links.add_link'),
                # 'has_change_permission': request.user.has_perm('links.change_link'),
                # 'has_delete_permission': request.user.has_perm('links.delete_link'),
        })
        return super(ExternalSiteAdmin, self).changelist_view(request, extra_context)

#admin.site.register(ObjectLink, ObjectLinkAdmin)
admin.site.register(ExternalLink, ExternalLinkAdmin)
admin.site.register(ExternalSite, ExternalSiteAdmin)
admin.site.register(LinkType, LinkTypeAdmin)

"""
# patch the cms page admin to show generic links
if getattr(settings,'SHOW_GENERIC_LINKS_INLINE_FOR_CMS_PAGE_ADMIN', False):
    from cms.admin.pageadmin import PageAdmin
    from cms.models import Page
    admin.site.unregister(Page)
    print dir(admin.site)
    if not hasattr(PageAdmin,'inlines'):
        PageAdmin.inlines = []
    else:
        PageAdmin.inlines = list(PageAdmin.inlines)
    PageAdmin.inlines = PageAdmin.inlines + [ObjectLinkInline]
    admin.site.register(Page, PageAdmin)
"""
<commit_msg>fixed import error; silly<commit_after>from urlparse import urlparse
from urllib import urlopen

from django import forms
from django.db.models import ForeignKey
from django.contrib import admin, messages
from django.contrib.contenttypes import generic

from widgetry import fk_lookup
from widgetry.views import search

from arkestra_utilities.mixins import SupplyRequestMixin

from links.models import ObjectLink, ExternalLink, ExternalSite, LinkType
from links import schema

#LINK_SCHEMA = getattr(settings, 'LINK_SCHEMA', {})

class LinkAdmin(admin.ModelAdmin):
    related_search_fields = ['destination_content_type']

    def formfield_for_dbfield(self, db_field, **kwargs):
        """
        Overrides the default widget for Foreignkey fields if they are
        specified in the related_search_fields class attribute.
        """
        if isinstance(db_field, ForeignKey) and \
                db_field.name in self.related_search_fields:
            kwargs['widget'] = fk_lookup.FkLookup(db_field.rel.to)
        return super(LinkAdmin, self).formfield_for_dbfield(db_field, **kwargs)

class ObjectLinkInlineForm(forms.ModelForm):
    class Meta:
        model=ObjectLink

    def __init__(self, *args, **kwargs):
        super(ObjectLinkInlineForm, self).__init__(*args, **kwargs)
        if self.instance.pk is not None:
            if self.instance.destination_content_type:
                destination_content_type = self.instance.destination_content_type.model_class()
        else:
            destination_content_type = None
        #self.fields['destination_object_id'].widget = GenericForeignKeySearchInput(LINK_SCHEMA, 'id_%s-destination_content_type' % self.prefix, destination_content_type)
        self.fields['destination_object_id'].widget = fk_lookup.GenericFkLookup('id_%s-destination_content_type' % self.prefix, destination_content_type)
        self.fields['destination_content_type'].widget.choices = schema.content_type_choices()

class ObjectLinkAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('destination_content_type', 'destination_object_id',),
            }),
        ('Additional options', {
            'fields': ('text_override', 'description_override', 'html_title_attribute',),
            'classes': ('collapse',),
            }),
        )

class ObjectLinkInline(generic.GenericStackedInline):
    model = ObjectLink
    form = ObjectLinkInlineForm
    extra = 3
    fieldsets = (
        (None, {
            'fields': (
                'destination_content_type', 'destination_object_id',
                ('include_description'),
            ),
        }),
        ('Overrides', {
            'fields': ('metadata_override', 'heading_override', 'text_override','description_override', 'html_title_attribute',
            ),
            'classes': ('collapse',),
        })
    )

def get_or_create_external_link(request, input_url, external_url, title, description=""):
    """
    When provided with candidate attributes for an ExternalLink object, will:
    * return the URL of an ExternalLink that matches
    * create an ExternalLink if there's no match, then return its URL
    """
    if input_url or external_url:
        # run checks - doesn't return anything
        check_urls(request, input_url or external_url.url, ["https", "http"])

    if input_url:
        # get or create the external_link based on the url
        external_url, created = ExternalLink.objects.get_or_create(url=input_url, defaults = {
            "url": input_url,
            "title": title,
            "description": description,
        })

        if created:
            message = "A link for this item has been added to the External Links database: %s." % external_url.url
            messages.add_message(request, messages.INFO, message)
        else:
            message = "Using existing External Link: %s." % external_url.url
            messages.add_message(request, messages.INFO, message)

    elif external_url:
        message = "This is an external item: %s." % external_url.url
        messages.add_message(request, messages.INFO, message)

    return external_url

def check_urls(request, url, allowed_schemes = None):
    """
    Checks and reports on a URL that might end up in the database
    """
    allowed_schemes = allowed_schemes or [kind.scheme for kind in LinkType.objects.all()]
    # parse the url and get some attributes
    purl = urlparse(url)
    scheme = purl.scheme

    # make sure it's a kind we allow before anything else
    if not scheme in allowed_schemes:
        permitted_schemes = (", ".join(allowed_schemes[:-1]) + " and " + allowed_schemes[-1]) if len(allowed_schemes) > 1 else allowed_schemes[0]
        if scheme:
            message = "Sorry, link type %s is not permitted. Permitted types are %s." % (scheme, permitted_schemes)
        else:
            message = "Please provide a complete URL of the form http://example.com/. Permitted schemes are %s." % permitted_schemes

        raise forms.ValidationError(message)

    # for hypertext types only
    if str(scheme) == "http" or scheme == "https":
        # can we reach the domain?
        try:
            url_test = urlopen(url)
        except IOError:
            message = "Hostname " + purl.netloc + " not found. Please check that it is correct."
            raise forms.ValidationError(message)

        # check for a 404 (needs python 2.6)
        if url_test.getcode() == 404:
            message = "Warning: the link %s appears not to work. Please check that it is correct." %url
            messages.add_message(request, messages.WARNING, message)

        # check for a redirect
        if url_test.geturl() != url:
            message = "Warning: your URL " + url + " doesn't match the site's, which is: " + url_test.geturl()
            messages.add_message(request, messages.WARNING, message)

    # for mailto types only
    elif str(scheme) == "mailto":
        message = "Warning: this email address hasn't been checked. I hope it's correct."
        messages.add_message(request, messages.WARNING, message)

class ExternalLinkForm(forms.ModelForm):
    class Meta:
        model = ExternalLink

    def clean(self):
        url = self.cleaned_data.get('url', "")
        title = self.cleaned_data.get('title', "")

        # check if the url is a duplicate
        # if the url exists, and this would be a new instance in the database, it's a duplicate
        print "ExternalLinkForm self.instance.pk", self.instance
        if self.Meta.model.objects.filter(url=url) and not self.instance.pk:
            message = "Sorry, this link appears to exist already"
            raise forms.ValidationError(message)

        # warn if the title is a duplicate
        # if the title exists, and this would be a new instance in the database, it's a duplicate
        if self.Meta.model.objects.filter(title=title) and not self.instance.pk:
            message = "Warning: the link title %s already exists in the database - consider changing it." %title
            messages.add_message(self.request, messages.WARNING, message)

        return self.cleaned_data

class ExternalLinkAdmin(SupplyRequestMixin, admin.ModelAdmin):
    readonly_fields = ('external_site', 'kind',)
    search_fields = ('title', 'external_site__site','description', 'url')
    list_display = ('title', 'url', )
    form = ExternalLinkForm

    def save_model(self, request, obj, form, change):
        return super(ExternalLinkAdmin, self).save_model(request, obj, form, change)

class LinkTypeAdmin(admin.ModelAdmin):
    pass

class ExternalSiteForm(forms.ModelForm):
    class Meta:
        model = ExternalSite

    def clean(self):
        # if the site isn't named, use the domain name
        site = self.cleaned_data.get("site", None)
        domain = self.cleaned_data.get("domain", None)
        if not site:
            self.cleaned_data["site"] = domain
        return self.cleaned_data

class ExternalSiteAdmin(admin.ModelAdmin):
    readonly_fields = ('parent',)
    form = ExternalSiteForm

    def changelist_view(self, request, extra_context=None):
        extra_context = extra_context or {}
        extra_context.update({
                'root_domains': ExternalSite.objects.filter(level = 0),
                # 'has_add_permission': request.user.has_perm('links.add_link'),
                # 'has_change_permission': request.user.has_perm('links.change_link'),
                # 'has_delete_permission': request.user.has_perm('links.delete_link'),
        })
        return super(ExternalSiteAdmin, self).changelist_view(request, extra_context)

#admin.site.register(ObjectLink, ObjectLinkAdmin)
admin.site.register(ExternalLink, ExternalLinkAdmin)
admin.site.register(ExternalSite, ExternalSiteAdmin)
admin.site.register(LinkType, LinkTypeAdmin)

"""
# patch the cms page admin to show generic links
if getattr(settings,'SHOW_GENERIC_LINKS_INLINE_FOR_CMS_PAGE_ADMIN', False):
    from cms.admin.pageadmin import PageAdmin
    from cms.models import Page
    admin.site.unregister(Page)
    print dir(admin.site)
    if not hasattr(PageAdmin,'inlines'):
        PageAdmin.inlines = []
    else:
        PageAdmin.inlines = list(PageAdmin.inlines)
    PageAdmin.inlines = PageAdmin.inlines + [ObjectLinkInline]
    admin.site.register(Page, PageAdmin)
"""
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import scrapy
import json

from locations.items import GeojsonPointItem

class BeerStoreSpider(scrapy.Spider):

    name = "beer_store"
    allowed_domains = ["www.thebeerstore.ca/"]
    start_urls = (
        'http://www.thebeerstore.ca/storelocations.json',
    )

    def parse(self, response):
        results = json.loads(response.body_as_unicode())
        features = results["features"]
        for data in features:
            description = data['properties']['description']
            end_str = "<a href"
            properties = {
                'ref': data['properties']['storeid'],
                'lat': data['geometry']['coordinates'][0],
                'lon': data['geometry']['coordinates'][1],
                'name': data['properties']['name'],
                'addr_full': description[:description.find(end_str)]
            }

            yield GeojsonPointItem(**properties)
<commit_msg>Flip the lat and lon slots<commit_after># -*- coding: utf-8 -*-
import scrapy
import json

from locations.items import GeojsonPointItem

class BeerStoreSpider(scrapy.Spider):

    name = "beer_store"
    allowed_domains = ["www.thebeerstore.ca/"]
    start_urls = (
        'http://www.thebeerstore.ca/storelocations.json',
    )

    def parse(self, response):
        results = json.loads(response.body_as_unicode())
        features = results["features"]
        for data in features:
            description = data['properties']['description']
            end_str = "<a href"
            properties = {
                'ref': data['properties']['storeid'],
                'lon': data['geometry']['coordinates'][0],
                'lat': data['geometry']['coordinates'][1],
                'name': data['properties']['name'],
                'addr_full': description[:description.find(end_str)]
            }

            yield GeojsonPointItem(**properties)
<|endoftext|>