<commit_before>#!/usr/bin/env python
#
#  Bottle session manager.  See README for full documentation.
#
#  Written by: Sean Reifschneider <jafo@tummy.com>
#
#  License: 3-clause BSD

from __future__ import with_statement

import bottle

def authenticator(session_manager, login_url='/auth/login'):
    '''Create an authenticator decorator.

    :param session_manager: A session manager class to be used for storing
            and retrieving session data.  Probably based on
            :class:`BaseSession`.
    :param login_url: The URL to redirect to if a login is required.
            (default: ``'/auth/login'``).
    '''
    def valid_user(login_url=login_url):
        def decorator(handler, *a, **ka):
            import functools

            @functools.wraps(handler)
            def check_auth(*a, **ka):
                try:
                    data = session_manager.get_session()
                    if not data['valid']:
                        raise KeyError('Invalid login')
                except (KeyError, TypeError):
                    bottle.response.set_cookie('validuserloginredirect',
                            bottle.request.fullpath, path='/',
                            expires=3600)
                    bottle.redirect(login_url)

                #  set environment
                if data.get('name'):
                    bottle.request.environ['REMOTE_USER'] = data['name']

                return handler(*a, **ka)
            return check_auth
        return decorator
    return(valid_user)

import pickle
import os
import uuid

class BaseSession(object):
    '''Base class which implements some of the basic functionality required for
    session managers.  Cannot be used directly.

    :param cookie_expires: Expiration time of session ID cookie, either `None`
            if the cookie is not to expire, a number of seconds in the future,
            or a datetime object.  (default: 30 days)
    '''
    def __init__(self, cookie_expires=86400 * 30):
        self.cookie_expires = cookie_expires

    def load(self, sessionid):
        raise NotImplementedError

    def save(self, sessionid, data):
        raise NotImplementedError

    def make_session_id(self):
        return str(uuid.uuid4())

    def allocate_new_session_id(self):
        #  retry allocating a unique sessionid
        for i in xrange(100):
            sessionid = self.make_session_id()
            if not self.load(sessionid):
                return sessionid
        raise ValueError('Unable to allocate unique session')

    def get_session(self):
        #  get existing or create new session identifier
        sessionid = bottle.request.COOKIES.get('sessionid')
        if not sessionid:
            sessionid = self.allocate_new_session_id()
            bottle.response.set_cookie(
                    'sessionid', sessionid, path='/',
                    expires=self.cookie_expires)

        #  load existing or create new session
        data = self.load(sessionid)
        if not data:
            data = {'sessionid': sessionid, 'valid': False}
            self.save(data)

        return data

class PickleSession(BaseSession):
    '''Class which stores session information in the file-system.

    :param session_dir: Directory that session information is stored in.
            (default: ``'/tmp'``).
    '''
    def __init__(self, session_dir='/tmp', *args, **kwargs):
        super(PickleSession, self).__init__(*args, **kwargs)
        self.session_dir = session_dir

    def load(self, sessionid):
        filename = os.path.join(self.session_dir, 'session-%s' % sessionid)
        if not os.path.exists(filename):
            return None
        with open(filename, 'r') as fp:
            session = pickle.load(fp)
        return session

    def save(self, data):
        sessionid = data['sessionid']
        fileName = os.path.join(self.session_dir, 'session-%s' % sessionid)
        tmpName = fileName + '.' + str(uuid.uuid4())
        with open(tmpName, 'w') as fp:
            self.session = pickle.dump(data, fp)
        os.rename(tmpName, fileName)

class CookieSession(BaseSession):
    '''Session manager class which stores session in a signed browser cookie.

    :param cookie_name: Name of the cookie to store the session in.
            (default: ``session_data``)
    :param secret: Secret to be used for "secure cookie".  If ``None``,
            a random secret will be generated and written to a temporary
            file for future use.  This may not be suitable for systems which
            have untrusted users on it.  (default: ``None``)
    :param secret_file: File to read the secret from.  If ``secret`` is
            ``None`` and ``secret_file`` is set, the first line of this file
            is read, and stripped, to produce the secret.
    '''

    def __init__(
            self, secret=None, secret_file=None, cookie_name='session_data',
            *args, **kwargs):
        super(CookieSession, self).__init__(*args, **kwargs)
        self.cookie_name = cookie_name

        if not secret and secret_file is not None:
            with open(secret_file, 'r') as fp:
                secret = fp.readline().strip()

        if not secret:
            import string
            import random
            import tempfile
            import sys

            tmpfilename = os.path.join(tempfile.gettempdir(),
                    '%s.secret' % os.path.basename(sys.argv[0]))

            if os.path.exists(tmpfilename):
                with open(tmpfilename, 'r') as fp:
                    secret = fp.readline().strip()
            else:
                #  save off a secret to a tmp file
                secret = ''.join([random.choice(string.letters)
                        for x in range(32)])

                old_umask = os.umask(077)
                with open(tmpfilename, 'w') as fp:
                    fp.write(secret)
                os.umask(old_umask)

        self.secret = secret

    def load(self, sessionid):
        cookie = bottle.request.get_cookie(self.cookie_name,
                secret=self.secret)
        if cookie == None:
            return {}
        return pickle.loads(cookie)

    def save(self, data):
        bottle.response.set_cookie(
                self.cookie_name, pickle.dumps(data), secret=self.secret,
                path='/', expires=self.cookie_expires, secure=True,
                httponly=True)
<commit_msg>Changing cookie expires to absolute unix time.<commit_after>#!/usr/bin/env python
#
#  Bottle session manager.  See README for full documentation.
#
#  Written by: Sean Reifschneider <jafo@tummy.com>
#
#  License: 3-clause BSD

from __future__ import with_statement

import bottle

def authenticator(session_manager, login_url='/auth/login'):
    '''Create an authenticator decorator.

    :param session_manager: A session manager class to be used for storing
            and retrieving session data.  Probably based on
            :class:`BaseSession`.
    :param login_url: The URL to redirect to if a login is required.
            (default: ``'/auth/login'``).
    '''
    def valid_user(login_url=login_url):
        def decorator(handler, *a, **ka):
            import functools

            @functools.wraps(handler)
            def check_auth(*a, **ka):
                try:
                    data = session_manager.get_session()
                    if not data['valid']:
                        raise KeyError('Invalid login')
                except (KeyError, TypeError):
                    bottle.response.set_cookie('validuserloginredirect',
                            bottle.request.fullpath, path='/',
                            expires=(int(time.time()) + 3600))
                    bottle.redirect(login_url)

                #  set environment
                if data.get('name'):
                    bottle.request.environ['REMOTE_USER'] = data['name']

                return handler(*a, **ka)
            return check_auth
        return decorator
    return(valid_user)

import pickle
import os
import uuid

class BaseSession(object):
    '''Base class which implements some of the basic functionality required for
    session managers.  Cannot be used directly.

    :param cookie_expires: Expiration time of session ID cookie, either `None`
            if the cookie is not to expire, a number of seconds in the future,
            or a datetime object.  (default: 30 days)
    '''
    def __init__(self, cookie_expires=86400 * 30):
        self.cookie_expires = cookie_expires

    def load(self, sessionid):
        raise NotImplementedError

    def save(self, sessionid, data):
        raise NotImplementedError

    def make_session_id(self):
        return str(uuid.uuid4())

    def allocate_new_session_id(self):
        #  retry allocating a unique sessionid
        for i in xrange(100):
            sessionid = self.make_session_id()
            if not self.load(sessionid):
                return sessionid
        raise ValueError('Unable to allocate unique session')

    def get_session(self):
        #  get existing or create new session identifier
        sessionid = bottle.request.COOKIES.get('sessionid')
        if not sessionid:
            sessionid = self.allocate_new_session_id()
            bottle.response.set_cookie(
                    'sessionid', sessionid, path='/',
                    expires=(int(time.time()) + self.cookie_expires))

        #  load existing or create new session
        data = self.load(sessionid)
        if not data:
            data = {'sessionid': sessionid, 'valid': False}
            self.save(data)

        return data

class PickleSession(BaseSession):
    '''Class which stores session information in the file-system.

    :param session_dir: Directory that session information is stored in.
            (default: ``'/tmp'``).
    '''
    def __init__(self, session_dir='/tmp', *args, **kwargs):
        super(PickleSession, self).__init__(*args, **kwargs)
        self.session_dir = session_dir

    def load(self, sessionid):
        filename = os.path.join(self.session_dir, 'session-%s' % sessionid)
        if not os.path.exists(filename):
            return None
        with open(filename, 'r') as fp:
            session = pickle.load(fp)
        return session

    def save(self, data):
        sessionid = data['sessionid']
        fileName = os.path.join(self.session_dir, 'session-%s' % sessionid)
        tmpName = fileName + '.' + str(uuid.uuid4())
        with open(tmpName, 'w') as fp:
            self.session = pickle.dump(data, fp)
        os.rename(tmpName, fileName)

class CookieSession(BaseSession):
    '''Session manager class which stores session in a signed browser cookie.

    :param cookie_name: Name of the cookie to store the session in.
            (default: ``session_data``)
    :param secret: Secret to be used for "secure cookie".  If ``None``,
            a random secret will be generated and written to a temporary
            file for future use.  This may not be suitable for systems which
            have untrusted users on it.  (default: ``None``)
    :param secret_file: File to read the secret from.  If ``secret`` is
            ``None`` and ``secret_file`` is set, the first line of this file
            is read, and stripped, to produce the secret.
    '''

    def __init__(
            self, secret=None, secret_file=None, cookie_name='session_data',
            *args, **kwargs):
        super(CookieSession, self).__init__(*args, **kwargs)
        self.cookie_name = cookie_name

        if not secret and secret_file is not None:
            with open(secret_file, 'r') as fp:
                secret = fp.readline().strip()

        if not secret:
            import string
            import random
            import tempfile
            import sys

            tmpfilename = os.path.join(tempfile.gettempdir(),
                    '%s.secret' % os.path.basename(sys.argv[0]))

            if os.path.exists(tmpfilename):
                with open(tmpfilename, 'r') as fp:
                    secret = fp.readline().strip()
            else:
                #  save off a secret to a tmp file
                secret = ''.join([random.choice(string.letters)
                        for x in range(32)])

                old_umask = os.umask(077)
                with open(tmpfilename, 'w') as fp:
                    fp.write(secret)
                os.umask(old_umask)

        self.secret = secret

    def load(self, sessionid):
        cookie = bottle.request.get_cookie(self.cookie_name,
                secret=self.secret)
        if cookie == None:
            return {}
        return pickle.loads(cookie)

    def save(self, data):
        bottle.response.set_cookie(
                self.cookie_name, pickle.dumps(data), secret=self.secret,
                path='/', expires=int(time.time()) + self.cookie_expires,
                secure=True, httponly=True)
