<commit_before># Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
#
# @nolint
import re
import os
import subprocess

import logger

# If you are using a code grep query service and want to resolve
# certain global symbols to local directories,
# add them as REPOS below. We will essentially replace a global
# match against something like:
#   www/myFile.py
# to:
#   ~/www/myFile.py
REPOS = ['www']

MASTER_REGEX = re.compile(
    '(\/?([a-z.A-Z0-9\-_]+\/)+[@a-zA-Z0-9\-_.]+\.[a-zA-Z0-9]{1,10})[:-]{0,1}(\d+)?')
HOMEDIR_REGEX = re.compile(
    '(~\/([a-z.A-Z0-9\-_]+\/)+[@a-zA-Z0-9\-_.]+\.[a-zA-Z0-9]{1,10})[:-]{0,1}(\d+)?')
OTHER_BGS_RESULT_REGEX = re.compile(
    '(\/?([a-z.A-Z0-9\-_]+\/)+[a-zA-Z0-9_.]{3,})[:-]{0,1}(\d+)')
JUST_FILE = re.compile('([a-zA-Z0-9\-_]+\.[a-zA-Z]{1,10})\s+')
FILE_NO_PERIODS = re.compile(
    '([a-zA-Z0-9\-_\/]{1,}\/[a-zA-Z0-9\-_]{1,})(\s|$|:)+')

# Attempts to resolve the root directory of the
# repository in which path resides (i.e. the current directory).
# both git and hg have commands for this, so let's just use those.
def getRepoPath():
    proc = subprocess.Popen(["git rev-parse --show-toplevel"],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            shell=True)

    (stdout, stderr) = proc.communicate()

    # If there was no error return the output
    if not stderr:
        logger.addEvent('using_git')
        return stdout

    proc = subprocess.Popen(["hg root"],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            shell=True)

    (stdout, stderr) = proc.communicate()

    # If there was no error return the output
    if not stderr:
        logger.addEvent('using_hg')
        return stdout

    # Not a git or hg repo, go with ~/www as a default
    logger.addEvent('used_outside_repo')
    return '~/www'

PREPEND_PATH = getRepoPath().strip() + '/'

# returns a filename and (optional) line number
# if it matches
def matchLine(line):
    # Homedirs need a separate regex. TODO -- fix this copypasta
    matches = HOMEDIR_REGEX.search(line)
    if matches:
        groups = matches.groups()
        file = groups[0]
        num = 0 if groups[2] is None else int(groups[2])
        return (file, num, matches)

    matches = MASTER_REGEX.search(line)
    # the master regex matches tbgs results with
    # line numbers, so we prefer that and test it first
    if matches:
        groups = matches.groups()
        file = groups[0]
        num = 0 if groups[2] is None else int(groups[2])
        # one real quick check -- did we find a better match
        # earlier in the regex?
        other_matches = OTHER_BGS_RESULT_REGEX.search(line)
        if not other_matches:
            return (file, num, matches)
        if other_matches.start() >= matches.start():
            # return as before
            return (file, num, matches)
        # we actually want the BGS result, not the one after
        groups = other_matches.groups()
        return (groups[0], int(groups[2]), other_matches)

    # if something clearly looks like an *bgs result but
    # just has a weird filename (like all caps with no extension)
    # then we can match that as well. Note that we require
    # the line number match since otherwise this would be too lax
    # of a regex.
    matches = OTHER_BGS_RESULT_REGEX.search(line)
    if matches:
        groups = matches.groups()
        file = groups[0]
        num = 0 if groups[2] is None else int(groups[2])
        return (file, num, matches)

    # ok maybe its just a normal file (with a dot)
    # so lets test for that if the above fails
    matches = JUST_FILE.search(line)
    if matches:
        file = matches.groups()[0]
        return (file, 0, matches)

    # ok finally it might be a file with no periods. we test
    # this last since its more restrictive, because we dont
    # want to match things like cx('foo/root'). hence
    # we require some minimum number of slashes and minimum
    # file name length
    matches = FILE_NO_PERIODS.search(line)
    if not matches:
        return None
    file = matches.groups()[0]
    return (file, 0, matches)

def prependDir(file):
    if not file or len(file) < 2:
        return file

    if file[0] == '/':
        return file

    if file[0:2] == '~/':
        # need to absolute it
        return os.path.expanduser(file)

    # if it starts with ./ (grep), then that's the easiest because abspath
    # will resolve this
    if file[0:2] in ['./', '..', '~/']:
        return file

    # some peeps do forcedir and expand the path beforehand,
    # so lets check for that case here
    first = file.split(os.sep)[0]
    if first == 'home':
        # already absolute, easy
        return '/' + file

    if first in REPOS:
        return '~/' + file

    if '/' not in file:
        # assume current dir like ./
        return './' + file

    # git show has a/stuff and b/stuff
    if file[0:2] == 'a/' or file[0:2] == 'b/':
        return PREPEND_PATH + file[2:]

    splitUp = file.split('/')
    if splitUp[0] == 'www':
        return PREPEND_PATH + '/'.join(splitUp[1:])

    # hope
    return PREPEND_PATH + '/'.join(splitUp)
<commit_msg>Fix #50: permit any number of periods in file and dir names<commit_after># Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
#
# @nolint
import re
import os
import subprocess

import logger

# If you are using a code grep query service and want to resolve
# certain global symbols to local directories,
# add them as REPOS below. We will essentially replace a global
# match against something like:
#   www/myFile.py
# to:
#   ~/www/myFile.py
REPOS = ['www']

MASTER_REGEX = re.compile(
    '(\/?([a-z.A-Z0-9\-_]+\/)+[@a-zA-Z0-9\-_.]+\.[a-zA-Z0-9]{1,10})[:-]{0,1}(\d+)?')
HOMEDIR_REGEX = re.compile(
    '(~\/([a-z.A-Z0-9\-_]+\/)+[@a-zA-Z0-9\-_.]+\.[a-zA-Z0-9]{1,10})[:-]{0,1}(\d+)?')
OTHER_BGS_RESULT_REGEX = re.compile(
    '(\/?([a-z.A-Z0-9\-_]+\/)+[a-zA-Z0-9_.]{3,})[:-]{0,1}(\d+)')
JUST_FILE = re.compile('([a-z.A-Z0-9\-_]+\.[a-zA-Z]{1,10})\s+')
FILE_NO_PERIODS = re.compile(
    '([a-z.A-Z0-9\-_\/]{1,}\/[a-zA-Z0-9\-_]{1,})(\s|$|:)+')

# Attempts to resolve the root directory of the
# repository in which path resides (i.e. the current directory).
# both git and hg have commands for this, so let's just use those.
def getRepoPath():
    proc = subprocess.Popen(["git rev-parse --show-toplevel"],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            shell=True)

    (stdout, stderr) = proc.communicate()

    # If there was no error return the output
    if not stderr:
        logger.addEvent('using_git')
        return stdout

    proc = subprocess.Popen(["hg root"],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            shell=True)

    (stdout, stderr) = proc.communicate()

    # If there was no error return the output
    if not stderr:
        logger.addEvent('using_hg')
        return stdout

    # Not a git or hg repo, go with ~/www as a default
    logger.addEvent('used_outside_repo')
    return '~/www'

PREPEND_PATH = getRepoPath().strip() + '/'

# returns a filename and (optional) line number
# if it matches
def matchLine(line):
    # Homedirs need a separate regex. TODO -- fix this copypasta
    matches = HOMEDIR_REGEX.search(line)
    if matches:
        groups = matches.groups()
        file = groups[0]
        num = 0 if groups[2] is None else int(groups[2])
        return (file, num, matches)

    matches = MASTER_REGEX.search(line)
    # the master regex matches tbgs results with
    # line numbers, so we prefer that and test it first
    if matches:
        groups = matches.groups()
        file = groups[0]
        num = 0 if groups[2] is None else int(groups[2])
        # one real quick check -- did we find a better match
        # earlier in the regex?
        other_matches = OTHER_BGS_RESULT_REGEX.search(line)
        if not other_matches:
            return (file, num, matches)
        if other_matches.start() >= matches.start():
            # return as before
            return (file, num, matches)
        # we actually want the BGS result, not the one after
        groups = other_matches.groups()
        return (groups[0], int(groups[2]), other_matches)

    # if something clearly looks like an *bgs result but
    # just has a weird filename (like all caps with no extension)
    # then we can match that as well. Note that we require
    # the line number match since otherwise this would be too lax
    # of a regex.
    matches = OTHER_BGS_RESULT_REGEX.search(line)
    if matches:
        groups = matches.groups()
        file = groups[0]
        num = 0 if groups[2] is None else int(groups[2])
        return (file, num, matches)

    # ok maybe its just a normal file (with a dot)
    # so lets test for that if the above fails
    matches = JUST_FILE.search(line)
    if matches:
        file = matches.groups()[0]
        return (file, 0, matches)

    # ok finally it might be a file with no periods. we test
    # this last since its more restrictive, because we dont
    # want to match things like cx('foo/root'). hence
    # we require some minimum number of slashes and minimum
    # file name length
    matches = FILE_NO_PERIODS.search(line)
    if not matches:
        return None
    file = matches.groups()[0]
    return (file, 0, matches)

