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

from __future__ import print_function

import curses
import argparse
import logging
import sys

from data import *
from config import *
from view import *

def get_view(window, datum, config, file_num, total_files, position):
    cursor = position[:]
    link = position[:] if config.annotation_type == AnnType.link else [-1, -1]
    return View(window, cursor, link, datum, config, file_num, total_files)

def annotate(window, config, filenames):
    out_filename = "files_still_to_do"
    overwrite = False
    for i in range(len(sys.argv)):
        if sys.argv[i] == '-log' and len(sys.argv) > i + 1:
            out_filename = sys.argv[i + 1]
        if sys.argv[i] == '-overwrite':
            overwrite = True
    out = open(out_filename, "w")

    # Set color combinations
    for num, fore, back in COLORS:
        curses.init_pair(num, fore, back)

    # No blinking cursor
    curses.curs_set(0)

    cfilename = 0
    filename, start_pos = filenames[cfilename]
    datum = Datum(filename, config)
    view = get_view(window, datum, config, cfilename, len(filenames), start_pos)

    at_end = None
    while True:
        if at_end is None:
            # Draw screen
            view.render()
            view.must_show_linking_pos = False
            # Get input
            user_input = window.getch()

            # Note - First two are SHIFT + DOWN and SHIFT + UP, determined by
            # hand on two laptops.
            if user_input in [ord('c'), 337]:
                if config.annotation_type == AnnType.link: view.move_up(True)
                else: view.move_to_top()
            elif user_input in [ord('v'), 336]:
                if config.annotation_type == AnnType.link: view.move_down(True)
                else: view.move_to_bottom()
            elif user_input == curses.KEY_SLEFT:
                if config.annotation_type == AnnType.link: view.move_left(True)
                else: view.move_to_start()
            elif user_input == curses.KEY_SRIGHT:
                if config.annotation_type == AnnType.link: view.move_right(True)
                else: view.move_to_end()
            elif user_input == curses.KEY_UP: view.move_up()
            elif user_input == curses.KEY_DOWN: view.move_down()
            elif user_input == curses.KEY_LEFT: view.move_left()
            elif user_input == curses.KEY_RIGHT: view.move_right()
            elif user_input == ord("h"): view.toggle_help()
            elif user_input == ord("n"):
                if config.annotation_type != AnnType.link:
                    view.next_number()
            elif user_input == ord("p"):
                if config.annotation_type != AnnType.link:
                    view.next_number()
            elif user_input == ord("d"):
                datum.modify_annotation(view.cursor, view.linking_pos)
                if config.annotation_type == AnnType.link:
                    if config.annotation == AnnScope.line:
                        view.move_down(True)
                        view.cursor[0] = view.linking_pos[0]
                        view.cursor[1] = view.linking_pos[1]
                        view.move_up()
                    else:
                        view.move_right(True)
                        view.cursor[0] = view.linking_pos[0]
                        view.cursor[1] = view.linking_pos[1]
                        view.move_left()
                    view.must_show_linking_pos = True
            elif user_input == ord("D"):
                datum.modify_annotation(view.cursor, view.linking_pos)
            elif user_input == ord("u"):
                datum.remove_annotation(view.cursor, view.linking_pos)
            elif user_input in [ord('s'), ord('b'), ord('r')]:
                if config.annotation_type != AnnType.link:
                    datum.modify_annotation(view.cursor, view.linking_pos,
                            chr(user_input))
            elif user_input == ord("/") or user_input == ord("\\"):
                # If we can get another file, do
                datum.write_out()
                filenames[cfilename] = (filename, view.cursor)
                direction = 1 if user_input == ord("/") else -1
                if 0 <= cfilename + direction < len(filenames):
                    cfilename += direction
                    filename, start_pos = filenames[cfilename]
                    datum = Datum(filename, config)
                    view = get_view(window, datum, config, cfilename,
                            len(filenames), start_pos)
                elif direction > 0:
                    at_end = 'end'
                else:
                    at_end = 'start'
            elif user_input == ord("q"):
                datum.write_out()
                filenames[cfilename] = (filename, view.cursor)
                break
        else:
            # Draw screen
            view.render_edgecase(at_end)
            # Get input
            user_input = window.getch()
            if at_end == 'start' and user_input == ord("/"):
                at_end = None
            elif at_end == 'end' and user_input == ord("\\"):
                at_end = None
            elif user_input == ord("q"): break

        window.clear()

    for filename, start_pos in filenames:
        print("{} {} {}".format(filename, start_pos[0], start_pos[1]), file=out)
    out.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='A tool for annotating text data with extra information.')
    parser.add_argument('data', help='File containing a list of files to annotate')
    parser.add_argument('--output', help='File containing a list of files to annotate', choices=['overwrite', 'inline', 'standoff'], default="overwrite")
    parser.add_argument('--log_prefix', help='Prefix for logging files (otherwise none)')
    args = parser.parse_args()

    ### Start interface
    filenames = read_filenames(args.data)
    if len(filenames) == 0:
        print("File '{}' contained no filenames".format(args.data))
        sys.exit(0)
    config = get_default_config(args)
    curses.wrapper(annotate, config, filenames)
<commit_msg>Feature -- Show link when it is moved<commit_after>#!/usr/bin/env python3

from __future__ import print_function

import curses
import argparse
import logging
import sys

from data import *
from config import *
from view import *

def get_view(window, datum, config, file_num, total_files, position):
    cursor = position[:]
    link = position[:] if config.annotation_type == AnnType.link else [-1, -1]
    return View(window, cursor, link, datum, config, file_num, total_files)

def annotate(window, config, filenames):
    out_filename = "files_still_to_do"
    overwrite = False
    for i in range(len(sys.argv)):
        if sys.argv[i] == '-log' and len(sys.argv) > i + 1:
            out_filename = sys.argv[i + 1]
        if sys.argv[i] == '-overwrite':
            overwrite = True
    out = open(out_filename, "w")

    # Set color combinations
    for num, fore, back in COLORS:
        curses.init_pair(num, fore, back)

    # No blinking cursor
    curses.curs_set(0)

    cfilename = 0
    filename, start_pos = filenames[cfilename]
    datum = Datum(filename, config)
    view = get_view(window, datum, config, cfilename, len(filenames), start_pos)

    at_end = None
    while True:
        if at_end is None:
            # Draw screen
            view.render()
            view.must_show_linking_pos = False
            # Get input
            user_input = window.getch()

            # Note - First two are SHIFT + DOWN and SHIFT + UP, determined by
            # hand on two laptops.
            if user_input in [ord('c'), 337]:
                if config.annotation_type == AnnType.link:
                    view.move_up(True)
                    view.must_show_linking_pos = True
                else:
                    view.move_to_top()
            elif user_input in [ord('v'), 336]:
                if config.annotation_type == AnnType.link:
                    view.move_down(True)
                    view.must_show_linking_pos = True
                else:
                    view.move_to_bottom()
            elif user_input == curses.KEY_SLEFT:
                if config.annotation_type == AnnType.link: view.move_left(True)
                else: view.move_to_start()
            elif user_input == curses.KEY_SRIGHT:
                if config.annotation_type == AnnType.link: view.move_right(True)
                else: view.move_to_end()
            elif user_input == curses.KEY_UP: view.move_up()
            elif user_input == curses.KEY_DOWN: view.move_down()
            elif user_input == curses.KEY_LEFT: view.move_left()
            elif user_input == curses.KEY_RIGHT: view.move_right()
            elif user_input == ord("h"): view.toggle_help()
            elif user_input == ord("n"):
                if config.annotation_type != AnnType.link:
                    view.next_number()
            elif user_input == ord("p"):
                if config.annotation_type != AnnType.link:
                    view.next_number()
            elif user_input == ord("d"):
                datum.modify_annotation(view.cursor, view.linking_pos)
                if config.annotation_type == AnnType.link:
                    if config.annotation == AnnScope.line:
                        view.move_down(True)
                        view.cursor[0] = view.linking_pos[0]
                        view.cursor[1] = view.linking_pos[1]
                        view.move_up()
                    else:
                        view.move_right(True)
                        view.cursor[0] = view.linking_pos[0]
                        view.cursor[1] = view.linking_pos[1]
                        view.move_left()
                    view.must_show_linking_pos = True
            elif user_input == ord("D"):
                datum.modify_annotation(view.cursor, view.linking_pos)
            elif user_input == ord("u"):
                datum.remove_annotation(view.cursor, view.linking_pos)
            elif user_input in [ord('s'), ord('b'), ord('r')]:
                if config.annotation_type != AnnType.link:
                    datum.modify_annotation(view.cursor, view.linking_pos,
                            chr(user_input))
            elif user_input == ord("/") or user_input == ord("\\"):
                # If we can get another file, do
                datum.write_out()
                filenames[cfilename] = (filename, view.cursor)
                direction = 1 if user_input == ord("/") else -1
                if 0 <= cfilename + direction < len(filenames):
                    cfilename += direction
                    filename, start_pos = filenames[cfilename]
                    datum = Datum(filename, config)
                    view = get_view(window, datum, config, cfilename,
                            len(filenames), start_pos)
                elif direction > 0:
                    at_end = 'end'
                else:
                    at_end = 'start'
            elif user_input == ord("q"):
                datum.write_out()
                filenames[cfilename] = (filename, view.cursor)
                break
        else:
            # Draw screen
            view.render_edgecase(at_end)
            # Get input
            user_input = window.getch()
            if at_end == 'start' and user_input == ord("/"):
                at_end = None
            elif at_end == 'end' and user_input == ord("\\"):
                at_end = None
            elif user_input == ord("q"): break

        window.clear()

    for filename, start_pos in filenames:
        print("{} {} {}".format(filename, start_pos[0], start_pos[1]), file=out)
    out.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='A tool for annotating text data with extra information.')
    parser.add_argument('data', help='File containing a list of files to annotate')
    parser.add_argument('--output', help='File containing a list of files to annotate', choices=['overwrite', 'inline', 'standoff'], default="overwrite")
    parser.add_argument('--log_prefix', help='Prefix for logging files (otherwise none)')
    args = parser.parse_args()

    ### Start interface
    filenames = read_filenames(args.data)
    if len(filenames) == 0:
        print("File '{}' contained no filenames".format(args.data))
        sys.exit(0)
    config = get_default_config(args)
    curses.wrapper(annotate, config, filenames)
<|endoftext|>


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

import os
import os.path
import sys

from robot.errors import DataError

from .encoding import system_decode
from .platform import WINDOWS, PY2
from .robottypes import is_unicode

if sys.version_info < (2,7):
    def _abspath(path):
        if WINDOWS and os.path.splitunc(path)[0]:
            return os.path.abspath(path)
        return os.path.abspath(os.path.join(os.getcwdu(), path))
else:
    _abspath = os.path.abspath

if PY2:
    from urllib import pathname2url

    def path_to_url(path):
        return pathname2url(path.encode('UTF-8'))
else:
    from urllib.request import pathname2url as path_to_url

if WINDOWS:
    CASE_INSENSITIVE_FILESYSTEM = True