<|endoftext|>


<commit_before>from __future__ import annotations

from typing import NamedTuple, Optional

from .style import Style

from itertools import zip_longest
from typing import Iterable, List, Tuple

class Segment(NamedTuple):
    """A piece of text with associated style."""

    text: str
    style: Optional[Style] = None

    def __repr__(self) -> str:
        """Simplified repr."""
        return f"Segment({self.text!r}, {self.style!r})"

    @classmethod
    def line(self) -> Segment:
        """Make a new line segment."""
        return Segment("\n")

    @classmethod
    def apply_style(
        cls, segments: Iterable[Segment], style: Style = None
    ) -> Iterable[Segment]:
        """Apply a style to an iterable of segments.

        Args:
            segments (Iterable[Segment]): Segments to process.
            style (Style, optional): A style to apply. Defaults to None.

        Returns:
            Iterable[Segments]: A new iterable of segments (possibly the same iterable).
        """
        if style is None:
            return segments
        apply = style.__add__
        return (cls(text, apply(style)) for text, style in segments)

    @classmethod
    def split_and_crop_lines(
        cls, segments: Iterable[Segment], length: int, wrap=False
    ) -> Iterable[List[Segment]]:
        """Split segments in to lines, and crop lines greater than a given length.

        Args:
            segments (Iterable[Segment]): An iterable of segments, probably
                generated from console.render.
            length (Optional[int]): Desired line length.

        Returns:
            Iterable[List[Segment]]: An iterable of lines of segments.
        """
        lines: List[List[Segment]] = [[]]
        append = lines[-1].append

        for segment in segments:
            if "\n" in segment.text:
                text, style = segment
                while text:
                    _text, new_line, text = text.partition("\n")
                    if _text:
                        append(cls(_text, style))
                    if new_line:
                        yield cls.adjust_line_length(lines[-1], length)
                        lines.append([])
                        append = lines[-1].append
            else:
                append(segment)
        if lines[-1]:
            yield cls.adjust_line_length(lines[-1], length)

    @classmethod
    def adjust_line_length(
        cls, line: List[Segment], length: int, style: Style = None
    ) -> List[Segment]:
        """Adjust a line to a given width (cropping or padding as required.

        Args:
            segments (Iterable[Segment]): A list of segments in a single line.
            length (int): The desired width of the line.
            style (Style, optional): The style of padding if used (space on the end). Defaults to None.

        Returns:
            List[Segment]: A line of segments with the desired length.
        """
        line_length = sum(len(text) for text, _style in line)
        if length < length:
            return line[:] + [Segment(" " * (length - line_length), style)]
        elif line_length > length:
            line_length = 0
            new_line: List[Segment] = []
            append = new_line.append
            for segment in line:
                segment_length = len(segment.text)
                if line_length + segment_length < length:
                    append(segment)
                    line_length += segment_length
                else:
                    text, style = segment
                    append(Segment(text[: length - line_length], style))
                    break
            return new_line
        return line[:]

    @classmethod
    def get_line_length(cls, line: List[Segment]) -> int:
        """Get the length of list of segments.

        Args:
            line (List[Segment]): A line encoded as a list of Segments (assumes no '\n' characters),

        Returns:
            int: The length of the line.
        """
        return sum(len(text) for text, _ in line)

    @classmethod
    def get_shape(cls, lines: List[List[Segment]]) -> Tuple[int, int]:
        """Get the shape (enclosing rectangle) of a list of lines

        Args:
            lines (List[List[Segment]]): A list of lines (no '\n' characters)

        Returns:
            Tuple[int, int]: Width and height in characters
        """
        get_line_length = cls.get_line_length
        max_width = max(get_line_length(line) for line in lines)
        return (max_width, len(lines))

    @classmethod
    def set_shape(
        cls,
        lines: List[List[Segment]],
        width: int,
        height: int = None,
        style: Style = None,
    ) -> List[List[Segment]]:
        """Set the shape of a list of lines (enclosing rectangle)

        Args:
            lines (List[List[Segment]]): A list of lines.
            width (int): Desired width.
            height (int, optional): Desired height or None for no change..
            style (Style, optional): Style of any padding added. Defaults to None.

        Returns:
            [type]: New list of lines that fits width x height.
        """
        if height is None:
            height = len(lines)
        new_lines: List[List[Segment]] = []
        pad_line = [Segment(" " * width, style)]
        append = new_lines.append
        adjust_line_length = cls.adjust_line_length
        for line, _ in zip_longest(lines, range(height)):
            if line is None:
                append(pad_line)
            else:
                append(adjust_line_length(line, width, style=style))
        return new_lines

    @classmethod
    def simplify(cls, segments: Iterable[Segment]) -> Iterable[Segment]:
        """Simplify an iterable of segments by combining contiguous segments with the same style.

        Args:
            segments (Iterable[Segment]): An iterable segments.

        Returns:
            Iterable[Segment]: A possibly smaller iterable of segments that will render the same way.
        """
        iter_segments = iter(segments)
        try:
            last_segment = next(iter_segments)
        except StopIteration:
            return

        _Segment = Segment
        for segment in iter_segments:
            if last_segment.style == segment.style:
                last_segment = _Segment(
                    last_segment.text + segment.text, last_segment.style
                )
            else:
                yield last_segment
                last_segment = segment
        yield last_segment

if __name__ == "__main__":
    lines = [[Segment("Hello")]]
    lines = Segment.set_shape(lines, 50, 4, style=Style.parse("on blue"))
    for line in lines:
        print(line)

    print(Style.parse("on blue") + Style.parse("on red"))
<commit_msg>no need for copy<commit_after>from __future__ import annotations

from typing import NamedTuple, Optional

from .style import Style

from itertools import zip_longest
from typing import Iterable, List, Tuple

class Segment(NamedTuple):
    """A piece of text with associated style."""

    text: str
    style: Optional[Style] = None

    def __repr__(self) -> str:
        """Simplified repr."""
        return f"Segment({self.text!r}, {self.style!r})"

    @classmethod
    def line(self) -> Segment:
        """Make a new line segment."""
        return Segment("\n")

    @classmethod
    def apply_style(
        cls, segments: Iterable[Segment], style: Style = None
    ) -> Iterable[Segment]:
        """Apply a style to an iterable of segments.

        Args:
            segments (Iterable[Segment]): Segments to process.
            style (Style, optional): A style to apply. Defaults to None.

        Returns:
            Iterable[Segments]: A new iterable of segments (possibly the same iterable).
        """
        if style is None:
            return segments
        apply = style.__add__
        return (cls(text, apply(style)) for text, style in segments)

    @classmethod
    def split_and_crop_lines(
        cls, segments: Iterable[Segment], length: int, wrap=False
    ) -> Iterable[List[Segment]]:
        """Split segments in to lines, and crop lines greater than a given length.

        Args:
            segments (Iterable[Segment]): An iterable of segments, probably
                generated from console.render.
            length (Optional[int]): Desired line length.

        Returns:
            Iterable[List[Segment]]: An iterable of lines of segments.
        """
        lines: List[List[Segment]] = [[]]
        append = lines[-1].append

        for segment in segments:
            if "\n" in segment.text:
                text, style = segment
                while text:
                    _text, new_line, text = text.partition("\n")
                    if _text:
                        append(cls(_text, style))
                    if new_line:
                        yield cls.adjust_line_length(lines[-1], length)
                        lines.append([])
                        append = lines[-1].append
            else:
                append(segment)
        if lines[-1]:
            yield cls.adjust_line_length(lines[-1], length)

    @classmethod
    def adjust_line_length(
        cls, line: List[Segment], length: int, style: Style = None
    ) -> List[Segment]:
        """Adjust a line to a given width (cropping or padding as required.

        Args:
            segments (Iterable[Segment]): A list of segments in a single line.
            length (int): The desired width of the line.
            style (Style, optional): The style of padding if used (space on the end). Defaults to None.

        Returns:
            List[Segment]: A line of segments with the desired length.
        """
        line_length = sum(len(text) for text, _style in line)
        if length < length:
            return line[:] + [Segment(" " * (length - line_length), style)]
        elif line_length > length:
            line_length = 0
            new_line: List[Segment] = []
            append = new_line.append
            for segment in line:
                segment_length = len(segment.text)
                if line_length + segment_length < length:
                    append(segment)
                    line_length += segment_length
                else:
                    text, style = segment
                    append(Segment(text[: length - line_length], style))
                    break
            return new_line
        return line

    @classmethod
    def get_line_length(cls, line: List[Segment]) -> int:
        """Get the length of list of segments.

        Args:
            line (List[Segment]): A line encoded as a list of Segments (assumes no '\n' characters),

        Returns:
            int: The length of the line.
        """
        return sum(len(text) for text, _ in line)

    @classmethod
    def get_shape(cls, lines: List[List[Segment]]) -> Tuple[int, int]:
        """Get the shape (enclosing rectangle) of a list of lines

        Args:
            lines (List[List[Segment]]): A list of lines (no '\n' characters)

        Returns:
            Tuple[int, int]: Width and height in characters
        """
        get_line_length = cls.get_line_length
        max_width = max(get_line_length(line) for line in lines)
        return (max_width, len(lines))

    @classmethod
    def set_shape(
        cls,
        lines: List[List[Segment]],
        width: int,
        height: int = None,
        style: Style = None,
    ) -> List[List[Segment]]:
        """Set the shape of a list of lines (enclosing rectangle)

        Args:
            lines (List[List[Segment]]): A list of lines.
            width (int): Desired width.
            height (int, optional): Desired height or None for no change..
            style (Style, optional): Style of any padding added. Defaults to None.

        Returns:
            [type]: New list of lines that fits width x height.
        """
        if height is None:
            height = len(lines)
        new_lines: List[List[Segment]] = []
        pad_line = [Segment(" " * width, style)]
        append = new_lines.append
        adjust_line_length = cls.adjust_line_length
        for line, _ in zip_longest(lines, range(height)):
            if line is None:
                append(pad_line)
            else:
                append(adjust_line_length(line, width, style=style))
        return new_lines

    @classmethod
    def simplify(cls, segments: Iterable[Segment]) -> Iterable[Segment]:
        """Simplify an iterable of segments by combining contiguous segments with the same style.

        Args:
            segments (Iterable[Segment]): An iterable segments.

        Returns:
            Iterable[Segment]: A possibly smaller iterable of segments that will render the same way.
        """
        iter_segments = iter(segments)
        try:
            last_segment = next(iter_segments)
        except StopIteration:
            return

        _Segment = Segment
        for segment in iter_segments:
            if last_segment.style == segment.style:
                last_segment = _Segment(
                    last_segment.text + segment.text, last_segment.style
                )
            else:
                yield last_segment
                last_segment = segment
        yield last_segment