def prependDir(file):
    if not file or len(file) < 2:
        return file

    if file[0] == '/':
        return file

    if file[0:2] == '~/':
        # need to absolute it
        return os.path.expanduser(file)

    # if it starts with ./ (grep), then that's the easiest because abspath
    # will resolve this
    if file[0:2] in ['./', '..', '~/']:
        return file

    # some peeps do forcedir and expand the path beforehand,
    # so lets check for that case here
    first = file.split(os.sep)[0]
    if first == 'home':
        # already absolute, easy
        return '/' + file

    if first in REPOS:
        return '~/' + file

    if '/' not in file:
        # assume current dir like ./
        return './' + file

    # git show has a/stuff and b/stuff
    if file[0:2] == 'a/' or file[0:2] == 'b/':
        return PREPEND_PATH + file[2:]

    splitUp = file.split('/')
    if splitUp[0] == 'www':
        return PREPEND_PATH + '/'.join(splitUp[1:])

    # hope
    return PREPEND_PATH + '/'.join(splitUp)
<|endoftext|>


<commit_before><commit_msg>added radix sort function to sort a list of numbers<commit_after>"""Implement Radix sort in python."""

def radix_sort(lst):
    """Sort a list of numbers from lowest to highest."""
    max_digit = max([len(str(num)) for num in lst])

    for digit in range(max_digit):
        buckets = [[] for x in range(10)]

        for num in lst:
            idx = num % (10 ** (digit + 1)) // (10 ** digit)
            buckets[idx].append(num)

        lst = []

        [lst.extend(bucket) for bucket in buckets]

    return lst
<|endoftext|>


<commit_before>import sys
from settings import rbv_base_path
sys.path.insert(0, rbv_base_path)

from rbv import app as application
<commit_msg>fixed wsgi, again<commit_after>import sys
sys.path.insert(0, </path/to/RBV>/src)

from rbv import app as application
<|endoftext|>


<commit_before>import sys
sys.path.insert(0, settings.rbv_base_path)

from rbv import app as application
<commit_msg>fix wsgi<commit_after>import sys
from settings import rbv_base_path
sys.path.insert(0, rbv_base_path)

from rbv import app as application
<|endoftext|>


<commit_before>import os
from setuptools import setup, find_packages

with open(os.path.join(os.path.dirname(__file__), 'README.txt')) as f:
    long_description = f.read()

setup(
    name="pyshark",
    version="0.4.3",
    packages=find_packages(),
    package_data={'': ['*.ini', '*.pcapng']},
    install_requires=['lxml', 'py'],
    tests_require=['pytest'],
    url="https://github.com/KimiNewt/pyshark",
    license="MIT",
    long_description=long_description,
    author="KimiNewt",
    description="Python wrapper for tshark, allowing python packet parsing using wireshark dissectors",
    keywords="wireshark capture packets parsing packet",

    classifiers=[
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
    ],
)
<commit_msg>Updated setup version<commit_after>import os
from setuptools import setup, find_packages

with open(os.path.join(os.path.dirname(__file__), 'README.txt')) as f:
    long_description = f.read()

setup(
    name="pyshark",
    version="0.4.4",
    packages=find_packages(),
    package_data={'': ['*.ini', '*.pcapng']},
    install_requires=['lxml', 'py'],
    tests_require=['pytest'],
    url="https://github.com/KimiNewt/pyshark",
    license="MIT",
    long_description=long_description,
    author="KimiNewt",
    description="Python wrapper for tshark, allowing python packet parsing using wireshark dissectors",
    keywords="wireshark capture packets parsing packet",

    classifiers=[
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
    ],
)
<|endoftext|>


<commit_before>#! /usr/bin/python

import os
import setuptools
import sys

# FIXME explain why this is here
sys.path.insert(0,
    os.path.join(
        os.path.dirname(__file__),
        "lib",
        ))

import opensub

setuptools.setup(
    author="Bence Romsics",
    author_email="rubasov+opensub@gmail.com",
    classifiers=[
        "Environment :: Console",
        "Intended Audience :: End Users/Desktop",
        "License :: OSI Approved :: BSD License",
        "Natural Language :: English",
        "Operating System :: POSIX",
        "Programming Language :: Python :: 2.7",
        "Topic :: Multimedia :: Video",
        "Topic :: Utilities",
        ],
    description="CLI utilities for opensubtitles.org.",
    install_requires=[
        "docopt",
        "six",
        ],
    name="opensub",
    package_dir={
        "": "lib",
        },
    packages=[
        "opensub",
        ],
    scripts=[
        "bin/opensub-get",
        "bin/opensub-hash",
        ],
    #test_suite="nose.collector",  # FIXME
    tests_require=[
        "nose",
        ],
    url="http://github.com/rubasov/...",  # FIXME
    version=opensub.__version__,
    zip_safe=False,
    )
<commit_msg>Don't run the tests through setup.py .<commit_after>#! /usr/bin/python

import os
import setuptools
import sys

# FIXME explain why this is here
sys.path.insert(0,
    os.path.join(
        os.path.dirname(__file__),
        "lib",
        ))

import opensub

setuptools.setup(
    author="Bence Romsics",
    author_email="rubasov+opensub@gmail.com",
    classifiers=[
        "Environment :: Console",
        "Intended Audience :: End Users/Desktop",
        "License :: OSI Approved :: BSD License",
        "Natural Language :: English",
        "Operating System :: POSIX",
        "Programming Language :: Python :: 2.7",
        "Topic :: Multimedia :: Video",
        "Topic :: Utilities",
        ],
    description="CLI utilities for opensubtitles.org.",
    install_requires=[
        "docopt",
        "six",
        ],
    name="opensub",
    package_dir={
        "": "lib",
        },
    packages=[
        "opensub",
        ],
    scripts=[
        "bin/opensub-get",
        "bin/opensub-hash",
        ],
    tests_require=[
        "nose",
        ],
    url="http://github.com/rubasov/...",  # FIXME
    version=opensub.__version__,
    zip_safe=False,
    )
<|endoftext|>


<commit_before>import os
from setuptools import setup, find_packages

with open(os.path.join(os.path.dirname(__file__), 'README.txt')) as f:
    long_description = f.read()

setup(
    name="pyshark",
    version="0.3.7.11",
    packages=find_packages(),
    package_data={'': ['*.ini', '*.pcapng']},
    # Temporarily using trollius 1.0.4 until issue https://github.com/haypo/trollius/issues/4 is resolved.
    install_requires=['lxml', 'py', 'trollius==1.0.4', 'logbook'],
    tests_require=['mock', 'pytest'],
    url="https://github.com/KimiNewt/pyshark",
    long_description=long_description,
    author="KimiNewt",
    description="Python wrapper for tshark, allowing python packet parsing using wireshark dissectors",
    keywords="wireshark capture packets parsing packet",
    use_2to3=True,
    classifiers=[
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 3',
    ],
)
<commit_msg>Updated setup version<commit_after>import os
from setuptools import setup, find_packages

with open(os.path.join(os.path.dirname(__file__), 'README.txt')) as f:
    long_description = f.read()

setup(
    name="pyshark",
    version="0.3.8",
    packages=find_packages(),
    package_data={'': ['*.ini', '*.pcapng']},
    # Temporarily using trollius 1.0.4 until issue https://github.com/haypo/trollius/issues/4 is resolved.
    install_requires=['lxml', 'py', 'trollius==1.0.4', 'logbook'],
    tests_require=['mock', 'pytest'],
    url="https://github.com/KimiNewt/pyshark",
    long_description=long_description,
    author="KimiNewt",
    description="Python wrapper for tshark, allowing python packet parsing using wireshark dissectors",
    keywords="wireshark capture packets parsing packet",
    use_2to3=True,
    classifiers=[
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 3',
    ],
)
<|endoftext|>


<commit_before>import vlc

class Track(object):

    def __init__(self, logger, instance, mediapath):
        self.path = mediapath
        self.fullpath = str(mediapath.resolve())
        self.instance = instance
        self.logger = logger
        self.logger.debug("self.fullpath: %s", self.fullpath)

        self.media = self.instance.media_new(self.fullpath)
        self.has_id3 = self.parse_new_track(self.media)

        self.artist = None
        self.album = None
        self.title = None
        self.track_nb = None

    def parse_new_track(self, media):
        media.parse()
        self.artist = media.get_meta(vlc.Meta.Artist)
        self.album = media.get_meta(vlc.Meta.Album) or "Unknown Album"
        self.title = media.get_meta(vlc.Meta.Title)
        # Display a little symbol instead of the track number if it's unknown
        self.track_nb = media.get_meta(vlc.Meta.TrackNumber) or "*"

        # Display track info if we have at least an artist and a title,
        # else display the file name
        return self.artist and self.title

    def get_track_info_line(self):
        if self.has_id3:
            return "{s.track_nb} - {s.artist} - {s.title} ({s.album})"\
                   .format(s=self)
        else:
            return self.fullpath

#    def get_current_time(self):
<commit_msg>fix not showing id3 tags<commit_after>import vlc

class Track(object):

    def __init__(self, logger, instance, mediapath):

        self.artist = None
        self.album = None
        self.title = None
        self.track_nb = None

        self.path = mediapath
        self.fullpath = str(mediapath.resolve())
        self.instance = instance
        self.logger = logger
        self.logger.debug("track init: self.fullpath: %s", self.fullpath)

        self.media = self.instance.media_new(self.fullpath)
        self.has_id3 = self.parse_new_track(self.media)

    def parse_new_track(self, media):
        media.parse()
        self.artist = media.get_meta(vlc.Meta.Artist)
        self.album = media.get_meta(vlc.Meta.Album) or "Unknown Album"
        self.title = media.get_meta(vlc.Meta.Title)
        # Display a little symbol instead of the track number if it's unknown
        self.track_nb = media.get_meta(vlc.Meta.TrackNumber) or "*"
        self.logger.debug(
            "parsed track with nb {s.track_nb} artist {s.artist} title {s.title} album {s.album}".format(s=self))
        # Display track info if we have at least an artist and a title,
        # else display the file name
        self.logger.debug("end parse_new_track")
        return self.artist and self.title

    def get_track_info_line(self):
        if self.has_id3:
            return "{s.track_nb} - {s.artist} - {s.title} ({s.album})".format(s=self)
        else:
            return self.fullpath