else:
    try:
        CASE_INSENSITIVE_FILESYSTEM = os.listdir('/tmp') == os.listdir('/TMP')
    except OSError:
        CASE_INSENSITIVE_FILESYSTEM = False

def normpath(path, case_normalize=False):
    """Replacement for os.path.normpath with some enhancements.

    1. Non-Unicode paths are converted to Unicode using file system encoding.
    2. Optionally lower-case paths on case-insensitive file systems.
       That includes Windows and also OSX in default configuration.
    3. Turn ``c:`` into ``c:\\`` on Windows instead of keeping it as ``c:``.
    """
    if not is_unicode(path):
        path = system_decode(path)
    path = os.path.normpath(path)
    if case_normalize and CASE_INSENSITIVE_FILESYSTEM:
        path = path.lower()
    if WINDOWS and len(path) == 2 and path[1] == ':':
        return path + '\\'
    return path

def abspath(path, case_normalize=False):
    """Replacement for os.path.abspath with some enhancements and bug fixes.

    1. Non-Unicode paths are converted to Unicode using file system encoding.
    2. Optionally lower-case paths on case-insensitive file systems.
       That includes Windows and also OSX in default configuration.
    3. Turn ``c:`` into ``c:\\`` on Windows instead of ``c:\\current\\path``.
    4. Handle non-ASCII characters on working directory with Python < 2.6.5:
       http://bugs.python.org/issue3426
    """
    path = normpath(path, case_normalize)
    return normpath(_abspath(path), case_normalize)

def get_link_path(target, base):
    """Returns a relative path to ``target`` from ``base``.

    If ``base`` is an existing file, then its parent directory is considered to
    be the base. Otherwise ``base`` is assumed to be a directory.

    The returned path is URL encoded. On Windows returns an absolute path with
    ``file:`` prefix if the target is on a different drive.
    """
    path = _get_link_path(target, base)
    url = path_to_url(path)
    if os.path.isabs(path):
        url = 'file:' + url
    return url

def _get_link_path(target, base):
    target = abspath(target)
    base = abspath(base)
    if os.path.isfile(base):
        base = os.path.dirname(base)
    if base == target:
        return '.'
    base_drive, base_path = os.path.splitdrive(base)
    # Target and base on different drives
    if os.path.splitdrive(target)[0] != base_drive:
        return target
    common_len = len(_common_path(base, target))
    if base_path == os.sep:
        return target[common_len:]
    if common_len == len(base_drive) + len(os.sep):
        common_len -= len(os.sep)
    dirs_up = os.sep.join([os.pardir] * base[common_len:].count(os.sep))
    path = os.path.join(dirs_up, target[common_len + len(os.sep):])
    return os.path.normpath(path)

def _common_path(p1, p2):
    """Returns the longest path common to p1 and p2.

    Rationale: as os.path.commonprefix is character based, it doesn't consider
    path separators as such, so it may return invalid paths:
    commonprefix(('/foo/bar/', '/foo/baz.txt')) -> '/foo/ba' (instead of /foo)
    """
    while p1 and p2:
        if p1 == p2:
            return p1
        if len(p1) > len(p2):
            p1 = os.path.dirname(p1)
        else:
            p2 = os.path.dirname(p2)
    return ''

def find_file(path, basedir='.', file_type=None):
    path = os.path.normpath(path.replace('/', os.sep))
    for base in [basedir] + sys.path:
        if not (base and os.path.isdir(base)):
            continue
        if not is_unicode(base):
            base = system_decode(base)
        ret = os.path.abspath(os.path.join(base, path))
        if os.path.isfile(ret):
            return ret
        if os.path.isdir(ret) and os.path.isfile(os.path.join(ret, '__init__.py')):
            return ret
    default = file_type or 'File'
    file_type = {'Library': 'Test library',
                 'Variables': 'Variable file',
                 'Resource': 'Resource file'}.get(file_type, default)
    raise DataError("%s '%s' does not exist." % (file_type, path))
<commit_msg>robotpath findfile finds absolute paths directly. finding a file when no directories were path did not work. #2192<commit_after>#  Copyright 2008-2015 Nokia Solutions and Networks
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import os
import os.path
import sys

from robot.errors import DataError

from .encoding import system_decode
from .platform import WINDOWS, PY2
from .robottypes import is_unicode

if sys.version_info < (2,7):
    def _abspath(path):
        if WINDOWS and os.path.splitunc(path)[0]:
            return os.path.abspath(path)
        return os.path.abspath(os.path.join(os.getcwdu(), path))
else:
    _abspath = os.path.abspath

if PY2:
    from urllib import pathname2url

    def path_to_url(path):
        return pathname2url(path.encode('UTF-8'))
else:
    from urllib.request import pathname2url as path_to_url

if WINDOWS:
    CASE_INSENSITIVE_FILESYSTEM = True
else:
    try:
        CASE_INSENSITIVE_FILESYSTEM = os.listdir('/tmp') == os.listdir('/TMP')
    except OSError:
        CASE_INSENSITIVE_FILESYSTEM = False

def normpath(path, case_normalize=False):
    """Replacement for os.path.normpath with some enhancements.

    1. Non-Unicode paths are converted to Unicode using file system encoding.
    2. Optionally lower-case paths on case-insensitive file systems.
       That includes Windows and also OSX in default configuration.
    3. Turn ``c:`` into ``c:\\`` on Windows instead of keeping it as ``c:``.
    """
    if not is_unicode(path):
        path = system_decode(path)
    path = os.path.normpath(path)
    if case_normalize and CASE_INSENSITIVE_FILESYSTEM:
        path = path.lower()
    if WINDOWS and len(path) == 2 and path[1] == ':':
        return path + '\\'
    return path

def abspath(path, case_normalize=False):
    """Replacement for os.path.abspath with some enhancements and bug fixes.

    1. Non-Unicode paths are converted to Unicode using file system encoding.
    2. Optionally lower-case paths on case-insensitive file systems.
       That includes Windows and also OSX in default configuration.
    3. Turn ``c:`` into ``c:\\`` on Windows instead of ``c:\\current\\path``.
    4. Handle non-ASCII characters on working directory with Python < 2.6.5:
       http://bugs.python.org/issue3426
    """
    path = normpath(path, case_normalize)
    return normpath(_abspath(path), case_normalize)

def get_link_path(target, base):
    """Returns a relative path to ``target`` from ``base``.

    If ``base`` is an existing file, then its parent directory is considered to
    be the base. Otherwise ``base`` is assumed to be a directory.

    The returned path is URL encoded. On Windows returns an absolute path with
    ``file:`` prefix if the target is on a different drive.
    """
    path = _get_link_path(target, base)
    url = path_to_url(path)
    if os.path.isabs(path):
        url = 'file:' + url
    return url

def _get_link_path(target, base):
    target = abspath(target)
    base = abspath(base)
    if os.path.isfile(base):
        base = os.path.dirname(base)
    if base == target:
        return '.'
    base_drive, base_path = os.path.splitdrive(base)
    # Target and base on different drives
    if os.path.splitdrive(target)[0] != base_drive:
        return target
    common_len = len(_common_path(base, target))
    if base_path == os.sep:
        return target[common_len:]
    if common_len == len(base_drive) + len(os.sep):
        common_len -= len(os.sep)
    dirs_up = os.sep.join([os.pardir] * base[common_len:].count(os.sep))
    path = os.path.join(dirs_up, target[common_len + len(os.sep):])
    return os.path.normpath(path)

def _common_path(p1, p2):
    """Returns the longest path common to p1 and p2.

    Rationale: as os.path.commonprefix is character based, it doesn't consider
    path separators as such, so it may return invalid paths:
    commonprefix(('/foo/bar/', '/foo/baz.txt')) -> '/foo/ba' (instead of /foo)
    """
    while p1 and p2:
        if p1 == p2:
            return p1
        if len(p1) > len(p2):
            p1 = os.path.dirname(p1)
        else:
            p2 = os.path.dirname(p2)
    return ''

def find_file(path, basedir='.', file_type=None):
    path = os.path.normpath(path.replace('/', os.sep))
    if os.path.isabs(path) and os.path.exists(path):
        return path
    for base in [basedir] + sys.path:
        if not (base and os.path.isdir(base)):
            continue
        if not is_unicode(base):
            base = system_decode(base)
        ret = os.path.abspath(os.path.join(base, path))
        if os.path.isfile(ret):
            return ret
        if os.path.isdir(ret) and os.path.isfile(os.path.join(ret, '__init__.py')):
            return ret
    default = file_type or 'File'
    file_type = {'Library': 'Test library',
                 'Variables': 'Variable file',
                 'Resource': 'Resource file'}.get(file_type, default)
    raise DataError("%s '%s' does not exist." % (file_type, path))
<|endoftext|>


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

#
#  This file is part of Bakefile (http://bakefile.sourceforge.net)
#
#  Copyright (C) 2003,2004 Vaclav Slavik
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 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, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#  $Id$
#

BAKEFILE_VERSION = "0.2.0"

import sys, os.path
try:
    from optparse import OptionParser
except ImportError:
    from optik import OptionParser

import formats

def addIncludePaths(includes):
    import config
    for p in includes:
        config.searchPath.append(os.path.normpath(p))

class BakefileOptionParser(OptionParser):
    def __init__(self):
        OptionParser.__init__(self,
                              version='Bakefile %s' % BAKEFILE_VERSION,
                              usage='usage: %prog [options] inputfile.bkl')
    def print_help(self, file=None):
        if file is None:
            file = sys.stdout
        OptionParser.print_help(self, file)

        # show all available formats:
        if self.values.includes != None:
            addIncludePaths(self.values.includes)
        file.write('\n%s' % formats.showFormats())