if __name__ == "__main__":
    lines = [[Segment("Hello")]]
    lines = Segment.set_shape(lines, 50, 4, style=Style.parse("on blue"))
    for line in lines:
        print(line)

    print(Style.parse("on blue") + Style.parse("on red"))
<|endoftext|>


<commit_before>from activities.app_configs import UserSocialAuthAppConfig

class MovesConfig(UserSocialAuthAppConfig):
    """
    Configure the Moves activity application.
    """

    name = __package__
    verbose_name = 'Moves'

    data_description = {
        'name': '',
        'description': '',
    }
<commit_msg>🔧 add in_development flag<commit_after>from activities.app_configs import UserSocialAuthAppConfig

class MovesConfig(UserSocialAuthAppConfig):
    """
    Configure the Moves activity application.
    """

    name = __package__
    verbose_name = 'Moves'

    in_development = True

    data_description = {
        'name': '',
        'description': '',
    }
<|endoftext|>


<commit_before>## Copyright (c) 2012-2014 Aldebaran Robotics. All rights reserved.
## Use of this source code is governed by a BSD-style license that can be
## found in the COPYING file.

"""Library to extract, generate, update and compile translatable
sentences with gettext

"""

import os
import functools
import subprocess

from qisys import ui
import qisys.command
import qilinguist.project

class GettextProject(qilinguist.project.LinguistProject):

    def __init__(self, *args, **kwargs):
        super(GettextProject, self).__init__(*args, **kwargs)

    @property
    def pot_file(self):
        return os.path.join(self.po_path, self.domain + ".pot")

    def get_po_file(self, locale):
        return os.path.join(self.po_path, locale + ".po")

    @property
    def mo_path(self):
        mo_path = os.path.join(self.po_path, "share", "locale", self.name)
        qisys.sh.mkdir(mo_path, recursive=True)
        return mo_path

    def update(self):
        """ Read the source files, generate the
        template, generate the catalogs

        """
        self.extract_pot_file()

        # for each locale generate or update PO file
        for locale in self.linguas:
            # check if PO file exists
            output_file = self.get_po_file(locale)
            if os.path.exists(output_file):
                # Update PO file if it exists
                self.update_po_file(locale)
            else:
                # generate PO file if it doesn't exist
                self.generate_po_file(locale)

    def release(self):
        """ Compile every catalog """
        mo_output_dir = os.path.join(self.path, "po",
                                     "share", "locale", self.name)
        qisys.sh.mkdir(mo_output_dir, recursive=True)
        for locale in self.linguas:
            self.generate_mo_file(locale)

    def extract_pot_file(self):
        """Extract sentence from source file and generate POT file"""
        # get input files and directory
        input_files = self.get_sources()

        cmd = ["xgettext", "--default-domain=" + self.domain]
        # See info xgettext 5.1.6
        # https://www.gnu.org/software/gettext/manual/gettext.html#xgettext-Invocation
        #cmd.extend(["--keyword=_", "--keyword=translate:1,1t",
        #        "--keyword=translate:1c,2,2t", "--keyword=translate:1,2,3t",
        #        "--keyword=translate:1c,2,3,4t", "--keyword=gettext:1",
        #        "--keyword=pgettext:1c,2", "--keyword=ngettext:1,2",
        #        "--keyword=npgettext:1c,2,3", "--keyword=tr:1,1t",
        #        "--keyword=tr:1c,2,2t", "--keyword=tr:1,2,3t",
        #        "--keyword=tr:1c,2,3,4t"])
        # 4t are here for python because self count as an argument
        cmd.extend([
            "--keyword=_:1",
            "--keyword=translate:1,1t", "--keyword=translate:1,2t",
            "--keyword=translate:1,3t", "--keyword=translate:1,4t",
            "--keyword=tr:1,1t", "--keyword=tr:1,2t",
            "--keyword=tr:1,3t", "--keyword=tr:1,4t"
            ])
        # generate sorted output
        cmd.append("--sort-output")

        cmd.extend(["--output-dir", self.po_path])
        cmd.extend(["--output", self.pot_file])

        # add directories to list for input files search
        cmd.extend(["--directory", self.path])
        cmd.extend(input_files)

        qisys.command.call(cmd)

    # pylint: disable-msg=E0213
    def need_pot_file(func):
        @functools.wraps(func)
        def new_func(self, *args, **kwargs):
            if not os.path.exists(self.pot_file):
                ui.error("No pot file found. Maybe no translatable strings "
                        "were found?")
                return
            #pylint: disable-msg=E1102
            res = func(self, *args, **kwargs)
            return res
        return new_func

    @need_pot_file
    def update_po_file(self, locale):
        """Update PO file from POT"""
        update_file = self.get_po_file(locale)

        cmd = ["msgmerge", "--sort-output", "--update", "--backup=off"]
        cmd.extend(["--directory", self.po_path])
        cmd.append(update_file)
        cmd.append(self.pot_file)

        ui.info(ui.green, "Updating", ui.reset, ui.bold,
                os.path.relpath(update_file, self.path))
        qisys.command.call(cmd, quiet=True)

    def generate_po_file(self, locale):
        """Generate PO file"""
        output_file = self.get_po_file(locale)

        cmd = ["msginit", "--no-translator"]
        cmd.extend(["--locale", locale])
        cmd.extend(["--output-file", output_file])
        cmd.extend(["--input", self.pot_file])

        ui.info(ui.green, "Creating", ui.reset, ui.bold,
                os.path.relpath(output_file, self.path))
        # remove annoying stderr output
        subprocess.check_call(cmd, stdout=subprocess.PIPE,
                                   stderr=subprocess.STDOUT)

    def generate_mo_file(self, locale):
        """ Generate .mo file for the given locale

        """
        ui.info(ui.green, "Generating translation for", ui.reset,
                ui.bold, locale)
        input_file = self.get_po_file(locale)
        if not os.path.exists(input_file):
            ui.error("No .po found for locale: ", locale, "\n",
                     "(looked in %s)" % input_file, "\n",
                     "Did you run qilinguist update?")
            return

        output_file = os.path.join(self.mo_path, locale, "LC_MESSAGES",
                                   self.domain + ".mo")
        to_make = os.path.dirname(output_file)
        qisys.sh.mkdir(to_make, recursive=True)

        cmd = ["msgfmt", "--check", "--statistics"]

        # required by libqi:
        conf_file = os.path.join(self.mo_path, ".confintl")
        with open(conf_file, "w") as fp:
            fp.write("# THIS FILE IS AUTOGENERATED\n"
                    "#Do not delete or modify it\n"
                    "# This file is used to find translation dictionaries\n")

        cmd.extend(["--output-file", output_file])
        cmd.extend(["--directory", self.po_path])
        cmd.append(input_file)

        qisys.command.call(cmd)

    def install(self, destination):
        full_dest = os.path.join(destination, "share", "locale")
        def filter(f):
            return f.endswith(".mo")
        to_install = os.path.join(self.po_path, "share", "locale")
        qisys.sh.install(to_install, full_dest, filter_fun=filter)

    def __repr__(self):
        return "<GettextProject %s in %s>" % (self.name, self.src)
<commit_msg>Add context support extraction for qilinguist<commit_after>## Copyright (c) 2012-2014 Aldebaran Robotics. All rights reserved.
## Use of this source code is governed by a BSD-style license that can be
## found in the COPYING file.

"""Library to extract, generate, update and compile translatable
sentences with gettext

"""

import os
import functools
import subprocess

from qisys import ui
import qisys.command
import qilinguist.project