#    def get_current_time(self):
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2011 thomasv@gitorious
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from itertools import imap
import threading
import time
import hashlib
import sys
import ltc_scrypt

__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
__b58base = len(__b58chars)

global PUBKEY_ADDRESS
global SCRIPT_ADDRESS
PUBKEY_ADDRESS = 30
SCRIPT_ADDRESS = 33

def rev_hex(s):
    return s.decode('hex')[::-1].encode('hex')

def int_to_hex(i, length=1):
    s = hex(i)[2:].rstrip('L')
    s = "0"*(2*length - len(s)) + s
    return rev_hex(s)

def var_int(i):
    if i < 0xfd:
        return int_to_hex(i)
    elif i <= 0xffff:
        return "fd" + int_to_hex(i, 2)
    elif i <= 0xffffffff:
        return "fe" + int_to_hex(i, 4)
    else:
        return "ff" + int_to_hex(i, 8)

Hash = lambda x: hashlib.sha256(hashlib.sha256(x).digest()).digest()

HashScrypt = lambda x: ltc_scrypt.getPoWHash(x)

hash_encode = lambda x: x[::-1].encode('hex')

hash_decode = lambda x: x.decode('hex')[::-1]

def header_to_string(res):
    pbh = res.get('prev_block_hash')
    if pbh is None:
        pbh = '0'*64

    return int_to_hex(res.get('version'), 4) \
        + rev_hex(pbh) \
        + rev_hex(res.get('merkle_root')) \
        + int_to_hex(int(res.get('timestamp')), 4) \
        + int_to_hex(int(res.get('bits')), 4) \
        + int_to_hex(int(res.get('nonce')), 4)

def hex_to_int(s):
    return int('0x' + s[::-1].encode('hex'), 16)

def header_from_string(s):
    return {
        'version': hex_to_int(s[0:4]),
        'prev_block_hash': hash_encode(s[4:36]),
        'merkle_root': hash_encode(s[36:68]),
        'timestamp': hex_to_int(s[68:72]),
        'bits': hex_to_int(s[72:76]),
        'nonce': hex_to_int(s[76:80]),
    }

############ functions from pywallet #####################

def hash_160(public_key):
    try:
        md = hashlib.new('ripemd160')
        md.update(hashlib.sha256(public_key).digest())
        return md.digest()
    except:
        import ripemd
        md = ripemd.new(hashlib.sha256(public_key).digest())
        return md.digest()

def public_key_to_pubkey_address(public_key):
    return hash_160_to_pubkey_address(hash_160(public_key))

def public_key_to_bc_address(public_key):
    """ deprecated """
    return public_key_to_pubkey_address(public_key)

def hash_160_to_pubkey_address(h160, addrtype=None):
    """ deprecated """
    if not addrtype:
        addrtype = PUBKEY_ADDRESS
    return hash_160_to_address(h160, addrtype)

def hash_160_to_pubkey_address(h160):
    return hash_160_to_address(h160, PUBKEY_ADDRESS)

def hash_160_to_script_address(h160):
    return hash_160_to_address(h160, SCRIPT_ADDRESS)

def hash_160_to_address(h160, addrtype = 30):
    """ Checks if the provided hash is actually 160bits or 20 bytes long and returns the address, else None
    """
    if h160 is None or len(h160) is not 20:
        return None
    vh160 = chr(addrtype) + h160
    h = Hash(vh160)
    addr = vh160 + h[0:4]
    return b58encode(addr)

def bc_address_to_hash_160(addr):
    if addr is None or len(addr) is 0:
        return None
    bytes = b58decode(addr, 25)
    return bytes[1:21] if bytes is not None else None

def b58encode(v):
    """encode v, which is a string of bytes, to base58."""

    long_value = 0L
    for (i, c) in enumerate(v[::-1]):
        long_value += (256**i) * ord(c)

    result = ''
    while long_value >= __b58base:
        div, mod = divmod(long_value, __b58base)
        result = __b58chars[mod] + result
        long_value = div
    result = __b58chars[long_value] + result

    # Bitcoin does a little leading-zero-compression:
    # leading 0-bytes in the input become leading-1s
    nPad = 0
    for c in v:
        if c == '\0':
            nPad += 1
        else:
            break

    return (__b58chars[0]*nPad) + result

def b58decode(v, length):
    """ decode v into a string of len bytes."""
    long_value = 0L
    for (i, c) in enumerate(v[::-1]):
        long_value += __b58chars.find(c) * (__b58base**i)

    result = ''
    while long_value >= 256:
        div, mod = divmod(long_value, 256)
        result = chr(mod) + result
        long_value = div
    result = chr(long_value) + result

    nPad = 0
    for c in v:
        if c == __b58chars[0]:
            nPad += 1
        else:
            break

    result = chr(0)*nPad + result
    if length is not None and len(result) != length:
        return None

    return result

def EncodeBase58Check(vchIn):
    hash = Hash(vchIn)
    return b58encode(vchIn + hash[0:4])

def DecodeBase58Check(psz):
    vchRet = b58decode(psz, None)
    key = vchRet[0:-4]
    csum = vchRet[-4:]
    hash = Hash(key)
    cs32 = hash[0:4]
    if cs32 != csum:
        return None
    else:
        return key

########### end pywallet functions #######################

def random_string(length):
    with open("/dev/urandom", 'rb') as f:
        return b58encode( f.read(length) )

def timestr():
    return time.strftime("[%d/%m/%Y-%H:%M:%S]")

### logger
import logging
import logging.handlers

logger = logging.getLogger('electrum-doged')

def init_logger(logfile):
    hdlr = logging.handlers.WatchedFileHandler(logfile)
    formatter = logging.Formatter('%(asctime)s %(message)s', "[%d/%m/%Y-%H:%M:%S]")
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)

def print_log(*args):
    logger.info(" ".join(imap(str, args)))

def print_warning(message):
    logger.warning(message)

<commit_msg>Update utils.py<commit_after>#!/usr/bin/env python
#
# Electrum - lightweight DogecoinDark client
# Copyright (C) 2011 thomasv@gitorious
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from itertools import imap
import threading
import time
import hashlib
import sys
import ltc_scrypt

__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
__b58base = len(__b58chars)

global PUBKEY_ADDRESS
global SCRIPT_ADDRESS
PUBKEY_ADDRESS = 30
SCRIPT_ADDRESS = 33

def rev_hex(s):
    return s.decode('hex')[::-1].encode('hex')

def int_to_hex(i, length=1):
    s = hex(i)[2:].rstrip('L')
    s = "0"*(2*length - len(s)) + s
    return rev_hex(s)

def var_int(i):
    if i < 0xfd:
        return int_to_hex(i)
    elif i <= 0xffff:
        return "fd" + int_to_hex(i, 2)
    elif i <= 0xffffffff:
        return "fe" + int_to_hex(i, 4)
    else:
        return "ff" + int_to_hex(i, 8)

Hash = lambda x: hashlib.sha256(hashlib.sha256(x).digest()).digest()

HashScrypt = lambda x: ltc_scrypt.getPoWHash(x)

hash_encode = lambda x: x[::-1].encode('hex')

hash_decode = lambda x: x.decode('hex')[::-1]

def header_to_string(res):
    pbh = res.get('prev_block_hash')
    if pbh is None:
        pbh = '0'*64

    return int_to_hex(res.get('version'), 4) \
        + rev_hex(pbh) \
        + rev_hex(res.get('merkle_root')) \
        + int_to_hex(int(res.get('timestamp')), 4) \
        + int_to_hex(int(res.get('bits')), 4) \
        + int_to_hex(int(res.get('nonce')), 4)

def hex_to_int(s):
    return int('0x' + s[::-1].encode('hex'), 16)

def header_from_string(s):
    return {
        'version': hex_to_int(s[0:4]),
        'prev_block_hash': hash_encode(s[4:36]),
        'merkle_root': hash_encode(s[36:68]),
        'timestamp': hex_to_int(s[68:72]),
        'bits': hex_to_int(s[72:76]),
        'nonce': hex_to_int(s[76:80]),
    }

############ functions from pywallet #####################

def hash_160(public_key):
    try:
        md = hashlib.new('ripemd160')
        md.update(hashlib.sha256(public_key).digest())
        return md.digest()
    except:
        import ripemd
        md = ripemd.new(hashlib.sha256(public_key).digest())
        return md.digest()

def public_key_to_pubkey_address(public_key):
    return hash_160_to_pubkey_address(hash_160(public_key))

def public_key_to_bc_address(public_key):
    """ deprecated """
    return public_key_to_pubkey_address(public_key)

def hash_160_to_pubkey_address(h160, addrtype=None):
    """ deprecated """
    if not addrtype:
        addrtype = PUBKEY_ADDRESS
    return hash_160_to_address(h160, addrtype)

def hash_160_to_pubkey_address(h160):
    return hash_160_to_address(h160, PUBKEY_ADDRESS)

def hash_160_to_script_address(h160):
    return hash_160_to_address(h160, SCRIPT_ADDRESS)