def run(args):
    import reader, writer
    import config

    parser = BakefileOptionParser()
    parser.add_option('-f', '--format',
                      action="store", dest='format',
                      help='format of generated makefile')
    parser.add_option('-o', '--output',
                      action="store", dest='outfile',
                      help='output file')
    parser.add_option('-D',
                      action="append", dest='defines', metavar='VAR=VALUE',
                      help='override variable or option definition')
    parser.add_option('-I',
                      action="append", dest='includes', metavar='PATH',
                      help='search for bakefile rules in PATH')
    parser.add_option('-v', '--verbose',
                      action="store_true", dest='verbose', default=0,
                      help='display detailed information')
    parser.add_option('-q', '--quiet',
                      action="store_true", dest='quiet', default=0,
                      help='supress all output except of errors')
    parser.add_option('', '--output-deps',
                      action="store", dest='deps_file', metavar='DEPSFILE',
                      help="output dependencies information for bakefile_gen")
    parser.add_option('', '--output-changes',
                      action="store", dest='changes_file', metavar='MODSFILE',
                      help="output list of modified files to a file")
    parser.add_option('', '--xml-cache',
                      action="store", dest='xml_cache', metavar='CACHEFILE',
                      help="cache file where bakefile_gen stores pre-parsed XML files")
    parser.add_option('', '--debug',
                      action="store_true", dest='debug', default=0,
                      help="show debugging information (you don't want this)")
    parser.add_option('', '--dump',
                      action="store_true", dest='dump', default=0,
                      help='dump parsed makefile content instead of '+
                           'generating makefile')

    options, args = parser.parse_args(args)

    if len(args) != 1:
        parser.error('incorrect number of arguments, exactly 1 .bkl required')
        sys.exit(1)

    config.debug = options.debug
    config.quiet = options.quiet
    if config.quiet:
        config.verbose = 0
    else:
        config.verbose = options.verbose

    if options.includes != None:
        addIncludePaths(options.includes)

    if options.xml_cache != None:
        try:
            import xmlparser, pickle, shelve
            xmlparser.cache = shelve.open(options.xml_cache,
                                          protocol=pickle.HIGHEST_PROTOCOL)
        except ImportError: pass
        except AttributeError: pass # python < 2.3 didn't have HIGHEST_PROTOCOL
        except KeyError: pass # python < 2.3 didn't have protocol argument

    formats.loadFormats()
    if options.format == None:
        parser.error('you must specify output format (use -f option)')
    config.format = options.format
    if not formats.isValidFormat(config.format):
        parser.error('invalid format\n\n' + formats.showFormats())

    if options.deps_file != None:
        config.track_deps = 1
        config.deps_file = options.deps_file

    if options.changes_file != None:
        config.changes_file = options.changes_file

    if options.outfile != None:
        config.output_file = options.outfile
    else:
        fmt = formats.formats[config.format]
        if fmt.defaultFile == None or fmt.defaultFile == '':
            parser.error('you must specify output file (use -o option)')
        else:
            config.output_file = \
                os.path.join(os.path.dirname(args[0]), fmt.defaultFile)

    config.defines = {}
    if options.defines != None:
        for define in options.defines:
            d = define.split('=')
            if len(d) == 1:
                config.defines[d[0]] = ''
            else:
                config.defines[d[0]] = '='.join(d[1:])

    if not reader.read(args[0]):
        sys.exit(1)

    if options.dump:
        import mk_dump
        mk_dump.dumpMakefile()
    else:
        if not writer.write():
            sys.exit(1)

    if config.track_deps:
        import dependencies
        dependencies.save(config.deps_file)

if __name__ == '__main__':
    if sys.version_info[0:3] < (2,2,2):
        sys.stderr.write('error: Bakefile requires at least Python 2.2.2\n')
        sys.exit(1)

    do_profiling = 0 # set to 1 if debugging bottlenecks

    try:
        if do_profiling:
            import hotshot
            prof = hotshot.Profile('bakefile.prof')
            prof.runcall(run, sys.argv[1:])
            prof.close()
        else:
            run(sys.argv[1:])
    except KeyboardInterrupt:
        sys.stderr.write('\nerror: bakefile cancelled by user\n')
        sys.exit(1)
<commit_msg>better error message when unknown format was specified: show the format<commit_after>#!/usr/bin/env python

#
#  This file is part of Bakefile (http://bakefile.sourceforge.net)
#
#  Copyright (C) 2003,2004 Vaclav Slavik
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 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, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#  $Id$
#

BAKEFILE_VERSION = "0.2.0"

import sys, os.path
try:
    from optparse import OptionParser
except ImportError:
    from optik import OptionParser

import formats

def addIncludePaths(includes):
    import config
    for p in includes:
        config.searchPath.append(os.path.normpath(p))

class BakefileOptionParser(OptionParser):
    def __init__(self):
        OptionParser.__init__(self,
                              version='Bakefile %s' % BAKEFILE_VERSION,
                              usage='usage: %prog [options] inputfile.bkl')
    def print_help(self, file=None):
        if file is None:
            file = sys.stdout
        OptionParser.print_help(self, file)

        # show all available formats:
        if self.values.includes != None:
            addIncludePaths(self.values.includes)
        file.write('\n%s' % formats.showFormats())

def run(args):
    import reader, writer
    import config

    parser = BakefileOptionParser()
    parser.add_option('-f', '--format',
                      action="store", dest='format',
                      help='format of generated makefile')
    parser.add_option('-o', '--output',
                      action="store", dest='outfile',
                      help='output file')
    parser.add_option('-D',
                      action="append", dest='defines', metavar='VAR=VALUE',
                      help='override variable or option definition')
    parser.add_option('-I',
                      action="append", dest='includes', metavar='PATH',
                      help='search for bakefile rules in PATH')
    parser.add_option('-v', '--verbose',
                      action="store_true", dest='verbose', default=0,
                      help='display detailed information')
    parser.add_option('-q', '--quiet',
                      action="store_true", dest='quiet', default=0,
                      help='supress all output except of errors')
    parser.add_option('', '--output-deps',
                      action="store", dest='deps_file', metavar='DEPSFILE',
                      help="output dependencies information for bakefile_gen")
    parser.add_option('', '--output-changes',
                      action="store", dest='changes_file', metavar='MODSFILE',
                      help="output list of modified files to a file")
    parser.add_option('', '--xml-cache',
                      action="store", dest='xml_cache', metavar='CACHEFILE',
                      help="cache file where bakefile_gen stores pre-parsed XML files")
    parser.add_option('', '--debug',
                      action="store_true", dest='debug', default=0,
                      help="show debugging information (you don't want this)")
    parser.add_option('', '--dump',
                      action="store_true", dest='dump', default=0,
                      help='dump parsed makefile content instead of '+
                           'generating makefile')

    options, args = parser.parse_args(args)

    if len(args) != 1:
        parser.error('incorrect number of arguments, exactly 1 .bkl required')
        sys.exit(1)

    config.debug = options.debug
    config.quiet = options.quiet
    if config.quiet:
        config.verbose = 0
    else:
        config.verbose = options.verbose

    if options.includes != None:
        addIncludePaths(options.includes)

    if options.xml_cache != None:
        try:
            import xmlparser, pickle, shelve
            xmlparser.cache = shelve.open(options.xml_cache,
                                          protocol=pickle.HIGHEST_PROTOCOL)
        except ImportError: pass
        except AttributeError: pass # python < 2.3 didn't have HIGHEST_PROTOCOL
        except KeyError: pass # python < 2.3 didn't have protocol argument

    formats.loadFormats()
    if options.format == None:
        parser.error('you must specify output format (use -f option)')
    config.format = options.format
    if not formats.isValidFormat(config.format):
        parser.error("unknown format '%s'\n\n" % config.format +
                     formats.showFormats())

    if options.deps_file != None:
        config.track_deps = 1
        config.deps_file = options.deps_file

    if options.changes_file != None:
        config.changes_file = options.changes_file

    if options.outfile != None:
        config.output_file = options.outfile
    else:
        fmt = formats.formats[config.format]
        if fmt.defaultFile == None or fmt.defaultFile == '':
            parser.error('you must specify output file (use -o option)')
        else:
            config.output_file = \
                os.path.join(os.path.dirname(args[0]), fmt.defaultFile)

    config.defines = {}
    if options.defines != None:
        for define in options.defines:
            d = define.split('=')
            if len(d) == 1:
                config.defines[d[0]] = ''
            else:
                config.defines[d[0]] = '='.join(d[1:])

    if not reader.read(args[0]):
        sys.exit(1)

    if options.dump:
        import mk_dump
        mk_dump.dumpMakefile()
    else:
        if not writer.write():
            sys.exit(1)

    if config.track_deps:
        import dependencies
        dependencies.save(config.deps_file)

if __name__ == '__main__':
    if sys.version_info[0:3] < (2,2,2):
        sys.stderr.write('error: Bakefile requires at least Python 2.2.2\n')
        sys.exit(1)

    do_profiling = 0 # set to 1 if debugging bottlenecks

    try:
        if do_profiling:
            import hotshot
            prof = hotshot.Profile('bakefile.prof')
            prof.runcall(run, sys.argv[1:])
            prof.close()
        else:
            run(sys.argv[1:])
    except KeyboardInterrupt:
        sys.stderr.write('\nerror: bakefile cancelled by user\n')
        sys.exit(1)
<|endoftext|>


<commit_before>import sys
import os
from workflow import Workflow

# GitHub repo for self-updating
GITHUB_UPDATE_CONF = {'github_slug': 'fniephaus/alfred-brightness'}
# GitHub Issues
HELP_URL = 'https://github.com/fniephaus/alfred-brightness/issues'

def main(wf):
    user_input = wf.args[0]
    if user_input != '':
        try:
            if int(user_input) <= 100 and int(user_input) >= 0:
                wf.add_item('%s%%' % user_input,
                            arg='%s' % (int(user_input) / 100.0), valid=True)
            else:
                wf.add_item('Enter value between 0 and 100')
        except ValueError:
            wf.add_item('Enter value between 0 and 100')

    for i in range(0, 120, 20):
        wf.add_item('%s%%' % i, arg='%s' % (i / 100.0), valid=True)

    try:
        current_value = os.popen('./brightness').readline()
        current = int(100 * float(current_value))
        wf.add_item('Current brightness: %s%%' % current, valid=False)
    except ValueError:
        wf.add_item('Cannot get current brightness')

    wf.send_feedback()

if __name__ == '__main__':
    wf = Workflow(update_settings=GITHUB_UPDATE_CONF, help_url=HELP_URL)
    sys.exit(wf.run(main))
<commit_msg>Add configuration options and min/max capabilities<commit_after>import sys
import os
from workflow import Workflow

# GitHub repo for self-updating
GITHUB_UPDATE_CONF = {'github_slug': 'fniephaus/alfred-brightness'}
# GitHub Issues
HELP_URL = 'https://github.com/fniephaus/alfred-brightness/issues'

# default configuration
brightness_levels = '0,20,40,60,80,100'.split(',')
show_min = False
show_max = False
value_min = 0
value_max = 100
keyword_min = 'min'
keyword_max = 'max'

# get configuration from the workflow configuration values
if 'brightness_levels' in os.environ:
    brightness_levels = os.environ['brightness_levels'].split(',')

if 'show_min' in os.environ:
    show_min = os.environ['show_min']

if 'show_max' in os.environ:
    show_max = os.environ['show_max']

if 'value_min' in os.environ and os.environ['value_min'].isdigit() and int(os.environ['value_min']) >= 0:
    value_min = int(os.environ['value_min'])

if 'value_max' in os.environ and os.environ['value_max'].isdigit() and int(os.environ['value_max']) <= 100:
    value_max = int(os.environ['value_max'])

if 'keyword_min' in os.environ:
    keyword_min = os.environ['keyword_min']

if 'keyword_max' in os.environ:
    keyword_max = os.environ['keyword_max']