class GettextProject(qilinguist.project.LinguistProject):

    def __init__(self, *args, **kwargs):
        super(GettextProject, self).__init__(*args, **kwargs)

    @property
    def pot_file(self):
        return os.path.join(self.po_path, self.domain + ".pot")

    def get_po_file(self, locale):
        return os.path.join(self.po_path, locale + ".po")

    @property
    def mo_path(self):
        mo_path = os.path.join(self.po_path, "share", "locale", self.name)
        qisys.sh.mkdir(mo_path, recursive=True)
        return mo_path

    def update(self):
        """ Read the source files, generate the
        template, generate the catalogs

        """
        self.extract_pot_file()

        # for each locale generate or update PO file
        for locale in self.linguas:
            # check if PO file exists
            output_file = self.get_po_file(locale)
            if os.path.exists(output_file):
                # Update PO file if it exists
                self.update_po_file(locale)
            else:
                # generate PO file if it doesn't exist
                self.generate_po_file(locale)

    def release(self):
        """ Compile every catalog """
        mo_output_dir = os.path.join(self.path, "po",
                                     "share", "locale", self.name)
        qisys.sh.mkdir(mo_output_dir, recursive=True)
        for locale in self.linguas:
            self.generate_mo_file(locale)

    def extract_pot_file(self):
        """Extract sentence from source file and generate POT file"""
        # get input files and directory
        input_files = self.get_sources()

        cmd = ["xgettext", "--default-domain=" + self.domain]
        # See info xgettext 5.1.6
        # https://www.gnu.org/software/gettext/manual/gettext.html#xgettext-Invocation
        # 4t are here for python because self count as an argument
        cmd.extend([
            "--keyword=_:1",
            "--keyword=translate:1,1t", "--keyword=translate:1,2t",
            "--keyword=translate:1,3t", "--keyword=translate:1,4t",
            "--keyword=translate:1,4c,4t", "--keyword=translate:1,4c,5t",
            "--keyword=tr:1,1t", "--keyword=tr:1,2t",
            "--keyword=tr:1,3t", "--keyword=tr:1,4t",
            "--keyword=tr:1,4c,4t", "--keyword=tr:1,4c,5t"
            "--keyword=trContext:1,2c,2t", "--keyword=trContext:1,2c,3t",
            "--keyword=translateContext:1,2c,2t", "--keyword=translateContext:1,2c,3t",

            ])
        # generate sorted output
        cmd.append("--sort-output")

        cmd.extend(["--output-dir", self.po_path])
        cmd.extend(["--output", self.pot_file])

        # add directories to list for input files search
        cmd.extend(["--directory", self.path])
        cmd.extend(input_files)

        qisys.command.call(cmd)

    # pylint: disable-msg=E0213
    def need_pot_file(func):
        @functools.wraps(func)
        def new_func(self, *args, **kwargs):
            if not os.path.exists(self.pot_file):
                ui.error("No pot file found. Maybe no translatable strings "
                        "were found?")
                return
            #pylint: disable-msg=E1102
            res = func(self, *args, **kwargs)
            return res
        return new_func

    @need_pot_file
    def update_po_file(self, locale):
        """Update PO file from POT"""
        update_file = self.get_po_file(locale)

        cmd = ["msgmerge", "--sort-output", "--update", "--backup=off"]
        cmd.extend(["--directory", self.po_path])
        cmd.append(update_file)
        cmd.append(self.pot_file)

        ui.info(ui.green, "Updating", ui.reset, ui.bold,
                os.path.relpath(update_file, self.path))
        qisys.command.call(cmd, quiet=True)

    def generate_po_file(self, locale):
        """Generate PO file"""
        output_file = self.get_po_file(locale)

        cmd = ["msginit", "--no-translator"]
        cmd.extend(["--locale", locale])
        cmd.extend(["--output-file", output_file])
        cmd.extend(["--input", self.pot_file])

        ui.info(ui.green, "Creating", ui.reset, ui.bold,
                os.path.relpath(output_file, self.path))
        # remove annoying stderr output
        subprocess.check_call(cmd, stdout=subprocess.PIPE,
                                   stderr=subprocess.STDOUT)

    def generate_mo_file(self, locale):
        """ Generate .mo file for the given locale

        """
        ui.info(ui.green, "Generating translation for", ui.reset,
                ui.bold, locale)
        input_file = self.get_po_file(locale)
        if not os.path.exists(input_file):
            ui.error("No .po found for locale: ", locale, "\n",
                     "(looked in %s)" % input_file, "\n",
                     "Did you run qilinguist update?")
            return

        output_file = os.path.join(self.mo_path, locale, "LC_MESSAGES",
                                   self.domain + ".mo")
        to_make = os.path.dirname(output_file)
        qisys.sh.mkdir(to_make, recursive=True)

        cmd = ["msgfmt", "--check", "--statistics"]

        # required by libqi:
        conf_file = os.path.join(self.mo_path, ".confintl")
        with open(conf_file, "w") as fp:
            fp.write("# THIS FILE IS AUTOGENERATED\n"
                    "#Do not delete or modify it\n"
                    "# This file is used to find translation dictionaries\n")

        cmd.extend(["--output-file", output_file])
        cmd.extend(["--directory", self.po_path])
        cmd.append(input_file)

        qisys.command.call(cmd)

    def install(self, destination):
        full_dest = os.path.join(destination, "share", "locale")
        def filter(f):
            return f.endswith(".mo")
        to_install = os.path.join(self.po_path, "share", "locale")
        qisys.sh.install(to_install, full_dest, filter_fun=filter)

    def __repr__(self):
        return "<GettextProject %s in %s>" % (self.name, self.src)
<|endoftext|>


<commit_before>from __future__ import absolute_import, division, print_function

import os
import sys
import imp
import logging
from collections import namedtuple
"""
Objects used to configure Glue at runtime.
"""

__all__ = ['Registry', 'SettingRegistry', 'ExporterRegistry',
           'ColormapRegistry', 'DataFactoryRegistry', 'QtClientRegistry',
           'LinkFunctionRegistry', 'LinkHelperRegistry', 'QtToolRegistry',
           'SingleSubsetLayerActionRegistry', 'ProfileFitterRegistry',
           'qt_client', 'data_factory', 'link_function', 'link_helper',
           'colormaps', 'exporters', 'settings', 'fit_plugin', 'auto_refresh']

class Registry(object):

    """Container to hold groups of objects or settings.

    Registry instances are used by Glue to track objects
    used for various tasks like data linking, widget creation, etc.
    They have the following properties:

        - A `members` property, which lists each item in the registry
        - A `default_members` function, which can be overridden to lazily
          initialize the members list
        - A call interface, allowing the instance to be used as a decorator
          for users to add new items to the registry in their config files
    """

    def __init__(self):
        self._members = []
        self._plugins = []
        self._loaded = False

    @property
    def members(self):
        """ A list of the members in the registry.
        The return value is a list. The contents of the list
        are specified in each subclass"""
        self._load_plugins()
        if not self._loaded:
            self._members = self.default_members() + self._members
            self._loaded = True

        return self._members

    def default_members(self):
        """The member items provided by default. These are put in this
        method so that code is only imported when needed"""
        return []

    def add(self, value):
        """
        Add a new item to the registry.
        """
        self._members.append(value)

    def lazy_add(self, value):
        """
        Add a reference to a plugin which will be loaded when needed.
        """
        self._plugins.append(value)

    def _load_plugins(self):
        from .plugins import load_plugin
        while self._plugins:
            plugin = self._plugins.pop()
            load_plugin(plugin)

    def __iter__(self):
        return iter(self.members)

    def __len__(self):
        return len(self.members)

    def __contains__(self, value):
        return value in self.members

    def __call__(self, arg):
        """This is provided so that registry instances can be used
        as decorators. The decorators should add the decorated
        code object to the registry, and return the original function"""
        self.add(arg)
        return arg

class SettingRegistry(Registry):

    """Stores key/value settings that code can use to customize Glue

    Each member is a tuple of 3 items:
      - key: the setting name [str]
      - value: the default setting [object]
      - validator: A function which tests whether the input is a valid value,
                   and raises a ValueError if invalid. On valid input,
                   returns the (possibly sanitized) setting value.
    """

    def add(self, key, value, validator=str):
        self.members.append((key, value, validator))

    def default_members(self):
        import glue.plugins  # plugins will populate this registry
        return []

class ExporterRegistry(Registry):

    """Stores functions which can export an applocation to an output file

    The members property is a list of exporters, each represented
    as a (label, save_function, can_save_function, outmode) tuple.

    save_function takes an (application, path) as input, and saves
    the session

    can_save_function takes an application as input, and raises an
    exception if saving this session is not possible

    outmode is a string, with one of 3 values:
      'file': indicates that exporter creates a file
      'directory': exporter creates a directory
      'label': exporter doesn't write to disk, but needs a label
    """

    def default_members(self):
        import glue.plugins  # discover plugins
        return []

    def add(self, label, exporter, checker, outmode='file'):
        """
        Add a new exporter
        :param label: Short label for the exporter
        :type label: str

        :param exporter: exporter function
        :type exporter: function(application, path)

        :param checker: function that checks if save is possible
        :type exporter: function(application)

        ``exporter`` should raise an exception if export isn't possible.

        :param outmode: What kind of output is created?
        :type outmode: str ('file' | 'directory' | 'label')
        """
        self.members.append((label, exporter, checker, outmode))

class ColormapRegistry(Registry):

    """Stores colormaps for the Image Viewer. The members property is
    a list of colormaps, each represented as a [name,cmap] pair.
    """

    def default_members(self):
        import matplotlib.cm as cm
        members = []
        members.append(['Gray', cm.gray])
        members.append(['Purple-Blue', cm.PuBu])
        members.append(['Yellow-Green-Blue', cm.YlGnBu])
        members.append(['Yellow-Orange-Red', cm.YlOrRd])
        members.append(['Red-Purple', cm.RdPu])
        members.append(['Blue-Green', cm.BuGn])
        members.append(['Hot', cm.hot])
        members.append(['Red-Blue', cm.RdBu])
        members.append(['Red-Yellow-Blue', cm.RdYlBu])
        members.append(['Purple-Orange', cm.PuOr])
        members.append(['Purple-Green', cm.PRGn])
        return members

    def add(self, label, cmap):
        """
        Add colormap *cmap* with label *label*.
        """
        self.members.append([label, cmap])