def hash_160_to_address(h160, addrtype = 30):
    """ Checks if the provided hash is actually 160bits or 20 bytes long and returns the address, else None
    """
    if h160 is None or len(h160) is not 20:
        return None
    vh160 = chr(addrtype) + h160
    h = Hash(vh160)
    addr = vh160 + h[0:4]
    return b58encode(addr)

def bc_address_to_hash_160(addr):
    if addr is None or len(addr) is 0:
        return None
    bytes = b58decode(addr, 25)
    return bytes[1:21] if bytes is not None else None

def b58encode(v):
    """encode v, which is a string of bytes, to base58."""

    long_value = 0L
    for (i, c) in enumerate(v[::-1]):
        long_value += (256**i) * ord(c)

    result = ''
    while long_value >= __b58base:
        div, mod = divmod(long_value, __b58base)
        result = __b58chars[mod] + result
        long_value = div
    result = __b58chars[long_value] + result

    # Bitcoin does a little leading-zero-compression:
    # leading 0-bytes in the input become leading-1s
    nPad = 0
    for c in v:
        if c == '\0':
            nPad += 1
        else:
            break

    return (__b58chars[0]*nPad) + result

def b58decode(v, length):
    """ decode v into a string of len bytes."""
    long_value = 0L
    for (i, c) in enumerate(v[::-1]):
        long_value += __b58chars.find(c) * (__b58base**i)

    result = ''
    while long_value >= 256:
        div, mod = divmod(long_value, 256)
        result = chr(mod) + result
        long_value = div
    result = chr(long_value) + result

    nPad = 0
    for c in v:
        if c == __b58chars[0]:
            nPad += 1
        else:
            break

    result = chr(0)*nPad + result
    if length is not None and len(result) != length:
        return None

    return result

def EncodeBase58Check(vchIn):
    hash = Hash(vchIn)
    return b58encode(vchIn + hash[0:4])

def DecodeBase58Check(psz):
    vchRet = b58decode(psz, None)
    key = vchRet[0:-4]
    csum = vchRet[-4:]
    hash = Hash(key)
    cs32 = hash[0:4]
    if cs32 != csum:
        return None
    else:
        return key

########### end pywallet functions #######################

def random_string(length):
    with open("/dev/urandom", 'rb') as f:
        return b58encode( f.read(length) )

def timestr():
    return time.strftime("[%d/%m/%Y-%H:%M:%S]")

### logger
import logging
import logging.handlers

logger = logging.getLogger('electrum-doged')

def init_logger(logfile):
    hdlr = logging.handlers.WatchedFileHandler(logfile)
    formatter = logging.Formatter('%(asctime)s %(message)s', "[%d/%m/%Y-%H:%M:%S]")
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)

def print_log(*args):
    logger.info(" ".join(imap(str, args)))

def print_warning(message):
    logger.warning(message)

<|endoftext|>


<commit_before>import pickle

class dataModel:
	def __init__(self):
		self.saveFile = "save.p"
		try:
			self.productList = pickle.load( open("save.p", "rb"))
		except EOFError:
			self.productList = {}

	def outputText(self):
		pickle.dump(self.productList, open("save.p", "wb"))

class product:
	def __init__(self, data_model, prod_id, pro_list, con_list):
		self.id = prod_id
		self.proList = pro_list
		self.conList = con_list
		self.model = data_model
		self.currentID = 0;

	def getTopPros(self, n=1):
		self.proList.sort(key=lambda x: x.votes, reverse=True)
		return self.proList[0:n]

	def getTopCons(self, n=1):
		self.conList.sort(key=lambda x: x.votes, reverse=True)
		return self.conList[0:n]

	def getPros(self):
		self.proList.sort(key=lambda x: x.votes, reverse=True)
		return self.proList

	def getCons(self):
		self.conList.sort(key=lambda x: x.votes, reverse=True)
		return self.conList

	def addPro(self, message):
		self.currentID += 1
		if (self.model.productList.get(self.id, -1) != -1):
			self.proList.append(pro_con(self.currentID, message, 1))
		else:
			proList = [pro_con(self.currentID, message, 1)]
			self.model.productList[self.id] = product(self.model, self.id, proList, [])

	def addCon(self, message):
		self.currentID += 1
		if (self.model.productList.get(self.id, -1) != -1):
			self.conList.append(pro_con(self.currentID, message, 1))
		else:
			conList = [pro_con(self.currentID, message, 1)]
			self.model.productList[self.id] = product(self.model, self.id, conList, [])

	def voteUp(self, pcID):
		for i in conList:
			if i==pcID:
				i.votes += 1
		for i in proList:
			if i==pcID:
				i.votes +=1

	def voteUp(self, pcID):
		for i in conList:
			if i==pcID:
				i.votes -= 1
		for i in proList:
			if i==pcID:
				i.votes -=1

class pro_con:
	def __init__(self, pro_con_id, message, votes):
		self.id = pro_con_id
		self.message = message
		self.votes = votes

def populate():
	model = dataModel()
	proList = [pro_con(1, "Better than anything I've ever used", 1759), pro_con(3, "Looks Pretty", -15)]
	conList = [pro_con(2, "Worst product ever", 158), pro_con(4, "Meh", -1000000)]
	model.productList[1] = product(model, 1, proList, conList)
	model.outputText()

if __name__ == "__main__":
	populate()

<commit_msg>Fixed bug in structure.py<commit_after>import pickle

class dataModel:
	def __init__(self):
		self.saveFile = "save.p"
		try:
			self.productList = pickle.load( open("save.p", "rb"))
		except EOFError:
			self.productList = {}

	def outputText(self):
		pickle.dump(self.productList, open("save.p", "wb"))

class product:
	def __init__(self, data_model, prod_id, pro_list, con_list):
		self.id = prod_id
		self.proList = pro_list
		self.conList = con_list
		self.model = data_model
		self.currentID = 0;

	def getTopPros(self, n=1):
		self.proList.sort(key=lambda x: x.votes, reverse=True)
		return self.proList[0:n]

	def getTopCons(self, n=1):
		self.conList.sort(key=lambda x: x.votes, reverse=True)
		return self.conList[0:n]

	def getPros(self):
		self.proList.sort(key=lambda x: x.votes, reverse=True)
		return self.proList

	def getCons(self):
		self.conList.sort(key=lambda x: x.votes, reverse=True)
		return self.conList

	def addPro(self, message):
		self.currentID += 1
		if (self.model.productList.get(self.id, -1) != -1):
			self.proList.append(pro_con(self.currentID, message, 1))
		else:
			proList = [pro_con(self.currentID, message, 1)]
			self.model.productList[self.id] = product(self.model, self.id, proList, [])

	def addCon(self, message):
		self.currentID += 1
		if (self.model.productList.get(self.id, -1) != -1):
			self.conList.append(pro_con(self.currentID, message, 1))
		else:
			conList = [pro_con(self.currentID, message, 1)]
			self.model.productList[self.id] = product(self.model, self.id, conList, [])

	def voteUp(self, pcID):
		for i in self.conList:
			if i==pcID:
				i.votes += 1
		for i in self.proList:
			if i==pcID:
				i.votes +=1

	def voteUp(self, pcID):
		for i in self.conList:
			if i==pcID:
				i.votes -= 1
		for i in self.proList:
			if i==pcID:
				i.votes -=1

class pro_con:
	def __init__(self, pro_con_id, message, votes):
		self.id = pro_con_id
		self.message = message
		self.votes = votes

def populate():
	model = dataModel()
	proList = [pro_con(1, "Better than anything I've ever used", 1759), pro_con(3, "Looks Pretty", -15)]
	conList = [pro_con(2, "Worst product ever", 158), pro_con(4, "Meh", -1000000)]
	model.productList[1] = product(model, 1, proList, conList)
	model.outputText()

if __name__ == "__main__":
	populate()

<|endoftext|>


<commit_before>import sublime
import sublime_plugin

import sys
import struct
import subprocess
import time
import os

def assemble_quoted_parts(tokens):
    """Takes a list of space-separated tokens and returns a generator that
    produces a sequence valid for Popen where quoted strings are reassambled
    again as a single token."""
    QUOTES = "\"'"
    is_between_quotes = False
    quote = ''
    buf = ''
    for el in tokens:
        if is_between_quotes:
            buf += el + ' '
            # XXX Will fail in the presence of \" or escaped quote?
            if el[-1] == quote:
                is_between_quotes = False
                buf = buf.rstrip()
                yield buf
                buf = ''
            continue

        if not el[0] in QUOTES:
            yield el
        else:
            is_between_quotes = True
            quote = el[0]
            buf += el + ' '

    # Missing quote, but it isn't our problem.
    if buf:
        yield buf.rstrip()

# XXX: Make async.