def main(wf):
    user_input = wf.args[0]
    error = 'Enter value between 0 and 100'

    if show_min == 'true':
        error += (', ' if show_max == 'true' else ' or ') + '"%s"' % keyword_min

    if show_max == 'true':
        error += (', or ' if show_min == 'true' else ' or ') + '"%s"' % keyword_max

    if user_input != '' and user_input != keyword_min and user_input != keyword_max:
        try:
            if int(user_input) <= 100 and int(user_input) >= 0:
                wf.add_item('%s%%' % user_input,
                            arg='%s' % (int(user_input) / 100.0), valid=True)
            else:
                wf.add_item(error)
        except ValueError:
            wf.add_item(error)

    if show_min == 'true':
        wf.add_item('%s (%s%%)' % (keyword_min, value_min),
                    arg='%s' % (value_min / 100.0),
                    valid=True)

    if show_max == 'true':
        wf.add_item('%s (%s%%)' % (keyword_max, value_max),
                    arg='%s' % (value_max / 100.0),
                    valid=True)

    for i in brightness_levels:
        if i.isdigit():
            wf.add_item('%s%%' % i, arg='%s' % (int(i) / 100.0), valid=True)

    try:
        current_value = os.popen('./brightness').readline()
        current = int(100 * float(current_value))
        wf.add_item('Current brightness: %s%%' % current, valid=False)
    except ValueError:
        wf.add_item('Cannot get current brightness')

    wf.send_feedback()

if __name__ == '__main__':
    wf = Workflow(update_settings=GITHUB_UPDATE_CONF, help_url=HELP_URL)
    sys.exit(wf.run(main))
<|endoftext|>


<commit_before>import nltk
import random
import sys
from tqdm import *

adjs = ['so', 'such', 'very', 'much', 'many', 'how']
emots = ['wow', 'amaze', 'excite']

sentence = "The brown fox jumped over the lazy dog. The test, the sentence."

def dogeify(text):
	print " "
	textArray = text.split()
	resultArray = []
	index = 0
	for word in tqdm(textArray):
		#progress = int(((index + 1) / float(len(textArray))) * 100)
		#sys.stdout.write("\r%d%% Complete..." % progress)
		if word.lower() == "the":
			if index == 0 or index > 0 and textArray[index-1][len(textArray[index-1])-1] == ".":
				resultArray.append("Very")
			else:
				randNum = random.randint(1,2)
				if randNum == 1:
					resultArray.append("such")
				elif randNum == 2:
					resultArray.append("so")
				#resultArray.append("such")
		else:
			if nltk.pos_tag(nltk.word_tokenize(word))[0][1][:1] == "N":
				print word
			if nltk.pos_tag(nltk.word_tokenize(word))[0][1][:1] == "V":
				randNum = random.randint(1,2)
				if randNum == 1:
					#resultArray.append("much")
					resultArray.append(random.choice(adjs))
					resultArray.append(word)
				elif randNum == 2:
					#resultArray.append("many")
					resultArray.append(random.choice(adjs))
					resultArray.append(word)
			else:
				resultArray.append(word)
		index += 1
	return " ".join(resultArray)

def superdogeify(text):
	print " "
	textArray = text.split()
	nouns = []
	resultArray = []
	for word in tqdm(textArray):
		if nltk.pos_tag(nltk.word_tokenize(word))[0][1][:1] == "N":
			nouns.append(word)
	for word in nouns:
		randNum = random.randint(1,100)
		if randNum <= 10:
			resultArray.append("So wow.")
		elif randNum <= 20:
			resultArray.append("Wow.")
		resultArray.append(random.choice(adjs).capitalize())
		resultArray.append((word.rstrip('?:!.,;') + "."))
	index = 0
	for originalWord in resultArray:
		word = originalWord
		word = word.replace('dog', 'doge')
		word = word.replace('please', 'plz')
		word = word.replace('really', 'rly')
		word = word.replace('cat', 'cate')
		resultArray[index] = word
		index += 1
	return " ".join(resultArray)

def main():
	userString = raw_input("Enter text:")
	result = superdogeify(userString)
	print ""
	print result

if __name__ == "__main__":
	main()

<commit_msg>flask<commit_after>import nltk
import random
import sys
from tqdm import *

adjs = ['so', 'such', 'very', 'much', 'many', 'how']
emots = ['wow', 'amaze', 'excite']

sentence = "The brown fox jumped over the lazy dog. The test, the sentence."

def dogeify(text):
	print " "
	textArray = text.split()
	resultArray = []
	index = 0
	for word in tqdm(textArray):
		#progress = int(((index + 1) / float(len(textArray))) * 100)
		#sys.stdout.write("\r%d%% Complete..." % progress)
		if word.lower() == "the":
			if index == 0 or index > 0 and textArray[index-1][len(textArray[index-1])-1] == ".":
				resultArray.append("Very")
			else:
				randNum = random.randint(1,2)
				if randNum == 1:
					resultArray.append("such")
				elif randNum == 2:
					resultArray.append("so")
				#resultArray.append("such")
		else:
			if nltk.pos_tag(nltk.word_tokenize(word))[0][1][:1] == "N":
				print word
			if nltk.pos_tag(nltk.word_tokenize(word))[0][1][:1] == "V":
				randNum = random.randint(1,2)
				if randNum == 1:
					#resultArray.append("much")
					resultArray.append(random.choice(adjs))
					resultArray.append(word)
				elif randNum == 2:
					#resultArray.append("many")
					resultArray.append(random.choice(adjs))
					resultArray.append(word)
			else:
				resultArray.append(word)
		index += 1
	return " ".join(resultArray)

def superdogeify(text):
	print " "
	textArray = text.split()
	nouns = []
	resultArray = []
	for word in tqdm(textArray):
		if nltk.pos_tag(nltk.word_tokenize(word))[0][1][:1] == "N":
			nouns.append(word)
	for word in nouns:
		randNum = random.randint(1,100)
		if randNum <= 10:
			resultArray.append("So wow.")
		elif randNum <= 20:
			resultArray.append("Wow.")
		resultArray.append(random.choice(adjs).capitalize())
		resultArray.append((word.rstrip('?:!.,;') + "."))
	index = 0
	for originalWord in resultArray:
		word = originalWord
		word = word.replace('dog', 'doge')
		word = word.replace('please', 'plz')
		word = word.replace('really', 'rly')
		word = word.replace('cat', 'cate')
		resultArray[index] = word
		index += 1
	return " ".join(resultArray)

def main():
	#userString = raw_input("Enter text:")
	userString = """
	Two households, both alike in dignity,
In fair Verona, where we lay our scene,
From ancient grudge break to new mutiny,
Where civil blood makes civil hands unclean.
From forth the fatal loins of these two foes
A pair of star-cross'd lovers take their life;
Whose misadventured piteous overthrows
Do with their death bury their parents' strife.
The fearful passage of their death-mark'd love,
And the continuance of their parents' rage,
Which, but their children's end, nought could remove,
Is now the two hours' traffic of our stage;
The which if you with patient ears attend,
What here shall miss, our toil shall strive to mend.
"""
	result = dogeify(userString)
	print ""
	print result

if __name__ == "__main__":
	main()

<|endoftext|>


<commit_before>from __future__ import print_function

from cefpython3 import cefpython
from panda3d.core import *

import os
import sys
import atexit

class CefClientHandler:
	browser = None
	texture = None

	def __init__(self, browser, texture):
		self.browser = browser
		self.texture = texture

	def OnPaint(self, browser, paintElementType, dirtyRects, buffer, width, height):
		if width != self.texture.get_x_size() or height != self.texture.get_y_size():
			return
		img = self.texture.modifyRamImage()
		if paintElementType == cefpython.PET_POPUP:
			print("width=%s, height=%s" % (width, height))
		elif paintElementType == cefpython.PET_VIEW:
			img.setData(buffer.GetString(mode="bgra", origin="bottom-left"))
		else:
			raise Exception("Unknown paintElementType: %s" % paintElementType)

	def GetViewRect(self, browser, rect):
		rect.append(0)
		rect.append(0)
		rect.append(self.texture.get_x_size())
		rect.append(self.texture.get_y_size())
		return True

	def OnLoadError(self, browser, frame, errorCode, errorText, failedURL):
		print("load error", browser, frame, errorCode, errorText, failedURL)

class CEFPanda(object):
	_UI_SCALE = 1.0

	def __init__(self):
		cefpython.Initialize({
			"log_severity": cefpython.LOGSEVERITY_INFO,
			"release_dcheck_enabled": True,  # Enable only when debugging
			# This directories must be set on Linux
			"locales_dir_path": cefpython.GetModuleDirectory()+"/locales",
			"resources_dir_path": cefpython.GetModuleDirectory(),
			"browser_subprocess_path": "%s/%s" % (
				cefpython.GetModuleDirectory(), "subprocess"),
		})
		self._cef_texture = Texture()
		self._cef_texture.set_compression(Texture.CMOff)
		self._cef_texture.set_component_type(Texture.TUnsignedByte)
		self._cef_texture.set_format(Texture.FRgba4)

		card_maker = CardMaker("browser2d")
		card_maker.set_frame(-self._UI_SCALE, self._UI_SCALE, -self._UI_SCALE, self._UI_SCALE)
		node = card_maker.generate()
		self._cef_node = render2d.attachNewNode(node)
		self._cef_node.set_texture(self._cef_texture)
		self._cef_node.set_transparency(TransparencyAttrib.MAlpha)

		winhnd = base.win.getWindowHandle().getIntHandle()
		wininfo = cefpython.WindowInfo()
		wininfo.SetAsOffscreen(winhnd)
		wininfo.SetTransparentPainting(True)

		self.browser = cefpython.CreateBrowserSync(
			wininfo,
			{},
			navigateUrl=''
		)
		self.browser.SetClientHandler(CefClientHandler(self.browser, self._cef_texture))

		self._is_loaded = False
		self._js_onload_queue = []
		self.browser.SetClientCallback("OnLoadEnd", self._load_end)

		self.jsbindings = cefpython.JavascriptBindings()
		self.browser.SetJavascriptBindings(self.jsbindings)
		self._js_func_queue = []

		self._set_browser_size()
		base.accept('window-event', self._set_browser_size)

		if sys.platform == 'win32':
			base.win.addPythonEventHandler(self._handle_win_key, "_handle_win_keys")
		else:
			base.buttonThrowers[0].node().setKeystrokeEvent('keystroke')
			base.accept('keystroke', self._handle_key)

		base.taskMgr.add(self._cef_message_loop, "CefMessageLoop")

		def shutdown_cef():
			cefpython.Shutdown()

		atexit.register(shutdown_cef)

	def _load_end(self, *args, **kwargs):
		self._is_loaded = True

		# Register any functions
		for i in self._js_func_queue:
			self.set_js_function(*i)

		self._js_func_queue = []

		# Execute any queued javascript
		for i in self._js_onload_queue:
			self.execute_js(i)

		self._js_onload_queue = []

	# Due to an issue in CEFPython, a load_url() call needs to be made
	# before load_string() will work.
	# CEFPython issue: https://code.google.com/p/chromiumembedded/issues/detail?id=763
	def load_string(self, string, url):
		url = os.path.abspath(url)
		if sys.platform != 'win32':
			url = 'file://' + url
		self._is_loaded = False
		self.browser.GetMainFrame().LoadString(string, url)

	def load(self, url):
		if url:
			url = os.path.abspath(url)
			if sys.platform != 'win32':
				url = 'file://' + url
			self._is_loaded = False
			self.browser.GetMainFrame().LoadUrl(url)
		else:
			self.browser.GetMainFrame().LoadUrl('about:blank')

	def execute_js(self, js, onload=False):
		if onload and not self._is_loaded:
			self._js_onload_queue.append(js)
		else:
			self.browser.GetMainFrame().ExecuteJavascript(js)

	def set_js_function(self, name, func):
		if not self._is_loaded:
			self._js_func_queue.append((name, func))
		else:
			self.jsbindings.SetFunction(name, func)
			self.jsbindings.Rebind()

	def _set_browser_size(self, window=None):
		width = int(round(base.win.getXSize() * self._UI_SCALE))
		height = int(round(base.win.getYSize() * self._UI_SCALE))

		# We only want to resize if the window size actually changed.
		if self._cef_texture.get_x_size() != width and self._cef_texture.get_y_size() != height:
			self._cef_texture.set_x_size(width)
			self._cef_texture.set_y_size(height)

			# Clear the texture
			img = PNMImage(width, height)
			img.fill(0, 0, 0)
			img.alpha_fill(0)
			self._cef_texture.load(img)
			# img = CPTA_uchar(PTA_uchar.empty_array(self._cef_texture.get_expected_ram_image_size()))
			# self._cef_texture.set_ram_image(img)
			self.browser.WasResized()

	def _handle_win_key(self, key):
		msgtypes = {
			0x100: cefpython.KEYEVENT_KEYDOWN,
			0x101: cefpython.KEYEVENT_KEYUP,
			0x102: cefpython.KEYEVENT_CHAR,
		}
		if key.getMsg() in msgtypes.keys():
			keyevent = {
				"type": msgtypes[key.getMsg()],
				"windows_key_code": key.getWparam(),
			}

			self.browser.SendKeyEvent(keyevent)

	def _handle_key(self, keyname):
		keycode = ord(keyname)

		if keycode == cefpython.VK_BACK:
			keycode = 65288
		elif keycode == cefpython.VK_RETURN:
			keycode = 65293

		keyevent = {
			"type": cefpython.KEYEVENT_RAWKEYDOWN,
			"native_key_code": keycode,
		}
		self.browser.SendKeyEvent(keyevent)

		keyevent['type'] = cefpython.KEYEVENT_KEYUP
		self.browser.SendKeyEvent(keyevent)

		keyevent['type'] = cefpython.KEYEVENT_CHAR
		self.browser.SendKeyEvent(keyevent)

	def _cef_message_loop(self, task):
		cefpython.MessageLoopWork()
		self.browser.SendFocusEvent(True)

		# if base.mouseWatcherNode.has_mouse():
		# 	mouse = base.mouseWatcherNode.getMouse()
		# 	rx, ry = mouse.get_x(), mouse.get_y()
		# 	x = (rx + 1.0) / 2.0 * self._cef_texture.get_x_size()
		# 	y = (ry + 1.0) / 2.0 * self._cef_texture.get_y_size()
		# 	y = self._cef_texture.get_y_size() - y
		# 	self.browser.SendMouseMoveEvent(x, y, mouseLeave=False)

		return task.cont