class DataFactoryRegistry(Registry):

    """Stores data factories. Data factories take filenames as input,
    and return :class:`~glue.core.data.Data` instances

    The members property returns a list of (function, label, identifier)
    namedtuples:

    - Function is the factory that creates the data object
    - label is a short human-readable description of the factory
    - identifier is a function that takes ``(filename, **kwargs)`` as input
      and returns True if the factory can open the file

    New data factories can be registered via::

        @data_factory('label_name', identifier, default='txt')
        def new_factory(file_name):
            ...

    This has the additional side-effect of associating
    this this factory with filenames ending in ``txt`` by default
    """
    item = namedtuple('DataFactory', 'function label identifier')

    def default_members(self):
        from .core.data_factories import __factories__
        return [self.item(f, f.label, f.identifier) for f in __factories__]

    def __call__(self, label, identifier=None, default=''):
        from .core.data_factories import set_default_factory
        if identifier is None:
            identifier = lambda *a, **k: False

        def adder(func):
            set_default_factory(default, func)
            self.add(self.item(func, label, identifier))
            return func
        return adder

class QtClientRegistry(Registry):

    """Stores QT widgets to visualize data.

    The members property is a list of Qt widget classes

    New widgets can be registered via::

        @qt_client
        class CustomWidget(QMainWindow):
            ...
    """

    def default_members(self):
        try:
            from .qt.widgets import default_widgets
            from .qt.custom_viewer import CUSTOM_WIDGETS
            return default_widgets + CUSTOM_WIDGETS
        except ImportError as e:
            logging.getLogger(__name__).warning(
                "could not import glue.qt in ConfigObject")
            return []

class QtToolRegistry(Registry):

    def __init__(self):
        self._members = {}
        self._loaded = False

    @property
    def members(self):
        if not self._loaded:
            defaults = self.default_members()
            for key in defaults:
                if key in self._members:
                    self._members[key].extend(defaults[key])
                else:
                    self._members[key] = defaults[key]
            self._loaded = True
        return self._members

    def default_members(self):
        defaults = {}
        for viewer in qt_client.members:
            try:
                defaults[viewer] = viewer._get_default_tools()
            except AttributeError:
                logging.getLogger(__name__).warning(
                    "could not get default tools for {0}".format(viewer.__name__))
                defaults[viewer] = []

        return defaults

    def add(self, tool_cls, widget_cls=None):
        """
        Add a tool class to the registry, optionally specifying which widget
        class it should apply to (``widget_cls``). if ``widget_cls`` is set
        to `None`, the tool applies to all classes.
        """
        if widget_cls in self.members:
            self.members[widget_cls].append(tool_cls)
        else:
            self.members[widget_cls] = [tool_cls]

class LinkFunctionRegistry(Registry):

    """Stores functions to convert between quantities

    The members properety is a list of (function, info_string,
    output_labels) namedtuples. `info_string` is describes what the
    function does. `output_labels` is a list of names for each output.

    New link functions can be registered via

        @link_function(info="maps degrees to arcseconds",
                       output_labels=['arcsec'])
        def degrees2arcsec(degrees):
            return degress * 3600

    Link functions are expected to receive and return numpy arrays
    """
    item = namedtuple('LinkFunction', 'function info output_labels')

    def default_members(self):
        from .core import link_helpers
        return list(self.item(l, "", l.output_args)
                    for l in link_helpers.__LINK_FUNCTIONS__)

    def __call__(self, info="", output_labels=None):
        out = output_labels or []

        def adder(func):
            self.add(self.item(func, info, out))
            return func
        return adder

class SingleSubsetLayerActionRegistry(Registry):

    """ Stores custom menu actions available when user selects a single
        subset in the data collection view

        This members property is a list of (label, tooltip, callback)
        tuples. callback is a function that takes a Subset and DataCollection
        as input
    """
    item = namedtuple('SingleSubsetLayerAction', 'label tooltip callback icon')

    def default_members(self):
        return []

    def __call__(self, label, callback, tooltip=None, icon=None):
        self.add(self.item(label, callback, tooltip, icon))

class LinkHelperRegistry(Registry):

    """Stores helper objects that compute many ComponentLinks at once

    The members property is a list of (object, info_string,
    input_labels) tuples. `Object` is the link helper. `info_string`
    describes what `object` does. `input_labels` is a list labeling
    the inputs.

    Each link helper takes a list of ComponentIDs as inputs, and
    returns an iterable object (e.g. list) of ComponentLinks.

    New helpers can be registered via

        @link_helper('Links degrees and arcseconds in both directions',
                     ['degree', 'arcsecond'])
        def new_helper(degree, arcsecond):
            return [ComponentLink([degree], arcsecond, using=lambda d: d*3600),
                    ComponentLink([arcsecond], degree, using=lambda a: a/3600)]
    """
    item = namedtuple('LinkHelper', 'helper info input_labels')

    def default_members(self):
        from .core.link_helpers import __LINK_HELPERS__ as helpers
        return list(self.item(l, l.info_text, l.input_args)
                    for l in helpers)

    def __call__(self, info, input_labels):
        def adder(func):
            self.add(self.item(func, info, input_labels))
            return func
        return adder

class ProfileFitterRegistry(Registry):
    item = namedtuple('ProfileFitter', 'cls')

    def add(self, cls):
        """
        Add colormap *cmap* with label *label*.
        """
        self.members.append(cls)

    def default_members(self):
        from .core.fitters import __FITTERS__
        return list(__FITTERS__)

class BooleanSetting(object):

    def __init__(self, default=True):
        self.state = default

    def __call__(self, state=None):
        if state not in [None, True, False]:
            raise ValueError("Invalid True/False setting: %s" % state)

        if state is not None:
            self.state = state

        return self.state

qt_client = QtClientRegistry()
tool_registry = QtToolRegistry()
data_factory = DataFactoryRegistry()
link_function = LinkFunctionRegistry()
link_helper = LinkHelperRegistry()
colormaps = ColormapRegistry()
exporters = ExporterRegistry()
settings = SettingRegistry()
fit_plugin = ProfileFitterRegistry()
single_subset_action = SingleSubsetLayerActionRegistry()

# watch loaded data files for changes?
auto_refresh = BooleanSetting(False)
enable_contracts = BooleanSetting(False)

def load_configuration(search_path=None):
    ''' Find and import a config.py file

    Returns:

       The module object

    Raises:

       Exception, if no module was found
    '''
    search_order = search_path or _default_search_order()
    result = imp.new_module('config')

    for config_file in search_order:
        dir = os.path.dirname(config_file)
        try:
            sys.path.append(dir)
            config = imp.load_source('config', config_file)
            result = config
        except IOError:
            pass
        except Exception as e:
            raise type(e)("Error loading config file %s:\n%s" % (config_file, e), sys.exc_info()[2])
        finally:
            sys.path.remove(dir)

    return result

def _default_search_order():
    """
    The default configuration file search order:

       * current working directory
       * environ var GLUERC
       * HOME/.glue/config.py
       * Glue's own default config
    """
    search_order = [os.path.join(os.getcwd(), 'config.py')]
    if 'GLUERC' in os.environ:
        search_order.append(os.environ['GLUERC'])
    search_order.append(os.path.expanduser('~/.glue/config.py'))
    return search_order[::-1]
<commit_msg>Renamed _plugins to _lazy_members<commit_after>from __future__ import absolute_import, division, print_function

import os
import sys
import imp
import logging
from collections import namedtuple
"""
Objects used to configure Glue at runtime.
"""

__all__ = ['Registry', 'SettingRegistry', 'ExporterRegistry',
           'ColormapRegistry', 'DataFactoryRegistry', 'QtClientRegistry',
           'LinkFunctionRegistry', 'LinkHelperRegistry', 'QtToolRegistry',
           'SingleSubsetLayerActionRegistry', 'ProfileFitterRegistry',
           'qt_client', 'data_factory', 'link_function', 'link_helper',
           'colormaps', 'exporters', 'settings', 'fit_plugin', 'auto_refresh']

class Registry(object):

    """Container to hold groups of objects or settings.

    Registry instances are used by Glue to track objects
    used for various tasks like data linking, widget creation, etc.
    They have the following properties:

        - A `members` property, which lists each item in the registry
        - A `default_members` function, which can be overridden to lazily
          initialize the members list
        - A call interface, allowing the instance to be used as a decorator
          for users to add new items to the registry in their config files
    """

    def __init__(self):
        self._members = []
        self._lazy_members = []
        self._loaded = False

    @property
    def members(self):
        """ A list of the members in the registry.
        The return value is a list. The contents of the list
        are specified in each subclass"""
        self._load_lazy_members()
        if not self._loaded:
            self._members = self.default_members() + self._members
            self._loaded = True

        return self._members

    def default_members(self):
        """The member items provided by default. These are put in this
        method so that code is only imported when needed"""
        return []

    def add(self, value):
        """
        Add a new item to the registry.
        """
        self._members.append(value)

    def lazy_add(self, value):
        """
        Add a reference to a plugin which will be loaded when needed.
        """
        self._lazy_members.append(value)

    def _load_lazy_members(self):
        from .plugins import load_plugin
        while self._lazy_members:
            plugin = self._lazy_members.pop()
            load_plugin(plugin)

    def __iter__(self):
        return iter(self.members)

    def __len__(self):
        return len(self.members)

    def __contains__(self, value):
        return value in self.members

    def __call__(self, arg):
        """This is provided so that registry instances can be used
        as decorators. The decorators should add the decorated
        code object to the registry, and return the original function"""
        self.add(arg)
        return arg

class SettingRegistry(Registry):

    """Stores key/value settings that code can use to customize Glue

    Each member is a tuple of 3 items:
      - key: the setting name [str]
      - value: the default setting [object]
      - validator: A function which tests whether the input is a valid value,
                   and raises a ValueError if invalid. On valid input,
                   returns the (possibly sanitized) setting value.
    """

    def add(self, key, value, validator=str):
        self.members.append((key, value, validator))

    def default_members(self):
        import glue.plugins  # plugins will populate this registry
        return []

