<commit_before>#import shared
#import time
#from multiprocessing import Pool, cpu_count
import hashlib
from struct import unpack, pack
import sys
#import os

def _set_idle():
    if 'linux' in sys.platform:
        import os
        os.nice(20)
    else:
        try:
            sys.getwindowsversion()
            import win32api,win32process,win32con
            pid = win32api.GetCurrentProcessId()
            handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
            win32process.SetPriorityClass(handle, win32process.IDLE_PRIORITY_CLASS)
        except:
            #Windows 64-bit
            pass

def _pool_worker(nonce, initialHash, target, pool_size):
    _set_idle()
    trialValue = 99999999999999999999
    while trialValue > target:
        nonce += pool_size
        trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
    return [trialValue, nonce]

def _doSafePoW(target, initialHash):
    nonce = 0
    trialValue = 99999999999999999999
    while trialValue > target:
        nonce += 1
        trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
    return [trialValue, nonce]

def _doFastPoW(target, initialHash):
    import shared
    import time
    from multiprocessing import Pool, cpu_count
    try:
        pool_size = cpu_count()
    except:
        pool_size = 4
    try:
        maxCores = config.getint('bitmessagesettings', 'maxcores')
    except:
        maxCores = 99999
    if pool_size > maxCores:
        pool_size = maxCores
    pool = Pool(processes=pool_size)
    result = []
    for i in range(pool_size):
        result.append(pool.apply_async(_pool_worker, args = (i, initialHash, target, pool_size)))
    while True:
        if shared.shutdown:
            pool.terminate()
            pool.join() #Don't return anything (doing so will cause exceptions because we'll return an unusable response). Sit here and wait for this thread to close.
            return
        for i in range(pool_size):
            if result[i].ready():
                result = result[i].get()
                pool.terminate()
                pool.join() #Wait for the workers to exit...
                return result[0], result[1]
        time.sleep(0.2)

def run(target, initialHash):
    if 'linux' in sys.platform:
        return _doFastPoW(target, initialHash)
    else:
        return _doSafePoW(target, initialHash)
<commit_msg>Prevent proofOfWork pool threads from returning any data if shut down before finished with a POW<commit_after>#import shared
#import time
#from multiprocessing import Pool, cpu_count
import hashlib
from struct import unpack, pack
import sys
#import os

def _set_idle():
    if 'linux' in sys.platform:
        import os
        os.nice(20)
    else:
        try:
            sys.getwindowsversion()
            import win32api,win32process,win32con
            pid = win32api.GetCurrentProcessId()
            handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
            win32process.SetPriorityClass(handle, win32process.IDLE_PRIORITY_CLASS)
        except:
            #Windows 64-bit
            pass

def _pool_worker(nonce, initialHash, target, pool_size):
    _set_idle()
    trialValue = 99999999999999999999
    while trialValue > target:
        nonce += pool_size
        trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
    return [trialValue, nonce]

def _doSafePoW(target, initialHash):
    nonce = 0
    trialValue = 99999999999999999999
    while trialValue > target:
        nonce += 1
        trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
    return [trialValue, nonce]

def _doFastPoW(target, initialHash):
    import shared
    import time
    from multiprocessing import Pool, cpu_count
    try:
        pool_size = cpu_count()
    except:
        pool_size = 4
    try:
        maxCores = config.getint('bitmessagesettings', 'maxcores')
    except:
        maxCores = 99999
    if pool_size > maxCores:
        pool_size = maxCores
    pool = Pool(processes=pool_size)
    result = []
    for i in range(pool_size):
        result.append(pool.apply_async(_pool_worker, args = (i, initialHash, target, pool_size)))
    while True:
        if shared.shutdown:
            pool.terminate()
            while True:
                time.sleep(10) # Don't let this thread return here; it will return nothing and cause an exception in bitmessagemain.py
            return
        for i in range(pool_size):
            if result[i].ready():
                result = result[i].get()
                pool.terminate()
                pool.join() #Wait for the workers to exit...
                return result[0], result[1]
        time.sleep(0.2)

def run(target, initialHash):
    if 'linux' in sys.platform:
        return _doFastPoW(target, initialHash)
    else:
        return _doSafePoW(target, initialHash)
<|endoftext|>


<commit_before>DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
    },
}

INSTALLED_APPS = (
    'django_filters',
    'django_filters.tests'
)

ROOT_URLCONF = None

USE_TZ = True
<commit_msg>Push to kick travis<commit_after>DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
    },
}

INSTALLED_APPS = (
    'django_filters',
    'django_filters.tests',
)

ROOT_URLCONF = None

USE_TZ = True
<|endoftext|>


<commit_before>'''lines-of-code counter for python

pyloc scans a list of directory trees for python source files,
counting lines of code. For each tree, two counts are reported:

 * A line count include comments and blank lines
 * A line count with out comments and blank lines

Finally, sums of both counts are reported.

The meat of this code was shamelessly stolen from
http://code.activestate.com/recipes/527746/
'''

from optparse import OptionParser
import os
import fnmatch

def walk(root='.', recurse=True, pattern='*'):
    '''generate a files in a directory tree walk matching a pattern

    Walks a directory tree, generating the files it finds that match a
    provided pattern

    :param root: the root of the tree
    :param recurse: whether to recurse

    :param pattern: a regular expression pattern for matching against
                    filenames
    '''
    for path, subdirs, files in os.walk(root):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                yield os.path.join(path, name)
        if not recurse:
            break

def loc(root='', recurse=True):
    '''count lines of code in a directory structure

    Generates two counts:

     * maximal size (source LOC) with blank lines and comments
     * minimal size (logical LOC) stripping same

    Sums all Python files in the specified folder.
    By default recurses through subfolders.

    :param root: the root of the tree to search
    :param recurse: whether to recurse
    '''
    count_mini, count_maxi = 0, 0
    for fspec in walk(root, recurse, '*.py'):
        skip = False
        for line in open(fspec).readlines():
            count_maxi += 1

            line = line.strip()
            if line:
                if line.startswith('#'):
                    continue
                if line.startswith('"""'):
                    skip = not skip
                    continue
                if not skip:
                    count_mini += 1

    return count_mini, count_maxi

def parse_args():
    parser = OptionParser(usage='%prog [options] [dir1 dir2 . . .]')
    return (parser.parse_args(),parser)

def main():
    ((options, args), parser) = parse_args()

    values = [(dir, loc(dir)) for dir in args]
    values.append(('TOTAL',
                   (sum([x[1][0] for x in values]),
                    sum([x[1][1] for x in values]))))
    max_dir_len = max([len(x[0]) for x in values])
    max_mini_len = max([len(str(x[1][0])) for x in values])
    max_maxi_len = max([len(str(x[1][1])) for x in values])

    for (dir, (mini, maxi)) in values:
        print '%-*s %*s %*s' % (max_dir_len, dir,
                               max_mini_len, mini,
                               max_maxi_len, maxi)

if __name__ == '__main__':
    main()
<commit_msg>added support for specifying single files as roots<commit_after>'''lines-of-code counter for python

pyloc scans a list of directory trees for python source files,
counting lines of code. For each tree, two counts are reported:

 * A line count include comments and blank lines
 * A line count with out comments and blank lines

Finally, sums of both counts are reported.

The meat of this code was shamelessly stolen from
http://code.activestate.com/recipes/527746/
'''

from optparse import OptionParser
import os, os.path
import fnmatch

def walk(root='.', recurse=True, pattern='*'):
    '''generate a files in a directory tree walk matching a pattern

    Walks a directory tree, generating the files it finds that match a
    provided pattern

    :param root: the root of the tree
    :param recurse: whether to recurse

    :param pattern: a regular expression pattern for matching against
                    filenames
    '''

    if os.path.isfile(root):
        yield root
    else:
        for path, subdirs, files in os.walk(root):
            for name in files:
                if fnmatch.fnmatch(name, pattern):
                    yield os.path.join(path, name)
            if not recurse:
                break

def loc(root='', recurse=True):
    '''count lines of code in a directory structure

    Generates two counts:

     * maximal size (source LOC) with blank lines and comments
     * minimal size (logical LOC) stripping same

    Sums all Python files in the specified folder.
    By default recurses through subfolders.

    :param root: the root of the tree to search
    :param recurse: whether to recurse
    '''
    count_mini, count_maxi = 0, 0
    for fspec in walk(root, recurse, '*.py'):
        skip = False
        for line in open(fspec).readlines():
            count_maxi += 1

            line = line.strip()
            if line:
                if line.startswith('#'):
                    continue
                if line.startswith('"""'):
                    skip = not skip
                    continue
                if not skip:
                    count_mini += 1

    return count_mini, count_maxi

def parse_args():
    parser = OptionParser(usage='%prog [options] [dir1 dir2 . . .]')
    return (parser.parse_args(),parser)

def main():
    ((options, args), parser) = parse_args()

    values = [(dir, loc(dir)) for dir in args]
    values.append(('TOTAL',
                   (sum([x[1][0] for x in values]),
                    sum([x[1][1] for x in values]))))
    max_dir_len = max([len(x[0]) for x in values])
    max_mini_len = max([len(str(x[1][0])) for x in values])
    max_maxi_len = max([len(str(x[1][1])) for x in values])

    for (dir, (mini, maxi)) in values:
        print '%-*s %*s %*s' % (max_dir_len, dir,
                               max_mini_len, mini,
                               max_maxi_len, maxi)

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


<commit_before># pylint: disable=W0401

from .core import Item, Field, Ignore
from .exc import Invalid
from .validation import Validator
from .fields import (
    Number,
    Float,
    Int,
    List,
    Mapping,
    Set,
    Unicode,
    UtcDateTime,
)
<commit_msg>validators missing<commit_after># pylint: disable=W0401

from .core import Item, Field, Ignore
from .exc import Invalid
from .validation import Validator, OneOf, NoneOf
from .fields import (
    Number,
    Float,
    Int,
    List,
    Mapping,
    Set,
    Unicode,
    UtcDateTime,
)
<|endoftext|>


<commit_before>#!/usr/bin/env python

import shlex
import string
import subprocess
import sys
import tempfile
from contextlib import contextmanager
from csv import QUOTE_NONE
from distutils.version import LooseVersion
from errno import ENOENT
from functools import wraps
from glob import iglob
from io import BytesIO
from os import environ, extsep, remove
from os.path import normcase, normpath, realpath
from pkgutil import find_loader
from threading import Timer

try:
    from PIL import Image
except ImportError:
    import Image

tesseract_cmd = 'tesseract'

numpy_installed = find_loader('numpy') is not None
if numpy_installed:
    from numpy import ndarray

pandas_installed = find_loader('pandas') is not None
if pandas_installed:
    import pandas as pd

RGB_MODE = 'RGB'
SUPPORTED_FORMATS = {'JPEG', 'PNG', 'PBM', 'PGM', 'PPM', 'TIFF', 'BMP', 'GIF'}

OSD_KEYS = {
    'Page number': ('page_num', int),
    'Orientation in degrees': ('orientation', int),
    'Rotate': ('rotate', int),
    'Orientation confidence': ('orientation_conf', float),
    'Script': ('script', str),
    'Script confidence': ('script_conf', float),
}

class Output:
    BYTES = 'bytes'
    DATAFRAME = 'data.frame'
    DICT = 'dict'
    STRING = 'string'