class HGServer(object):
    """I drive a Mercurial command server (Mercurial>=1.9).

    Mercurial command server protocol: http://mercurial.selenic.com/wiki/CommandServer
    """
    def __init__(self, hg_exe='hg'):
        startupinfo = None
        if os.name == 'nt':
            # Hide the child process window on Windows.
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

        self.server = subprocess.Popen(
                                [hg_exe, "serve", "--cmdserver", "pipe"],
                                stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                startupinfo=startupinfo
                                )

        self.receive_greeting()
        self.encoding = self.get_encoding()

    def receive_greeting(self):
        try:
            channel, data = self.read_data()
        except struct.error:
            err = self.server.stderr.read()
            self.shut_down()
            raise EnvironmentError(err)
        except EnvironmentError:
            raise
            self.shut_down()

        try:
            caps, enc = data.split('\n')
        except ValueError:
            # Means the server isn't returnind the promised data, so the
            # environment is wrong.
            print "SublimeHG:err: SublimeHG requires Mercurial>=1.9. (Probable cause.)"
            self.shut_down()
            raise EnvironmentError("SublimeHG requires Mercurial>=1.9")

        caps = ', '.join(caps.split()[1:])
        print "SublimeHG:   :", "Capabilities:", caps
        print "SublimeHG:   : Encoding:", enc.split()[1]

    def read_data(self):
        channel, length = struct.unpack('>cI', self.server.stdout.read(5))
        if channel in 'IL':
            return channel, length

        return channel, self.server.stdout.read(length)

    def write_block(self, cmd, *args):
        self.server.stdin.write(cmd + '\n')

        if args:
            data = '\0'.join(args) if len(args) > 1 else args[0]
            length = struct.pack('>l', len(data))
            self.server.stdin.write(length + data)
            self.server.stdin.flush()

    def run_command(self, *args):
        if len(args) == 1 and ' ' in args[0]:
            args = args[0].split()

        if args[0] == 'hg':
            print "SublimeHG:   : Stripped superfluous 'hg' from '%s'" % ' '.join(args)
            args = args[1:]

        args = list(assemble_quoted_parts(args))
        print "XXX", args

        print "SublimeHG:inf: Sending command '%s'" % ' '.join(args)
        self.write_block('runcommand', *args)

        rv = ''
        while True:
            channel, line = self.read_data()
            if channel == 'o':
                rv += line
            elif channel == 'r':
                print "SublimeHG:ret: %s" % struct.unpack('>l', line)[0]
                return rv[:-1]
            else:
                print "SublimeHG:err: " + line
                rv = "Could not complete operation. Was your command complete?"
                self.shut_down()
                return rv

    def get_encoding(self):
        self.write_block('getencoding')
        return self.read_data()[1]

    def shut_down(self):
        print "SublimeHG: Shutting down HG server..."
        self.server.stdin.close()

class HgCmdLineCommand(sublime_plugin.TextCommand):

    def configure(self):
        s = sublime.load_settings('Global.sublime-settings')
        user_exe = s.get('packages.sublime_hg.hg_exe')
        self.hg_exe = user_exe or 'hg'

    def run(self, edit, cmd=None):
        self.edit = edit
        if not cmd:
            self.view.window().show_input_panel('Hg command:', 'status', self.on_done, None, None)
            return
        self.on_done(cmd)

    def on_done(self, s):
        old_cd = os.getcwd()
        os.chdir(os.path.dirname(self.view.file_name()))

        if not hasattr(self, 'hg_exe'):
            self.configure()

        try:
            hgs = HGServer(self.hg_exe)
        except EnvironmentError, e:
            sublime.status_message("SublimeHG:err:" + str(e))
            # hgs.shut_down()
            os.chdir(old_cd)
            return

        try:
            data = hgs.run_command(s.encode(hgs.encoding))
            p = self.view.window().get_output_panel('hgs')
            data = "Mercurial says...\n\n" + data
            p.insert(self.edit, 0, data)
            self.view.window().run_command('show_panel', {'panel': 'output.hgs'})
        except UnicodeDecodeError, e:
            print "Oops (funny characters!)..."
            print e
        finally:
            hgs.shut_down()
            os.chdir(old_cd)

SUBLIMEHG_CMDS = [
    "add",
    "annotate",
    # "clone",
    "commit",
    "commit (this file)",
    "diff",
    # "export",
    # "forget",
    "help",
    "init",
    "log",
    "merge",
    "pull",
    "push",
    # "qdiff",
    # "qnew",
    # "qpop",
    # "qpush",
    # "qrefresh",
    # "remove",
    "serve",
    "status",
    "summary",
    "update",
]

class HgCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.edit = edit
        self.view.window().show_quick_panel(SUBLIMEHG_CMDS, self.on_done)

    def on_done(self, s):
        if s == -1: return

        if SUBLIMEHG_CMDS[s] == 'commit':
            self.view.run_command("hg_commit")
            return
        elif SUBLIMEHG_CMDS[s] == 'commit (this file)':
            fn = self.view.file_name()
            if not fn: return
            self.view.run_command("hg_commit", {"what": fn})
            return

        self.view.run_command("hg_cmd_line", {"cmd": SUBLIMEHG_CMDS[s]})

class HgCommit(sublime_plugin.TextCommand):
    def run(self, edit, what=''):
        self.what = what
        self.view.window().show_input_panel("Hg commit message:", '', self.on_done, None, None)

    def on_done(self, s):
        if ' ' in self.what:
            self.view.run_command("hg_cmd_line", {"cmd":"commit '%s' -m \"%s\"" % (self.what, s)})
            return
        self.view.run_command("hg_cmd_line", {"cmd":"commit %s -m \"%s\"" % (self.what, s)})
<commit_msg>"Improved logging."<commit_after>import sublime
import sublime_plugin

import sys
import struct
import subprocess
import time
import os

def assemble_quoted_parts(tokens):
    """Takes a list of space-separated tokens and returns a generator that
    produces a sequence valid for Popen where quoted strings are reassambled
    again as a single token."""
    QUOTES = "\"'"
    is_between_quotes = False
    quote = ''
    buf = ''
    for el in tokens:
        if is_between_quotes:
            buf += el + ' '
            # XXX Will fail in the presence of \" or escaped quote?
            if el[-1] == quote:
                is_between_quotes = False
                buf = buf.rstrip()
                yield buf
                buf = ''
            continue

        if not el[0] in QUOTES:
            yield el
        else:
            is_between_quotes = True
            quote = el[0]
            buf += el + ' '

    # Missing quote, but it isn't our problem.
    if buf:
        yield buf.rstrip()

# XXX: Make async.

class HGServer(object):
    """I drive a Mercurial command server (Mercurial>=1.9).

    Mercurial command server protocol: http://mercurial.selenic.com/wiki/CommandServer
    """
    def __init__(self, hg_exe='hg'):
        startupinfo = None
        if os.name == 'nt':
            # Hide the child process window on Windows.
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

        self.server = subprocess.Popen(
                                [hg_exe, "serve", "--cmdserver", "pipe"],
                                stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                startupinfo=startupinfo
                                )

        self.receive_greeting()
        self.encoding = self.get_encoding()

    def receive_greeting(self):
        try:
            channel, data = self.read_data()
        except struct.error:
            err = self.server.stderr.read()
            self.shut_down()
            raise EnvironmentError(err)
        except EnvironmentError:
            raise
            self.shut_down()

        try:
            caps, enc = data.split('\n')
        except ValueError:
            # Means the server isn't returnind the promised data, so the
            # environment is wrong.
            print "SublimeHG:err: SublimeHG requires Mercurial>=1.9. (Probable cause.)"
            self.shut_down()
            raise EnvironmentError("SublimeHG requires Mercurial>=1.9")

        caps = ', '.join(caps.split()[1:])
        print "SublimeHG:inf:", "Capabilities:", caps
        print "SublimeHG:inf: Encoding:", enc.split()[1]

    def read_data(self):
        channel, length = struct.unpack('>cI', self.server.stdout.read(5))
        if channel in 'IL':
            return channel, length

        return channel, self.server.stdout.read(length)

    def write_block(self, cmd, *args):
        self.server.stdin.write(cmd + '\n')

        if args:
            data = '\0'.join(args) if len(args) > 1 else args[0]
            length = struct.pack('>l', len(data))
            self.server.stdin.write(length + data)
            self.server.stdin.flush()

    def run_command(self, *args):
        if len(args) == 1 and ' ' in args[0]:
            args = args[0].split()

        if args[0] == 'hg':
            print "SublimeHG:inf: Stripped superfluous 'hg' from '%s'" % ' '.join(args)
            args = args[1:]

        args = list(assemble_quoted_parts(args))

        print "SublimeHG:inf: Sending command '%s' as %s" % (' '.join(args), args)
        self.write_block('runcommand', *args)

        rv = ''
        while True:
            channel, line = self.read_data()
            if channel == 'o':
                rv += line
            elif channel == 'r':
                print "SublimeHG:ret: %s" % struct.unpack('>l', line)[0]
                return rv[:-1]
            else:
                print "SublimeHG:err: " + line
                rv = "Could not complete operation. Was your command complete?"
                self.shut_down()
                return rv

    def get_encoding(self):
        self.write_block('getencoding')
        return self.read_data()[1]

    def shut_down(self):
        print "SublimeHG: Shutting down HG server..."
        self.server.stdin.close()

class HgCmdLineCommand(sublime_plugin.TextCommand):

    def configure(self):
        s = sublime.load_settings('Global.sublime-settings')
        user_exe = s.get('packages.sublime_hg.hg_exe')
        self.hg_exe = user_exe or 'hg'

    def run(self, edit, cmd=None):
        self.edit = edit
        if not cmd:
            self.view.window().show_input_panel('Hg command:', 'status', self.on_done, None, None)
            return
        self.on_done(cmd)

    def on_done(self, s):
        old_cd = os.getcwd()
        os.chdir(os.path.dirname(self.view.file_name()))

        if not hasattr(self, 'hg_exe'):
            self.configure()

        try:
            hgs = HGServer(self.hg_exe)
        except EnvironmentError, e:
            sublime.status_message("SublimeHG:err:" + str(e))
            # hgs.shut_down()
            os.chdir(old_cd)
            return

        try:
            data = hgs.run_command(s.encode(hgs.encoding))
            p = self.view.window().get_output_panel('hgs')
            data = "Mercurial says...\n\n" + data
            p.insert(self.edit, 0, data)
            self.view.window().run_command('show_panel', {'panel': 'output.hgs'})
        except UnicodeDecodeError, e:
            print "Oops (funny characters!)..."
            print e
        finally:
            hgs.shut_down()
            os.chdir(old_cd)