class ExporterRegistry(Registry):

    """Stores functions which can export an applocation to an output file

    The members property is a list of exporters, each represented
    as a (label, save_function, can_save_function, outmode) tuple.

    save_function takes an (application, path) as input, and saves
    the session

    can_save_function takes an application as input, and raises an
    exception if saving this session is not possible

    outmode is a string, with one of 3 values:
      'file': indicates that exporter creates a file
      'directory': exporter creates a directory
      'label': exporter doesn't write to disk, but needs a label
    """

    def default_members(self):
        import glue.plugins  # discover plugins
        return []

    def add(self, label, exporter, checker, outmode='file'):
        """
        Add a new exporter
        :param label: Short label for the exporter
        :type label: str

        :param exporter: exporter function
        :type exporter: function(application, path)

        :param checker: function that checks if save is possible
        :type exporter: function(application)

        ``exporter`` should raise an exception if export isn't possible.

        :param outmode: What kind of output is created?
        :type outmode: str ('file' | 'directory' | 'label')
        """
        self.members.append((label, exporter, checker, outmode))

class ColormapRegistry(Registry):

    """Stores colormaps for the Image Viewer. The members property is
    a list of colormaps, each represented as a [name,cmap] pair.
    """

    def default_members(self):
        import matplotlib.cm as cm
        members = []
        members.append(['Gray', cm.gray])
        members.append(['Purple-Blue', cm.PuBu])
        members.append(['Yellow-Green-Blue', cm.YlGnBu])
        members.append(['Yellow-Orange-Red', cm.YlOrRd])
        members.append(['Red-Purple', cm.RdPu])
        members.append(['Blue-Green', cm.BuGn])
        members.append(['Hot', cm.hot])
        members.append(['Red-Blue', cm.RdBu])
        members.append(['Red-Yellow-Blue', cm.RdYlBu])
        members.append(['Purple-Orange', cm.PuOr])
        members.append(['Purple-Green', cm.PRGn])
        return members

    def add(self, label, cmap):
        """
        Add colormap *cmap* with label *label*.
        """
        self.members.append([label, cmap])

class DataFactoryRegistry(Registry):

    """Stores data factories. Data factories take filenames as input,
    and return :class:`~glue.core.data.Data` instances

    The members property returns a list of (function, label, identifier)
    namedtuples:

    - Function is the factory that creates the data object
    - label is a short human-readable description of the factory
    - identifier is a function that takes ``(filename, **kwargs)`` as input
      and returns True if the factory can open the file

    New data factories can be registered via::

        @data_factory('label_name', identifier, default='txt')
        def new_factory(file_name):
            ...

    This has the additional side-effect of associating
    this this factory with filenames ending in ``txt`` by default
    """
    item = namedtuple('DataFactory', 'function label identifier')

    def default_members(self):
        from .core.data_factories import __factories__
        return [self.item(f, f.label, f.identifier) for f in __factories__]

    def __call__(self, label, identifier=None, default=''):
        from .core.data_factories import set_default_factory
        if identifier is None:
            identifier = lambda *a, **k: False

        def adder(func):
            set_default_factory(default, func)
            self.add(self.item(func, label, identifier))
            return func
        return adder

class QtClientRegistry(Registry):

    """Stores QT widgets to visualize data.

    The members property is a list of Qt widget classes

    New widgets can be registered via::

        @qt_client
        class CustomWidget(QMainWindow):
            ...
    """

    def default_members(self):
        try:
            from .qt.widgets import default_widgets
            from .qt.custom_viewer import CUSTOM_WIDGETS
            return default_widgets + CUSTOM_WIDGETS
        except ImportError as e:
            logging.getLogger(__name__).warning(
                "could not import glue.qt in ConfigObject")
            return []

class QtToolRegistry(Registry):

    def __init__(self):
        self._members = {}
        self._loaded = False

    @property
    def members(self):
        if not self._loaded:
            defaults = self.default_members()
            for key in defaults:
                if key in self._members:
                    self._members[key].extend(defaults[key])
                else:
                    self._members[key] = defaults[key]
            self._loaded = True
        return self._members

    def default_members(self):
        defaults = {}
        for viewer in qt_client.members:
            try:
                defaults[viewer] = viewer._get_default_tools()
            except AttributeError:
                logging.getLogger(__name__).warning(
                    "could not get default tools for {0}".format(viewer.__name__))
                defaults[viewer] = []

        return defaults

    def add(self, tool_cls, widget_cls=None):
        """
        Add a tool class to the registry, optionally specifying which widget
        class it should apply to (``widget_cls``). if ``widget_cls`` is set
        to `None`, the tool applies to all classes.
        """
        if widget_cls in self.members:
            self.members[widget_cls].append(tool_cls)
        else:
            self.members[widget_cls] = [tool_cls]

class LinkFunctionRegistry(Registry):

    """Stores functions to convert between quantities

    The members properety is a list of (function, info_string,
    output_labels) namedtuples. `info_string` is describes what the
    function does. `output_labels` is a list of names for each output.

    New link functions can be registered via

        @link_function(info="maps degrees to arcseconds",
                       output_labels=['arcsec'])
        def degrees2arcsec(degrees):
            return degress * 3600

    Link functions are expected to receive and return numpy arrays
    """
    item = namedtuple('LinkFunction', 'function info output_labels')

    def default_members(self):
        from .core import link_helpers
        return list(self.item(l, "", l.output_args)
                    for l in link_helpers.__LINK_FUNCTIONS__)

    def __call__(self, info="", output_labels=None):
        out = output_labels or []

        def adder(func):
            self.add(self.item(func, info, out))
            return func
        return adder

class SingleSubsetLayerActionRegistry(Registry):

    """ Stores custom menu actions available when user selects a single
        subset in the data collection view

        This members property is a list of (label, tooltip, callback)
        tuples. callback is a function that takes a Subset and DataCollection
        as input
    """
    item = namedtuple('SingleSubsetLayerAction', 'label tooltip callback icon')

    def default_members(self):
        return []

    def __call__(self, label, callback, tooltip=None, icon=None):
        self.add(self.item(label, callback, tooltip, icon))

class LinkHelperRegistry(Registry):

    """Stores helper objects that compute many ComponentLinks at once

    The members property is a list of (object, info_string,
    input_labels) tuples. `Object` is the link helper. `info_string`
    describes what `object` does. `input_labels` is a list labeling
    the inputs.

    Each link helper takes a list of ComponentIDs as inputs, and
    returns an iterable object (e.g. list) of ComponentLinks.

    New helpers can be registered via

        @link_helper('Links degrees and arcseconds in both directions',
                     ['degree', 'arcsecond'])
        def new_helper(degree, arcsecond):
            return [ComponentLink([degree], arcsecond, using=lambda d: d*3600),
                    ComponentLink([arcsecond], degree, using=lambda a: a/3600)]
    """
    item = namedtuple('LinkHelper', 'helper info input_labels')

    def default_members(self):
        from .core.link_helpers import __LINK_HELPERS__ as helpers
        return list(self.item(l, l.info_text, l.input_args)
                    for l in helpers)

    def __call__(self, info, input_labels):
        def adder(func):
            self.add(self.item(func, info, input_labels))
            return func
        return adder

class ProfileFitterRegistry(Registry):
    item = namedtuple('ProfileFitter', 'cls')

    def add(self, cls):
        """
        Add colormap *cmap* with label *label*.
        """
        self.members.append(cls)

    def default_members(self):
        from .core.fitters import __FITTERS__
        return list(__FITTERS__)

class BooleanSetting(object):

    def __init__(self, default=True):
        self.state = default

    def __call__(self, state=None):
        if state not in [None, True, False]:
            raise ValueError("Invalid True/False setting: %s" % state)

        if state is not None:
            self.state = state

        return self.state

qt_client = QtClientRegistry()
tool_registry = QtToolRegistry()
data_factory = DataFactoryRegistry()
link_function = LinkFunctionRegistry()
link_helper = LinkHelperRegistry()
colormaps = ColormapRegistry()
exporters = ExporterRegistry()
settings = SettingRegistry()
fit_plugin = ProfileFitterRegistry()
single_subset_action = SingleSubsetLayerActionRegistry()

# watch loaded data files for changes?
auto_refresh = BooleanSetting(False)
enable_contracts = BooleanSetting(False)

def load_configuration(search_path=None):
    ''' Find and import a config.py file

    Returns:

       The module object

    Raises:

       Exception, if no module was found
    '''
    search_order = search_path or _default_search_order()
    result = imp.new_module('config')

    for config_file in search_order:
        dir = os.path.dirname(config_file)
        try:
            sys.path.append(dir)
            config = imp.load_source('config', config_file)
            result = config
        except IOError:
            pass
        except Exception as e:
            raise type(e)("Error loading config file %s:\n%s" % (config_file, e), sys.exc_info()[2])
        finally:
            sys.path.remove(dir)

    return result

def _default_search_order():
    """
    The default configuration file search order:

       * current working directory
       * environ var GLUERC
       * HOME/.glue/config.py
       * Glue's own default config
    """
    search_order = [os.path.join(os.getcwd(), 'config.py')]
    if 'GLUERC' in os.environ:
        search_order.append(os.environ['GLUERC'])
    search_order.append(os.path.expanduser('~/.glue/config.py'))
    return search_order[::-1]
<|endoftext|>


<commit_before>
from py_entitymatching.catalog.catalog import Catalog

__version__ = '0.3.1'

_catalog = Catalog.Instance()

# downsampling related methods

from py_entitymatching.sampler.down_sample import down_sample
# # io related methods
#
from py_entitymatching.io.parsers import read_csv_metadata, to_csv_metadata
from py_entitymatching.io.pickles import load_object, load_table, save_object, save_table
#
# import catalog related methods
from py_entitymatching.catalog.catalog_manager import get_property, get_all_properties, \
    set_property, del_property, del_all_properties, init_properties, copy_properties
from py_entitymatching.catalog.catalog_manager import get_catalog, del_catalog, \
    get_catalog_len, show_properties, show_properties_for_id
from py_entitymatching.catalog.catalog_manager import is_property_present_for_df, \
    is_dfinfo_present, is_catalog_empty