<commit_msg>CEFPanda: Quiet some log messages on Linux.<commit_after>from __future__ import print_function

from cefpython3 import cefpython
from panda3d.core import *

import os
import sys
import atexit

class CefClientHandler:
	browser = None
	texture = None

	def __init__(self, browser, texture):
		self.browser = browser
		self.texture = texture

	def OnPaint(self, browser, paintElementType, dirtyRects, buffer, width, height):
		if width != self.texture.get_x_size() or height != self.texture.get_y_size():
			return
		img = self.texture.modifyRamImage()
		if paintElementType == cefpython.PET_POPUP:
			print("width=%s, height=%s" % (width, height))
		elif paintElementType == cefpython.PET_VIEW:
			img.setData(buffer.GetString(mode="bgra", origin="bottom-left"))
		else:
			raise Exception("Unknown paintElementType: %s" % paintElementType)

	def GetViewRect(self, browser, rect):
		rect.append(0)
		rect.append(0)
		rect.append(self.texture.get_x_size())
		rect.append(self.texture.get_y_size())
		return True

	def OnLoadError(self, browser, frame, errorCode, errorText, failedURL):
		print("load error", browser, frame, errorCode, errorText, failedURL)

class CEFPanda(object):
	_UI_SCALE = 1.0

	def __init__(self):
		cefpython.Initialize({
			"log_severity": cefpython.LOGSEVERITY_INFO,
			"release_dcheck_enabled": False,  # Enable only when debugging
			# This directories must be set on Linux
			"locales_dir_path": cefpython.GetModuleDirectory()+"/locales",
			"resources_dir_path": cefpython.GetModuleDirectory(),
			"browser_subprocess_path": "%s/%s" % (
				cefpython.GetModuleDirectory(), "subprocess"),
		})
		self._cef_texture = Texture()
		self._cef_texture.set_compression(Texture.CMOff)
		self._cef_texture.set_component_type(Texture.TUnsignedByte)
		self._cef_texture.set_format(Texture.FRgba4)

		card_maker = CardMaker("browser2d")
		card_maker.set_frame(-self._UI_SCALE, self._UI_SCALE, -self._UI_SCALE, self._UI_SCALE)
		node = card_maker.generate()
		self._cef_node = render2d.attachNewNode(node)
		self._cef_node.set_texture(self._cef_texture)
		self._cef_node.set_transparency(TransparencyAttrib.MAlpha)

		winhnd = base.win.getWindowHandle().getIntHandle()
		wininfo = cefpython.WindowInfo()
		wininfo.SetAsOffscreen(winhnd)
		wininfo.SetTransparentPainting(True)

		self.browser = cefpython.CreateBrowserSync(
			wininfo,
			{},
			navigateUrl=''
		)
		self.browser.SetClientHandler(CefClientHandler(self.browser, self._cef_texture))

		self._is_loaded = False
		self._js_onload_queue = []
		self.browser.SetClientCallback("OnLoadEnd", self._load_end)

		self.jsbindings = cefpython.JavascriptBindings()
		self.browser.SetJavascriptBindings(self.jsbindings)
		self._js_func_queue = []

		self._set_browser_size()
		base.accept('window-event', self._set_browser_size)

		if sys.platform == 'win32':
			base.win.addPythonEventHandler(self._handle_win_key, "_handle_win_keys")
		else:
			base.buttonThrowers[0].node().setKeystrokeEvent('keystroke')
			base.accept('keystroke', self._handle_key)

		base.taskMgr.add(self._cef_message_loop, "CefMessageLoop")

		def shutdown_cef():
			cefpython.Shutdown()

		atexit.register(shutdown_cef)

	def _load_end(self, *args, **kwargs):
		self._is_loaded = True

		# Register any functions
		for i in self._js_func_queue:
			self.set_js_function(*i)

		self._js_func_queue = []

		# Execute any queued javascript
		for i in self._js_onload_queue:
			self.execute_js(i)

		self._js_onload_queue = []

	# Due to an issue in CEFPython, a load_url() call needs to be made
	# before load_string() will work.
	# CEFPython issue: https://code.google.com/p/chromiumembedded/issues/detail?id=763
	def load_string(self, string, url):
		url = os.path.abspath(url)
		if sys.platform != 'win32':
			url = 'file://' + url
		self._is_loaded = False
		self.browser.GetMainFrame().LoadString(string, url)

	def load(self, url):
		if url:
			url = os.path.abspath(url)
			if sys.platform != 'win32':
				url = 'file://' + url
			self._is_loaded = False
			self.browser.GetMainFrame().LoadUrl(url)
		else:
			self.browser.GetMainFrame().LoadUrl('about:blank')

	def execute_js(self, js, onload=False):
		if onload and not self._is_loaded:
			self._js_onload_queue.append(js)
		else:
			self.browser.GetMainFrame().ExecuteJavascript(js)

	def set_js_function(self, name, func):
		if not self._is_loaded:
			self._js_func_queue.append((name, func))
		else:
			self.jsbindings.SetFunction(name, func)
			self.jsbindings.Rebind()

	def _set_browser_size(self, window=None):
		width = int(round(base.win.getXSize() * self._UI_SCALE))
		height = int(round(base.win.getYSize() * self._UI_SCALE))

		# We only want to resize if the window size actually changed.
		if self._cef_texture.get_x_size() != width and self._cef_texture.get_y_size() != height:
			self._cef_texture.set_x_size(width)
			self._cef_texture.set_y_size(height)

			# Clear the texture
			img = PNMImage(width, height)
			img.fill(0, 0, 0)
			img.alpha_fill(0)
			self._cef_texture.load(img)
			# img = CPTA_uchar(PTA_uchar.empty_array(self._cef_texture.get_expected_ram_image_size()))
			# self._cef_texture.set_ram_image(img)
			self.browser.WasResized()

	def _handle_win_key(self, key):
		msgtypes = {
			0x100: cefpython.KEYEVENT_KEYDOWN,
			0x101: cefpython.KEYEVENT_KEYUP,
			0x102: cefpython.KEYEVENT_CHAR,
		}
		if key.getMsg() in msgtypes.keys():
			keyevent = {
				"type": msgtypes[key.getMsg()],
				"windows_key_code": key.getWparam(),
			}

			self.browser.SendKeyEvent(keyevent)

	def _handle_key(self, keyname):
		keycode = ord(keyname)

		if keycode == cefpython.VK_BACK:
			keycode = 65288
		elif keycode == cefpython.VK_RETURN:
			keycode = 65293

		keyevent = {
			"type": cefpython.KEYEVENT_RAWKEYDOWN,
			"native_key_code": keycode,
		}
		self.browser.SendKeyEvent(keyevent)

		keyevent['type'] = cefpython.KEYEVENT_KEYUP
		self.browser.SendKeyEvent(keyevent)

		keyevent['type'] = cefpython.KEYEVENT_CHAR
		self.browser.SendKeyEvent(keyevent)

	def _cef_message_loop(self, task):
		cefpython.MessageLoopWork()
		self.browser.SendFocusEvent(True)

		# if base.mouseWatcherNode.has_mouse():
		# 	mouse = base.mouseWatcherNode.getMouse()
		# 	rx, ry = mouse.get_x(), mouse.get_y()
		# 	x = (rx + 1.0) / 2.0 * self._cef_texture.get_x_size()
		# 	y = (ry + 1.0) / 2.0 * self._cef_texture.get_y_size()
		# 	y = self._cef_texture.get_y_size() - y
		# 	self.browser.SendMouseMoveEvent(x, y, mouseLeave=False)

		return task.cont
<|endoftext|>


<commit_before>import importlib
import os
import sys
import copy
from contextlib import contextmanager

try:
    from contextlib import _GeneratorContextManager as GCM
except ImportError:
    from contextlib import GeneratorContextManager as GCM

import ruamel.yaml
from voluptuous import Schema, Optional, Invalid, ALLOW_EXTRA

from dvc.utils.compat import urlparse, builtin_str
from dvc.repo import Repo
from dvc.exceptions import SummonError, FileMissingError
from dvc.external_repo import external_repo

summon_schema = Schema(
    {
        "objects": [
            {
                "name": str,
                "summon": {
                    "type": "python",
                    "call": str,
                    Optional("args"): dict,
                    Optional("deps"): [str],
                },
            }
        ]
    },
    required=True,
    extra=ALLOW_EXTRA
)