SUBLIMEHG_CMDS = [
    "add",
    "annotate",
    # "clone",
    "commit",
    "commit (this file)",
    "diff",
    # "export",
    # "forget",
    "help",
    "init",
    "log",
    "merge",
    "pull",
    "push",
    # "qdiff",
    # "qnew",
    # "qpop",
    # "qpush",
    # "qrefresh",
    # "remove",
    "serve",
    "status",
    "summary",
    "update",
]

class HgCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.edit = edit
        self.view.window().show_quick_panel(SUBLIMEHG_CMDS, self.on_done)

    def on_done(self, s):
        if s == -1: return

        if SUBLIMEHG_CMDS[s] == 'commit':
            self.view.run_command("hg_commit")
            return
        elif SUBLIMEHG_CMDS[s] == 'commit (this file)':
            fn = self.view.file_name()
            if not fn: return
            self.view.run_command("hg_commit", {"what": fn})
            return

        self.view.run_command("hg_cmd_line", {"cmd": SUBLIMEHG_CMDS[s]})

class HgCommit(sublime_plugin.TextCommand):
    def run(self, edit, what=''):
        self.what = what
        self.view.window().show_input_panel("Hg commit message:", '', self.on_done, None, None)

    def on_done(self, s):
        if ' ' in self.what:
            self.view.run_command("hg_cmd_line", {"cmd":"commit '%s' -m \"%s\"" % (self.what, s)})
            return
        self.view.run_command("hg_cmd_line", {"cmd":"commit %s -m \"%s\"" % (self.what, s)})
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
/***************************************************************************
 Svir
                                 A QGIS plugin
 OpenQuake Social Vulnerability and Integrated Risk
                              -------------------
        begin                : 2013-10-24
        copyright            : (C) 2013 by GEM Foundation
        email                : devops@openquake.org
 ***************************************************************************/

# Copyright (c) 2010-2013, GEM Foundation.
#
# OpenQuake 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.
#
# OpenQuake 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 General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.
"""
import os.path

from PyQt4.QtCore import (QSettings,
                          QTranslator,
                          QCoreApplication,
                          qVersion,
                          QVariant)

from PyQt4.QtGui import (QApplication,
                         QAction,
                         QIcon,
                         QProgressDialog,
                         QMessageBox)

from qgis.core import (QgsVectorLayer,
                       QgsMapLayerRegistry,
                       QgsRasterLayer,
                       QgsField,
                       QgsFeature,
                       QgsGeometry,
                       QgsFields,
                       QgsSpatialIndex,
                       QgsFeatureRequest,
                       QgsVectorDataProvider)

from qgis.gui import QgsMessageBar

from qgis.analysis import QgsZonalStatistics

import resources_rc

# Import the code for the dialog
from svirdialog import SvirDialog

# Name of the attribute, in the input loss data layer, containing loss info
LOSS_ATTRIBUTE_NAME = "RSCORE"  # "loss"
REGION_ID_ATTRIBUTE_NAME = "STFID"

class Svir:

    @staticmethod
    def tr(message):
        return QApplication.translate('Svir', message)

    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        # initialize plugin directory
        self.plugin_dir = os.path.dirname(__file__)
        # initialize locale
        locale = QSettings().value("locale/userLocale")[0:2]
        locale_path = os.path.join(self.plugin_dir, 'i18n',
                                   'svir_{}.qm'.format(locale))

        if os.path.exists(locale_path):
            self.translator = QTranslator()
            self.translator.load(locale_path)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)

        # Create the dialog (after translation) and keep reference
        self.dlg = SvirDialog()

        self.loss_layer_is_vector = True
        # Input layer containing loss data
        self.loss_layer = None
        # Input layer specifying regions for aggregation
        self.regions_layer = None
        # Output layer containing aggregated loss data
        self.aggregation_layer = None
        self.initial_action = None
        self.purge_empty_regions_action = None

    def initGui(self):
        # Create action that will start plugin configuration
        self.initial_action = QAction(
            QIcon(":/plugins/svir/start_plugin_icon.png"),
            u"Aggregate loss by region", self.iface.mainWindow())
        # connect the action to the run method
        self.initial_action.triggered.connect(self.run)

        # Add toolbar button and menu item
        self.iface.addToolBarIcon(self.initial_action)
        self.iface.addPluginToMenu(u"&SVIR", self.initial_action)

    def unload(self):
        # Remove the plugin menu item and icon
        self.iface.removePluginMenu(u"&SVIR", self.initial_action)
        self.iface.removeToolBarIcon(self.initial_action)
        self.iface.removePluginMenu(u"&SVIR",
                                    self.purge_empty_regions_action)
        self.iface.removeToolBarIcon(self.purge_empty_regions_action)

    # run method that performs all the real work
    def run(self):
        # show the dialog
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result == 1:
            self.loss_layer_is_vector = self.dlg.loss_map_is_vector
            self.load_layers(self.dlg.ui.regions_layer_le.text(),
                             self.dlg.ui.loss_layer_le.text(),
                             self.loss_layer_is_vector)
            self.create_aggregation_layer()
            self.calculate_stats()
            # Create and enable toolbar button and menu item
            # for purging empty regions
            # Create action
            self.purge_empty_regions_action = QAction(
                QIcon(":/plugins/svir/purge_empty_regions_icon.png"),
                u"Purge empty regions",
                self.iface.mainWindow())
            # Connect the action to the purge_empty_regions method
            self.purge_empty_regions_action.triggered.connect(
                self.create_new_aggregation_layer_with_no_empty_regions)
            # Add toolbar button and menu item
            self.iface.addToolBarIcon(self.purge_empty_regions_action)
            self.iface.addPluginToMenu(u"&SVIR",
                                       self.purge_empty_regions_action)
            msg = 'Select "Purge empty regions" from SVIR plugin menu ' \
                  'to create a new aggregation layer with the regions ' \
                  'containing at least one loss point'
            self.iface.messageBar().pushMessage(self.tr("Info"),
                                            self.tr(msg),
                                            level=QgsMessageBar.INFO)

    def load_layers(self, aggregation_layer_path,
                    loss_layer_path,
                    loss_layer_is_vector):
        # Load aggregation layer
        self.regions_layer = QgsVectorLayer(aggregation_layer_path,
                                            self.tr('Regions'), 'ogr')
        # Add aggregation layer to registry
        if self.regions_layer.isValid():
            QgsMapLayerRegistry.instance().addMapLayer(self.regions_layer)
        else:
            raise RuntimeError('Aggregation layer invalid')
        # Load loss layer
        if loss_layer_is_vector:
            self.loss_layer = QgsVectorLayer(loss_layer_path,
                                             self.tr('Loss map'), 'ogr')
        else:
            self.loss_layer = QgsRasterLayer(loss_layer_path,
                                             self.tr('Loss map'))
        # Add loss layer to registry
        if self.loss_layer.isValid():
            QgsMapLayerRegistry.instance().addMapLayer(self.loss_layer)
        else:
            raise RuntimeError('Loss layer invalid')
        # Zoom depending on the regions layer's extent
        self.canvas.setExtent(self.regions_layer.extent())

    def create_aggregation_layer(self):
        """
        Create a new aggregation layer which contains the polygons from
        the regions layer. Two new attributes (count and sum) are
        initialized to 0 and will represent the count of loss points in a
        region and the sum of loss values for the same region.
        """
        # create layer
        self.aggregation_layer = QgsVectorLayer("Polygon",
                                                "Aggregated Losses",
                                                "memory")
        pr = self.aggregation_layer.dataProvider()
        caps = pr.capabilities()

        # Begin layer initialization
        self.aggregation_layer.startEditing()
        self.aggregation_layer.beginEditCommand("Layer initialization")

        # add count and sum fields for aggregating statistics
        pr.addAttributes(
            [QgsField(REGION_ID_ATTRIBUTE_NAME, QVariant.String),
             QgsField("count", QVariant.Int),
             QgsField("sum",  QVariant.Double)])

        # copy regions from regions layer
        for region_feature in self.regions_layer.getFeatures():
            feat = QgsFeature()
            # copy the polygon from the input aggregation layer
            feat.setGeometry(QgsGeometry(region_feature.geometry()))
            # Define the count and sum fields to initialize to 0
            fields = QgsFields()
            fields.append(QgsField(QgsField(REGION_ID_ATTRIBUTE_NAME,
                                             QVariant.String)))
            fields.append(QgsField(QgsField("count", QVariant.Int)))
            fields.append(QgsField(QgsField("sum", QVariant.Double)))
            # Add fields to the new feature
            feat.setFields(fields)
            feat[REGION_ID_ATTRIBUTE_NAME] = region_feature[REGION_ID_ATTRIBUTE_NAME]
            feat['count'] = 0
            feat['sum'] = 0.0
            # Add the new feature to the layer
            if caps & QgsVectorDataProvider.AddFeatures:
                pr.addFeatures([feat])
            # Update the layer including the new feature
            self.aggregation_layer.updateFeature(feat)

        # End layer initialization
        self.aggregation_layer.endEditCommand()
        self.aggregation_layer.commitChanges()

        # update layer's extent when new features have been added
        # because change of extent in provider is not propagated to the layer
        self.aggregation_layer.updateExtents()

        # Add aggregation layer to registry
        if self.aggregation_layer.isValid():
            QgsMapLayerRegistry.instance().addMapLayer(self.aggregation_layer)
        else:
            raise RuntimeError('Aggregation layer invalid')

    def calculate_stats(self):
        if self.loss_layer_is_vector:
            self.calculate_vector_stats()
        else:
            self.calculate_raster_stats()

    def calculate_vector_stats(self):
        # get points from loss layer
        loss_features = self.loss_layer.getFeatures()
        # get regions from aggregation layer
        region_features = self.aggregation_layer.getFeatures()

        # create spatial index
        spatial_index = QgsSpatialIndex()
        for loss_feature in loss_features:
            spatial_index.insertFeature(loss_feature)
        loss_features.rewind()      # reset iterator

        # Begin updating count and sum attributes
        self.aggregation_layer.startEditing()
        self.aggregation_layer.beginEditCommand(
            "Edit count and sum attributes")

        # check if there are no loss points contained in any of the regions
        # and later display a warning if that occurs
        no_loss_points_in_any_region = True
        for region_feature in region_features:
            points_count = 0
            loss_sum = 0
            has_intersections = False
            region_geometry = region_feature.geometry()
            # Find ids of points within the bounding box of the region
            point_ids = spatial_index.intersects(region_geometry.boundingBox())
            if len(point_ids) > 0:
                has_intersections = True
            if has_intersections:
                # For points that are within the bounding box of the region
                for point_id in point_ids:
                    # Get the point feature by the point's id
                    request = QgsFeatureRequest().setFilterFid(point_id)
                    point_feature = self.loss_layer.getFeatures(request).next()
                    point_geometry = QgsGeometry(point_feature.geometry())
                    # check if the point is actually inside the region and
                    # it is not only contained by its bounding box
                    if region_geometry.contains(point_geometry):
                        points_count += 1
                        # we have found at least one loss point inside a region
                        no_loss_points_in_any_region = False
                        point_loss = point_feature[LOSS_ATTRIBUTE_NAME]
                        loss_sum += point_loss
                region_feature['count'] = points_count
                region_feature['sum'] = loss_sum
                self.aggregation_layer.updateFeature(region_feature)
        # End updating count and sum attributes
        self.aggregation_layer.endEditCommand()
        self.aggregation_layer.commitChanges()
        # display a warning in case none of the loss points are inside
        # any of the regions
        if no_loss_points_in_any_region:
            msg = "No loss points are contained by any of the regions!"
            self.iface.messageBar().pushMessage(self.tr("Warning"),
                                                self.tr(msg),
                                                level=QgsMessageBar.INFO)

    def calculate_raster_stats(self):
        zonal_statistics = QgsZonalStatistics(
            self.aggregation_layer,
            self.loss_layer.dataProvider().dataSourceUri())
        progress_dialog = QProgressDialog(
            self.tr('Calculating zonal statistics'),
            self.tr('Abort...'),
            0,
            0)
        zonal_statistics.calculateStatistics(progress_dialog)
        if progress_dialog.wasCanceled():
            QMessageBox.error(
                self, self.tr('ZonalStats: Error'),
                self.tr('You aborted aggregation, '
                        'so there are no data for analysis. Exiting...'))

    def create_new_aggregation_layer_with_no_empty_regions(self):
        """
        Create a new aggregation layer containing all the regions of the
        aggregation layer that contain at least one loss point
        """
        # create layer
        self.purged_layer = QgsVectorLayer(
            "Polygon",
            "Aggregated Losses (no empty regions)",
            "memory")
        pr = self.purged_layer.dataProvider()
        caps = pr.capabilities()

        # Begin layer initialization
        self.purged_layer.startEditing()
        self.purged_layer.beginEditCommand("Layer initialization")

        # add count and sum fields for aggregating statistics
        pr.addAttributes(
            [QgsField(REGION_ID_ATTRIBUTE_NAME, QVariant.String),
             QgsField("count", QVariant.Int),
             QgsField("sum",  QVariant.Double)])

        # copy regions from aggregation layer
        for region_feature in self.aggregation_layer.getFeatures():
            # copy only regions which contain at least one loss point
            if region_feature['count'] >= 1:
                feat = region_feature
                # Add the new feature to the layer
                if caps & QgsVectorDataProvider.AddFeatures:
                    pr.addFeatures([feat])
                # Update the layer including the new feature
                self.aggregation_layer.updateFeature(feat)

        # End layer initialization
        self.purged_layer.endEditCommand()
        self.purged_layer.commitChanges()

        # update layer's extent when new features have been added
        # because change of extent in provider is not propagated to the layer
        self.purged_layer.updateExtents()

        # Add purged layer to registry
        if self.purged_layer.isValid():
            QgsMapLayerRegistry.instance().addMapLayer(self.purged_layer)
            msg = "Regions containing at least one loss point have been " \
                  "copied into a new aggregation layer."
            self.iface.messageBar().pushMessage(self.tr("Info"),
                                    self.tr(msg),
                                    level=QgsMessageBar.INFO)
        else:
            raise RuntimeError('Purged layer invalid')<commit_msg>Fixed some PEP 8 styling<commit_after># -*- coding: utf-8 -*-
"""
/***************************************************************************
 Svir
                                 A QGIS plugin
 OpenQuake Social Vulnerability and Integrated Risk
                              -------------------
        begin                : 2013-10-24
        copyright            : (C) 2013 by GEM Foundation
        email                : devops@openquake.org
 ***************************************************************************/