class PandasNotSupported(EnvironmentError):
    def __init__(self):
        super(PandasNotSupported, self).__init__('Missing pandas package')

class TesseractError(RuntimeError):
    def __init__(self, status, message):
        self.status = status
        self.message = message
        self.args = (status, message)

class TesseractNotFoundError(EnvironmentError):
    def __init__(self):
        super(TesseractNotFoundError, self).__init__(
            tesseract_cmd + " is not installed or it's not in your path",
        )

class TSVNotSupported(EnvironmentError):
    def __init__(self):
        super(TSVNotSupported, self).__init__(
            'TSV output not supported. Tesseract >= 3.05 required',
        )

def kill(process, code):
    process.kill()
    process.returncode = code

@contextmanager
def timeout_manager(proc, seconds=0):
    try:
        if not seconds:
            yield proc.communicate()[1]
            return

        timeout_code = -1
        timer = Timer(seconds, kill, [proc, timeout_code])
        timer.start()
        try:
            _, error_string = proc.communicate()
            yield error_string
        finally:
            timer.cancel()
            if proc.returncode == timeout_code:
                raise RuntimeError('Tesseract process timeout')
    finally:
        proc.stdin.close()
        proc.stdout.close()
        proc.stderr.close()

def run_once(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        if wrapper._result is wrapper:
            wrapper._result = func(*args, **kwargs)
        return wrapper._result

    wrapper._result = wrapper
    return wrapper

def get_errors(error_string):
    return u' '.join(
        line for line in error_string.decode('utf-8').splitlines()
    ).strip()

def cleanup(temp_name):
    """ Tries to remove temp files by filename wildcard path. """
    for filename in iglob(temp_name + '*' if temp_name else temp_name):
        try:
            remove(filename)
        except OSError as e:
            if e.errno != ENOENT:
                raise e

def prepare(image):
    if numpy_installed and isinstance(image, ndarray):
        image = Image.fromarray(image)

    if not isinstance(image, Image.Image):
        raise TypeError('Unsupported image object')

    extension = 'PNG' if not image.format else image.format
    if extension not in SUPPORTED_FORMATS:
        raise TypeError('Unsupported image format/type')

    if not image.mode.startswith(RGB_MODE):
        image = image.convert(RGB_MODE)

    if 'A' in image.getbands():
        # discard and replace the alpha channel with white background
        background = Image.new(RGB_MODE, image.size, (255, 255, 255))
        background.paste(image, (0, 0), image)
        image = background

    image.format = extension
    return image, extension

@contextmanager
def save(image):
    with tempfile.NamedTemporaryFile(prefix='tess_') as f:
        if isinstance(image, str):
            yield f.name, realpath(normpath(normcase(image)))
            return

        image, extension = prepare(image)
        input_file_name = f.name + extsep + extension
        image.save(input_file_name, format=extension, **image.info)

        try:
            yield f.name, input_file_name
        finally:
            cleanup(input_file_name)

def subprocess_args(include_stdout=True):
    # See https://github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess
    # for reference and comments.

    kwargs = {
        'stdin': subprocess.PIPE,
        'stderr': subprocess.PIPE,
        'startupinfo': None,
        'env': environ,
    }

    if hasattr(subprocess, 'STARTUPINFO'):
        kwargs['startupinfo'] = subprocess.STARTUPINFO()
        kwargs['startupinfo'].dwFlags |= subprocess.STARTF_USESHOWWINDOW
        kwargs['startupinfo'].wShowWindow = subprocess.SW_HIDE

    if include_stdout:
        kwargs['stdout'] = subprocess.PIPE

    return kwargs

def run_tesseract(
    input_filename,
    output_filename_base,
    extension,
    lang,
    config='',
    nice=0,
    timeout=0,
):
    cmd_args = []

    if not sys.platform.startswith('win32') and nice != 0:
        cmd_args += ('nice', '-n', str(nice))

    cmd_args += (tesseract_cmd, input_filename, output_filename_base)

    if lang is not None:
        cmd_args += ('-l', lang)

    if config:
        cmd_args += shlex.split(config)

    if extension and extension not in {'box', 'osd', 'tsv'}:
        cmd_args.append(extension)

    try:
        proc = subprocess.Popen(cmd_args, **subprocess_args())
    except OSError as e:
        if e.errno != ENOENT:
            raise e
        raise TesseractNotFoundError()

    with timeout_manager(proc, timeout) as error_string:
        if proc.returncode:
            raise TesseractError(proc.returncode, get_errors(error_string))

def run_and_get_output(
    image,
    extension='',
    lang=None,
    config='',
    nice=0,
    timeout=0,
    return_bytes=False,
):

    with save(image) as (temp_name, input_filename):
        kwargs = {
            'input_filename': input_filename,
            'output_filename_base': temp_name,
            'extension': extension,
            'lang': lang,
            'config': config,
            'nice': nice,
            'timeout': timeout,
        }

        run_tesseract(**kwargs)
        filename = kwargs['output_filename_base'] + extsep + extension
        with open(filename, 'rb') as output_file:
            if return_bytes:
                return output_file.read()
            return output_file.read().decode('utf-8').strip()

def file_to_dict(tsv, cell_delimiter, str_col_idx):
    result = {}
    rows = [row.split(cell_delimiter) for row in tsv.split('\n')]
    if not rows:
        return result

    header = rows.pop(0)
    length = len(header)
    if len(rows[-1]) < length:
        # Fixes bug that occurs when last text string in TSV is null, and
        # last row is missing a final cell in TSV file
        rows[-1].append('')

    if str_col_idx < 0:
        str_col_idx += length

    for i, head in enumerate(header):
        result[head] = list()
        for row in rows:
            if len(row) <= i:
                continue

            val = row[i]
            if row[i].isdigit() and i != str_col_idx:
                val = int(row[i])
            result[head].append(val)

    return result

def is_valid(val, _type):
    if _type is int:
        return val.isdigit()

    if _type is float:
        try:
            float(val)
            return True
        except ValueError:
            return False

    return True

def osd_to_dict(osd):
    return {
        OSD_KEYS[kv[0]][0]: OSD_KEYS[kv[0]][1](kv[1])
        for kv in (line.split(': ') for line in osd.split('\n'))
        if len(kv) == 2 and is_valid(kv[1], OSD_KEYS[kv[0]][1])
    }

@run_once
def get_tesseract_version():
    """
    Returns LooseVersion object of the Tesseract version
    """
    try:
        return LooseVersion(
            subprocess.check_output(
                [tesseract_cmd, '--version'], stderr=subprocess.STDOUT,
            )
            .decode('utf-8')
            .split()[1]
            .lstrip(string.printable[10:]),
        )
    except OSError:
        raise TesseractNotFoundError()

def image_to_string(
    image, lang=None, config='', nice=0, output_type=Output.STRING, timeout=0,
):
    """
    Returns the result of a Tesseract OCR run on the provided image to string
    """
    args = [image, 'txt', lang, config, nice, timeout]

    return {
        Output.BYTES: lambda: run_and_get_output(*(args + [True])),
        Output.DICT: lambda: {'text': run_and_get_output(*args)},
        Output.STRING: lambda: run_and_get_output(*args),
    }[output_type]()

def image_to_pdf_or_hocr(
    image, lang=None, config='', nice=0, extension='pdf', timeout=0,
):
    """
    Returns the result of a Tesseract OCR run on the provided image to pdf/hocr
    """

    if extension not in {'pdf', 'hocr'}:
        raise ValueError('Unsupported extension: {}'.format(extension))
    args = [image, extension, lang, config, nice, timeout, True]

    return run_and_get_output(*args)

def image_to_boxes(
    image, lang=None, config='', nice=0, output_type=Output.STRING, timeout=0,
):
    """
    Returns string containing recognized characters and their box boundaries
    """
    config += ' batch.nochop makebox'
    args = [image, 'box', lang, config, nice, timeout]

    return {
        Output.BYTES: lambda: run_and_get_output(*(args + [True])),
        Output.DICT: lambda: file_to_dict(
            'char left bottom right top page\n' + run_and_get_output(*args),
            ' ',
            0,
        ),
        Output.STRING: lambda: run_and_get_output(*args),
    }[output_type]()

def get_pandas_output(args, config=None):
    if not pandas_installed:
        raise PandasNotSupported()

    kwargs = {'quoting': QUOTE_NONE, 'sep': '\t'}
    try:
        kwargs.update(config)
    except (TypeError, ValueError):
        pass

    return pd.read_csv(BytesIO(run_and_get_output(*args)), **kwargs)

def image_to_data(
    image,
    lang=None,
    config='',
    nice=0,
    output_type=Output.STRING,
    timeout=0,
    pandas_config=None,
):
    """
    Returns string containing box boundaries, confidences,
    and other information. Requires Tesseract 3.05+
    """

    if get_tesseract_version() < '3.05':
        raise TSVNotSupported()

    config = '{} {}'.format('-c tessedit_create_tsv=1', config.strip()).strip()
    args = [image, 'tsv', lang, config, nice, timeout]

    return {
        Output.BYTES: lambda: run_and_get_output(*(args + [True])),
        Output.DATAFRAME: lambda: get_pandas_output(
            args + [True], pandas_config,
        ),
        Output.DICT: lambda: file_to_dict(run_and_get_output(*args), '\t', -1),
        Output.STRING: lambda: run_and_get_output(*args),
    }[output_type]()

def image_to_osd(
    image, lang='osd', config='', nice=0, output_type=Output.STRING, timeout=0,
):
    """
    Returns string containing the orientation and script detection (OSD)
    """
    config = '{}-psm 0 {}'.format(
        '' if get_tesseract_version() < '3.05' else '-', config.strip(),
    ).strip()
    args = [image, 'osd', lang, config, nice, timeout]

    return {
        Output.BYTES: lambda: run_and_get_output(*(args + [True])),
        Output.DICT: lambda: osd_to_dict(run_and_get_output(*args)),
        Output.STRING: lambda: run_and_get_output(*args),
    }[output_type]()

def main():
    if len(sys.argv) == 2:
        filename, lang = sys.argv[1], None
    elif len(sys.argv) == 4 and sys.argv[1] == '-l':
        filename, lang = sys.argv[3], sys.argv[2]
    else:
        sys.stderr.write('Usage: pytesseract [-l lang] input_file\n')
        exit(2)

    try:
        with Image.open(filename) as img:
            print(image_to_string(img, lang=lang))
    except IOError:
        sys.stderr.write('ERROR: Could not open file "%s"\n' % filename)
        exit(1)

if __name__ == '__main__':
    main()
<commit_msg>Fix cleanup of temporary files<commit_after>#!/usr/bin/env python

import shlex
import string
import subprocess
import sys
from contextlib import contextmanager
from csv import QUOTE_NONE
from distutils.version import LooseVersion
from errno import ENOENT
from functools import wraps
from glob import iglob
from io import BytesIO
from os import environ, extsep, remove
from os.path import normcase, normpath, realpath
from pkgutil import find_loader
from tempfile import NamedTemporaryFile
from threading import Timer

try:
    from PIL import Image
except ImportError:
    import Image

tesseract_cmd = 'tesseract'

numpy_installed = find_loader('numpy') is not None
if numpy_installed:
    from numpy import ndarray

pandas_installed = find_loader('pandas') is not None
if pandas_installed:
    import pandas as pd

RGB_MODE = 'RGB'
SUPPORTED_FORMATS = {'JPEG', 'PNG', 'PBM', 'PGM', 'PPM', 'TIFF', 'BMP', 'GIF'}

OSD_KEYS = {
    'Page number': ('page_num', int),
    'Orientation in degrees': ('orientation', int),
    'Rotate': ('rotate', int),
    'Orientation confidence': ('orientation_conf', float),
    'Script': ('script', str),
    'Script confidence': ('script_conf', float),
}

class Output:
    BYTES = 'bytes'
    DATAFRAME = 'data.frame'
    DICT = 'dict'
    STRING = 'string'

class PandasNotSupported(EnvironmentError):
    def __init__(self):
        super(PandasNotSupported, self).__init__('Missing pandas package')

class TesseractError(RuntimeError):
    def __init__(self, status, message):
        self.status = status
        self.message = message
        self.args = (status, message)

class TesseractNotFoundError(EnvironmentError):
    def __init__(self):
        super(TesseractNotFoundError, self).__init__(
            tesseract_cmd + " is not installed or it's not in your path",
        )

class TSVNotSupported(EnvironmentError):
    def __init__(self):
        super(TSVNotSupported, self).__init__(
            'TSV output not supported. Tesseract >= 3.05 required',
        )

def kill(process, code):
    process.kill()
    process.returncode = code

@contextmanager
def timeout_manager(proc, seconds=0):
    try:
        if not seconds:
            yield proc.communicate()[1]
            return

        timeout_code = -1
        timer = Timer(seconds, kill, [proc, timeout_code])
        timer.start()
        try:
            _, error_string = proc.communicate()
            yield error_string
        finally:
            timer.cancel()
            if proc.returncode == timeout_code:
                raise RuntimeError('Tesseract process timeout')
    finally:
        proc.stdin.close()
        proc.stdout.close()
        proc.stderr.close()

def run_once(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        if wrapper._result is wrapper:
            wrapper._result = func(*args, **kwargs)
        return wrapper._result

    wrapper._result = wrapper
    return wrapper

def get_errors(error_string):
    return u' '.join(
        line for line in error_string.decode('utf-8').splitlines()
    ).strip()

def cleanup(temp_name):
    """ Tries to remove temp files by filename wildcard path. """
    for filename in iglob(temp_name + '*' if temp_name else temp_name):
        try:
            remove(filename)
        except OSError as e:
            if e.errno != ENOENT:
                raise e

def prepare(image):
    if numpy_installed and isinstance(image, ndarray):
        image = Image.fromarray(image)

    if not isinstance(image, Image.Image):
        raise TypeError('Unsupported image object')

    extension = 'PNG' if not image.format else image.format
    if extension not in SUPPORTED_FORMATS:
        raise TypeError('Unsupported image format/type')

    if not image.mode.startswith(RGB_MODE):
        image = image.convert(RGB_MODE)

    if 'A' in image.getbands():
        # discard and replace the alpha channel with white background
        background = Image.new(RGB_MODE, image.size, (255, 255, 255))
        background.paste(image, (0, 0), image)
        image = background

    image.format = extension
    return image, extension

@contextmanager
def save(image):
    try:
        with NamedTemporaryFile(prefix='tess_') as f:
            if isinstance(image, str):
                yield f.name, realpath(normpath(normcase(image)))
                return

            image, extension = prepare(image)
            input_file_name = f.name + extsep + extension
            image.save(input_file_name, format=extension, **image.info)
            yield f.name, input_file_name
    finally:
        cleanup(input_file_name)

def subprocess_args(include_stdout=True):
    # See https://github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess
    # for reference and comments.

    kwargs = {
        'stdin': subprocess.PIPE,
        'stderr': subprocess.PIPE,
        'startupinfo': None,
        'env': environ,
    }

    if hasattr(subprocess, 'STARTUPINFO'):
        kwargs['startupinfo'] = subprocess.STARTUPINFO()
        kwargs['startupinfo'].dwFlags |= subprocess.STARTF_USESHOWWINDOW
        kwargs['startupinfo'].wShowWindow = subprocess.SW_HIDE

    if include_stdout:
        kwargs['stdout'] = subprocess.PIPE

    return kwargs

def run_tesseract(
    input_filename,
    output_filename_base,
    extension,
    lang,
    config='',
    nice=0,
    timeout=0,
):
    cmd_args = []

    if not sys.platform.startswith('win32') and nice != 0:
        cmd_args += ('nice', '-n', str(nice))

    cmd_args += (tesseract_cmd, input_filename, output_filename_base)

    if lang is not None:
        cmd_args += ('-l', lang)

    if config:
        cmd_args += shlex.split(config)

    if extension and extension not in {'box', 'osd', 'tsv'}:
        cmd_args.append(extension)

    try:
        proc = subprocess.Popen(cmd_args, **subprocess_args())
    except OSError as e:
        if e.errno != ENOENT:
            raise e
        raise TesseractNotFoundError()

    with timeout_manager(proc, timeout) as error_string:
        if proc.returncode:
            raise TesseractError(proc.returncode, get_errors(error_string))

def run_and_get_output(
    image,
    extension='',
    lang=None,
    config='',
    nice=0,
    timeout=0,
    return_bytes=False,
):

    with save(image) as (temp_name, input_filename):
        kwargs = {
            'input_filename': input_filename,
            'output_filename_base': temp_name,
            'extension': extension,
            'lang': lang,
            'config': config,
            'nice': nice,
            'timeout': timeout,
        }

        run_tesseract(**kwargs)
        filename = kwargs['output_filename_base'] + extsep + extension
        with open(filename, 'rb') as output_file:
            if return_bytes:
                return output_file.read()
            return output_file.read().decode('utf-8').strip()

def file_to_dict(tsv, cell_delimiter, str_col_idx):
    result = {}
    rows = [row.split(cell_delimiter) for row in tsv.split('\n')]
    if not rows:
        return result

    header = rows.pop(0)
    length = len(header)
    if len(rows[-1]) < length:
        # Fixes bug that occurs when last text string in TSV is null, and
        # last row is missing a final cell in TSV file
        rows[-1].append('')

    if str_col_idx < 0:
        str_col_idx += length

    for i, head in enumerate(header):
        result[head] = list()
        for row in rows:
            if len(row) <= i:
                continue

            val = row[i]
            if row[i].isdigit() and i != str_col_idx:
                val = int(row[i])
            result[head].append(val)

    return result

def is_valid(val, _type):
    if _type is int:
        return val.isdigit()

    if _type is float:
        try:
            float(val)
            return True
        except ValueError:
            return False

    return True

def osd_to_dict(osd):
    return {
        OSD_KEYS[kv[0]][0]: OSD_KEYS[kv[0]][1](kv[1])
        for kv in (line.split(': ') for line in osd.split('\n'))
        if len(kv) == 2 and is_valid(kv[1], OSD_KEYS[kv[0]][1])
    }

@run_once
def get_tesseract_version():
    """
    Returns LooseVersion object of the Tesseract version
    """
    try:
        return LooseVersion(
            subprocess.check_output(
                [tesseract_cmd, '--version'], stderr=subprocess.STDOUT,
            )
            .decode('utf-8')
            .split()[1]
            .lstrip(string.printable[10:]),
        )
    except OSError:
        raise TesseractNotFoundError()

def image_to_string(
    image, lang=None, config='', nice=0, output_type=Output.STRING, timeout=0,
):
    """
    Returns the result of a Tesseract OCR run on the provided image to string
    """
    args = [image, 'txt', lang, config, nice, timeout]

    return {
        Output.BYTES: lambda: run_and_get_output(*(args + [True])),
        Output.DICT: lambda: {'text': run_and_get_output(*args)},
        Output.STRING: lambda: run_and_get_output(*args),
    }[output_type]()

def image_to_pdf_or_hocr(
    image, lang=None, config='', nice=0, extension='pdf', timeout=0,
):
    """
    Returns the result of a Tesseract OCR run on the provided image to pdf/hocr
    """

    if extension not in {'pdf', 'hocr'}:
        raise ValueError('Unsupported extension: {}'.format(extension))
    args = [image, extension, lang, config, nice, timeout, True]

    return run_and_get_output(*args)

def image_to_boxes(
    image, lang=None, config='', nice=0, output_type=Output.STRING, timeout=0,
):
    """
    Returns string containing recognized characters and their box boundaries
    """
    config += ' batch.nochop makebox'
    args = [image, 'box', lang, config, nice, timeout]

    return {
        Output.BYTES: lambda: run_and_get_output(*(args + [True])),
        Output.DICT: lambda: file_to_dict(
            'char left bottom right top page\n' + run_and_get_output(*args),
            ' ',
            0,
        ),
        Output.STRING: lambda: run_and_get_output(*args),
    }[output_type]()

def get_pandas_output(args, config=None):
    if not pandas_installed:
        raise PandasNotSupported()

    kwargs = {'quoting': QUOTE_NONE, 'sep': '\t'}
    try:
        kwargs.update(config)
    except (TypeError, ValueError):
        pass

    return pd.read_csv(BytesIO(run_and_get_output(*args)), **kwargs)

def image_to_data(
    image,
    lang=None,
    config='',
    nice=0,
    output_type=Output.STRING,
    timeout=0,
    pandas_config=None,
):
    """
    Returns string containing box boundaries, confidences,
    and other information. Requires Tesseract 3.05+
    """

    if get_tesseract_version() < '3.05':
        raise TSVNotSupported()

    config = '{} {}'.format('-c tessedit_create_tsv=1', config.strip()).strip()
    args = [image, 'tsv', lang, config, nice, timeout]

    return {
        Output.BYTES: lambda: run_and_get_output(*(args + [True])),
        Output.DATAFRAME: lambda: get_pandas_output(
            args + [True], pandas_config,
        ),
        Output.DICT: lambda: file_to_dict(run_and_get_output(*args), '\t', -1),
        Output.STRING: lambda: run_and_get_output(*args),
    }[output_type]()

def image_to_osd(
    image, lang='osd', config='', nice=0, output_type=Output.STRING, timeout=0,
):
    """
    Returns string containing the orientation and script detection (OSD)
    """
    config = '{}-psm 0 {}'.format(
        '' if get_tesseract_version() < '3.05' else '-', config.strip(),
    ).strip()
    args = [image, 'osd', lang, config, nice, timeout]

    return {
        Output.BYTES: lambda: run_and_get_output(*(args + [True])),
        Output.DICT: lambda: osd_to_dict(run_and_get_output(*args)),
        Output.STRING: lambda: run_and_get_output(*args),
    }[output_type]()

def main():
    if len(sys.argv) == 2:
        filename, lang = sys.argv[1], None
    elif len(sys.argv) == 4 and sys.argv[1] == '-l':
        filename, lang = sys.argv[3], sys.argv[2]
    else:
        sys.stderr.write('Usage: pytesseract [-l lang] input_file\n')
        exit(2)

    try:
        with Image.open(filename) as img:
            print(image_to_string(img, lang=lang))
    except IOError:
        sys.stderr.write('ERROR: Could not open file "%s"\n' % filename)
        exit(1)

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


<commit_before>'''
Created on Mar 28, 2016

@author: Brandon
'''

from math import sin, cos, sqrt, atan2, radians
import dijkstra
import googleNodes
import requests
import json
import googlemaps
from datetime import datetime

def distanceCalculator(startLatitude,startLongitude,endLatitude,endLongitude):
    R = 6373.0
    lat1 = radians(startLatitude)
    lon1 = radians(startLongitude)

    lat2=radians(endLatitude)
    lon2=radians(endLongitude)
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
    c = 2 * atan2(sqrt(a), sqrt(1 - a))
    distance = R * c
    return distance

def checkValidThree(chargerNames, supportedChargers):

    currentSupportedIndex = 0
    while currentSupportedIndex<len(chargerNames):
        if(chargerNames[currentSupportedIndex] in supportedChargers):
            return True
        currentSupportedIndex +=1

    return False

def jsonFetch(distance, midLat, midLong):
    url = 'http://api.openchargemap.io/v2/poi/'
    payload = {'output': 'json', 'distanceunit': 'KM', 'distance': str(distance), 'latitude': str(midLat), 'longitude': str(midLong)}
    r = requests.get(url, params=payload)
    return json.loads(r.text)

def jsonParsingIntoRequestList(requestList, data, startindex, suppportedChargers):
    x=startindex
    for point in data:

        chargerList = []
        chargerIDList = []
        lat = point['AddressInfo']['Latitude']
        long = point['AddressInfo']['Longitude']

        addressLine = point['AddressInfo']['AddressLine1']
        addressTitle = point['AddressInfo']['Title']
        addressDetails = [lat, long, addressLine, addressTitle]

        for chargerType in point['Connections']:
            if (chargerType['Level']!=None and 'ID' in chargerType['Level']):
                chargerList.append(chargerType['Level']['ID'])
            else:
                chargerList.append(None)
            if (chargerType['ConnectionType']!=None and 'ID' in chargerType['ConnectionType']):
                chargerIDList.append(str(chargerType['ConnectionType']['ID']))

        if (addressDetails not in requestList and (1 in chargerList or 2 in chargerList or None in chargerList)):
            requestList.append([lat, long, str(x),addressLine,addressTitle])
            x=x+1

        elif ((3 in chargerList)):
            found=checkValidThree(chargerIDList,suppportedChargers)
            if found:
                requestList.append([lat, long, str(x),addressLine,addressTitle])

    return x, requestList

def graphGenerator(requestList, maxDistance):
    graph = {}
    print("generating graph. This may take a while.")
    for x in requestList:
        temp_graph = {}
        for y in requestList:
            if (x[2]!=y[2]):
                distance = distanceCalculator(x[0],x[1],y[0],y[1])
                if (distance<maxDistance/1.5):
                    temp_graph[y[2]]=distance
                    print("added connection from {} to {} with distance {}km".format(x[2],y[2],distance))

        if (len(temp_graph)!=0):
            graph[x[2]]=temp_graph
    return graph

def requestGenerator(startLatitude, startLongitude, endLatitude, endLongitude, suppportedChargers):
    start = "start"
    end = "end"

    requestList = []
    requestList.append([startLatitude, startLongitude, start])
    requestList.append([endLatitude, endLongitude, end])
    distance = distanceCalculator(startLatitude,startLongitude,endLatitude,endLongitude)

    #generate datapoints for start index
    print("Fetching JSON from open charge map")
    data = jsonFetch(distance, startLatitude, startLongitude)
    print("parsing JSON into list")
    x, requestList = jsonParsingIntoRequestList(requestList,data,1, suppportedChargers)

    #generate datapoints for end index
    print("Fetching JSON from open charge map")
    data = jsonFetch(distance, endLatitude, endLongitude)
    print("parsing JSON into list")
    x, requestList = jsonParsingIntoRequestList(requestList,data,x, suppportedChargers)

    return requestList

def main(startLatitude, startLongitude, endLatitude, endLongitude, maxDistance, supportedChargers):
    print("Main Start")
    requestList = requestGenerator(startLatitude,startLongitude,endLatitude,endLongitude, supportedChargers)
    print("generating graph")
    graph = graphGenerator(requestList,maxDistance)
    #shortestPath = dijkstra.shortestPath(graph,"start","end")

    #actualDistances = googleNodes.googleMapsActualPath(requestList, shortestPath)
    correctPath = False
    #print(shortestPath)
    count = 0
    print("searching for best path")
    while correctPath == False:
        correctPath=True
        currentDistanceCounter = 0

        print("calculating path")
        shortestPath = dijkstra.shortestPath(graph,"start","end")
        #print("shortestPath {}".format(shortestPath))
        if shortestPath == None or None in shortestPath or count>50:
            print("too many trimming iterations")
            print(shortestPath)
            return None
            break
        else:
            actualDistances=googleNodes.googleMapsActualPath(requestList,shortestPath)
            print("actual distances of the way points in KM are {}".format(actualDistances))

        print("verifying path")
        while currentDistanceCounter<len(actualDistances):
            if actualDistances[currentDistanceCounter]/1000>maxDistance:
                correctPath = False
                graph[str(shortestPath[currentDistanceCounter])].pop(str(shortestPath[currentDistanceCounter+1]),None)
            else:
                graph[str(shortestPath[currentDistanceCounter])][str(shortestPath[currentDistanceCounter+1])]=actualDistances[currentDistanceCounter]/1000
            currentDistanceCounter += 1
#        if(correctPath==True):
#            break
        #print(graph)
        print("actual distances {}".format(actualDistances))
        count+=1

    if shortestPath==None:
        print("No path found")
        return None
    print("Packaging results")

    waypointsData = []
    waypointsData.append([requestList[0][0],requestList[0][1],None,None])
    for point in shortestPath:
        if point != "start" and point != "end":
            waypointsData.append([requestList[int(point)+1][0],requestList[int(point)+1][1],requestList[int(point)+1][3],requestList[int(point)+1][4]])
            #print(requestList[int(point)+1])
    waypointsData.append([requestList[1][0],requestList[1][1],None,None])
    #print(waypoints)
    waypoints = {}
    waypoints['Waypoints']=waypointsData
    jsonWaypoints = json.dumps(waypoints)
    print(jsonWaypoints)
    return jsonWaypoints

#current Inputs
#startLatitude = 39.5
#startLongitude = -121.9
#endLatitude = 40
#endLongitude = -121
#maxDistance = 100
#supportedChargers = []
#waypoints = main(startLatitude,startLongitude,endLatitude,endLongitude,maxDistance,supportedChargers)

#print(waypoints)
#https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=longtitude,latitude&destinations=40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626&key=YOUR_API_KEY<commit_msg>Removed JSON formatting<commit_after>'''
Created on Mar 28, 2016

@author: Brandon
'''

from math import sin, cos, sqrt, atan2, radians
import dijkstra
import googleNodes
import requests
import json
import googlemaps
from datetime import datetime

def distanceCalculator(startLatitude,startLongitude,endLatitude,endLongitude):
    R = 6373.0
    lat1 = radians(startLatitude)
    lon1 = radians(startLongitude)

    lat2=radians(endLatitude)
    lon2=radians(endLongitude)
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
    c = 2 * atan2(sqrt(a), sqrt(1 - a))
    distance = R * c
    return distance

def checkValidThree(chargerNames, supportedChargers):

    currentSupportedIndex = 0
    while currentSupportedIndex<len(chargerNames):
        if(chargerNames[currentSupportedIndex] in supportedChargers):
            return True
        currentSupportedIndex +=1

    return False

def jsonFetch(distance, midLat, midLong):
    url = 'http://api.openchargemap.io/v2/poi/'
    payload = {'output': 'json', 'distanceunit': 'KM', 'distance': str(distance), 'latitude': str(midLat), 'longitude': str(midLong)}
    r = requests.get(url, params=payload)
    return json.loads(r.text)

def jsonParsingIntoRequestList(requestList, data, startindex, suppportedChargers):
    x=startindex
    for point in data:

        chargerList = []
        chargerIDList = []
        lat = point['AddressInfo']['Latitude']
        long = point['AddressInfo']['Longitude']

        addressLine = point['AddressInfo']['AddressLine1']
        addressTitle = point['AddressInfo']['Title']
        addressDetails = [lat, long, addressLine, addressTitle]

        for chargerType in point['Connections']:
            if (chargerType['Level']!=None and 'ID' in chargerType['Level']):
                chargerList.append(chargerType['Level']['ID'])
            else:
                chargerList.append(None)
            if (chargerType['ConnectionType']!=None and 'ID' in chargerType['ConnectionType']):
                chargerIDList.append(str(chargerType['ConnectionType']['ID']))

        if (addressDetails not in requestList and (1 in chargerList or 2 in chargerList or None in chargerList)):
            requestList.append([lat, long, str(x),addressLine,addressTitle])
            x=x+1

        elif ((3 in chargerList)):
            found=checkValidThree(chargerIDList,suppportedChargers)
            if found:
                requestList.append([lat, long, str(x),addressLine,addressTitle])

    return x, requestList

def graphGenerator(requestList, maxDistance):
    graph = {}
    print("generating graph. This may take a while.")
    for x in requestList:
        temp_graph = {}
        for y in requestList:
            if (x[2]!=y[2]):
                distance = distanceCalculator(x[0],x[1],y[0],y[1])
                if (distance<maxDistance/1.5):
                    temp_graph[y[2]]=distance
                    print("added connection from {} to {} with distance {}km".format(x[2],y[2],distance))

        if (len(temp_graph)!=0):
            graph[x[2]]=temp_graph
    return graph

def requestGenerator(startLatitude, startLongitude, endLatitude, endLongitude, suppportedChargers):
    start = "start"
    end = "end"

    requestList = []
    requestList.append([startLatitude, startLongitude, start])
    requestList.append([endLatitude, endLongitude, end])
    distance = distanceCalculator(startLatitude,startLongitude,endLatitude,endLongitude)

    #generate datapoints for start index
    print("Fetching JSON from open charge map")
    data = jsonFetch(distance, startLatitude, startLongitude)
    print("parsing JSON into list")
    x, requestList = jsonParsingIntoRequestList(requestList,data,1, suppportedChargers)

    #generate datapoints for end index
    print("Fetching JSON from open charge map")
    data = jsonFetch(distance, endLatitude, endLongitude)
    print("parsing JSON into list")
    x, requestList = jsonParsingIntoRequestList(requestList,data,x, suppportedChargers)

    return requestList

def main(startLatitude, startLongitude, endLatitude, endLongitude, maxDistance, supportedChargers):
    print("Main Start")
    requestList = requestGenerator(startLatitude,startLongitude,endLatitude,endLongitude, supportedChargers)
    print("generating graph")
    graph = graphGenerator(requestList,maxDistance)
    #shortestPath = dijkstra.shortestPath(graph,"start","end")

    #actualDistances = googleNodes.googleMapsActualPath(requestList, shortestPath)
    correctPath = False
    #print(shortestPath)
    count = 0
    print("searching for best path")
    while correctPath == False:
        correctPath=True
        currentDistanceCounter = 0

        print("calculating path")
        shortestPath = dijkstra.shortestPath(graph,"start","end")
        #print("shortestPath {}".format(shortestPath))
        if shortestPath == None or None in shortestPath or count>50:
            print("too many trimming iterations")
            print(shortestPath)
            return None
            break
        else:
            actualDistances=googleNodes.googleMapsActualPath(requestList,shortestPath)
            print("actual distances of the way points in KM are {}".format(actualDistances))

        print("verifying path")
        while currentDistanceCounter<len(actualDistances):
            if actualDistances[currentDistanceCounter]/1000>maxDistance:
                correctPath = False
                graph[str(shortestPath[currentDistanceCounter])].pop(str(shortestPath[currentDistanceCounter+1]),None)
            else:
                graph[str(shortestPath[currentDistanceCounter])][str(shortestPath[currentDistanceCounter+1])]=actualDistances[currentDistanceCounter]/1000
            currentDistanceCounter += 1
#        if(correctPath==True):
#            break
        #print(graph)
        print("actual distances {}".format(actualDistances))
        count+=1

    if shortestPath==None:
        print("No path found")
        return None
    print("Packaging results")

    waypointsData = []
    waypointsData.append([requestList[0][0],requestList[0][1],None,None])
    for point in shortestPath:
        if point != "start" and point != "end":
            waypointsData.append([requestList[int(point)+1][0],requestList[int(point)+1][1],requestList[int(point)+1][3],requestList[int(point)+1][4]])
            #print(requestList[int(point)+1])
    waypointsData.append([requestList[1][0],requestList[1][1],None,None])
    #print(waypoints)
    waypoints = {}
    waypoints['Waypoints']=waypointsData
    return waypoints

#current Inputs
#startLatitude = 39.5
#startLongitude = -121.9
#endLatitude = 40
#endLongitude = -121
#maxDistance = 100
#supportedChargers = []
#waypoints = main(startLatitude,startLongitude,endLatitude,endLongitude,maxDistance,supportedChargers)

#print(waypoints)
#https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=longtitude,latitude&destinations=40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626&key=YOUR_API_KEY<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
#  This file is part of django-oembed-works.
#
#  django-oembed-works is a Django app that provides support for the oEmbed format.
#
#  Development Web Site:
#    - http://www.codetrax.org/projects/django-oembed-works
#  Public Source Code Repository:
#    - https://source.codetrax.org/hgroot/django-oembed-works
#
#  Copyright 2010 George Notaras <gnot [at] g-loaded.eu>
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

from django.contrib import admin
from django.db.models.loading import cache

<commit_msg>Removed StoredOEmbedResponseAdmin<commit_after># -*- coding: utf-8 -*-
#
#  This file is part of django-oembed-works.
#
#  django-oembed-works is a Django app that provides support for the oEmbed format.
#
#  Development Web Site:
#    - http://www.codetrax.org/projects/django-oembed-works
#  Public Source Code Repository:
#    - https://source.codetrax.org/hgroot/django-oembed-works
#
#  Copyright 2010 George Notaras <gnot [at] g-loaded.eu>
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

from django.contrib import admin
from django.db.models.loading import cache

#class StoredOEmbedResponseAdmin(admin.ModelAdmin):
#    pass
#admin.site.register(cache.get_model('oembed_works', 'StoredOEmbedResponse'), StoredOEmbedResponseAdmin)
<|endoftext|>


<commit_before>#----------------------------------------------------------------------
# Copyright (c) 2013 Raytheon BBN Technologies
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
#
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
#----------------------------------------------------------------------

"Tools and utilities for talking to the stitching computation service."

import xmlrpclib

class Result(object):
    CODE = 'code'
    VALUE = 'value'
    GENI_CODE = 'geni_code'

    def __init__(self, xmlrpc_result):
        self.result = xmlrpc_result
    def isSuccess(self):
        return (self.CODE in self.result
                and self.GENI_CODE in self.result[self.CODE]
                and int(self.result[self.CODE][self.GENI_CODE]) == 0)
    def value(self):
        if self.VALUE in self.result:
            return self.result[self.VALUE]
        else:
            raise Exception("No value in result")

class Service(object):
    def __init__(self, url):
        self.url = url

    def ComputePath(self, slice_urn, request_rspec, options):
        """Invoke the XML-RPC service with the request rspec.
        Create an SCSPath from the result.
        """
        server = xmlrpclib.ServerProxy(self.url)
        arg = dict(slice_urn=slice_urn, request_rspec=request_rspec,
                   request_options=options)
        try:
            result = server.ComputePath(arg)
            geni_result = Result(result)
            if geni_result.isSuccess():
                return PathInfo(geni_result.value())
            else:
                raise Exception("ComputePath invocation failed")
        except xmlrpclib.Error as v:
            print "ERROR", v
            raise

class PathInfo(object):
    SERVICE_RSPEC = 'service_rspec'
    WORKFLOW_DATA = 'workflow_data'
    DEPS = 'dependencies'
    def __init__(self, raw_result):
        self.raw = raw_result
        self.links = list()
        wd = raw_result[self.WORKFLOW_DATA]
        for link_name in wd:
            link = Link(link_name)
            link.parse_dependencies(wd[link_name][self.DEPS])
            self.links.append(link)
    def rspec(self):
        return self.raw[self.SERVICE_RSPEC]
    def workflow_data(self):
        return self.links
    def dump_workflow_data(self):
        """Print out the raw workflow data for debugging."""
        wd = self.raw[self.WORKFLOW_DATA]
        for link_name in wd:
            print "Link %r:" % (link_name)
            self.dump_link_data(wd[link_name], "  ")
    def dump_link_data(self, link_data, indent=""):
        print "%sDepends on:" % (indent)
        for d in link_data[self.DEPS]:
            self.dump_dependency(d, indent + "  ")
    def dump_dependency(self, dep_data, indent=""):
        keys = sorted(dep_data.keys())
        deps = []
        if self.DEPS in keys:
            deps = dep_data[self.DEPS]
            keys.remove(self.DEPS)
        for k in keys:
            print "%s%r: %r" % (indent, k, dep_data[k])
        if deps:
            print "%sDepends on:" % (indent)
            for d in deps:
                self.dump_dependency(d, indent + "  ")

class Dependency(object):
    AGG_URN = 'aggregate_urn'
    AGG_URL = 'aggregate_url'
    DEPS = 'dependencies'
    IMPORT_VLANS = 'import_vlans'
    HOP_URN = 'hop_urn'
    def __init__(self, data):
        for k in (self.AGG_URN, self.AGG_URL, self.IMPORT_VLANS, self.HOP_URN):
            object.__setattr__(self, k, data[k])
        self.dependencies = list()
        if self.DEPS in data:
            for d in data[self.DEPS]:
                self.dependencies.append(Dependency(d))

class Link(object):
    def __init__(self, name):
        self.name = name
        self.dependencies = list()
    def parse_dependencies(self, data):
        for d in data:
            self.dependencies.append(Dependency(d))
<commit_msg>Return raw workflow instead of parsed.<commit_after>#----------------------------------------------------------------------
# Copyright (c) 2013 Raytheon BBN Technologies
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
#
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
#----------------------------------------------------------------------

"Tools and utilities for talking to the stitching computation service."

import xmlrpclib

class Result(object):
    CODE = 'code'
    VALUE = 'value'
    GENI_CODE = 'geni_code'

    def __init__(self, xmlrpc_result):
        self.result = xmlrpc_result
    def isSuccess(self):
        return (self.CODE in self.result
                and self.GENI_CODE in self.result[self.CODE]
                and int(self.result[self.CODE][self.GENI_CODE]) == 0)
    def value(self):
        if self.VALUE in self.result:
            return self.result[self.VALUE]
        else:
            raise Exception("No value in result")

class Service(object):
    def __init__(self, url):
        self.url = url

    def ComputePath(self, slice_urn, request_rspec, options):
        """Invoke the XML-RPC service with the request rspec.
        Create an SCSPath from the result.
        """
        server = xmlrpclib.ServerProxy(self.url)
        arg = dict(slice_urn=slice_urn, request_rspec=request_rspec,
                   request_options=options)
        try:
            result = server.ComputePath(arg)
            geni_result = Result(result)
            if geni_result.isSuccess():
                return PathInfo(geni_result.value())
            else:
                raise Exception("ComputePath invocation failed")
        except xmlrpclib.Error as v:
            print "ERROR", v
            raise

class PathInfo(object):
    SERVICE_RSPEC = 'service_rspec'
    WORKFLOW_DATA = 'workflow_data'
    DEPS = 'dependencies'
    def __init__(self, raw_result):
        self.raw = raw_result
        self.links = list()
        wd = raw_result[self.WORKFLOW_DATA]
        for link_name in wd:
            link = Link(link_name)
            link.parse_dependencies(wd[link_name][self.DEPS])
            self.links.append(link)
    def rspec(self):
        return self.raw[self.SERVICE_RSPEC]
    def workflow_data(self):
        return self.raw[self.WORKFLOW_DATA]
    def dump_workflow_data(self):
        """Print out the raw workflow data for debugging."""
        wd = self.raw[self.WORKFLOW_DATA]
        for link_name in wd:
            print "Link %r:" % (link_name)
            self.dump_link_data(wd[link_name], "  ")
    def dump_link_data(self, link_data, indent=""):
        print "%sDepends on:" % (indent)
        for d in link_data[self.DEPS]:
            self.dump_dependency(d, indent + "  ")
    def dump_dependency(self, dep_data, indent=""):
        keys = sorted(dep_data.keys())
        deps = []
        if self.DEPS in keys:
            deps = dep_data[self.DEPS]
            keys.remove(self.DEPS)
        for k in keys:
            print "%s%r: %r" % (indent, k, dep_data[k])
        if deps:
            print "%sDepends on:" % (indent)
            for d in deps:
                self.dump_dependency(d, indent + "  ")

class Dependency(object):
    AGG_URN = 'aggregate_urn'
    AGG_URL = 'aggregate_url'
    DEPS = 'dependencies'
    IMPORT_VLANS = 'import_vlans'
    HOP_URN = 'hop_urn'
    def __init__(self, data):
        for k in (self.AGG_URN, self.AGG_URL, self.IMPORT_VLANS, self.HOP_URN):
            object.__setattr__(self, k, data[k])
        self.dependencies = list()
        if self.DEPS in data:
            for d in data[self.DEPS]:
                self.dependencies.append(Dependency(d))

class Link(object):
    def __init__(self, name):
        self.name = name
        self.dependencies = list()
    def parse_dependencies(self, data):
        for d in data:
            self.dependencies.append(Dependency(d))
<|endoftext|>


<commit_before>from __future__ import absolute_import, division

import uuid

from datetime import datetime
from hashlib import sha1
from sqlalchemy import Table, Column, DateTime, ForeignKey, String, Text, Integer
from sqlalchemy.event import listen
from sqlalchemy.orm import relationship
from sqlalchemy.schema import UniqueConstraint, Index

from changes.config import db
from changes.constants import Result
from changes.db.types.enum import Enum
from changes.db.types.guid import GUID
from changes.db.types.json import JSONEncodedDict

test_group_m2m_table = Table(
    'testgroup_test',
    db.Model.metadata,
    Column('group_id', GUID, ForeignKey('testgroup.id'), nullable=False, primary_key=True),
    Column('test_id', GUID, ForeignKey('test.id'), nullable=False, primary_key=True)
)

class TestSuite(db.Model):
    """
    A test suite is usually representive of the tooling running the tests.

    Tests are unique per test suite.
    """
    __tablename__ = 'testsuite'
    __table_args__ = (
        UniqueConstraint('build_id', 'name_sha', name='_suite_key'),
        Index('idx_testsuite_project_id', 'project_id'),
    )

    id = Column(GUID, nullable=False, primary_key=True, default=uuid.uuid4)
    build_id = Column(GUID, ForeignKey('build.id'), nullable=False)
    project_id = Column(GUID, ForeignKey('project.id'), nullable=False)
    name_sha = Column(String(40), nullable=False, default=sha1('default').hexdigest())
    name = Column(Text, nullable=True, default='default')
    date_created = Column(DateTime, default=datetime.utcnow)

    build = relationship('Build')
    project = relationship('Project')

    def __init__(self, **kwargs):
        super(TestSuite, self).__init__(**kwargs)
        if self.id is None:
            self.id = uuid.uuid4()
        if self.date_created is None:
            self.date_created = datetime.utcnow()
        if self.name is None:
            self.name = 'default'

    def calculate_name_sha(self):
        return sha1(self.name or 'default').hexdigest()

class TestGroup(db.Model):
    """
    A TestGroup represents an aggregate tree of all leaves under it.

    e.g. if you have the test leaf "foo.bar.TestCase.test_foo" it might create
    a tree for "foo.bar.TestCase", "foo.bar" and "foo".
    """
    __tablename__ = 'testgroup'
    __table_args__ = (
        UniqueConstraint('build_id', 'suite_id', 'name_sha', name='_group_key'),
        Index('idx_testgroup_project_id', 'project_id'),
        Index('idx_testgroup_suite_id', 'suite_id'),
        Index('idx_testgroup_parent_id', 'parent_id'),
    )

    id = Column(GUID, nullable=False, primary_key=True, default=uuid.uuid4)
    build_id = Column(GUID, ForeignKey('build.id'), nullable=False)
    project_id = Column(GUID, ForeignKey('project.id'), nullable=False)
    suite_id = Column(GUID, ForeignKey('testsuite.id'))
    parent_id = Column(GUID, ForeignKey('testgroup.id'))
    name_sha = Column(String(40), nullable=False)
    name = Column(Text)
    duration = Column(Integer, default=0)
    num_tests = Column(Integer, default=0)
    num_failed = Column(Integer, default=0)
    data = Column(JSONEncodedDict)
    date_created = Column(DateTime, default=datetime.utcnow)

    build = relationship('Build')
    project = relationship('Project')
    testcases = relationship('TestCase', secondary=test_group_m2m_table, backref="groups")

    def __init__(self, **kwargs):
        super(TestGroup, self).__init__(**kwargs)
        if self.id is None:
            self.id = uuid.uuid4()
        if self.date_created is None:
            self.date_created = datetime.utcnow()
        if self.duration is None:
            self.duration = 0
        if self.num_tests is None:
            self.num_tests = 0
        print self.name, self.name_sha

    def calculate_name_sha(self):
        if not self.name:
            return
        return sha1(self.name).hexdigest()

class TestCase(db.Model):
    """
    An individual test result.
    """
    __tablename__ = 'test'
    __table_args__ = (
        UniqueConstraint('build_id', 'suite_id', 'label_sha', name='unq_test_key'),
        Index('idx_test_project_id', 'project_id'),
        Index('idx_test_suite_id', 'suite_id'),
    )

    id = Column(GUID, nullable=False, primary_key=True, default=uuid.uuid4)
    build_id = Column(GUID, ForeignKey('build.id'), nullable=False)
    project_id = Column(GUID, ForeignKey('project.id'), nullable=False)
    suite_id = Column(GUID, ForeignKey('testsuite.id'))
    name_sha = Column('label_sha', String(40), nullable=False)
    name = Column(Text, nullable=False)
    package = Column(Text, nullable=True)
    result = Column(Enum(Result))
    duration = Column(Integer)
    message = Column(Text)
    date_created = Column(DateTime, default=datetime.utcnow)

    build = relationship('Build')
    project = relationship('Project')

    def __init__(self, **kwargs):
        super(TestCase, self).__init__(**kwargs)
        if self.id is None:
            self.id = uuid.uuid4()
        if self.result is None:
            self.result = Result.unknown
        if self.date_created is None:
            self.date_created = datetime.utcnow()

def test_name_sha_func(attr):
    def set_name_sha(target, value, oldvalue, initiator):
        if attr == 'package':
            package = value
        else:
            package = target.package
        if attr == 'name':
            name = value
        else:
            name = target.name

        if package and name:
            new_sha = sha1('{0}.{1}'.format(package, name)).hexdigest()
        elif name:
            new_sha = sha1(name).hexdigest()

        if new_sha != target.name_sha:
            target.name_sha = new_sha
        return value
    return set_name_sha

def set_name_sha(target, value, oldvalue, initiator):
    if not value:
        return value

    new_sha = sha1(value).hexdigest()
    if new_sha != target.name_sha:
        target.name_sha = new_sha
    return value

listen(TestCase.package, 'set', test_name_sha_func('package'), retval=False)
listen(TestCase.name, 'set', test_name_sha_func('name'), retval=False)
listen(TestSuite.name, 'set', set_name_sha, retval=False)
listen(TestGroup.name, 'set', set_name_sha, retval=False)
<commit_msg>Remove print<commit_after>from __future__ import absolute_import, division

import uuid

from datetime import datetime
from hashlib import sha1
from sqlalchemy import Table, Column, DateTime, ForeignKey, String, Text, Integer
from sqlalchemy.event import listen
from sqlalchemy.orm import relationship
from sqlalchemy.schema import UniqueConstraint, Index

from changes.config import db
from changes.constants import Result
from changes.db.types.enum import Enum
from changes.db.types.guid import GUID
from changes.db.types.json import JSONEncodedDict

test_group_m2m_table = Table(
    'testgroup_test',
    db.Model.metadata,
    Column('group_id', GUID, ForeignKey('testgroup.id'), nullable=False, primary_key=True),
    Column('test_id', GUID, ForeignKey('test.id'), nullable=False, primary_key=True)
)

class TestSuite(db.Model):
    """
    A test suite is usually representive of the tooling running the tests.

    Tests are unique per test suite.
    """
    __tablename__ = 'testsuite'
    __table_args__ = (
        UniqueConstraint('build_id', 'name_sha', name='_suite_key'),
        Index('idx_testsuite_project_id', 'project_id'),
    )

    id = Column(GUID, nullable=False, primary_key=True, default=uuid.uuid4)
    build_id = Column(GUID, ForeignKey('build.id'), nullable=False)
    project_id = Column(GUID, ForeignKey('project.id'), nullable=False)
    name_sha = Column(String(40), nullable=False, default=sha1('default').hexdigest())
    name = Column(Text, nullable=True, default='default')
    date_created = Column(DateTime, default=datetime.utcnow)

    build = relationship('Build')
    project = relationship('Project')

    def __init__(self, **kwargs):
        super(TestSuite, self).__init__(**kwargs)
        if self.id is None:
            self.id = uuid.uuid4()
        if self.date_created is None:
            self.date_created = datetime.utcnow()
        if self.name is None:
            self.name = 'default'

    def calculate_name_sha(self):
        return sha1(self.name or 'default').hexdigest()

class TestGroup(db.Model):
    """
    A TestGroup represents an aggregate tree of all leaves under it.

    e.g. if you have the test leaf "foo.bar.TestCase.test_foo" it might create
    a tree for "foo.bar.TestCase", "foo.bar" and "foo".
    """
    __tablename__ = 'testgroup'
    __table_args__ = (
        UniqueConstraint('build_id', 'suite_id', 'name_sha', name='_group_key'),
        Index('idx_testgroup_project_id', 'project_id'),
        Index('idx_testgroup_suite_id', 'suite_id'),
        Index('idx_testgroup_parent_id', 'parent_id'),
    )

    id = Column(GUID, nullable=False, primary_key=True, default=uuid.uuid4)
    build_id = Column(GUID, ForeignKey('build.id'), nullable=False)
    project_id = Column(GUID, ForeignKey('project.id'), nullable=False)
    suite_id = Column(GUID, ForeignKey('testsuite.id'))
    parent_id = Column(GUID, ForeignKey('testgroup.id'))
    name_sha = Column(String(40), nullable=False)
    name = Column(Text)
    duration = Column(Integer, default=0)
    num_tests = Column(Integer, default=0)
    num_failed = Column(Integer, default=0)
    data = Column(JSONEncodedDict)
    date_created = Column(DateTime, default=datetime.utcnow)

    build = relationship('Build')
    project = relationship('Project')
    testcases = relationship('TestCase', secondary=test_group_m2m_table, backref="groups")

    def __init__(self, **kwargs):
        super(TestGroup, self).__init__(**kwargs)
        if self.id is None:
            self.id = uuid.uuid4()
        if self.date_created is None:
            self.date_created = datetime.utcnow()
        if self.duration is None:
            self.duration = 0
        if self.num_tests is None:
            self.num_tests = 0

    def calculate_name_sha(self):
        if not self.name:
            return
        return sha1(self.name).hexdigest()

class TestCase(db.Model):
    """
    An individual test result.
    """
    __tablename__ = 'test'
    __table_args__ = (
        UniqueConstraint('build_id', 'suite_id', 'label_sha', name='unq_test_key'),
        Index('idx_test_project_id', 'project_id'),
        Index('idx_test_suite_id', 'suite_id'),
    )

    id = Column(GUID, nullable=False, primary_key=True, default=uuid.uuid4)
    build_id = Column(GUID, ForeignKey('build.id'), nullable=False)
    project_id = Column(GUID, ForeignKey('project.id'), nullable=False)
    suite_id = Column(GUID, ForeignKey('testsuite.id'))
    name_sha = Column('label_sha', String(40), nullable=False)
    name = Column(Text, nullable=False)
    package = Column(Text, nullable=True)
    result = Column(Enum(Result))
    duration = Column(Integer)
    message = Column(Text)
    date_created = Column(DateTime, default=datetime.utcnow)

    build = relationship('Build')
    project = relationship('Project')

    def __init__(self, **kwargs):
        super(TestCase, self).__init__(**kwargs)
        if self.id is None:
            self.id = uuid.uuid4()
        if self.result is None:
            self.result = Result.unknown
        if self.date_created is None:
            self.date_created = datetime.utcnow()

def test_name_sha_func(attr):
    def set_name_sha(target, value, oldvalue, initiator):
        if attr == 'package':
            package = value
        else:
            package = target.package
        if attr == 'name':
            name = value
        else:
            name = target.name

        if package and name:
            new_sha = sha1('{0}.{1}'.format(package, name)).hexdigest()
        elif name:
            new_sha = sha1(name).hexdigest()

        if new_sha != target.name_sha:
            target.name_sha = new_sha
        return value
    return set_name_sha

def set_name_sha(target, value, oldvalue, initiator):
    if not value:
        return value

    new_sha = sha1(value).hexdigest()
    if new_sha != target.name_sha:
        target.name_sha = new_sha
    return value

listen(TestCase.package, 'set', test_name_sha_func('package'), retval=False)
listen(TestCase.name, 'set', test_name_sha_func('name'), retval=False)
listen(TestSuite.name, 'set', set_name_sha, retval=False)
listen(TestGroup.name, 'set', set_name_sha, retval=False)
<|endoftext|>


<commit_before>#encoding:utf-8

from utils import weighted_random_subreddit, SupplyResult

subreddit = weighted_random_subreddit({
    'AbandonedPorn': 593,
    'AdPorn': 81,
    'AdrenalinePorn': 146,
    'AerialPorn': 20,
    'AgriculturePorn': 12,
    'AlbumArtPorn': 50,
    'AnimalPorn': 85,
    'ApocalypsePorn': 14,
    'ArchitecturePorn': 205,
    'ArtPorn': 177,
    'ArtefactPorn': 202,
    'AutumnPorn': 18,
    'Beachporn': 9,
    'BonsaiPorn': 9,
    'BotanicalPorn': 38,
    'CabinPorn': 59,
    'CemeteryPorn': 23,
    'CityPorn': 328,
    'ClimbingPorn': 22,
    'ComicBookPorn': 7,
    'ConcertPorn': 2,
    'CulinaryPorn': 18,
    'DesignPorn': 355,
    'DessertPorn': 46,
    'DestructionPorn': 58,
    'EarthPorn': 15150,
    'EarthlingPorn': 5,
    'ExposurePorn': 194,
    'F1Porn': 24,
    'FirePorn': 31,
    'FoodPorn': 868,
    'FossilPorn': 11,
    'FractalPorn': 24,
    'GamerPorn': 11,
    'GeekPorn': 34,
    'GunPorn': 81,
    'HellscapePorn': 9,
    'HistoryPorn': 894,
    'Houseporn': 54,
    'HumanPorn': 176,
    'InfraredPorn': 6,
    'InfrastructurePorn': 73,
    'InstrumentPorn': 17,
    'Knifeporn': 11,
    'MachinePorn': 189,
    'MacroPorn': 89,
    'MapPorn': 451,
    'MegalithPorn': 12,
    'MetalPorn': 11,
    'MicroPorn': 34,
    'MilitaryPorn': 173,
    'MotorcyclePorn': 17,
    'MoviePosterPorn': 96,
    'MushroomPorn': 8,
    'NewsPorn': 28,
    'OrganizationPorn': 22,
    'QuotesPorn': 371,
    'RetroGamePorn': 4,
    'RidesPorn': 5,
    'RoomPorn': 721,
    'SkyPorn': 65,
    'SportsPorn': 5,
    'SpringPorn': 5,
    'StarshipPorn': 21,
    'StreetArtPorn': 28,
    'SummerPorn': 5,
    'TeaPorn': 12,
    'TechnologyPorn': 22,
    'TelevisionPosterPorn': 6,
    'ThingsCutInHalfPorn': 234,
    'VideoPorn': 11,
    'ViewPorn': 10,
    'VillagePorn': 60,
    'WeatherPorn': 16,
    'avporn': 12,
    'boatporn': 15,
    'bookporn': 48,
    'bridgeporn': 7,
    'carporn': 362,
    'churchporn': 8,
    'desertporn': 6,
    'fashionporn': 14,
    'futureporn': 106,
    'geologyporn': 26,
    'lakeporn': 5,
    'lavaporn': 8,
    'mtgporn': 13,
    'policeporn': 15,
    'powerwashingporn': 301,
    'retailporn': 4,
    'ruralporn': 24,
    'seaporn': 20,
    'spaceflightporn': 10,
    'spaceporn': 543,
    'stadiumporn': 5,
    'steamporn': 4,
    'toolporn': 16,
    'uniformporn': 8,
    'waterporn': 73,
    'winterporn': 47
})
t_channel = '@r_porn'

def send_post(submission, r2t):
    limit_on_upvotes = {
        'AbandonedPorn': 1000,
        'AdPorn': 500,
        'AdrenalinePorn': 500,
        'AerialPorn': 50,
        'AgriculturePorn': 50,
        'AlbumArtPorn': 50,
        'AnimalPorn': 150,
        'ApocalypsePorn': 50,
        'ArchitecturePorn': 200,
        'ArtPorn': 150,
        'ArtefactPorn': 555,
        'AutumnPorn': 100,
        'Beachporn': 50,
        'BonsaiPorn': 100,
        'BotanicalPorn': 50,
        'CabinPorn': 555,
        'CemeteryPorn': 100,
        'CityPorn': 500,
        'ClimbingPorn': 100,
        'ComicBookPorn': 50,
        'ConcertPorn': 50,
        'CulinaryPorn': 50,
        'DesignPorn': 1000,
        'DessertPorn': 100,
        'DestructionPorn': 200,
        'EarthPorn': 5000,
        'EarthlingPorn': 50,
        'ExposurePorn': 200,
        'F1Porn': 100,
        'FirePorn': 100,
        'FoodPorn': 1234,
        'FossilPorn': 50,
        'FractalPorn': 50,
        'GamerPorn': 50,
        'GeekPorn': 100,
        'GunPorn': 300,
        'HellscapePorn': 100,
        'HistoryPorn': 2000,
        'Houseporn': 200,
        'HumanPorn': 314,
        'InfraredPorn': 50,
        'InfrastructurePorn': 256,
        'InstrumentPorn': 50,
        'Knifeporn': 50,
        'MachinePorn': 500,
        'MacroPorn': 128,
        'MapPorn': 300,
        'MegalithPorn': 100,
        'MetalPorn': 50,
        'MicroPorn': 200,
        'MilitaryPorn': 200,
        'MotorcyclePorn': 100,
        'MoviePosterPorn': 150,
        'MushroomPorn': 50,
        'NewsPorn': 100,
        'OrganizationPorn': 200,
        'QuotesPorn': 500,
        'RetroGamePorn': 50,
        'RidesPorn': 50,
        'RoomPorn': 1600,
        'SkyPorn': 100,
        'SportsPorn': 50,
        'SpringPorn': 50,
        'StarshipPorn': 150,
        'StreetArtPorn': 100,
        'SummerPorn': 50,
        'TeaPorn': 50,
        'TechnologyPorn': 100,
        'TelevisionPosterPorn': 50,
        'ThingsCutInHalfPorn': 666,
        'VideoPorn': 50,
        'ViewPorn': 50,
        'VillagePorn': 200,
        'WeatherPorn': 100,
        'avporn': 50,
        'boatporn': 50,
        'bookporn': 100,
        'bridgeporn': 50,
        'carporn': 666,
        'churchporn': 50,
        'desertporn': 50,
        'fashionporn': 100,
        'futureporn': 555,
        'geologyporn': 100,
        'lakeporn': 50,
        'lavaporn': 66,
        'mtgporn': 100,
        'policeporn': 150,
        'powerwashingporn': 2000,
        'retailporn': 50,
        'ruralporn': 50,
        'seaporn': 100,
        'spaceflightporn': 100,
        'spaceporn': 1000,
        'stadiumporn': 50,
        'steamporn': 50,
        'toolporn': 100,
        'uniformporn': 100,
        'waterporn': 127,
        'winterporn': 150
    }

    return r2t.send_simple(submission,
        check_dups=True,
        min_upvotes_limit=limit_on_upvotes[subreddit],
        text='{title}\n\n{self_text}\n\n{upvotes} upvotes\n/r/{subreddit_name}\n{short_link}\n{channel}',
        other='{title}\n{link}\n\n{upvotes} upvotes\n/r/{subreddit_name}\n{short_link}\n{channel}',
        album='{title}\n{link}\n\n{upvotes} upvotes\n/r/{subreddit_name}\n{short_link}\n{channel}',
        gif='{title}\n\n{upvotes} upvotes\n/r/{subreddit_name}\n{short_link}\n{channel}',
        img='{title}\n\n{upvotes} upvotes\n/r/{subreddit_name}\n{short_link}\n{channel}'
    )
<commit_msg>@r_porn weights changed.<commit_after>#encoding:utf-8

from utils import weighted_random_subreddit

subreddit = weighted_random_subreddit({
    'AbandonedPorn': 593,
    'AdPorn': 81,
    'AdrenalinePorn': 146,
    'AerialPorn': 20,
    'AgriculturePorn': 12,
    'AlbumArtPorn': 50,
    'AnimalPorn': 85,
    'ApocalypsePorn': 14,
    'ArchitecturePorn': 205,
    'ArtPorn': 177,
    'ArtefactPorn': 202,
    'AutumnPorn': 18,
    'Beachporn': 9,
    'BonsaiPorn': 9,
    'BotanicalPorn': 38,
    'CabinPorn': 59,
    'CemeteryPorn': 23,
    'CityPorn': 328,
    'ClimbingPorn': 22,
    'ComicBookPorn': 7,
    'ConcertPorn': 2,
    'CulinaryPorn': 18,
    'DesignPorn': 355,
    'DessertPorn': 46,
    'DestructionPorn': 58,
    'EarthPorn': 10000,
    'EarthlingPorn': 5,
    'ExposurePorn': 194,
    'F1Porn': 24,
    'FirePorn': 31,
    'FoodPorn': 868,
    'FossilPorn': 11,
    'FractalPorn': 24,
    'GamerPorn': 11,
    'GeekPorn': 34,
    'GunPorn': 81,
    'HellscapePorn': 9,
    'HistoryPorn': 894,
    'Houseporn': 54,
    'HumanPorn': 176,
    'InfraredPorn': 6,
    'InfrastructurePorn': 73,
    'InstrumentPorn': 17,
    'Knifeporn': 11,
    'MachinePorn': 189,
    'MacroPorn': 89,
    'MapPorn': 451,
    'MegalithPorn': 12,
    'MetalPorn': 11,
    'MicroPorn': 34,
    'MilitaryPorn': 173,
    'MotorcyclePorn': 17,
    'MoviePosterPorn': 96,
    'MushroomPorn': 8,
    'NewsPorn': 28,
    'OrganizationPorn': 22,
    'QuotesPorn': 200,
    'RetroGamePorn': 4,
    'RidesPorn': 5,
    'RoomPorn': 721,
    'SkyPorn': 65,
    'SportsPorn': 5,
    'SpringPorn': 5,
    'StarshipPorn': 21,
    'StreetArtPorn': 28,
    'SummerPorn': 5,
    'TeaPorn': 12,
    'TechnologyPorn': 22,
    'TelevisionPosterPorn': 6,
    'ThingsCutInHalfPorn': 234,
    'VideoPorn': 11,
    'ViewPorn': 10,
    'VillagePorn': 60,
    'WeatherPorn': 16,
    'avporn': 12,
    'boatporn': 15,
    'bookporn': 48,
    'bridgeporn': 7,
    'carporn': 362,
    'churchporn': 8,
    'desertporn': 6,
    'fashionporn': 14,
    'futureporn': 106,
    'geologyporn': 26,
    'lakeporn': 5,
    'lavaporn': 8,
    'mtgporn': 13,
    'policeporn': 15,
    'powerwashingporn': 301,
    'retailporn': 4,
    'ruralporn': 24,
    'seaporn': 20,
    'spaceflightporn': 10,
    'spaceporn': 543,
    'stadiumporn': 5,
    'steamporn': 4,
    'toolporn': 16,
    'uniformporn': 8,
    'waterporn': 73,
    'winterporn': 47
})
t_channel = '@r_porn'

def send_post(submission, r2t):
    limit_on_upvotes = {
        'AbandonedPorn': 1000,
        'AdPorn': 500,
        'AdrenalinePorn': 500,
        'AerialPorn': 50,
        'AgriculturePorn': 50,
        'AlbumArtPorn': 50,
        'AnimalPorn': 150,
        'ApocalypsePorn': 50,
        'ArchitecturePorn': 200,
        'ArtPorn': 150,
        'ArtefactPorn': 555,
        'AutumnPorn': 100,
        'Beachporn': 50,
        'BonsaiPorn': 100,
        'BotanicalPorn': 50,
        'CabinPorn': 555,
        'CemeteryPorn': 100,
        'CityPorn': 500,
        'ClimbingPorn': 100,
        'ComicBookPorn': 50,
        'ConcertPorn': 50,
        'CulinaryPorn': 50,
        'DesignPorn': 1000,
        'DessertPorn': 100,
        'DestructionPorn': 200,
        'EarthPorn': 5000,
        'EarthlingPorn': 50,
        'ExposurePorn': 200,
        'F1Porn': 100,
        'FirePorn': 100,
        'FoodPorn': 1234,
        'FossilPorn': 50,
        'FractalPorn': 50,
        'GamerPorn': 50,
        'GeekPorn': 100,
        'GunPorn': 300,
        'HellscapePorn': 100,
        'HistoryPorn': 2000,
        'Houseporn': 200,
        'HumanPorn': 314,
        'InfraredPorn': 50,
        'InfrastructurePorn': 256,
        'InstrumentPorn': 50,
        'Knifeporn': 50,
        'MachinePorn': 500,
        'MacroPorn': 128,
        'MapPorn': 300,
        'MegalithPorn': 100,
        'MetalPorn': 50,
        'MicroPorn': 200,
        'MilitaryPorn': 200,
        'MotorcyclePorn': 100,
        'MoviePosterPorn': 150,
        'MushroomPorn': 50,
        'NewsPorn': 100,
        'OrganizationPorn': 200,
        'QuotesPorn': 1000,
        'RetroGamePorn': 50,
        'RidesPorn': 50,
        'RoomPorn': 1600,
        'SkyPorn': 100,
        'SportsPorn': 50,
        'SpringPorn': 50,
        'StarshipPorn': 150,
        'StreetArtPorn': 100,
        'SummerPorn': 50,
        'TeaPorn': 50,
        'TechnologyPorn': 100,
        'TelevisionPosterPorn': 50,
        'ThingsCutInHalfPorn': 666,
        'VideoPorn': 50,
        'ViewPorn': 50,
        'VillagePorn': 200,
        'WeatherPorn': 100,
        'avporn': 50,
        'boatporn': 50,
        'bookporn': 100,
        'bridgeporn': 50,
        'carporn': 666,
        'churchporn': 50,
        'desertporn': 50,
        'fashionporn': 100,
        'futureporn': 555,
        'geologyporn': 100,
        'lakeporn': 50,
        'lavaporn': 66,
        'mtgporn': 100,
        'policeporn': 150,
        'powerwashingporn': 2000,
        'retailporn': 50,
        'ruralporn': 50,
        'seaporn': 100,
        'spaceflightporn': 100,
        'spaceporn': 1000,
        'stadiumporn': 50,
        'steamporn': 50,
        'toolporn': 100,
        'uniformporn': 100,
        'waterporn': 127,
        'winterporn': 150
    }

    return r2t.send_simple(submission,
        check_dups=True,
        min_upvotes_limit=limit_on_upvotes[subreddit],
        text='{title}\n\n{self_text}\n\n{upvotes} upvotes\n/r/{subreddit_name}\n{short_link}\n{channel}',
        other='{title}\n{link}\n\n{upvotes} upvotes\n/r/{subreddit_name}\n{short_link}\n{channel}',
        album='{title}\n{link}\n\n{upvotes} upvotes\n/r/{subreddit_name}\n{short_link}\n{channel}',
        gif='{title}\n\n{upvotes} upvotes\n/r/{subreddit_name}\n{short_link}\n{channel}',
        img='{title}\n\n{upvotes} upvotes\n/r/{subreddit_name}\n{short_link}\n{channel}'
    )
<|endoftext|>


<commit_before>#!/usr/bin/env python3
#
# Copyright (C) 2018 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# 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/>.
#
# File: snmp_ifmib.py
# Purpose:
#    Show SNMP MIB information
#    Used by the "run show snmp mib" commands.

import sys
import argparse
import netifaces
import subprocess

from vyos.config import Config

parser = argparse.ArgumentParser(description='Retrieve SNMP interfaces information')
parser.add_argument('--ifindex', action='store', nargs='?', const='all', help='Show interface index')
parser.add_argument('--ifalias', action='store', nargs='?', const='all', help='Show interface aliase')
parser.add_argument('--ifdescr', action='store', nargs='?', const='all', help='Show interface description')

def show_ifindex(i):
    proc = subprocess.Popen(['/bin/ip', 'link', 'show', i], stdout=subprocess.PIPE)
    (out, err) = proc.communicate()
    # convert output to string
    string = out.decode("utf-8")

    index = 'ifIndex = ' + string.split(':')[0]
    return index.replace('\n', '')

def show_ifalias(i):
    proc = subprocess.Popen(['/bin/ip', 'link', 'show', i], stdout=subprocess.PIPE)
    (out, err) = proc.communicate()
    # convert output to string
    string = out.decode("utf-8")

    if 'alias' in string:
        alias = 'ifAlias = ' + string.split('alias')[1].lstrip()
    else:
        alias = 'ifAlias = ' + i

    return alias.replace('\n', '')

def show_ifdescr(i):
    ven_id = ''
    dev_id = ''

    try:
        with open(r'/sys/class/net/' + i + '/device/vendor', 'r') as f:
            ven_id = f.read().replace('\n', '')
    except FileNotFoundError:
        pass

    try:
        with open(r'/sys/class/net/' + i + '/device/device', 'r') as f:
            dev_id = f.read().replace('\n', '')
    except FileNotFoundError:
         pass

    if ven_id == '' and dev_id == '':
        ret = 'ifDescr = {0}'.format(i)
        return ret

    device = str(ven_id) + ':' + str(dev_id)
    proc = subprocess.Popen(['/usr/bin/lspci', '-mm', '-d', device], stdout=subprocess.PIPE)
    (out, err) = proc.communicate()

    # convert output to string
    string = out.decode("utf-8").split('"')
    vendor = string[3]
    device = string[5]

    ret = 'ifDescr = {0} {1}'.format(vendor, device)
    return ret.replace('\n', '')

if __name__ == '__main__':
    args = parser.parse_args()

    # Do nothing if service is not configured
    c = Config()
    if not c.exists_effective('service snmp'):
        print("SNMP service is not configured")
        sys.exit(0)

    if args.ifindex:
        if args.ifindex == 'all':
            for i in netifaces.interfaces():
                print('{0}: {1}'.format(i, show_ifindex(i)))
        else:
            print('{0}: {1}'.format(args.ifindex, show_ifindex(args.ifindex)))

    elif args.ifalias:
        if args.ifalias == 'all':
            for i in netifaces.interfaces():
                print('{0}: {1}'.format(i, show_ifalias(i)))
        else:
            print('{0}: {1}'.format(args.ifalias, show_ifalias(args.ifalias)))

    elif args.ifdescr:
        if args.ifdescr == 'all':
            for i in netifaces.interfaces():
                print('{0}: {1}'.format(i, show_ifdescr(i)))
        else:
                print('{0}: {1}'.format(args.ifdescr, show_ifdescr(args.ifdescr)))

    else:
        #eth0: ifIndex = 2
        #      ifAlias = NET-MYBLL-MUCI-BACKBONE
        #      ifDescr = VMware VMXNET3 Ethernet Controller
        #lo: ifIndex = 1
        for i in netifaces.interfaces():
            print('{0}:\t{1}'.format(i, show_ifindex(i)))
            print('\t{0}'.format(show_ifalias(i)))
            print('\t{0}'.format(show_ifdescr(i)))

    sys.exit(1)
<commit_msg>[snmp] T1575 Adding additional check for lspci<commit_after>#!/usr/bin/env python3
#
# Copyright (C) 2018 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# 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/>.
#
# File: snmp_ifmib.py
# Purpose:
#    Show SNMP MIB information
#    Used by the "run show snmp mib" commands.

import sys
import argparse
import netifaces
import subprocess

from vyos.config import Config

parser = argparse.ArgumentParser(description='Retrieve SNMP interfaces information')
parser.add_argument('--ifindex', action='store', nargs='?', const='all', help='Show interface index')
parser.add_argument('--ifalias', action='store', nargs='?', const='all', help='Show interface aliase')
parser.add_argument('--ifdescr', action='store', nargs='?', const='all', help='Show interface description')

def show_ifindex(i):
    proc = subprocess.Popen(['/bin/ip', 'link', 'show', i], stdout=subprocess.PIPE)
    (out, err) = proc.communicate()
    # convert output to string
    string = out.decode("utf-8")

    index = 'ifIndex = ' + string.split(':')[0]
    return index.replace('\n', '')

def show_ifalias(i):
    proc = subprocess.Popen(['/bin/ip', 'link', 'show', i], stdout=subprocess.PIPE)
    (out, err) = proc.communicate()
    # convert output to string
    string = out.decode("utf-8")

    if 'alias' in string:
        alias = 'ifAlias = ' + string.split('alias')[1].lstrip()
    else:
        alias = 'ifAlias = ' + i

    return alias.replace('\n', '')

def show_ifdescr(i):
    ven_id = ''
    dev_id = ''

    try:
        with open(r'/sys/class/net/' + i + '/device/vendor', 'r') as f:
            ven_id = f.read().replace('\n', '')
    except FileNotFoundError:
        pass

    try:
        with open(r'/sys/class/net/' + i + '/device/device', 'r') as f:
            dev_id = f.read().replace('\n', '')
    except FileNotFoundError:
         pass

    if ven_id == '' and dev_id == '':
        ret = 'ifDescr = {0}'.format(i)
        return ret

    device = str(ven_id) + ':' + str(dev_id)
    proc = subprocess.Popen(['/usr/bin/lspci', '-mm', '-d', device], stdout=subprocess.PIPE)
    (out, err) = proc.communicate()

    vendor = ""
    device = ""

    # convert output to string
    string = out.decode("utf-8").split('"')
    if len(string) >= 3:
      vendor = string[3]

    if len(string) >= 5:
      device = string[5]

    ret = 'ifDescr = {0} {1}'.format(vendor, device)
    return ret.replace('\n', '')

if __name__ == '__main__':
    args = parser.parse_args()

    # Do nothing if service is not configured
    c = Config()
    if not c.exists_effective('service snmp'):
        print("SNMP service is not configured")
        sys.exit(0)

    if args.ifindex:
        if args.ifindex == 'all':
            for i in netifaces.interfaces():
                print('{0}: {1}'.format(i, show_ifindex(i)))
        else:
            print('{0}: {1}'.format(args.ifindex, show_ifindex(args.ifindex)))

    elif args.ifalias:
        if args.ifalias == 'all':
            for i in netifaces.interfaces():
                print('{0}: {1}'.format(i, show_ifalias(i)))
        else:
            print('{0}: {1}'.format(args.ifalias, show_ifalias(args.ifalias)))

    elif args.ifdescr:
        if args.ifdescr == 'all':
            for i in netifaces.interfaces():
                print('{0}: {1}'.format(i, show_ifdescr(i)))
        else:
                print('{0}: {1}'.format(args.ifdescr, show_ifdescr(args.ifdescr)))

    else:
        #eth0: ifIndex = 2
        #      ifAlias = NET-MYBLL-MUCI-BACKBONE
        #      ifDescr = VMware VMXNET3 Ethernet Controller
        #lo: ifIndex = 1
        for i in netifaces.interfaces():
            print('{0}:\t{1}'.format(i, show_ifindex(i)))
            print('\t{0}'.format(show_ifalias(i)))
            print('\t{0}'.format(show_ifdescr(i)))

    sys.exit(1)
<|endoftext|>