from py_entitymatching.catalog.catalog_manager import get_key, set_key, set_fk_ltable,\
    set_fk_rtable, get_ltable, get_rtable, validate_and_set_fk_ltable, \
    validate_and_set_fk_rtable, set_ltable, set_rtable, get_fk_rtable,  \
    get_fk_ltable

# # data exploration wrappers
from py_entitymatching.explorer.openrefine.openrefine_wrapper import data_explore_openrefine
from py_entitymatching.explorer.pandastable.pandastable_wrapper import data_explore_pandastable

#
# # blockers
from py_entitymatching.blocker.attr_equiv_blocker import AttrEquivalenceBlocker
from py_entitymatching.blocker.black_box_blocker import BlackBoxBlocker
from py_entitymatching.blocker.overlap_blocker import OverlapBlocker
from py_entitymatching.blocker.rule_based_blocker import RuleBasedBlocker
from py_entitymatching.blocker.sn_blocker import SortedNeighborhoodBlocker

# # blocker debugger
from py_entitymatching.debugblocker.debugblocker import debug_blocker
from py_entitymatching.debugblocker.backup_debugblocker import backup_debug_blocker

# # blocker combiner
from py_entitymatching.blockercombiner.blockercombiner import combine_blocker_outputs_via_union

# # sampling.rst
from py_entitymatching.sampler.single_table import sample_table

# #
from py_entitymatching.gui.table_gui import view_table, edit_table

# # labeling
from py_entitymatching.labeler.labeler import label_table

# # feature related stuff
from py_entitymatching.feature.simfunctions import *
from py_entitymatching.feature.tokenizers import *
from py_entitymatching.feature.attributeutils import get_attr_corres, get_attr_types
from py_entitymatching.feature.autofeaturegen import get_features, get_features_for_blocking, \
    get_features_for_matching
from py_entitymatching.feature.addfeatures import get_feature_fn, add_feature, \
    add_blackbox_feature, create_feature_table
from py_entitymatching.feature.extractfeatures import extract_feature_vecs

# # matcher related stuff
from py_entitymatching.matcher.matcherutils import split_train_test, impute_table
from py_entitymatching.matcher.dtmatcher import DTMatcher
from py_entitymatching.matcher.linregmatcher import LinRegMatcher
from py_entitymatching.matcher.logregmatcher import LogRegMatcher
from py_entitymatching.matcher.nbmatcher import NBMatcher
from py_entitymatching.matcher.rfmatcher import RFMatcher
from py_entitymatching.matcher.svmmatcher import SVMMatcher
from py_entitymatching.matcher.booleanrulematcher import BooleanRuleMatcher
try:
    from py_entitymatching.matcher.xgboostmatcher import XGBoostMatcher
except ImportError:
    pass

# # matcher selector
from py_entitymatching.matcherselector.mlmatcherselection import select_matcher

# # matcher debugger
from py_entitymatching.debugmatcher.debug_decisiontree_matcher import \
    debug_decisiontree_matcher, visualize_tree
from py_entitymatching.debugmatcher.debug_randomforest_matcher import \
    debug_randomforest_matcher

from py_entitymatching.debugmatcher.debug_gui_decisiontree_matcher import \
    vis_debug_dt, vis_tuple_debug_dt_matcher

from py_entitymatching.debugmatcher.debug_gui_randomforest_matcher import \
    vis_debug_rf, vis_tuple_debug_rf_matcher

# # match trigger
from py_entitymatching.triggers.matchtrigger import MatchTrigger

# # evaluation
from py_entitymatching.evaluation.evaluation import eval_matches, \
    get_false_negatives_as_df, get_false_positives_as_df, print_eval_summary

# # generic helper functions
from py_entitymatching.utils.generic_helper import get_install_path, load_dataset, \
    add_output_attributes

# # pandas helper functions
from py_entitymatching.utils.pandas_helper import filter_rows, project_cols, \
    mutate_col, rename_col, preserve_metadata, drop_cols

# global vars
_block_t = None
_block_s = None
_atypes1 = None
_atypes2 = None
_block_c = None

_match_t = None
_match_s = None
_match_c = None

# GUI related
_viewapp = None
<commit_msg>Incremented version number.<commit_after>
from py_entitymatching.catalog.catalog import Catalog

__version__ = '0.3.2'

_catalog = Catalog.Instance()

# downsampling related methods

from py_entitymatching.sampler.down_sample import down_sample
# # io related methods
#
from py_entitymatching.io.parsers import read_csv_metadata, to_csv_metadata
from py_entitymatching.io.pickles import load_object, load_table, save_object, save_table
#
# import catalog related methods
from py_entitymatching.catalog.catalog_manager import get_property, get_all_properties, \
    set_property, del_property, del_all_properties, init_properties, copy_properties
from py_entitymatching.catalog.catalog_manager import get_catalog, del_catalog, \
    get_catalog_len, show_properties, show_properties_for_id
from py_entitymatching.catalog.catalog_manager import is_property_present_for_df, \
    is_dfinfo_present, is_catalog_empty
from py_entitymatching.catalog.catalog_manager import get_key, set_key, set_fk_ltable,\
    set_fk_rtable, get_ltable, get_rtable, validate_and_set_fk_ltable, \
    validate_and_set_fk_rtable, set_ltable, set_rtable, get_fk_rtable,  \
    get_fk_ltable

# # data exploration wrappers
from py_entitymatching.explorer.openrefine.openrefine_wrapper import data_explore_openrefine
from py_entitymatching.explorer.pandastable.pandastable_wrapper import data_explore_pandastable

#
# # blockers
from py_entitymatching.blocker.attr_equiv_blocker import AttrEquivalenceBlocker
from py_entitymatching.blocker.black_box_blocker import BlackBoxBlocker
from py_entitymatching.blocker.overlap_blocker import OverlapBlocker
from py_entitymatching.blocker.rule_based_blocker import RuleBasedBlocker
from py_entitymatching.blocker.sn_blocker import SortedNeighborhoodBlocker

# # blocker debugger
from py_entitymatching.debugblocker.debugblocker import debug_blocker
from py_entitymatching.debugblocker.backup_debugblocker import backup_debug_blocker

# # blocker combiner
from py_entitymatching.blockercombiner.blockercombiner import combine_blocker_outputs_via_union

# # sampling.rst
from py_entitymatching.sampler.single_table import sample_table

# #
from py_entitymatching.gui.table_gui import view_table, edit_table

# # labeling
from py_entitymatching.labeler.labeler import label_table

# # feature related stuff
from py_entitymatching.feature.simfunctions import *
from py_entitymatching.feature.tokenizers import *
from py_entitymatching.feature.attributeutils import get_attr_corres, get_attr_types
from py_entitymatching.feature.autofeaturegen import get_features, get_features_for_blocking, \
    get_features_for_matching
from py_entitymatching.feature.addfeatures import get_feature_fn, add_feature, \
    add_blackbox_feature, create_feature_table
from py_entitymatching.feature.extractfeatures import extract_feature_vecs

# # matcher related stuff
from py_entitymatching.matcher.matcherutils import split_train_test, impute_table
from py_entitymatching.matcher.dtmatcher import DTMatcher
from py_entitymatching.matcher.linregmatcher import LinRegMatcher
from py_entitymatching.matcher.logregmatcher import LogRegMatcher
from py_entitymatching.matcher.nbmatcher import NBMatcher
from py_entitymatching.matcher.rfmatcher import RFMatcher
from py_entitymatching.matcher.svmmatcher import SVMMatcher
from py_entitymatching.matcher.booleanrulematcher import BooleanRuleMatcher
try:
    from py_entitymatching.matcher.xgboostmatcher import XGBoostMatcher
except ImportError:
    pass

# # matcher selector
from py_entitymatching.matcherselector.mlmatcherselection import select_matcher

# # matcher debugger
from py_entitymatching.debugmatcher.debug_decisiontree_matcher import \
    debug_decisiontree_matcher, visualize_tree
from py_entitymatching.debugmatcher.debug_randomforest_matcher import \
    debug_randomforest_matcher

from py_entitymatching.debugmatcher.debug_gui_decisiontree_matcher import \
    vis_debug_dt, vis_tuple_debug_dt_matcher

from py_entitymatching.debugmatcher.debug_gui_randomforest_matcher import \
    vis_debug_rf, vis_tuple_debug_rf_matcher

# # match trigger
from py_entitymatching.triggers.matchtrigger import MatchTrigger

# # evaluation
from py_entitymatching.evaluation.evaluation import eval_matches, \
    get_false_negatives_as_df, get_false_positives_as_df, print_eval_summary

# # generic helper functions
from py_entitymatching.utils.generic_helper import get_install_path, load_dataset, \
    add_output_attributes

# # pandas helper functions
from py_entitymatching.utils.pandas_helper import filter_rows, project_cols, \
    mutate_col, rename_col, preserve_metadata, drop_cols

# global vars
_block_t = None
_block_s = None
_atypes1 = None
_atypes2 = None
_block_c = None

_match_t = None
_match_s = None
_match_c = None

# GUI related
_viewapp = None
<|endoftext|>


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

import sys, os

# read
from openpyxl import load_workbook
# write
from openpyxl import Workbook

from openpyxl.compat import range
from openpyxl.cell import get_column_letter

#sys.path.append(os.path.abspath(os.path.join('..', 'excely')))
#from excely import blah