def get_url(path, repo=None, rev=None, remote=None):
    """Returns an url of a resource specified by path in repo"""
    with _make_repo(repo, rev=rev) as _repo:
        abspath = os.path.join(_repo.root_dir, path)
        out, = _repo.find_outs_by_path(abspath)
        remote_obj = _repo.cloud.get_remote(remote)
        return str(remote_obj.checksum_to_path_info(out.checksum))

def open(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
    """Opens a specified resource as a file descriptor"""
    args = (path,)
    kwargs = {
        "repo": repo,
        "remote": remote,
        "rev": rev,
        "mode": mode,
        "encoding": encoding,
    }
    return _OpenContextManager(_open, args, kwargs)

class _OpenContextManager(GCM):
    def __init__(self, func, args, kwds):
        self.gen = func(*args, **kwds)
        self.func, self.args, self.kwds = func, args, kwds

    def __getattr__(self, name):
        raise AttributeError(
            "dvc.api.open() should be used in a with statement"
        )

def _open(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
    with _make_repo(repo, rev=rev) as _repo:
        abspath = os.path.join(_repo.root_dir, path)
        with _repo.open(
            abspath, remote=remote, mode=mode, encoding=encoding
        ) as fd:
            yield fd

def read(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
    """Read a specified resource into string"""
    with open(
        path, repo=repo, rev=rev, remote=remote, mode=mode, encoding=encoding
    ) as fd:
        return fd.read()

@contextmanager
def _make_repo(repo_url, rev=None):
    if not repo_url or urlparse(repo_url).scheme == "":
        assert rev is None, "Custom revision is not supported for local repo"
        yield Repo(repo_url)
    else:
        with external_repo(url=repo_url, rev=rev) as repo:
            yield repo

def summon(name, fname="dvcsummon.yaml", args=None, repo=None, rev=None):
    # TODO: Write a meaningful docstring about `summon`
    with _make_repo(repo, rev=rev) as _repo:

        def pull_dependencies(deps):
            if not deps:
                return

            outs = [_repo.find_out_by_relpath(dep) for dep in deps]

            with _repo.state:
                for out in outs:
                    _repo.cloud.pull(out.get_used_cache())
                    out.checkout()

        path = os.path.join(_repo.root_dir, fname)
        obj = _get_object_from_summoners_file(name, path)
        info = obj["summon"]

        pull_dependencies(info.get("deps"))

        _args = copy.deepcopy(info.get("args", {}))
        _args.update(args or {})

        return _invoke_method(info["call"], _args, path=_repo.root_dir)

def _get_object_from_summoners_file(name, path):
    """
    Given a summonable object's name, search for it on the given file
    and bring it to life.
    """
    try:
        with open(path, "r") as fobj:
            content = summon_schema(ruamel.yaml.safe_load(fobj.read()))
            objects = content["objects"]
            return next(x for x in objects if x["name"] == name)
    except FileMissingError as exc:
        # XXX: Prints ugly absolute path.
        raise SummonError("No such YAML file with path: '{}'".format(path))
    except ruamel.yaml.YAMLError as exc:
        raise SummonError("Failed to parse YAML correctly", exc)
    except Invalid as exc:
        # XXX: Doesn't point out the error.
        raise SummonError("YAML file dosen't match with the schema", exc)
    except StopIteration:
        raise SummonError("No such object with name '{}'".format(name))

def _invoke_method(call, args, path):
    # XXX: Some issues with this approach:
    #   * Not thread safe
    #   * Import will pollute sys.modules
    #   * Weird errors if there is a name clash within sys.modules
    cwd = os.path.abspath(os.curdir)

    try:
        os.chdir(path)
        sys.path.insert(0, path)
        method = _import_string(call)
        return method(**args)
    finally:
        os.chdir(cwd)
        sys.path.pop()

def _import_string(import_name, silent=False):
    """Imports an object based on a string.
    Useful to delay import to not load everything on startup.
    Use dotted notaion in `import_name`, e.g. 'dvc.remote.gs.RemoteGS'.
    If the `silent` is True the return value will be `None` if the import
    fails.

    :return: imported object
    """
    import_name = builtin_str(import_name)

    try:
        if "." in import_name:
            module, obj = import_name.rsplit(".", 1)
        else:
            return importlib.import_module(import_name)
        return getattr(importlib.import_module(module), obj)
    except (ImportError, AttributeError):
        if not silent:
            raise
<commit_msg>:nail_care: black & flake<commit_after>import importlib
import os
import sys
import copy
from contextlib import contextmanager

try:
    from contextlib import _GeneratorContextManager as GCM
except ImportError:
    from contextlib import GeneratorContextManager as GCM

import ruamel.yaml
from voluptuous import Schema, Optional, Invalid, ALLOW_EXTRA

from dvc.utils.compat import urlparse, builtin_str
from dvc.repo import Repo
from dvc.exceptions import SummonError, FileMissingError
from dvc.external_repo import external_repo

summon_schema = Schema(
    {
        "objects": [
            {
                "name": str,
                "summon": {
                    "type": "python",
                    "call": str,
                    Optional("args"): dict,
                    Optional("deps"): [str],
                },
            }
        ]
    },
    required=True,
    extra=ALLOW_EXTRA,
)

def get_url(path, repo=None, rev=None, remote=None):
    """Returns an url of a resource specified by path in repo"""
    with _make_repo(repo, rev=rev) as _repo:
        abspath = os.path.join(_repo.root_dir, path)
        out, = _repo.find_outs_by_path(abspath)
        remote_obj = _repo.cloud.get_remote(remote)
        return str(remote_obj.checksum_to_path_info(out.checksum))

def open(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
    """Opens a specified resource as a file descriptor"""
    args = (path,)
    kwargs = {
        "repo": repo,
        "remote": remote,
        "rev": rev,
        "mode": mode,
        "encoding": encoding,
    }
    return _OpenContextManager(_open, args, kwargs)

class _OpenContextManager(GCM):
    def __init__(self, func, args, kwds):
        self.gen = func(*args, **kwds)
        self.func, self.args, self.kwds = func, args, kwds

    def __getattr__(self, name):
        raise AttributeError(
            "dvc.api.open() should be used in a with statement"
        )

def _open(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
    with _make_repo(repo, rev=rev) as _repo:
        abspath = os.path.join(_repo.root_dir, path)
        with _repo.open(
            abspath, remote=remote, mode=mode, encoding=encoding
        ) as fd:
            yield fd

def read(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
    """Read a specified resource into string"""
    with open(
        path, repo=repo, rev=rev, remote=remote, mode=mode, encoding=encoding
    ) as fd:
        return fd.read()

@contextmanager
def _make_repo(repo_url, rev=None):
    if not repo_url or urlparse(repo_url).scheme == "":
        assert rev is None, "Custom revision is not supported for local repo"
        yield Repo(repo_url)
    else:
        with external_repo(url=repo_url, rev=rev) as repo:
            yield repo

def summon(name, fname="dvcsummon.yaml", args=None, repo=None, rev=None):
    # TODO: Write a meaningful docstring about `summon`
    with _make_repo(repo, rev=rev) as _repo:

        def pull_dependencies(deps):
            if not deps:
                return

            outs = [_repo.find_out_by_relpath(dep) for dep in deps]

            with _repo.state:
                for out in outs:
                    _repo.cloud.pull(out.get_used_cache())
                    out.checkout()

        path = os.path.join(_repo.root_dir, fname)
        obj = _get_object_from_summoners_file(name, path)
        info = obj["summon"]

        pull_dependencies(info.get("deps"))

        _args = copy.deepcopy(info.get("args", {}))
        _args.update(args or {})

        return _invoke_method(info["call"], _args, path=_repo.root_dir)

def _get_object_from_summoners_file(name, path):
    """
    Given a summonable object's name, search for it on the given file
    and bring it to life.
    """
    try:
        with open(path, "r") as fobj:
            content = summon_schema(ruamel.yaml.safe_load(fobj.read()))
            objects = content["objects"]
            return next(x for x in objects if x["name"] == name)
    except FileMissingError:
        # XXX: Prints ugly absolute path.
        raise SummonError("No such YAML file with path: '{}'".format(path))
    except ruamel.yaml.YAMLError as exc:
        raise SummonError("Failed to parse YAML correctly", exc)
    except Invalid as exc:
        # XXX: Doesn't point out the error.
        raise SummonError("YAML file dosen't match with the schema", exc)
    except StopIteration:
        raise SummonError("No such object with name '{}'".format(name))

def _invoke_method(call, args, path):
    # XXX: Some issues with this approach:
    #   * Not thread safe
    #   * Import will pollute sys.modules
    #   * Weird errors if there is a name clash within sys.modules
    cwd = os.path.abspath(os.curdir)

    try:
        os.chdir(path)
        sys.path.insert(0, path)
        method = _import_string(call)
        return method(**args)
    finally:
        os.chdir(cwd)
        sys.path.pop()

def _import_string(import_name, silent=False):
    """Imports an object based on a string.
    Useful to delay import to not load everything on startup.
    Use dotted notaion in `import_name`, e.g. 'dvc.remote.gs.RemoteGS'.
    If the `silent` is True the return value will be `None` if the import
    fails.

    :return: imported object
    """
    import_name = builtin_str(import_name)

    try:
        if "." in import_name:
            module, obj = import_name.rsplit(".", 1)
        else:
            return importlib.import_module(import_name)
        return getattr(importlib.import_module(module), obj)
    except (ImportError, AttributeError):
        if not silent:
            raise
<|endoftext|>


<commit_before>"""Insert feedback UI in feed entry."""

import BeautifulSoup as bs
from colour import Color
from content_extraction import get_text, get_url
from datastores import get_model_db_unix_time
import datetime as dt
from feature_extraction import text2sentences
from flask import request
from fuzzywuzzy import fuzz
import logging as log
import numpy as np
from os import listdir
from os.path import getmtime, dirname
from scipy.stats import rankdata
from traceback import format_exc

def _feedbackurl(link, well_spent):
    return (
        u"http://"
        + request.host
        + u"/feedback/"
        + (u"l" if well_spent else u"d")
        + u"/"
        + link
    )

def _is_long(text):
    return len(text) > 1000

def _p(style, text):
    return u'
{text}
'.format(style=style, text=text)

def _a(href, target, text):
    return u'{text}'.format(
        href=href, target=target, text=text
    )

def _font(color, text):
    return u'{text}'.format(color=color, text=text)

def _span(text, color):
    # style = u'"border-bottom: 3px solid {color}"'
    # style = u'"text-decoration: underline; text-decoration-color: {color}"'
    style = u'"background-color: {color};  line-height: auto"'
    style = style.format(color=color)
    return u"{text}".format(text=text, style=style)

def _feedback_link(is_good, content_link):
    return _a(
        href=_feedbackurl(link=content_link, well_spent=is_good),
        target=u"_top",
        text=_font(
            color=u"green" if is_good else u"red",
            text=u"Time Well Spent" if is_good else u"Time Wasted",
        ),
    )

def _conditional_bar(mean_score, content_link):
    return _p(
        style=u"BACKGROUND-COLOR: #DBDBDB",
        text=(_feedback_link(True, content_link) if mean_score <= 0.5 else u"")
        + (u" or " if mean_score == 0.5 else u"")
        + (_feedback_link(False, content_link) if mean_score >= 0.5 else u"")
        + " "
        + src_date()
        + " "
        + model_date(),
    )

def _add_bar(text, mean_score, content_link):
    bar = _conditional_bar(mean_score, content_link)
    return bar + text + (bar if _is_long(text) else u"")

def _embedUI_entry(entry, score):
    mean_score = score.mean()
    # body = entry2text(entry)
    body = _highlight_text(get_text(entry=entry), score)
    # body = get_html(entry=entry)
    # body = _highlight_html(html, text, score) #broken
    url = get_url(entry)
    if u"description" in entry:
        entry[u"description"] = _add_bar(body, mean_score, url)
    if u"content" in entry:
        entry[u"content"][0].value = _add_bar(body, mean_score, url)
    if u"title" in entry:
        entry[u"title"] = u"{mean_score:} | {title}".format(
            mean_score=int(mean_score * 100), title=entry[u"title"]
        )
    return entry

def embedUI(parsed_feed, score):
    """Insert a UI element in each entry of a feed.

    Parameters
    ----------
    parsed_feed : feedparser.
        Description of parameter `parsed_feed`.
    score : type
        Description of parameter `score`.

    Returns
    -------
    type
        Description of returned object.

    """
    parsed_feed.entries = [
        _embedUI_entry(e, s) for e, s in zip(parsed_feed.entries, score)
    ]
    return parsed_feed

_colors = list(Color(hsl=(0.8, 1, 1)).range_to(Color(hsl=(0.8, 1, 0.8)), 256))

def _score2color(score):
    return _colors[min(int(score * 256), 255)].get_hex_l()

def _highlight_text(text, score):
    try:
        sentences = text2sentences(text)
        return u"".join([_highlight_sentence(x, s) for x, s in zip(sentences, score)])
    except Exception:
        log.error(format_exc())
        return text

def _highlight_sentence(sentence, score):
    return _span(
        u"{s:.2f} {x}".format(x=sentence, s=score), _score2color(score)
    )

def _best_match_score(x, sentences, score):
    assert len(sentences) == len(score), (len(sentences), len(score))
    return score[np.array([fuzz.ratio(x, s) for s in sentences]).argmax()]

def _highlight_html(html, text, score):
    # this doesn't work yet for paragraphs of multiple sentences
    sentences = text2sentences(text)
    soup = bs.BeautifulSoup(html)
    for x in soup.findAll(text=True):
        x.replaceWith(
            bs.BeautifulSoup(
                _highlight_sentence(x, _best_match_score(x, sentences, score))
            )
        )
    return unicode(soup)

def src_date():
    data = ""
    srcdir = dirname(__file__) or "."
    return dt.datetime.fromtimestamp(
        max(getmtime(srcdir + "/" + file) for file in listdir(srcdir))
    ).strftime("%Y-%m-%d %H:%M:%S")

def model_date():
    return dt.datetime.fromtimestamp(get_model_db_unix_time()).strftime(
        "%Y-%m-%d %H:%M:%S"
    )
<commit_msg>using rang of prob for text highlighting<commit_after>"""Insert feedback UI in feed entry."""

import BeautifulSoup as bs
from colour import Color
from content_extraction import get_text, get_url
from datastores import get_model_db_unix_time
import datetime as dt
from feature_extraction import text2sentences
from flask import request
from fuzzywuzzy import fuzz
import logging as log
import numpy as np
from os import listdir
from os.path import getmtime, dirname
from scipy.stats import rankdata
from traceback import format_exc

def _feedbackurl(link, well_spent):
    return (
        u"http://"
        + request.host
        + u"/feedback/"
        + (u"l" if well_spent else u"d")
        + u"/"
        + link
    )

def _is_long(text):
    return len(text) > 1000

def _p(style, text):
    return u'
{text}
'.format(style=style, text=text)

def _a(href, target, text):
    return u'{text}'.format(
        href=href, target=target, text=text
    )

def _font(color, text):
    return u'{text}'.format(color=color, text=text)

def _span(text, color):
    # style = u'"border-bottom: 3px solid {color}"'
    # style = u'"text-decoration: underline; text-decoration-color: {color}"'
    style = u'"background-color: {color};  line-height: auto"'
    style = style.format(color=color)
    return u"{text}".format(text=text, style=style)

def _feedback_link(is_good, content_link):
    return _a(
        href=_feedbackurl(link=content_link, well_spent=is_good),
        target=u"_top",
        text=_font(
            color=u"green" if is_good else u"red",
            text=u"Time Well Spent" if is_good else u"Time Wasted",
        ),
    )

def _conditional_bar(mean_score, content_link):
    return _p(
        style=u"BACKGROUND-COLOR: #DBDBDB",
        text=(_feedback_link(True, content_link) if mean_score <= 0.5 else u"")
        + (u" or " if mean_score == 0.5 else u"")
        + (_feedback_link(False, content_link) if mean_score >= 0.5 else u"")
        + " "
        + src_date()
        + " "
        + model_date(),
    )

def _add_bar(text, mean_score, content_link):
    bar = _conditional_bar(mean_score, content_link)
    return bar + text + (bar if _is_long(text) else u"")

def _embedUI_entry(entry, score):
    mean_score = score.mean()
    # body = entry2text(entry)
    body = _highlight_text(get_text(entry=entry), score)
    # body = get_html(entry=entry)
    # body = _highlight_html(html, text, score) #broken
    url = get_url(entry)
    if u"description" in entry:
        entry[u"description"] = _add_bar(body, mean_score, url)
    if u"content" in entry:
        entry[u"content"][0].value = _add_bar(body, mean_score, url)
    if u"title" in entry:
        entry[u"title"] = u"{mean_score:} | {title}".format(
            mean_score=int(mean_score * 100), title=entry[u"title"]
        )
    return entry

def embedUI(parsed_feed, score):
    """Insert a UI element in each entry of a feed.

    Parameters
    ----------
    parsed_feed : feedparser.
        Description of parameter `parsed_feed`.
    score : type
        Description of parameter `score`.

    Returns
    -------
    type
        Description of returned object.

    """
    parsed_feed.entries = [
        _embedUI_entry(e, s) for e, s in zip(parsed_feed.entries, score)
    ]
    return parsed_feed

_colors = list(Color(hsl=(0.8, 1, 1)).range_to(Color(hsl=(0.8, 1, 0.8)), 256))

def _score2color(score):
    return _colors[min(int(score * 256), 255)].get_hex_l()

def _highlight_text(text, score):
    try:
        sentences = text2sentences(text)
        rank = (rankdata(score) - 1) / (len(score) - 1.0) if len(score) > 1 else [0.5]
        return u"".join(
            [_highlight_sentence(x, s, r) for x, s, r in zip(sentences, score, rank)]
        )
    except Exception:
        log.error(format_exc())
        return text

def _highlight_sentence(sentence, score, rank):
    return _span(
        u"{s:.2f} {x}".format(x=sentence, s=score), _score2color(rank)
    )

def _best_match_score(x, sentences, score):
    assert len(sentences) == len(score), (len(sentences), len(score))
    return score[np.array([fuzz.ratio(x, s) for s in sentences]).argmax()]

def _highlight_html(html, text, score):
    # this doesn't work yet for paragraphs of multiple sentences
    sentences = text2sentences(text)
    soup = bs.BeautifulSoup(html)
    for x in soup.findAll(text=True):
        x.replaceWith(
            bs.BeautifulSoup(
                _highlight_sentence(x, _best_match_score(x, sentences, score))
            )
        )
    return unicode(soup)

def src_date():
    data = ""
    srcdir = dirname(__file__) or "."
    return dt.datetime.fromtimestamp(
        max(getmtime(srcdir + "/" + file) for file in listdir(srcdir))
    ).strftime("%Y-%m-%d %H:%M:%S")

def model_date():
    return dt.datetime.fromtimestamp(get_model_db_unix_time()).strftime(
        "%Y-%m-%d %H:%M:%S"
    )
<|endoftext|>


<commit_before>import BeautifulSoup as bs
from content_extraction import entry2url, entry2text
from colour import Color
from feature_extraction import text2sentences
from flask import request
from fuzzywuzzy import fuzz
import logging as log
import numpy as np
from traceback import format_exc

def _feedbackurl(link, well_spent):
    return (u"http://" + request.host + u"/feedback/" +
            (u"l" if well_spent else u"d") + u"/" + link)

def _is_long(text):
    return len(text) > 1000

def _p(style, text):
    return u'
{text}
'.format(style=style, text=text)

def _a(href, target, text):
    return u'{text}'.format(
        href=href, target=target, text=text)

def _font(color, text):
    return u'{text}'.format(
        color=color, text=text)

def _span(text, color):
    # style = u'"border-bottom: 3px solid {color}"'
    # style = u'"text-decoration: underline; text-decoration-color: {color}"'
    style = u'"background-color: {color};  line-height: auto"'
    style = style.format(color=color)
    return u'{text}'.format(text=text, style=style)

def _feedback_link(is_good, content_link):
    return _a(
        href=_feedbackurl(link=content_link, well_spent=is_good),
        target=u"_top",
        text=_font(
            color=u"green" if is_good else u"red",
            text=u"Time Well Spent" if is_good else u"Time Wasted"))

def _conditional_bar(mean_score, content_link):
    return _p(
        style=u"BACKGROUND-COLOR: #DBDBDB",
        text=(_feedback_link(True, content_link)
              if mean_score <= 0.5 else u'') +
        (u" or "
         if mean_score == 0.5 else u"") + (_feedback_link(False, content_link)
                                           if mean_score >= 0.5 else u''))

def _add_bar(text, mean_score, content_link):
    bar = _conditional_bar(mean_score, content_link)
    return bar + text + (bar if _is_long(text) else u'')

def _embedUI_entry(entry, score):
    mean_score = score.mean()
    text = entry2text(entry)
    high_text = _highlight_text(text, score)
    # html = entry2html(entry)
    # high_html = _highlight_html(html, text, score)
    url = entry2url(entry)
    if u'description' in entry:
        entry[u'description'] = _add_bar(high_text, mean_score, url)
    if u'content' in entry:
        entry[u'content'][0].value = _add_bar(high_text, mean_score, url)
        entry[u'content'][0].value = _add_bar(high_text, mean_score, url)
    if u'title' in entry:
        entry[u'title'] = u"{mean_score:} | {title}".format(
            mean_score=int(mean_score * 100), title=entry[u'title'])
    return entry

def embedUI(parsed_feed, score):
    parsed_feed.entries = [
        _embedUI_entry(e, s) for e, s in zip(parsed_feed.entries, score)
    ]
    return parsed_feed

_colors = list(Color(hsl=(0.9, 1, 1)).range_to(Color(hsl=(0.9, 1, 0.7)), 256))

def _score2color(score):
    return _colors[int(score * 256)].get_hex_l()

def _highlight_text(text, score):
    try:
        sentences = text2sentences(text)
        return u"".join(
            [_highlight_sentence(x, s) for x, s in zip(sentences, score)])
    except Exception:
        log.error(format_exc())
        return text

def _highlight_sentence(sentence, score):
    return _span(u"{s:.2f} {x}".format(x=sentence, s=score),
                 _score2color(score))

def _best_match_score(x, sentences, score):
    assert len(sentences) == len(score), (len(sentences), len(score))
    return score[np.array([fuzz.ratio(x, s) for s in sentences]).argmax()]

def _highlight_html(html, text, score):
    # this doesn't work yet for paragraphs of multiple sentences
    sentences = text2sentences(text)
    soup = bs.BeautifulSoup(html)
    for x in soup.findAll(text=True):
        x.replaceWith(
            bs.BeautifulSoup(
                _highlight_sentence(x, _best_match_score(x, sentences,
                                                         score))))
    return unicode(soup)
<commit_msg>shade unimportant vs highlight important<commit_after>import BeautifulSoup as bs
from content_extraction import entry2url, entry2text
from colour import Color
from feature_extraction import text2sentences
from flask import request
from fuzzywuzzy import fuzz
import logging as log
import numpy as np
from traceback import format_exc

def _feedbackurl(link, well_spent):
    return (u"http://" + request.host + u"/feedback/" +
            (u"l" if well_spent else u"d") + u"/" + link)

def _is_long(text):
    return len(text) > 1000

def _p(style, text):
    return u'
{text}
'.format(style=style, text=text)

def _a(href, target, text):
    return u'{text}'.format(
        href=href, target=target, text=text)

def _font(color, text):
    return u'{text}'.format(
        color=color, text=text)

def _span(text, color):
    # style = u'"border-bottom: 3px solid {color}"'
    # style = u'"text-decoration: underline; text-decoration-color: {color}"'
    style = u'"background-color: {color};  line-height: auto"'
    style = style.format(color=color)
    return u'{text}'.format(text=text, style=style)

def _feedback_link(is_good, content_link):
    return _a(
        href=_feedbackurl(link=content_link, well_spent=is_good),
        target=u"_top",
        text=_font(
            color=u"green" if is_good else u"red",
            text=u"Time Well Spent" if is_good else u"Time Wasted"))

def _conditional_bar(mean_score, content_link):
    return _p(
        style=u"BACKGROUND-COLOR: #DBDBDB",
        text=(_feedback_link(True, content_link)
              if mean_score <= 0.5 else u'') +
        (u" or "
         if mean_score == 0.5 else u"") + (_feedback_link(False, content_link)
                                           if mean_score >= 0.5 else u''))

def _add_bar(text, mean_score, content_link):
    bar = _conditional_bar(mean_score, content_link)
    return bar + text + (bar if _is_long(text) else u'')

def _embedUI_entry(entry, score):
    mean_score = score.mean()
    text = entry2text(entry)
    high_text = _highlight_text(text, score)
    # html = entry2html(entry)
    # high_html = _highlight_html(html, text, score)
    url = entry2url(entry)
    if u'description' in entry:
        entry[u'description'] = _add_bar(high_text, mean_score, url)
    if u'content' in entry:
        entry[u'content'][0].value = _add_bar(high_text, mean_score, url)
        entry[u'content'][0].value = _add_bar(high_text, mean_score, url)
    if u'title' in entry:
        entry[u'title'] = u"{mean_score:} | {title}".format(
            mean_score=int(mean_score * 100), title=entry[u'title'])
    return entry

def embedUI(parsed_feed, score):
    parsed_feed.entries = [
        _embedUI_entry(e, s) for e, s in zip(parsed_feed.entries, score)
    ]
    return parsed_feed

_colors = list(Color(hsl=(0, 0, 0.7)).range_to(Color(hsl=(0, 0, 1)), 256))

def _score2color(score):
    return _colors[int(score * 256)].get_hex_l()

def _highlight_text(text, score):
    try:
        sentences = text2sentences(text)
        return u"".join(
            [_highlight_sentence(x, s) for x, s in zip(sentences, score)])
    except Exception:
        log.error(format_exc())
        return text

def _highlight_sentence(sentence, score):
    return _span(u"{s:.2f} {x}".format(x=sentence, s=score),
                 _score2color(score))

def _best_match_score(x, sentences, score):
    assert len(sentences) == len(score), (len(sentences), len(score))
    return score[np.array([fuzz.ratio(x, s) for s in sentences]).argmax()]

def _highlight_html(html, text, score):
    # this doesn't work yet for paragraphs of multiple sentences
    sentences = text2sentences(text)
    soup = bs.BeautifulSoup(html)
    for x in soup.findAll(text=True):
        x.replaceWith(
            bs.BeautifulSoup(
                _highlight_sentence(x, _best_match_score(x, sentences,
                                                         score))))
    return unicode(soup)
<|endoftext|>


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

""" Convolutional network applied to CIFAR-10 dataset classification task.

References:
    Learning Multiple Layers of Features from Tiny Images, A. Krizhevsky, 2009.

Links:
    [CIFAR-10 Dataset](https://www.cs.toronto.edu/~kriz/cifar.html)

"""
from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.data_utils import shuffle, to_categorical
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d, global_avg_pool
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
from tflearn.activations import softmax
from fractal_block import fractal_conv2d
from tensorflow.contrib import slim
from tflearn.layers.normalization import batch_normalization

# Data loading and preprocessing
from tflearn.datasets import cifar10
(X, Y), (X_test, Y_test) = cifar10.load_data()
X, Y = shuffle(X, Y)
Y = to_categorical(Y, 10)
Y_test = to_categorical(Y_test, 10)

# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)

# Convolutional network building
net = input_data(shape=[None, 32, 32, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)

filters = [64,128,256,512,512]
for f in filters:
  net = fractal_conv2d(net, 4, f, 3,
                       normalizer_fn=batch_normalization)
  net = slim.max_pool2d(net,2, 2)

net = conv_2d(net, 10, 1)
net = global_avg_pool(net)
net = softmax(net)

net = regression(net, optimizer='adam',
                     loss='categorical_crossentropy',
                 learning_rate=.002)

# Train using classifier
model = tflearn.DNN(net, tensorboard_verbose=0)
model.fit(X, Y, n_epoch=50, shuffle=True, validation_set=(X_test, Y_test),
          show_metric=True, batch_size=32, run_id='cifar10_cnn')
<commit_msg>made hyperparams more like paper<commit_after># -*- coding: utf-8 -*-

""" Convolutional network applied to CIFAR-10 dataset classification task.

References:
    Learning Multiple Layers of Features from Tiny Images, A. Krizhevsky, 2009.

Links:
    [CIFAR-10 Dataset](https://www.cs.toronto.edu/~kriz/cifar.html)

"""
from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.data_utils import shuffle, to_categorical
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d, global_avg_pool
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
from tflearn.activations import softmax
from fractal_block import fractal_conv2d
from tensorflow.contrib import slim
from tflearn.layers.normalization import batch_normalization

# Data loading and preprocessing
from tflearn.datasets import cifar10
(X, Y), (X_test, Y_test) = cifar10.load_data()
X, Y = shuffle(X, Y)
Y = to_categorical(Y, 10)
Y_test = to_categorical(Y_test, 10)

# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)

# Convolutional network building
net = input_data(shape=[None, 32, 32, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)

filters = [64,128,256,512]
for f in filters:
  net = fractal_conv2d(net, 4, f, 3,
                       normalizer_fn=batch_normalization)
  net = slim.max_pool2d(net,2, 2)

net = fractal_conv2d(net, 4, 512, 2,
                     normalizer_fn=batch_normalization)

net = conv_2d(net, 10, 1)
net = global_avg_pool(net)
net = softmax(net)

net = regression(net, optimizer='adam',
                     loss='categorical_crossentropy',
                 learning_rate=.002)

# Train using classifier
model = tflearn.DNN(net, tensorboard_verbose=0)
model.fit(X, Y, n_epoch=50, shuffle=True, validation_set=(X_test, Y_test),
          show_metric=True, batch_size=32, run_id='cifar10_cnn')
<|endoftext|>


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

""" Convolutional network applied to CIFAR-10 dataset classification task.

References:
    Learning Multiple Layers of Features from Tiny Images, A. Krizhevsky, 2009.

Links:
    [CIFAR-10 Dataset](https://www.cs.toronto.edu/~kriz/cifar.html)

"""
from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.data_utils import shuffle, to_categorical
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d, global_avg_pool
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
from tflearn.activations import softmax
from fractal_block import fractal_conv2d
from tensorflow.contrib import slim
from tflearn.layers.normalization import batch_normalization

# Data loading and preprocessing
from tflearn.datasets import cifar10
(X, Y), (X_test, Y_test) = cifar10.load_data()
X, Y = shuffle(X, Y)
Y = to_categorical(Y, 10)
Y_test = to_categorical(Y_test, 10)

# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)

# Convolutional network building
net = input_data(shape=[None, 32, 32, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)

filters = [64,128,256,512,512]
for f in filters:
  net = fractal_conv2d(net, 4, f,3, normalizer_fn=batch_normalization)
  net = slim.max_pool2d(net,2)

net = conv_2d(net, 10, 3)
net = global_avg_pool(net)
net = regression(net, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

# Train using classifier
model = tflearn.DNN(net, tensorboard_verbose=0)
model.fit(X, Y, n_epoch=50, shuffle=True, validation_set=(X_test, Y_test),
          show_metric=True, batch_size=32, run_id='cifar10_cnn')
<commit_msg>using default adam lr<commit_after># -*- coding: utf-8 -*-

""" Convolutional network applied to CIFAR-10 dataset classification task.

References:
    Learning Multiple Layers of Features from Tiny Images, A. Krizhevsky, 2009.

Links:
    [CIFAR-10 Dataset](https://www.cs.toronto.edu/~kriz/cifar.html)

"""
from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.data_utils import shuffle, to_categorical
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d, global_avg_pool
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
from tflearn.activations import softmax
from fractal_block import fractal_conv2d
from tensorflow.contrib import slim
from tflearn.layers.normalization import batch_normalization

# Data loading and preprocessing
from tflearn.datasets import cifar10
(X, Y), (X_test, Y_test) = cifar10.load_data()
X, Y = shuffle(X, Y)
Y = to_categorical(Y, 10)
Y_test = to_categorical(Y_test, 10)

# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)

# Convolutional network building
net = input_data(shape=[None, 32, 32, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)

filters = [64,128,256,512,512]
for f in filters:
  net = fractal_conv2d(net, 4, f,3, normalizer_fn=batch_normalization)
  net = slim.max_pool2d(net,2)

net = conv_2d(net, 10, 3)
net = global_avg_pool(net)
net = regression(net, optimizer='adam',
                     loss='categorical_crossentropy')

# Train using classifier
model = tflearn.DNN(net, tensorboard_verbose=0)
model.fit(X, Y, n_epoch=50, shuffle=True, validation_set=(X_test, Y_test),
          show_metric=True, batch_size=32, run_id='cifar10_cnn')
<|endoftext|>