# Copyright (c) 2010-2013, GEM Foundation.
#
# OpenQuake 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.
#
# OpenQuake 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 General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.
"""
import os.path

from PyQt4.QtCore import (QSettings,
                          QTranslator,
                          QCoreApplication,
                          qVersion,
                          QVariant)

from PyQt4.QtGui import (QApplication,
                         QAction,
                         QIcon,
                         QProgressDialog,
                         QMessageBox)

from qgis.core import (QgsVectorLayer,
                       QgsMapLayerRegistry,
                       QgsRasterLayer,
                       QgsField,
                       QgsFeature,
                       QgsGeometry,
                       QgsFields,
                       QgsSpatialIndex,
                       QgsFeatureRequest,
                       QgsVectorDataProvider)

from qgis.gui import QgsMessageBar

from qgis.analysis import QgsZonalStatistics

import resources_rc

# Import the code for the dialog
from svirdialog import SvirDialog

# Name of the attribute, in the input loss data layer, containing loss info
LOSS_ATTRIBUTE_NAME = "RSCORE"  # "loss"
REGION_ID_ATTRIBUTE_NAME = "STFID"

class Svir:

    @staticmethod
    def tr(message):
        return QApplication.translate('Svir', message)

    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        # initialize plugin directory
        self.plugin_dir = os.path.dirname(__file__)
        # initialize locale
        locale = QSettings().value("locale/userLocale")[0:2]
        locale_path = os.path.join(self.plugin_dir, 'i18n',
                                   'svir_{}.qm'.format(locale))

        if os.path.exists(locale_path):
            self.translator = QTranslator()
            self.translator.load(locale_path)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)

        # Create the dialog (after translation) and keep reference
        self.dlg = SvirDialog()

        self.loss_layer_is_vector = True
        # Input layer containing loss data
        self.loss_layer = None
        # Input layer specifying regions for aggregation
        self.regions_layer = None
        # Output layer containing aggregated loss data
        self.aggregation_layer = None
        # Output layer containing aggregated loss data for non-empty regions
        self.purged_layer = None
        self.initial_action = None
        self.purge_empty_regions_action = None

    def initGui(self):
        # Create action that will start plugin configuration
        self.initial_action = QAction(
            QIcon(":/plugins/svir/start_plugin_icon.png"),
            u"Aggregate loss by region", self.iface.mainWindow())
        # connect the action to the run method
        self.initial_action.triggered.connect(self.run)

        # Add toolbar button and menu item
        self.iface.addToolBarIcon(self.initial_action)
        self.iface.addPluginToMenu(u"&SVIR", self.initial_action)

    def unload(self):
        # Remove the plugin menu item and icon
        self.iface.removePluginMenu(u"&SVIR", self.initial_action)
        self.iface.removeToolBarIcon(self.initial_action)
        self.iface.removePluginMenu(u"&SVIR",
                                    self.purge_empty_regions_action)
        self.iface.removeToolBarIcon(self.purge_empty_regions_action)

    # run method that performs all the real work
    def run(self):
        # show the dialog
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result == 1:
            self.loss_layer_is_vector = self.dlg.loss_map_is_vector
            self.load_layers(self.dlg.ui.regions_layer_le.text(),
                             self.dlg.ui.loss_layer_le.text(),
                             self.loss_layer_is_vector)
            self.create_aggregation_layer()
            self.calculate_stats()
            # Create and enable toolbar button and menu item
            # for purging empty regions
            # Create action
            self.purge_empty_regions_action = QAction(
                QIcon(":/plugins/svir/purge_empty_regions_icon.png"),
                u"Purge empty regions",
                self.iface.mainWindow())
            # Connect the action to the purge_empty_regions method
            self.purge_empty_regions_action.triggered.connect(
                self.create_new_aggregation_layer_with_no_empty_regions)
            # Add toolbar button and menu item
            self.iface.addToolBarIcon(self.purge_empty_regions_action)
            self.iface.addPluginToMenu(u"&SVIR",
                                       self.purge_empty_regions_action)
            msg = 'Select "Purge empty regions" from SVIR plugin menu ' \
                  'to create a new aggregation layer with the regions ' \
                  'containing at least one loss point'
            self.iface.messageBar().pushMessage(self.tr("Info"),
                                            self.tr(msg),
                                            level=QgsMessageBar.INFO)

    def load_layers(self, aggregation_layer_path,
                    loss_layer_path,
                    loss_layer_is_vector):
        # Load aggregation layer
        self.regions_layer = QgsVectorLayer(aggregation_layer_path,
                                            self.tr('Regions'), 'ogr')
        # Add aggregation layer to registry
        if self.regions_layer.isValid():
            QgsMapLayerRegistry.instance().addMapLayer(self.regions_layer)
        else:
            raise RuntimeError('Aggregation layer invalid')
        # Load loss layer
        if loss_layer_is_vector:
            self.loss_layer = QgsVectorLayer(loss_layer_path,
                                             self.tr('Loss map'), 'ogr')
        else:
            self.loss_layer = QgsRasterLayer(loss_layer_path,
                                             self.tr('Loss map'))
        # Add loss layer to registry
        if self.loss_layer.isValid():
            QgsMapLayerRegistry.instance().addMapLayer(self.loss_layer)
        else:
            raise RuntimeError('Loss layer invalid')
        # Zoom depending on the regions layer's extent
        self.canvas.setExtent(self.regions_layer.extent())

    def create_aggregation_layer(self):
        """
        Create a new aggregation layer which contains the polygons from
        the regions layer. Two new attributes (count and sum) are
        initialized to 0 and will represent the count of loss points in a
        region and the sum of loss values for the same region.
        """
        # create layer
        self.aggregation_layer = QgsVectorLayer("Polygon",
                                                "Aggregated Losses",
                                                "memory")
        pr = self.aggregation_layer.dataProvider()
        caps = pr.capabilities()

        # Begin layer initialization
        self.aggregation_layer.startEditing()
        self.aggregation_layer.beginEditCommand("Layer initialization")

        # add count and sum fields for aggregating statistics
        pr.addAttributes(
            [QgsField(REGION_ID_ATTRIBUTE_NAME, QVariant.String),
             QgsField("count", QVariant.Int),
             QgsField("sum",  QVariant.Double)])

        # copy regions from regions layer
        for region_feature in self.regions_layer.getFeatures():
            feat = QgsFeature()
            # copy the polygon from the input aggregation layer
            feat.setGeometry(QgsGeometry(region_feature.geometry()))
            # Define the count and sum fields to initialize to 0
            fields = QgsFields()
            fields.append(QgsField(QgsField(REGION_ID_ATTRIBUTE_NAME,
                                             QVariant.String)))
            fields.append(QgsField(QgsField("count", QVariant.Int)))
            fields.append(QgsField(QgsField("sum", QVariant.Double)))
            # Add fields to the new feature
            feat.setFields(fields)
            feat[REGION_ID_ATTRIBUTE_NAME] = region_feature[
                REGION_ID_ATTRIBUTE_NAME]
            feat['count'] = 0
            feat['sum'] = 0.0
            # Add the new feature to the layer
            if caps & QgsVectorDataProvider.AddFeatures:
                pr.addFeatures([feat])
            # Update the layer including the new feature
            self.aggregation_layer.updateFeature(feat)

        # End layer initialization
        self.aggregation_layer.endEditCommand()
        self.aggregation_layer.commitChanges()

        # update layer's extent when new features have been added
        # because change of extent in provider is not propagated to the layer
        self.aggregation_layer.updateExtents()

        # Add aggregation layer to registry
        if self.aggregation_layer.isValid():
            QgsMapLayerRegistry.instance().addMapLayer(self.aggregation_layer)
        else:
            raise RuntimeError('Aggregation layer invalid')

    def calculate_stats(self):
        if self.loss_layer_is_vector:
            self.calculate_vector_stats()
        else:
            self.calculate_raster_stats()

    def calculate_vector_stats(self):
        # get points from loss layer
        loss_features = self.loss_layer.getFeatures()
        # get regions from aggregation layer
        region_features = self.aggregation_layer.getFeatures()

        # create spatial index
        spatial_index = QgsSpatialIndex()
        for loss_feature in loss_features:
            spatial_index.insertFeature(loss_feature)
        loss_features.rewind()      # reset iterator

        # Begin updating count and sum attributes
        self.aggregation_layer.startEditing()
        self.aggregation_layer.beginEditCommand(
            "Edit count and sum attributes")

        # check if there are no loss points contained in any of the regions
        # and later display a warning if that occurs
        no_loss_points_in_any_region = True
        for region_feature in region_features:
            points_count = 0
            loss_sum = 0
            has_intersections = False
            region_geometry = region_feature.geometry()
            # Find ids of points within the bounding box of the region
            point_ids = spatial_index.intersects(region_geometry.boundingBox())
            if len(point_ids) > 0:
                has_intersections = True
            if has_intersections:
                # For points that are within the bounding box of the region
                for point_id in point_ids:
                    # Get the point feature by the point's id
                    request = QgsFeatureRequest().setFilterFid(point_id)
                    point_feature = self.loss_layer.getFeatures(request).next()
                    point_geometry = QgsGeometry(point_feature.geometry())
                    # check if the point is actually inside the region and
                    # it is not only contained by its bounding box
                    if region_geometry.contains(point_geometry):
                        points_count += 1
                        # we have found at least one loss point inside a region
                        no_loss_points_in_any_region = False
                        point_loss = point_feature[LOSS_ATTRIBUTE_NAME]
                        loss_sum += point_loss
                region_feature['count'] = points_count
                region_feature['sum'] = loss_sum
                self.aggregation_layer.updateFeature(region_feature)
        # End updating count and sum attributes
        self.aggregation_layer.endEditCommand()
        self.aggregation_layer.commitChanges()
        # display a warning in case none of the loss points are inside
        # any of the regions
        if no_loss_points_in_any_region:
            msg = "No loss points are contained by any of the regions!"
            self.iface.messageBar().pushMessage(self.tr("Warning"),
                                                self.tr(msg),
                                                level=QgsMessageBar.INFO)

    def calculate_raster_stats(self):
        zonal_statistics = QgsZonalStatistics(
            self.aggregation_layer,
            self.loss_layer.dataProvider().dataSourceUri())
        progress_dialog = QProgressDialog(
            self.tr('Calculating zonal statistics'),
            self.tr('Abort...'),
            0,
            0)
        zonal_statistics.calculateStatistics(progress_dialog)
        if progress_dialog.wasCanceled():
            QMessageBox.error(
                self, self.tr('ZonalStats: Error'),
                self.tr('You aborted aggregation, '
                        'so there are no data for analysis. Exiting...'))

    def create_new_aggregation_layer_with_no_empty_regions(self):
        """
        Create a new aggregation layer containing all the regions of the
        aggregation layer that contain at least one loss point
        """
        # create layer
        self.purged_layer = QgsVectorLayer(
            "Polygon",
            "Aggregated Losses (no empty regions)",
            "memory")
        pr = self.purged_layer.dataProvider()
        caps = pr.capabilities()

        # Begin layer initialization
        self.purged_layer.startEditing()
        self.purged_layer.beginEditCommand("Layer initialization")

        # add count and sum fields for aggregating statistics
        pr.addAttributes(
            [QgsField(REGION_ID_ATTRIBUTE_NAME, QVariant.String),
             QgsField("count", QVariant.Int),
             QgsField("sum",  QVariant.Double)])

        # copy regions from aggregation layer
        for region_feature in self.aggregation_layer.getFeatures():
            # copy only regions which contain at least one loss point
            if region_feature['count'] >= 1:
                feat = region_feature
                # Add the new feature to the layer
                if caps & QgsVectorDataProvider.AddFeatures:
                    pr.addFeatures([feat])
                # Update the layer including the new feature
                self.aggregation_layer.updateFeature(feat)

        # End layer initialization
        self.purged_layer.endEditCommand()
        self.purged_layer.commitChanges()

        # update layer's extent when new features have been added
        # because change of extent in provider is not propagated to the layer
        self.purged_layer.updateExtents()

        # Add purged layer to registry
        if self.purged_layer.isValid():
            QgsMapLayerRegistry.instance().addMapLayer(self.purged_layer)
            msg = "Regions containing at least one loss point have been " \
                  "copied into a new aggregation layer."
            self.iface.messageBar().pushMessage(self.tr("Info"),
                                                self.tr(msg),
                                                level=QgsMessageBar.INFO)
        else:
            raise RuntimeError('Purged layer invalid')<|endoftext|>


<commit_before>from google.appengine.api import urlfetch, apiproxy_stub_map, urlfetch_stub

from urllib import urlencode

def ping(name, url, changesURL, ping_url='http://blogsearch.google.com/ping'):
  payload = urlencode(dict(name=name, url=url, changesURL=changesURL))
  response = urlfetch.fetch('%s?%s' % (ping_url, payload))
  if response:
    return response.content

if __name__ == '__main__':
  test();
<commit_msg>removed cruft<commit_after>from google.appengine.api import urlfetch, apiproxy_stub_map, urlfetch_stub

from urllib import urlencode

def ping(name, url, changesURL, ping_url='http://blogsearch.google.com/ping'):
  payload = urlencode(dict(name=name, url=url, changesURL=changesURL))
  response = urlfetch.fetch('%s?%s' % (ping_url, payload))
  if response:
    return response.content
<|endoftext|>