# http://stackoverflow.com/questions/419163/what-does-if-name-main-do
if __name__ == '__main__':

    def spelled_word(in_word):
        """
        returns spelled_word
        """
        return in_word + 'foo'

    def read_in_write_out():
        """
        Read from Excel file in.xlsx and write to out.xlsx
        """
        # input file in project data directory
        in_filename = 'data/in.xlsx'
        in_workbook = load_workbook(in_filename)
        # ['Sheet 1']
        print(in_workbook.sheetnames)
        in_sheet = in_workbook.active
        print('in_sheet name ', in_workbook.sheetnames[0])

        out_workbook = Workbook()
        out_sheet = out_workbook.active
        out_sheet.title = "my_sheet"
        # output file in project root directory
        out_filename = 'data/out.xlsx'

        # http://stackoverflow.com/questions/37440855/how-do-i-iterate-through-cells-in-a-specific-column-using-openpyxl-1-6
        first_non_header_row = 2
        # column to read in in_sheet
        in_column = 2
        # column to write in out_sheet
        out_column = 1

        for row in range(first_non_header_row, in_sheet.max_row + 1):
            in_word = in_sheet.cell(row=row, column=in_column).value
            out_word = spelled_word(in_word)
            print(in_word + ', ' + out_word)
            # out_sheet set cell value
            _ = out_sheet.cell(column=out_column, row=row, value=out_word)

        out_workbook.save(filename = out_filename)

    def write_spelling_matches_to_file():
        """
        In both of two files, column b contains misspelled word.

        misspelled.xlsx
        column b isn't sorted, we don't want to sort this file.

        spellings.xlsx
        spellings number of rows is <= misspelled number of rows
        column b is sorted alphabetically ascending
        column d has correct spelling

        Iterate over every row in spellings
        For column b misspelled word, look in misspelled column b for match (one at most).
        If find match, copy spellings column a to misspelled column a.
        If find match, copy spellings column d to misspelled column d.
        """
        # misspelled in project root directory
        misspelled_file_name = 'data/misspelled.xlsx'
        misspelled_workbook = load_workbook(misspelled_file_name)
        misspelled_sheet = misspelled_workbook.active

        spellings_file_name = 'data/spellings.xlsx'
        spellings_workbook = load_workbook(spellings_file_name)
        spellings_sheet = spellings_workbook.active

        # http://stackoverflow.com/questions/37440855/how-do-i-iterate-through-cells-in-a-specific-column-using-openpyxl-1-6
        status_column = 1

        spellings_sheet_first_non_header_row = 2
        spellings_sheet_misspelled_column = 2
        spellings_sheet_correct_column = 4

        misspelled_sheet_first_non_header_row = 2
        misspelled_sheet_misspelled_column = 2
        misspelled_sheet_correct_column = 4

        # spellings_sheet
        for row in range(spellings_sheet_first_non_header_row, spellings_sheet.max_row + 1):
            spellings_sheet_misspelled_word = spellings_sheet.cell(row=row, column=spellings_sheet_misspelled_column).value
            spelled_word = spellings_sheet.cell(row=row, column=spellings_sheet_correct_column).value
            spelled_status = spellings_sheet.cell(row=row, column=status_column).value

            if ((spellings_sheet_misspelled_word is not None)
                and (spelled_word is not None)
                and (spelled_status is not None)):

                write_spelling_to_misspelled(misspelled_sheet,
                                             misspelled_sheet_correct_column,
                                             misspelled_sheet_first_non_header_row,
                                             misspelled_sheet_misspelled_column,
                                             row,
                                             spelled_status,
                                             spelled_word,
                                             spellings_sheet_misspelled_word,
                                             status_column)

        misspelled_workbook.save(filename=misspelled_file_name)

    def write_spelling_to_misspelled(misspelled_sheet, misspelled_sheet_correct_column,
                                     misspelled_sheet_first_non_header_row, misspelled_sheet_misspelled_column, row,
                                     spelled_status, spelled_word, spellings_sheet_misspelled_word, status_column):

        for misspelled_row in range(misspelled_sheet_first_non_header_row, misspelled_sheet.max_row + 1):
            misspelled_sheet_misspelled_word = misspelled_sheet.cell(row=misspelled_row,
                                                                     column=misspelled_sheet_misspelled_column).value
            misspelled_sheet_correct_column_is_empty = (
            (misspelled_sheet.cell(row=misspelled_row, column=misspelled_sheet_correct_column).value is None)
            or (misspelled_sheet.cell(row=misspelled_row, column=misspelled_sheet_correct_column).value == ''))

            if ((misspelled_sheet_misspelled_word is not None)
                and (misspelled_sheet_misspelled_word == spellings_sheet_misspelled_word)
                and misspelled_sheet_correct_column_is_empty):
                print('spellings_row: ' + str(row) + ', misspelled_row: ' + str(misspelled_row))
                print(spellings_sheet_misspelled_word + ', ' + spelled_word)
                misspelled_sheet.cell(row=misspelled_row, column=misspelled_sheet_correct_column).value = spelled_word
                misspelled_sheet.cell(row=misspelled_row, column=status_column).value = spelled_status

#read_in_write_out()

write_spelling_matches_to_file()

<commit_msg>Delete conditional for spelled_status is not None<commit_after>#!/usr/bin/env python

import sys, os

# read
from openpyxl import load_workbook
# write
from openpyxl import Workbook

from openpyxl.compat import range
from openpyxl.cell import get_column_letter

#sys.path.append(os.path.abspath(os.path.join('..', 'excely')))
#from excely import blah

# http://stackoverflow.com/questions/419163/what-does-if-name-main-do
if __name__ == '__main__':

    def spelled_word(in_word):
        """
        returns spelled_word
        """
        return in_word + 'foo'

    def read_in_write_out():
        """
        Read from Excel file in.xlsx and write to out.xlsx
        """
        # input file in project data directory
        in_filename = 'data/in.xlsx'
        in_workbook = load_workbook(in_filename)
        # ['Sheet 1']
        print(in_workbook.sheetnames)
        in_sheet = in_workbook.active
        print('in_sheet name ', in_workbook.sheetnames[0])

        out_workbook = Workbook()
        out_sheet = out_workbook.active
        out_sheet.title = "my_sheet"
        # output file in project root directory
        out_filename = 'data/out.xlsx'

        # http://stackoverflow.com/questions/37440855/how-do-i-iterate-through-cells-in-a-specific-column-using-openpyxl-1-6
        first_non_header_row = 2
        # column to read in in_sheet
        in_column = 2
        # column to write in out_sheet
        out_column = 1

        for row in range(first_non_header_row, in_sheet.max_row + 1):
            in_word = in_sheet.cell(row=row, column=in_column).value
            out_word = spelled_word(in_word)
            print(in_word + ', ' + out_word)
            # out_sheet set cell value
            _ = out_sheet.cell(column=out_column, row=row, value=out_word)

        out_workbook.save(filename = out_filename)

    def write_spelling_matches_to_file():
        """
        In both of two files, column b contains misspelled word.

        misspelled.xlsx
        column b isn't sorted, we don't want to sort this file.

        spellings.xlsx
        spellings number of rows is <= misspelled number of rows
        column b is sorted alphabetically ascending
        column d has correct spelling

        Iterate over every row in spellings
        For column b misspelled word, look in misspelled column b for match (one at most).
        If find match, copy spellings column a to misspelled column a.
        If find match, copy spellings column d to misspelled column d.
        """
        # misspelled in project root directory
        misspelled_file_name = 'data/misspelled.xlsx'
        misspelled_workbook = load_workbook(misspelled_file_name)
        misspelled_sheet = misspelled_workbook.active

        spellings_file_name = 'data/spellings.xlsx'
        spellings_workbook = load_workbook(spellings_file_name)
        spellings_sheet = spellings_workbook.active

        # http://stackoverflow.com/questions/37440855/how-do-i-iterate-through-cells-in-a-specific-column-using-openpyxl-1-6
        status_column = 1

        spellings_sheet_first_non_header_row = 2
        spellings_sheet_misspelled_column = 2
        spellings_sheet_correct_column = 4

        misspelled_sheet_first_non_header_row = 2
        misspelled_sheet_misspelled_column = 2
        misspelled_sheet_correct_column = 4

        # spellings_sheet
        for row in range(spellings_sheet_first_non_header_row, spellings_sheet.max_row + 1):
            spellings_sheet_misspelled_word = spellings_sheet.cell(row=row, column=spellings_sheet_misspelled_column).value
            spelled_word = spellings_sheet.cell(row=row, column=spellings_sheet_correct_column).value
            spelled_status = spellings_sheet.cell(row=row, column=status_column).value

            if ((spellings_sheet_misspelled_word is not None)
                # and (spelled_word is not None)
                # and (spelled_status is not None)):
                and (spelled_word is not None)):

                write_spelling_to_misspelled(misspelled_sheet,
                                             misspelled_sheet_correct_column,
                                             misspelled_sheet_first_non_header_row,
                                             misspelled_sheet_misspelled_column,
                                             row,
                                             spelled_status,
                                             spelled_word,
                                             spellings_sheet_misspelled_word,
                                             status_column)

        misspelled_workbook.save(filename=misspelled_file_name)

    def write_spelling_to_misspelled(misspelled_sheet, misspelled_sheet_correct_column,
                                     misspelled_sheet_first_non_header_row, misspelled_sheet_misspelled_column, row,
                                     spelled_status, spelled_word, spellings_sheet_misspelled_word, status_column):

        for misspelled_row in range(misspelled_sheet_first_non_header_row, misspelled_sheet.max_row + 1):
            misspelled_sheet_misspelled_word = misspelled_sheet.cell(row=misspelled_row,
                                                                     column=misspelled_sheet_misspelled_column).value
            misspelled_sheet_correct_column_is_empty = (
            (misspelled_sheet.cell(row=misspelled_row, column=misspelled_sheet_correct_column).value is None)
            or (misspelled_sheet.cell(row=misspelled_row, column=misspelled_sheet_correct_column).value == ''))

            if ((misspelled_sheet_misspelled_word is not None)
                and (misspelled_sheet_misspelled_word == spellings_sheet_misspelled_word)
                and misspelled_sheet_correct_column_is_empty):
                print('spellings_row: ' + str(row) + ', misspelled_row: ' + str(misspelled_row))
                print(spellings_sheet_misspelled_word + ', ' + spelled_word)
                misspelled_sheet.cell(row=misspelled_row, column=misspelled_sheet_correct_column).value = spelled_word
                misspelled_sheet.cell(row=misspelled_row, column=status_column).value = spelled_status

#read_in_write_out()

write_spelling_matches_to_file()

<|endoftext|>