<commit_before># -*- coding:utf-8 -*-
# Copyright (c) 2010 Hidekazu Ohnishi.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials provided
#      with the distribution.
#
#    * Neither the name of the author nor the names of its contributors
#      may be used to endorse or promote products derived from this
#      software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Lhafile, extension extract lzh file.

Its interface is likey zipfile module is include in regular python environment.
"""
from __future__ import unicode_literals
try:
    from cStringIO import StringIO
except ImportError:
    from io import BytesIO as BytesOrStringIO
import datetime
import os
import os.path
import struct
import sys
import lzhlib

crc16 = lzhlib.crc16

if sys.version_info[0] == 3:
    string_types = (str,)

    def ord(v):
        return v

else:
    string_types = (basestring,)

def unpack(format, data):
    return struct.unpack(str(format), data)

def is_lhafile(filename):
    try:
        LhaFile(filename)
    except:
        return False
    return True

class BadLhafile(Exception):
    pass

class LhaInfo(object):
    __slots__ = (
        'orig_filename',
        'filename',
        'directory',
        'date_time',
        'compress_type',
        'comment',
        'extra',
        'create_system',
        'create_version',
        'extract_version',
        'reserved',
        'flag_bits',
        'volume',
        'internal_attr',
        'external_attr',
        'header_offset',
        'file_offset',
        'CRC',
        'compress_size',
        'file_size',
        )

    def __init__(self):
        self.orig_filename = None
        self.filename = None
        self.directory = None
        self.date_time = None
        self.compress_type = None
        self.comment = None
        self.extra = None
        self.create_system = None
        self.create_version = None
        self.extract_version = None
        self.reserved = None
        self.flag_bits = None
        self.volume = None
        self.internal_attr = None
        self.external_attr = None
        self.header_offset = None
        self.file_offset = None
        self.CRC = None
        self.compress_size = None
        self.file_size = None

    def __str__(self):
        return '%s %s %08X %d %04X' % (self.filename, self.file_size,
                                       self.file_offset, self.compress_size, self.CRC)

    def __getstate__(self):
        return (self.orig_filename, self.filename, self.directory, self.date_time,
                self.compress_type, self.comment, self.extra, self.create_system,
                self.create_version, self.extract_version, self.reserved,
                self.flag_bits, self.volume, self.internal_attr, self.external_attr,
                self.header_offset, self.file_offset, self.CRC, self.compress_size,
                self.file_size)

    def __setstate__(self, state):
        (self.orig_filename, self.filename, self.directory, self.date_time,
         self.compress_type, self.comment, self.extra, self.create_system,
         self.create_version, self.extract_version, self.reserved,
         self.flag_bits, self.volume, self.internal_attr, self.external_attr,
         self.header_offset, self.file_offset, self.CRC, self.compress_size,
         self.file_size) = state

class LhaFile(object):
    """
    """

    SUPPORTED_COMPRESS_TYPE = (b'-lhd-', b'-lh0-', b'-lh5-', b'-lh6-', b'-lh7-')

    def __init__(self, file, mode="r", compression=None, callback=None, args=None):
        """ Open the LZH file """
        self.filelist = []
        self.NameToInfo = {}
        self.mode = key = mode.replace('b', '')[0]

        if isinstance(file, string_types):
            self._fileParsed = 0
            self.filename = file
            modeDict = {'r' : 'rb'}
            self.fp = open(file, modeDict[mode])
        else:
            self._fileParsed = 1
            self.fp = file
            self.filename = getattr(file, 'name', None)

        # Get file size
        initial_pos = self.fp.tell()
        self.fp.seek(0, 2)
        self.filesize = self.fp.tell()
        self.fp.seek(initial_pos, 0)

        if key == 'r':
            self._GetContents(callback=callback,args=args)
        else:
            if not self._fileParsed:
                self.fp.close()
                self.fp = None
            raise RuntimeError("Mode must be 'r'")

    def _GetContents(self, callback=None, args=None):
        try:
            info = self._RealGetContent()
            while info:
                if not info.compress_type in Lhafile.SUPPORTED_COMPRESS_TYPE:
                    raise RuntimeError("Unsupported file is contained %s" % (info.compress_type,))
                if callback:
                    callback(args, self.fp.tell(), self.filesize, info)
                self.filelist.append(info)
                self.NameToInfo[info.filename] = info
                info = self._RealGetContent()
        except BadLhafile as e:
            raise
            if not self._fileParsed:
                self.fp.close()
                self.fp = None

    def _RealGetContent(self):
        fp = self.fp
        filesize = self.filesize
        initial_pos = fp.tell()
        is_read = lambda x: fp.tell() + x < filesize
        if fp.tell() == filesize - 1:
            return None
        if not is_read(26):
            raise BadLhafile("Header is broken")
        # Check OS level
        os_level = ord(fp.read(21)[20])
        fp.seek(-21, 1)
        if not os_level in (0, 1, 2):
            raise BadLhafile("this file level is out of support range %d" % os_level)
        if os_level in (0, 1):
            header_size, checksum, signature, skip_size, \
            file_size, modify_time, reserved , os_level, \
            filename_length = unpack('<BB5sII4sBBB', fp.read(22))
            if is_read(filename_length + 2):
                filename, crc = unpack('<%dsH' % filename_length, fp.read(filename_length + 2))
            if os_level == 0:
                ext_header_size = 0
                pass
            elif os_level == 1:
                extra_data_size = header_size - (5+4+4+2+2+1+1+1+filename_length+2+1+2)
                os_identifier, extra_data, ext_header_size \
                = unpack('<c%dsH' % extra_data_size, fp.read(1 + extra_data_size + 2))
            sum_ext_header_size = 0
            directory = None
            comment = None
            compress_size = skip_size - sum_ext_header_size
        elif os_level == 2:
            header = fp.read(26)
            all_header_size, signature, compress_size, file_size, \
            modify_time, reserved, os_level, crc, os_identifier, \
            ext_header_size = unpack('<H5sIIIBBHBH', header)
            sum_ext_header_size = 0
            directory = None
            comment = None
        while ext_header_size != 0:
            sum_ext_header_size += ext_header_size
            if not is_read(ext_header_size):
                raise BadLhafile("File is broken")
            (ext,) = unpack('<B', fp.read(1))
            if ext == 0x00:
                # Common header
                dummy, ext_header_size \
                       = unpack('<%dsH' % (ext_header_size - 3), fp.read(ext_header_size-1))
            elif ext == 0x01:
                # Filename header
                filename, ext_header_size \
                          = unpack('<%dsH' % (ext_header_size - 3), fp.read(ext_header_size-1))
            elif ext == 0x02:
                # Directory name header
                directory, ext_header_size \
                           = unpack('<%dsH' % (ext_header_size - 3), fp.read(ext_header_size-1))
            elif ext == 0x3F:
                # Comment header
                comment, ext_header_size \
                         = unpack('<%dsH' % (ext_header_size - 3), fp.read(ext_header_size-1))
            elif ext == 0x40:
                # Attribute Header
                if not ext_header_size == 5:
                    raise BadLhafile("file is broken")
                attr, ext_header_size \
                    = unpack('<HH', fp.read(4))
            else:
                # Skip the other
                dummy, ext_header_size \
                       = unpack('<%dsH' % (ext_header_size - 3), fp.read(ext_header_size-1))
        # skip to next header
        file_offset = fp.tell()
        if os_level in (0, 1):
            compress_size = skip_size - sum_ext_header_size
        if not is_read(compress_size):
            raise BadLhafile("Compress data is too short")
        fp.seek(compress_size, 1)
        # modify_time
        if os_level in (0, 1):
            year = (ord(modify_time[3]) >> 1) + 1980
            month = ((ord(modify_time[3]) << 8 | ord(modify_time[2])) >> 5) & 0x0F
            day  = ord(modify_time[2]) & 0x1F
            hour = ord(modify_time[1]) >> 3
            minute = ((ord(modify_time[1]) << 8 | ord(modify_time[0])) >> 5) & 0x2F
            second = (ord(modify_time[0]) & 0x1F) * 2

            #print(os_level, year, month, day, hour, minute, second)
            try:
                date_time = datetime.datetime(year, month, day, hour, minute, second)
            except Exception:
                date_time = datetime.datetime(1970, 1, 1)
            create_time = date_time
        elif os_level in (2,):
            dummy_date = datetime.datetime(1970,1,1)
            date_time = dummy_date.fromtimestamp(modify_time)
            create_time = date_time
        info = LhaInfo()
        # FIXME: hardcoding ISO-8859-1 is not very nice
        filename = filename.decode("ISO-8859-1")
        if directory is None:
            # for lhaplus archive
            #sjisname = unicode(filename, 'cp932')
            #if '\\' in sjisname:
            #    sjispath = [s.encode('cp932') for s in sjisname.split(u'\\')]
            #    filename = os.sep.join(sjispath)
            #    directory = os.sep.join(sjispath[:-1])
            pass
        else:
            #print(repr(directory))
            # FIXME: hardcoding ISO-8859-1 is not very nice
            directory = directory.decode("ISO-8859-1")
            directory = os.sep.join(directory.split('\xff'))
            filename = os.path.join(directory, filename)
        info.directory = directory
        info.filename = filename
        info.compress_size = compress_size
        info.file_size = file_size
        info.CRC = crc
        info.header_offset = initial_pos
        info.file_offset = file_offset
        info.external_attr = None
        info.internal_attr = None
        info.reserved = 0
        info.comment = comment
        info.compress_type = signature
        info.date_time = date_time

        if "\x00" in info.filename:
            info.filename, info.comment = info.filename.split("\x00")

        return info

    def lhaname(self):
        return self.filename

    def namelist(self):
        if self.filelist:
            return [d.filename for d in self.filelist \
                    if d.compress_type in Lhafile.SUPPORTED_COMPRESS_TYPE]
        return None

    def infolist(self):
        return self.filelist

    def read(self, name):
        """Return file bytes (as a string) for 'name'. """
        if not self.fp:
            raise RuntimeError("Attempt to read LZH archive that was already closed")
        info = self.NameToInfo[name]
        if info.compress_type in Lhafile.SUPPORTED_COMPRESS_TYPE:
            self.fp.seek(info.file_offset)
            fin = BytesOrStringIO(self.fp.read(info.compress_size))
            fout = BytesOrStringIO()
            try:
                session = lzhlib.LZHDecodeSession(fin, fout, info)
                while not session.do_next():
                    pass
                outsize = session.output_pos
                crc = session.crc16
            except Exception as e:
                raise e
            if outsize != info.file_size:
                raise BadLhafile("%s output_size is not matched %d/%d %s" % \
                    (name, outsize, info.file_size, info.compress_type))
            if crc != info.CRC:
                raise BadLhafile("crc is not matched")

            fout.seek(0)
            bytes = fout.read()
        elif info.commpress_type == '-lhd-':
            raise RuntimeError("name is directory")
        else:
            raise RuntimeError("Unsupport format")
        return bytes

Lhafile = LhaFile
<commit_msg>Fix: both branches should return a module under the same name.<commit_after># -*- coding:utf-8 -*-
# Copyright (c) 2010 Hidekazu Ohnishi.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials provided
#      with the distribution.
#
#    * Neither the name of the author nor the names of its contributors
#      may be used to endorse or promote products derived from this
#      software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Lhafile, extension extract lzh file.

Its interface is likey zipfile module is include in regular python environment.
"""
from __future__ import unicode_literals
try:
    from cStringIO import BytesOrStringIO
except ImportError:
    from io import BytesIO as BytesOrStringIO
import datetime
import os
import os.path
import struct
import sys
import lzhlib

crc16 = lzhlib.crc16

if sys.version_info[0] == 3:
    string_types = (str,)

    def ord(v):
        return v

else:
    string_types = (basestring,)

def unpack(format, data):
    return struct.unpack(str(format), data)

def is_lhafile(filename):
    try:
        LhaFile(filename)
    except:
        return False
    return True

class BadLhafile(Exception):
    pass

class LhaInfo(object):
    __slots__ = (
        'orig_filename',
        'filename',
        'directory',
        'date_time',
        'compress_type',
        'comment',
        'extra',
        'create_system',
        'create_version',
        'extract_version',
        'reserved',
        'flag_bits',
        'volume',
        'internal_attr',
        'external_attr',
        'header_offset',
        'file_offset',
        'CRC',
        'compress_size',
        'file_size',
        )

    def __init__(self):
        self.orig_filename = None
        self.filename = None
        self.directory = None
        self.date_time = None
        self.compress_type = None
        self.comment = None
        self.extra = None
        self.create_system = None
        self.create_version = None
        self.extract_version = None
        self.reserved = None
        self.flag_bits = None
        self.volume = None
        self.internal_attr = None
        self.external_attr = None
        self.header_offset = None
        self.file_offset = None
        self.CRC = None
        self.compress_size = None
        self.file_size = None

    def __str__(self):
        return '%s %s %08X %d %04X' % (self.filename, self.file_size,
                                       self.file_offset, self.compress_size, self.CRC)

    def __getstate__(self):
        return (self.orig_filename, self.filename, self.directory, self.date_time,
                self.compress_type, self.comment, self.extra, self.create_system,
                self.create_version, self.extract_version, self.reserved,
                self.flag_bits, self.volume, self.internal_attr, self.external_attr,
                self.header_offset, self.file_offset, self.CRC, self.compress_size,
                self.file_size)

    def __setstate__(self, state):
        (self.orig_filename, self.filename, self.directory, self.date_time,
         self.compress_type, self.comment, self.extra, self.create_system,
         self.create_version, self.extract_version, self.reserved,
         self.flag_bits, self.volume, self.internal_attr, self.external_attr,
         self.header_offset, self.file_offset, self.CRC, self.compress_size,
         self.file_size) = state

class LhaFile(object):
    """
    """

    SUPPORTED_COMPRESS_TYPE = (b'-lhd-', b'-lh0-', b'-lh5-', b'-lh6-', b'-lh7-')

    def __init__(self, file, mode="r", compression=None, callback=None, args=None):
        """ Open the LZH file """
        self.filelist = []
        self.NameToInfo = {}
        self.mode = key = mode.replace('b', '')[0]

        if isinstance(file, string_types):
            self._fileParsed = 0
            self.filename = file
            modeDict = {'r' : 'rb'}
            self.fp = open(file, modeDict[mode])
        else:
            self._fileParsed = 1
            self.fp = file
            self.filename = getattr(file, 'name', None)

        # Get file size
        initial_pos = self.fp.tell()
        self.fp.seek(0, 2)
        self.filesize = self.fp.tell()
        self.fp.seek(initial_pos, 0)

        if key == 'r':
            self._GetContents(callback=callback,args=args)
        else:
            if not self._fileParsed:
                self.fp.close()
                self.fp = None
            raise RuntimeError("Mode must be 'r'")

    def _GetContents(self, callback=None, args=None):
        try:
            info = self._RealGetContent()
            while info:
                if not info.compress_type in Lhafile.SUPPORTED_COMPRESS_TYPE:
                    raise RuntimeError("Unsupported file is contained %s" % (info.compress_type,))
                if callback:
                    callback(args, self.fp.tell(), self.filesize, info)
                self.filelist.append(info)
                self.NameToInfo[info.filename] = info
                info = self._RealGetContent()
        except BadLhafile as e:
            raise
            if not self._fileParsed:
                self.fp.close()
                self.fp = None

    def _RealGetContent(self):
        fp = self.fp
        filesize = self.filesize
        initial_pos = fp.tell()
        is_read = lambda x: fp.tell() + x < filesize
        if fp.tell() == filesize - 1:
            return None
        if not is_read(26):
            raise BadLhafile("Header is broken")
        # Check OS level
        os_level = ord(fp.read(21)[20])
        fp.seek(-21, 1)
        if not os_level in (0, 1, 2):
            raise BadLhafile("this file level is out of support range %d" % os_level)
        if os_level in (0, 1):
            header_size, checksum, signature, skip_size, \
            file_size, modify_time, reserved , os_level, \
            filename_length = unpack('<BB5sII4sBBB', fp.read(22))
            if is_read(filename_length + 2):
                filename, crc = unpack('<%dsH' % filename_length, fp.read(filename_length + 2))
            if os_level == 0:
                ext_header_size = 0
                pass
            elif os_level == 1:
                extra_data_size = header_size - (5+4+4+2+2+1+1+1+filename_length+2+1+2)
                os_identifier, extra_data, ext_header_size \
                = unpack('<c%dsH' % extra_data_size, fp.read(1 + extra_data_size + 2))
            sum_ext_header_size = 0
            directory = None
            comment = None
            compress_size = skip_size - sum_ext_header_size
        elif os_level == 2:
            header = fp.read(26)
            all_header_size, signature, compress_size, file_size, \
            modify_time, reserved, os_level, crc, os_identifier, \
            ext_header_size = unpack('<H5sIIIBBHBH', header)
            sum_ext_header_size = 0
            directory = None
            comment = None
        while ext_header_size != 0:
            sum_ext_header_size += ext_header_size
            if not is_read(ext_header_size):
                raise BadLhafile("File is broken")
            (ext,) = unpack('<B', fp.read(1))
            if ext == 0x00:
                # Common header
                dummy, ext_header_size \
                       = unpack('<%dsH' % (ext_header_size - 3), fp.read(ext_header_size-1))
            elif ext == 0x01:
                # Filename header
                filename, ext_header_size \
                          = unpack('<%dsH' % (ext_header_size - 3), fp.read(ext_header_size-1))
            elif ext == 0x02:
                # Directory name header
                directory, ext_header_size \
                           = unpack('<%dsH' % (ext_header_size - 3), fp.read(ext_header_size-1))
            elif ext == 0x3F:
                # Comment header
                comment, ext_header_size \
                         = unpack('<%dsH' % (ext_header_size - 3), fp.read(ext_header_size-1))
            elif ext == 0x40:
                # Attribute Header
                if not ext_header_size == 5:
                    raise BadLhafile("file is broken")
                attr, ext_header_size \
                    = unpack('<HH', fp.read(4))
            else:
                # Skip the other
                dummy, ext_header_size \
                       = unpack('<%dsH' % (ext_header_size - 3), fp.read(ext_header_size-1))
        # skip to next header
        file_offset = fp.tell()
        if os_level in (0, 1):
            compress_size = skip_size - sum_ext_header_size
        if not is_read(compress_size):
            raise BadLhafile("Compress data is too short")
        fp.seek(compress_size, 1)
        # modify_time
        if os_level in (0, 1):
            year = (ord(modify_time[3]) >> 1) + 1980
            month = ((ord(modify_time[3]) << 8 | ord(modify_time[2])) >> 5) & 0x0F
            day  = ord(modify_time[2]) & 0x1F
            hour = ord(modify_time[1]) >> 3
            minute = ((ord(modify_time[1]) << 8 | ord(modify_time[0])) >> 5) & 0x2F
            second = (ord(modify_time[0]) & 0x1F) * 2

            #print(os_level, year, month, day, hour, minute, second)
            try:
                date_time = datetime.datetime(year, month, day, hour, minute, second)
            except Exception:
                date_time = datetime.datetime(1970, 1, 1)
            create_time = date_time
        elif os_level in (2,):
            dummy_date = datetime.datetime(1970,1,1)
            date_time = dummy_date.fromtimestamp(modify_time)
            create_time = date_time
        info = LhaInfo()
        # FIXME: hardcoding ISO-8859-1 is not very nice
        filename = filename.decode("ISO-8859-1")
        if directory is None:
            # for lhaplus archive
            #sjisname = unicode(filename, 'cp932')
            #if '\\' in sjisname:
            #    sjispath = [s.encode('cp932') for s in sjisname.split(u'\\')]
            #    filename = os.sep.join(sjispath)
            #    directory = os.sep.join(sjispath[:-1])
            pass
        else:
            #print(repr(directory))
            # FIXME: hardcoding ISO-8859-1 is not very nice
            directory = directory.decode("ISO-8859-1")
            directory = os.sep.join(directory.split('\xff'))
            filename = os.path.join(directory, filename)
        info.directory = directory
        info.filename = filename
        info.compress_size = compress_size
        info.file_size = file_size
        info.CRC = crc
        info.header_offset = initial_pos
        info.file_offset = file_offset
        info.external_attr = None
        info.internal_attr = None
        info.reserved = 0
        info.comment = comment
        info.compress_type = signature
        info.date_time = date_time

        if "\x00" in info.filename:
            info.filename, info.comment = info.filename.split("\x00")

        return info

    def lhaname(self):
        return self.filename

    def namelist(self):
        if self.filelist:
            return [d.filename for d in self.filelist \
                    if d.compress_type in Lhafile.SUPPORTED_COMPRESS_TYPE]
        return None

    def infolist(self):
        return self.filelist

    def read(self, name):
        """Return file bytes (as a string) for 'name'. """
        if not self.fp:
            raise RuntimeError("Attempt to read LZH archive that was already closed")
        info = self.NameToInfo[name]
        if info.compress_type in Lhafile.SUPPORTED_COMPRESS_TYPE:
            self.fp.seek(info.file_offset)
            fin = BytesOrStringIO(self.fp.read(info.compress_size))
            fout = BytesOrStringIO()
            try:
                session = lzhlib.LZHDecodeSession(fin, fout, info)
                while not session.do_next():
                    pass
                outsize = session.output_pos
                crc = session.crc16
            except Exception as e:
                raise e
            if outsize != info.file_size:
                raise BadLhafile("%s output_size is not matched %d/%d %s" % \
                    (name, outsize, info.file_size, info.compress_type))
            if crc != info.CRC:
                raise BadLhafile("crc is not matched")

            fout.seek(0)
            bytes = fout.read()
        elif info.commpress_type == '-lhd-':
            raise RuntimeError("name is directory")
        else:
            raise RuntimeError("Unsupport format")
        return bytes

Lhafile = LhaFile
<|endoftext|>


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

import pyExcelerator
import os, sys

style = pyExcelerator.XFStyle()
style.font.name = "Consolas"

# Multiple language Localizable.strings
class Document:

	def __init__(self, name, path, file):
		self.name = name
		self.path = path
		self.file = file
		# {language_name:{key:text, ...}, ...}
		self.__language_strings = {}
		self.language_names = []
		self.key_names = []

	def non_text_keys_for_language(self, language):
		if not self.__language_strings.has_key(language):
			return self.key_names
		strings = self.__language_strings[language]
		if len(strings) == 0:
			return self.key_names
		keys = []
		for key in self.key_names:
			if not strings.has_key(key):
				keys.append(key)
		return keys

	def strings_for_language(self, language):
		return self.__language_strings[language]

	def string_for_key_and_language(self, key, language):
		if not self.__language_strings.has_key(language) or not self.__language_strings[language].has_key(key):
			return None
		return self.__language_strings[language][key]

	def set_string_with_key_for_language(self, text, key, language):
		if key not in self.key_names:
			self.key_names.append(key)
		if not self.__language_strings.has_key(language):
			self.__language_strings[language] = {key:text}
		else:
			self.__language_strings[language][key] = text

	def save_strings_for_language_at_path(self, language, path):
		if not self.__language_strings.has_key(language):
			return False
		strings = self.__language_strings[language]
		print "writing document {0} ({1} keys) to\n{2}".format(self.name, len(self.key_names), path)
		dir = os.path.dirname(path)
		if not os.path.isdir(dir):
			os.makedirs(dir)
		file = open(path, 'wb')
		for (key, text) in strings.items():
			line = '"' + key + '" = "' + text.replace('"', '\\"') + '";\n'
			file.write(line.encode("utf-8"))
		file.close()
		return True

	def save_on_sheet(self, sheet):
		print "saving sheet " + self.name
		sheet.write(0, 0, self.path + "\n" + self.file, style=style)
		for (keyIndex, key) in enumerate(self.key_names):
			# print "saving key {0} on row {1}".format(key, rowIndex + 1)
			sheet.write(keyIndex + 1, 0, key, style=style)
		for (langIndex, lang) in enumerate(self.language_names):
			colIndex = langIndex + 1
			sheet.write(0, langIndex + 1, lang, style=style)
			for (keyIndex, key) in enumerate(self.key_names):
				rowIndex = keyIndex + 1
				value = self.string_for_key_and_language(key, lang)
				if value is not None:
					sheet.write(rowIndex, colIndex, value, style=style)

class Sheets:
	def __init__(self, excel_path):
		# [LocalizableDocument, ...]
		self.documents = []
		self.excel_path = excel_path
		if len(excel_path) > 0:
			self.load_excel_with_path(excel_path)

	def load_excel_with_path(self, path):
		sheets = pyExcelerator.parse_xls(path)
		for (sheet_name, values) in sheets:
			names = values[(0,0)].split("\n")
			doc = Document(sheet_name, names[0], names[1])

			cell_keys = values.keys()
			language_names = []
			col_index = 1
			while (0, col_index) in cell_keys:
				name = values[(0, col_index)]
				language_names.append(name)
				col_index += 1
			doc.language_names = language_names

			language_keys = []
			row_index = 1
			while (row_index, 0) in cell_keys:
				language_keys.append(values[(row_index, 0)])
				row_index += 1
			doc.key_names = language_keys

			for row_index, col_index in cell_keys:
				if row_index == 0 or col_index == 0:
					continue
				index = (row_index, col_index)
				v = values[index]
				lang = language_names[col_index - 1]
				key = language_keys[row_index - 1]
				doc.set_string_with_key_for_language(v, key, lang)
			print "loaded document '{0}' with {1} keys.".format(doc.name, len(doc.key_names))
			self.documents.append(doc)

	def save_excel(self, path = None):
		if path is None or len(path) == 0:
			path = self.excel_path
		workbook = pyExcelerator.Workbook()
		for doc in self.documents:
			worksheet = workbook.add_sheet(doc.name)
			doc.save_on_sheet(worksheet)
		tmp_path = os.path.dirname(path) + "/_" + os.path.basename(path)
		workbook.save(tmp_path)
		os.unlink(path)
		os.rename(tmp_path, path)

	def save_strings(self, basepath):
		for doc in self.documents:
			for lang in doc.language_names:
				path = basepath + "/" + doc.path + "/" + lang + ".lproj/" + doc.file + ".strings"
				doc.save_strings_for_language_at_path(lang, path)
<commit_msg>Saved strings files would keep same sequence with excel.<commit_after># -*- coding:utf-8 -*-

import pyExcelerator
import os, sys

style = pyExcelerator.XFStyle()
style.font.name = "Consolas"

# Multiple language Localizable.strings
class Document:

	def __init__(self, name, path, file):
		self.name = name
		self.path = path
		self.file = file
		# {language_name:{key:text, ...}, ...}
		self.__language_strings = {}
		self.language_names = []
		self.key_names = []

	def non_text_keys_for_language(self, language):
		if not self.__language_strings.has_key(language):
			return self.key_names
		strings = self.__language_strings[language]
		if len(strings) == 0:
			return self.key_names
		keys = []
		for key in self.key_names:
			if not strings.has_key(key):
				keys.append(key)
		return keys

	def strings_for_language(self, language):
		return self.__language_strings[language]

	def string_for_key_and_language(self, key, language):
		if not self.__language_strings.has_key(language) or not self.__language_strings[language].has_key(key):
			return None
		return self.__language_strings[language][key]

	def set_string_with_key_for_language(self, text, key, language):
		if key not in self.key_names:
			self.key_names.append(key)
		if not self.__language_strings.has_key(language):
			self.__language_strings[language] = {key:text}
		else:
			self.__language_strings[language][key] = text

	def save_strings_for_language_at_path(self, language, path):
		if not self.__language_strings.has_key(language):
			return False
		print "writing document {0} ({1} keys) to\n{2}".format(self.name, len(self.key_names), path)
		dir = os.path.dirname(path)
		if not os.path.isdir(dir):
			os.makedirs(dir)
		file = open(path, 'wb')
		strings = self.__language_strings[language]
		for key in self.key_names:
			text = strings[key]
			line = '"' + key + '" = "' + text.replace('"', '\\"') + '";\n'
			file.write(line.encode("utf-8"))
		file.close()
		return True

	def save_on_sheet(self, sheet):
		print "saving sheet " + self.name
		sheet.write(0, 0, self.path + "\n" + self.file, style=style)
		for (keyIndex, key) in enumerate(self.key_names):
			# print "saving key {0} on row {1}".format(key, rowIndex + 1)
			sheet.write(keyIndex + 1, 0, key, style=style)
		for (langIndex, lang) in enumerate(self.language_names):
			colIndex = langIndex + 1
			sheet.write(0, langIndex + 1, lang, style=style)
			for (keyIndex, key) in enumerate(self.key_names):
				rowIndex = keyIndex + 1
				value = self.string_for_key_and_language(key, lang)
				if value is not None:
					sheet.write(rowIndex, colIndex, value, style=style)

class Sheets:
	def __init__(self, excel_path):
		# [LocalizableDocument, ...]
		self.documents = []
		self.excel_path = excel_path
		if len(excel_path) > 0:
			self.load_excel_with_path(excel_path)

	def load_excel_with_path(self, path):
		sheets = pyExcelerator.parse_xls(path)
		for (sheet_name, values) in sheets:
			names = values[(0,0)].split("\n")
			doc = Document(sheet_name, names[0], names[1])

			cell_keys = values.keys()
			language_names = []
			col_index = 1
			while (0, col_index) in cell_keys:
				name = values[(0, col_index)]
				language_names.append(name)
				col_index += 1
			doc.language_names = language_names

			language_keys = []
			row_index = 1
			while (row_index, 0) in cell_keys:
				language_keys.append(values[(row_index, 0)])
				row_index += 1
			doc.key_names = language_keys

			for row_index, col_index in cell_keys:
				if row_index == 0 or col_index == 0:
					continue
				index = (row_index, col_index)
				v = values[index]
				lang = language_names[col_index - 1]
				key = language_keys[row_index - 1]
				doc.set_string_with_key_for_language(v, key, lang)
			print "loaded document '{0}' with {1} keys.".format(doc.name, len(doc.key_names))
			self.documents.append(doc)

	def save_excel(self, path = None):
		if path is None or len(path) == 0:
			path = self.excel_path
		workbook = pyExcelerator.Workbook()
		for doc in self.documents:
			worksheet = workbook.add_sheet(doc.name)
			doc.save_on_sheet(worksheet)
		tmp_path = os.path.dirname(path) + "/_" + os.path.basename(path)
		workbook.save(tmp_path)
		os.unlink(path)
		os.rename(tmp_path, path)

	def save_strings(self, basepath):
		for doc in self.documents:
			for lang in doc.language_names:
				path = basepath + "/" + doc.path + "/" + lang + ".lproj/" + doc.file + ".strings"
				doc.save_strings_for_language_at_path(lang, path)
<|endoftext|>


<commit_before>from ggame import App, RectangleAsset, ImageAsset, Sprite, LineStyle, Color, Frame, PolygonAsset
from math import sin, cos, radians, pi

SCREEN_WIDTH1 = 640
SCREEN_HEIGHT = 480
SCREEN_WIDTH = 2*SCREEN_WIDTH1
black = Color(0, 1)

red = Color(0xff0000, 1.0)
green = Color(0x00ff00, 1.0)
blue = Color(0x0000ff, 1.0)

thinline = LineStyle(1, black)
white = Color(0xffffff, 1)
gray = Color(0x8c8c8c, 1)
noline = LineStyle(0, black)
thinline1 = LineStyle(1, white)
flashlight= PolygonAsset([(-SCREEN_WIDTH, SCREEN_WIDTH), (-SCREEN_WIDTH, -SCREEN_WIDTH), (SCREEN_WIDTH, -SCREEN_WIDTH), (SCREEN_WIDTH, SCREEN_WIDTH), (10, SCREEN_WIDTH), (10, 0), (30, -60), (-10, -60), (10,0), (10, SCREEN_WIDTH)], noline, black)
Leftside = Sprite(flashlight, (320, 240))

cf = RectangleAsset(20, 30, thinline, gray)
laser = RectangleAsset(5, 10, noline, white)
class MC(Sprite):
    """
    Animated space ship
    """

    def __init__(self, position):
        super().__init__(cf, position)
        SpaceGame.listenKeyEvent("keydown", "d", self.dKey)
        SpaceGame.listenKeyEvent("keydown", "a", self.aKey)
        SpaceGame.listenKeyEvent("keydown", "s", self.sKey)
        SpaceGame.listenKeyEvent("keydown", "w", self.wKey)
        SpaceGame.listenKeyEvent("keydown", "e", self.eKey)
        SpaceGame.listenKeyEvent("keydown", "q", self.qKey)
        self.fxcenter = self.fycenter = 0.5
        self.xvector = cos(self.rotation)
        self.yvector = sin(self.rotation)
    def dKey(self, event):
        self.x += cos(self.rotation)
        self.y -= sin(self.rotation)
    def aKey(self, event):
        self.x -= cos(self.rotation)
        self.y += sin(self.rotation)
    def wKey(self, event):
        self.x -= cos((pi/2)-self.rotation)
        self.y -= sin((pi/2)-self.rotation)
    def sKey(self, event):
        self.x += cos((pi/2)-self.rotation)
        self.y += sin((pi/2)-self.rotation)
    def eKey(self, event):
        self.rotation += .09
    def qKey(self, event):
        self.rotation -= .09
    def step(self):
        if self.x < 0:
            self.x = 0

        elif self.x > SCREEN_WIDTH1:
            self.x = SCREEN_WIDTH1-20

        if self.y < 0:
            self.y = 0

        elif self.y > SCREEN_HEIGHT:
            self.y = SCREEN_HEIGHT-30

class background(Sprite):
    def __init__(self, position):
        super().__init__(flashlight, position)
        self.fxcenter = self.fycenter = 0.5
        SpaceGame.listenKeyEvent("keydown", "d", self.dKey)
        SpaceGame.listenKeyEvent("keydown", "a", self.aKey)
        SpaceGame.listenKeyEvent("keydown", "s", self.sKey)
        SpaceGame.listenKeyEvent("keydown", "w", self.wKey)
        SpaceGame.listenKeyEvent("keydown", "e", self.eKey)
        SpaceGame.listenKeyEvent("keydown", "q", self.qKey)
        self.fxcenter = self.fycenter = 0.5
        self.xvector = cos(self.rotation)
        self.yvector = sin(self.rotation)
    def dKey(self, event):
        self.x += cos(self.rotation)
        self.y -= sin(self.rotation)
    def aKey(self, event):
        self.x -= cos(self.rotation)
        self.y += sin(self.rotation)
    def wKey(self, event):
        self.x -= cos((pi/2)-self.rotation)
        self.y -= sin((pi/2)-self.rotation)
    def sKey(self, event):
        self.x += cos((pi/2)-self.rotation)
        self.y += sin((pi/2)-self.rotation)
    def eKey(self, event):
        self.rotation += .09
    def qKey(self, event):
        self.rotation -= .09
    def step(self):
        if self.x < 0:
            self.x = 0

        elif self.x > SCREEN_WIDTH1-20:
            self.x = SCREEN_WIDTH1-20

        if self.y < 0:
            self.y = 0

        elif self.y > SCREEN_HEIGHT-30:
            self.y = SCREEN_HEIGHT-30

class SpaceGame(App):
    """
    Tutorial4 space game example.
    """
    def __init__(self, width, height):
        super().__init__(width, height)
        black = Color(0, 1)
        noline = LineStyle(0, black)
        bg_asset = RectangleAsset(width, height, noline, white)
        bg = Sprite(bg_asset, (0,0))
    def step(self):

        MC1.step()

myapp = SpaceGame(SCREEN_WIDTH1, SCREEN_HEIGHT)
Leftside = background((320, 240))
MC1=MC((320,240))
myapp.run()<commit_msg>Updated from Brython Server: 3/31/2017 2:53:53 PM<commit_after>from ggame import App, RectangleAsset, ImageAsset, Sprite, LineStyle, Color, Frame, PolygonAsset
from math import sin, cos, radians, pi

SCREEN_WIDTH1 = 640
SCREEN_HEIGHT = 480
SCREEN_WIDTH = 2*SCREEN_WIDTH1
black = Color(0, 1)

red = Color(0xff0000, 1.0)
green = Color(0x00ff00, 1.0)
blue = Color(0x0000ff, 1.0)

thinline = LineStyle(1, black)
white = Color(0xffffff, 1)
gray = Color(0x8c8c8c, 1)
noline = LineStyle(0, black)
thinline1 = LineStyle(1, white)
flashlight= PolygonAsset([(-SCREEN_WIDTH, SCREEN_WIDTH), (-SCREEN_WIDTH, -SCREEN_WIDTH), (SCREEN_WIDTH, -SCREEN_WIDTH), (SCREEN_WIDTH, SCREEN_WIDTH), (10, SCREEN_WIDTH), (10, 0), (30, -60), (-10, -60), (10,0), (10, SCREEN_WIDTH)], noline, black)
Leftside = Sprite(flashlight, (320, 240))

cf = RectangleAsset(20, 30, thinline, gray)
laser = RectangleAsset(5, 10, noline, white)
class MC(Sprite):
    """
    Animated space ship
    """

    def __init__(self, position):
        super().__init__(cf, position)
        SpaceGame.listenKeyEvent("keydown", "d", self.dKey)
        SpaceGame.listenKeyEvent("keydown", "a", self.aKey)
        SpaceGame.listenKeyEvent("keydown", "s", self.sKey)
        SpaceGame.listenKeyEvent("keydown", "w", self.wKey)
        SpaceGame.listenKeyEvent("keydown", "e", self.eKey)
        SpaceGame.listenKeyEvent("keydown", "q", self.qKey)
        self.fxcenter = self.fycenter = 0.5
        self.xvector = cos(self.rotation)
        self.yvector = sin(self.rotation)
    def dKey(self, event):
        self.x += cos(self.rotation)
        self.y -= sin(self.rotation)
    def aKey(self, event):
        self.x -= cos(self.rotation)
        self.y += sin(self.rotation)
    def wKey(self, event):
        self.x -= cos((pi/2)-self.rotation)
        self.y -= sin((pi/2)-self.rotation)
    def sKey(self, event):
        self.x += cos((pi/2)-self.rotation)
        self.y += sin((pi/2)-self.rotation)
    def eKey(self, event):
        self.rotation += .09
    def qKey(self, event):
        self.rotation -= .09
    def step(self):
        if self.x < 0:
            self.x = 0

        elif self.x > SCREEN_WIDTH1-20:
            self.x = SCREEN_WIDTH1-20

        if self.y < 0:
            self.y = 0

        elif self.y > SCREEN_HEIGHT-20:
            self.y = SCREEN_HEIGHT-30

class background(Sprite):
    def __init__(self, position):
        super().__init__(flashlight, position)
        self.fxcenter = self.fycenter = 0.5
        SpaceGame.listenKeyEvent("keydown", "d", self.dKey)
        SpaceGame.listenKeyEvent("keydown", "a", self.aKey)
        SpaceGame.listenKeyEvent("keydown", "s", self.sKey)
        SpaceGame.listenKeyEvent("keydown", "w", self.wKey)
        SpaceGame.listenKeyEvent("keydown", "e", self.eKey)
        SpaceGame.listenKeyEvent("keydown", "q", self.qKey)
        self.fxcenter = self.fycenter = 0.5
        self.xvector = cos(self.rotation)
        self.yvector = sin(self.rotation)
    def dKey(self, event):
        self.x += cos(self.rotation)
        self.y -= sin(self.rotation)
    def aKey(self, event):
        self.x -= cos(self.rotation)
        self.y += sin(self.rotation)
    def wKey(self, event):
        self.x -= cos((pi/2)-self.rotation)
        self.y -= sin((pi/2)-self.rotation)
    def sKey(self, event):
        self.x += cos((pi/2)-self.rotation)
        self.y += sin((pi/2)-self.rotation)
    def eKey(self, event):
        self.rotation += .09
    def qKey(self, event):
        self.rotation -= .09
    def step(self):
        if MC1.x < 0:
            self.x = MC1.x

        elif MC1 > SCREEN_WIDTH1-20:
            self.x = MC1.x

        if MC1.y < 0:
            self.y = MC1.y

        elif MC1.y > SCREEN_HEIGHT-30:
            self.y = MC1.y

class SpaceGame(App):
    """
    Tutorial4 space game example.
    """
    def __init__(self, width, height):
        super().__init__(width, height)
        black = Color(0, 1)
        noline = LineStyle(0, black)
        bg_asset = RectangleAsset(width, height, noline, white)
        bg = Sprite(bg_asset, (0,0))
    def step(self):

        MC1.step()

myapp = SpaceGame(SCREEN_WIDTH1, SCREEN_HEIGHT)
Leftside = background((320, 240))
MC1=MC((320,240))
myapp.run()<|endoftext|>


<commit_before>import ast

class Type(object):
	def __eq__(self, other):
		return self.__class__ == other.__class__

class ReprId(object):

	def __hash__(self):
		return hash(repr(self))

	def __eq__(self, other):
		return repr(self) == repr(other)

	def __ne__(self, other):
		return not self.__eq__(other)

class base(ReprId):

	byval = False
	attribs = {}
	methods = {}
	type = Type()

	@property
	def name(self):
		return self.__class__.__name__

	@property
	def ir(self):
		return '%' + self.name

	def __repr__(self):
		return '<type: %s>' % self.__class__.__name__

class trait(ReprId):

	byval = False
	methods = {}
	type = Type()

	@property
	def name(self):
		return self.__class__.__name__

	@property
	def ir(self):
		return '%%s.wrap' % self.name

	def __repr__(self):
		return '<trait: %s>' % self.name

class concrete(base):
	pass

class template(base):

	@property
	def ir(self):
		raise TypeError('not a concrete type')

	def __repr__(self):
		return '<template: %s>' % self.__class__.__name__

	def __getitem__(self, params):

		params = params if isinstance(params, tuple) else (params,)
		name = '%s[%s]' % (self.name, ', '.join(p.name for p in params))
		internal = name.replace('$', '_').replace('.', '_')
		cls = ALL[(self.name, params)] = type(internal, (concrete,), {
			'ir': '%' + self.name + '$' + '.'.join(t.name for t in params),
			'name': name,
			'methods': {},
			'attribs': {},
		})

		trans = {k: v for (k, v) in zip(self.params, params)}
		for k, v in self.attribs.iteritems():
			if isinstance(v[1], Stub):
				cls.attribs[k] = v[0], trans[v[1].name]
			elif isinstance(v[1], WRAPPERS) and isinstance(v[1].over, Stub):
				cls.attribs[k] = v[0], v[1].__class__(trans[v[1].over.name])
			else:
				cls.attribs[k] = v[0], v[1]

		return cls()

class void(base):
	ir = 'void'
	byval = True

class float(base):
	@property
	def ir(self):
		raise TypeError('not a concrete type')

class int(base):
	@property
	def ir(self):
		raise TypeError('not a concrete type')

BASIC = {
	'bool': 'i1',
	'byte': 'i8',
	'i32': 'i32',
	'u32': 'i32',
	'word': 'i64',
	'uword': 'i64',
}

INTEGERS = {
	'byte': (False, 8),
	'i32': (True, 32),
	'u32': (False, 32),
	'word': (True, 64),
	'uword': (False, 64),
}

class module(base):
	def __init__(self, path=None):
		self.path = path
		self.functions = {}

class owner(base):

	def __init__(self, over):
		self.over = over

	@property
	def name(self):
		return '$%s' % self.over.name

	@property
	def ir(self):
		return self.over.ir + '*'

	def __repr__(self):
		return '<type: $%s>' % (self.over.name)

class ref(base):

	def __init__(self, over):
		self.over = over

	@property
	def name(self):
		return '&%s' % self.over.name

	@property
	def ir(self):
		return self.over.ir + '*'

	def __repr__(self):
		return '<type: &%s>' % (self.over.name)

class function(base):

	def __init__(self, rtype, formal):
		self.over = rtype, formal

	def __repr__(self):
		if not self.over[1] or isinstance(self.over[1][0], tuple):
			formal = ['%r' % i[1] for i in self.over[1]]
		else:
			formal = ['%r' % i for i in self.over[1]]
		return '<fun %r <- [%s]>' % (self.over[0], ', '.join(formal))

	@property
	def ir(self):
		raise NotImplementedError

class Stub(object):
	def __init__(self, name):
		self.name = name
	def __repr__(self):
		return '<%s(%r)>' % (self.__class__.__name__, self.name)

def get(t, stubs={}):
	if t is None:
		return void()
	elif isinstance(t, base):
		return t
	elif isinstance(t, basestring) and t[0] == '$':
		return owner(get(t[1:], stubs))
	elif isinstance(t, basestring) and t[0] == '&':
		return ref(get(t[1:], stubs))
	elif isinstance(t, basestring):
		return stubs[t] if t in stubs else ALL[t]()
	elif isinstance(t, ast.Name):
		return stubs[t.name] if t.name in stubs else ALL[t.name]()
	elif isinstance(t, ast.Elem):
		return ALL[t.obj.name](get(t.key, stubs))
	elif isinstance(t, ast.Owner):
		return owner(get(t.value, stubs))
	elif isinstance(t, ast.Ref):
		return ref(get(t.value, stubs))
	else:
		assert False, 'no type %s' % t

ALL = {}
for k in globals().keys():
	obj = globals()[k]
	if type(obj) == type and base in obj.__bases__:
		ALL[k] = globals()[k]

for k, cls in ALL.iteritems():
	for m, mdata in cls.methods.iteritems():
		rtype = get(mdata[1])
		atypes = [(n, get(t)) for (n, t) in mdata[2]]
		cls.methods[m] = (m[0], rtype, atypes)

SINTS = {int()}
UINTS = set()
INTS = set()
FLOATS = {float()}
WRAPPERS = owner, ref
GENERIC = int, float

def add(node):

	if isinstance(node, ast.Trait):
		parent = trait
	elif node.params:
		parent = template
	else:
		parent = base

	if isinstance(node, ast.Trait):
		fields = {'methods': {}}
	else:
		fields = {'methods': {}, 'attribs': {}}

	ALL[node.name.name] = type(node.name.name, (parent,), fields)

def fill(node):

	cls = ALL[node.name.name]
	if node.name.name in BASIC:
		cls.ir = BASIC[node.name.name]
		cls.byval = True

	stubs = {}
	if not isinstance(node, ast.Trait):
		cls.params = tuple(n.name for n in node.params)
		stubs = {n.name: Stub(n.name) for n in node.params}
		for i, (atype, name) in enumerate(node.attribs):
			cls.attribs[name.name] = i, get(atype, stubs)

	for method in node.methods:

		name = method.name.name
		irname = '%s.%s' % (node.name.name, name)
		rtype = void() if method.rtype is None else get(method.rtype, stubs)

		args = []
		for i, arg in enumerate(method.args):
			if not i and arg.name.name == 'self':
				wrapper = owner if name == '__del__' else ref
				args.append(('self', wrapper(get(node.name, stubs))))
			else:
				args.append((arg.name.name, get(arg.type, stubs)))

		cls.methods[name] = irname, rtype, args
		method.irname = irname

	obj = cls()
	if node.name.name in INTEGERS:
		cls.signed, cls.bits = INTEGERS[node.name.name]
		INTS.add(obj)
		if cls.signed:
			SINTS.add(obj)
		else:
			UINTS.add(obj)

	return obj
<commit_msg>Fix ir for traits.<commit_after>import ast

class Type(object):
	def __eq__(self, other):
		return self.__class__ == other.__class__

class ReprId(object):

	def __hash__(self):
		return hash(repr(self))

	def __eq__(self, other):
		return repr(self) == repr(other)

	def __ne__(self, other):
		return not self.__eq__(other)

class base(ReprId):

	byval = False
	attribs = {}
	methods = {}
	type = Type()

	@property
	def name(self):
		return self.__class__.__name__

	@property
	def ir(self):
		return '%' + self.name

	def __repr__(self):
		return '<type: %s>' % self.__class__.__name__

class trait(ReprId):

	byval = False
	methods = {}
	type = Type()

	@property
	def name(self):
		return self.__class__.__name__

	@property
	def ir(self):
		return '%%%s.wrap' % self.name

	def __repr__(self):
		return '<trait: %s>' % self.name

class concrete(base):
	pass

class template(base):

	@property
	def ir(self):
		raise TypeError('not a concrete type')

	def __repr__(self):
		return '<template: %s>' % self.__class__.__name__

	def __getitem__(self, params):

		params = params if isinstance(params, tuple) else (params,)
		name = '%s[%s]' % (self.name, ', '.join(p.name for p in params))
		internal = name.replace('$', '_').replace('.', '_')
		cls = ALL[(self.name, params)] = type(internal, (concrete,), {
			'ir': '%' + self.name + '$' + '.'.join(t.name for t in params),
			'name': name,
			'methods': {},
			'attribs': {},
		})

		trans = {k: v for (k, v) in zip(self.params, params)}
		for k, v in self.attribs.iteritems():
			if isinstance(v[1], Stub):
				cls.attribs[k] = v[0], trans[v[1].name]
			elif isinstance(v[1], WRAPPERS) and isinstance(v[1].over, Stub):
				cls.attribs[k] = v[0], v[1].__class__(trans[v[1].over.name])
			else:
				cls.attribs[k] = v[0], v[1]

		return cls()

class void(base):
	ir = 'void'
	byval = True

class float(base):
	@property
	def ir(self):
		raise TypeError('not a concrete type')

class int(base):
	@property
	def ir(self):
		raise TypeError('not a concrete type')

BASIC = {
	'bool': 'i1',
	'byte': 'i8',
	'i32': 'i32',
	'u32': 'i32',
	'word': 'i64',
	'uword': 'i64',
}

INTEGERS = {
	'byte': (False, 8),
	'i32': (True, 32),
	'u32': (False, 32),
	'word': (True, 64),
	'uword': (False, 64),
}

class module(base):
	def __init__(self, path=None):
		self.path = path
		self.functions = {}

class owner(base):

	def __init__(self, over):
		self.over = over

	@property
	def name(self):
		return '$%s' % self.over.name

	@property
	def ir(self):
		return self.over.ir + '*'

	def __repr__(self):
		return '<type: $%s>' % (self.over.name)

class ref(base):

	def __init__(self, over):
		self.over = over

	@property
	def name(self):
		return '&%s' % self.over.name

	@property
	def ir(self):
		return self.over.ir + '*'

	def __repr__(self):
		return '<type: &%s>' % (self.over.name)

class function(base):

	def __init__(self, rtype, formal):
		self.over = rtype, formal

	def __repr__(self):
		if not self.over[1] or isinstance(self.over[1][0], tuple):
			formal = ['%r' % i[1] for i in self.over[1]]
		else:
			formal = ['%r' % i for i in self.over[1]]
		return '<fun %r <- [%s]>' % (self.over[0], ', '.join(formal))

	@property
	def ir(self):
		raise NotImplementedError

class Stub(object):
	def __init__(self, name):
		self.name = name
	def __repr__(self):
		return '<%s(%r)>' % (self.__class__.__name__, self.name)

def get(t, stubs={}):
	if t is None:
		return void()
	elif isinstance(t, base):
		return t
	elif isinstance(t, basestring) and t[0] == '$':
		return owner(get(t[1:], stubs))
	elif isinstance(t, basestring) and t[0] == '&':
		return ref(get(t[1:], stubs))
	elif isinstance(t, basestring):
		return stubs[t] if t in stubs else ALL[t]()
	elif isinstance(t, ast.Name):
		return stubs[t.name] if t.name in stubs else ALL[t.name]()
	elif isinstance(t, ast.Elem):
		return ALL[t.obj.name](get(t.key, stubs))
	elif isinstance(t, ast.Owner):
		return owner(get(t.value, stubs))
	elif isinstance(t, ast.Ref):
		return ref(get(t.value, stubs))
	else:
		assert False, 'no type %s' % t

ALL = {}
for k in globals().keys():
	obj = globals()[k]
	if type(obj) == type and base in obj.__bases__:
		ALL[k] = globals()[k]

for k, cls in ALL.iteritems():
	for m, mdata in cls.methods.iteritems():
		rtype = get(mdata[1])
		atypes = [(n, get(t)) for (n, t) in mdata[2]]
		cls.methods[m] = (m[0], rtype, atypes)

SINTS = {int()}
UINTS = set()
INTS = set()
FLOATS = {float()}
WRAPPERS = owner, ref
GENERIC = int, float

def add(node):

	if isinstance(node, ast.Trait):
		parent = trait
	elif node.params:
		parent = template
	else:
		parent = base

	if isinstance(node, ast.Trait):
		fields = {'methods': {}}
	else:
		fields = {'methods': {}, 'attribs': {}}

	ALL[node.name.name] = type(node.name.name, (parent,), fields)

def fill(node):

	cls = ALL[node.name.name]
	if node.name.name in BASIC:
		cls.ir = BASIC[node.name.name]
		cls.byval = True

	stubs = {}
	if not isinstance(node, ast.Trait):
		cls.params = tuple(n.name for n in node.params)
		stubs = {n.name: Stub(n.name) for n in node.params}
		for i, (atype, name) in enumerate(node.attribs):
			cls.attribs[name.name] = i, get(atype, stubs)

	for method in node.methods:

		name = method.name.name
		irname = '%s.%s' % (node.name.name, name)
		rtype = void() if method.rtype is None else get(method.rtype, stubs)

		args = []
		for i, arg in enumerate(method.args):
			if not i and arg.name.name == 'self':
				wrapper = owner if name == '__del__' else ref
				args.append(('self', wrapper(get(node.name, stubs))))
			else:
				args.append((arg.name.name, get(arg.type, stubs)))

		cls.methods[name] = irname, rtype, args
		method.irname = irname

	obj = cls()
	if node.name.name in INTEGERS:
		cls.signed, cls.bits = INTEGERS[node.name.name]
		INTS.add(obj)
		if cls.signed:
			SINTS.add(obj)
		else:
			UINTS.add(obj)

	return obj
<|endoftext|>


<commit_before>"""
Author: Bauti Gallino
Credit: Vinzent, Liam S
Background Image: http://img4.wikia.nocookie.net/__cb20140610025745/ravens-talon/images/a/af/Metal_floor_by_goeshadow13-d5zy027.jpg
"""
from ggame import App, Sprite, ImageAsset, Frame
from ggame import SoundAsset, Sound, TextAsset, Color
from ggame import App, Color, LineStyle, Sprite
from ggame import RectangleAsset, CircleAsset, EllipseAsset, PolygonAsset
import math
import random

red = Color(0xff0000, 1.0)
green = Color(0x00ff00, 1.0)
blue = Color(0x0000ff, 1.0)
black = Color(0x000000, 1.0)

thinborder=LineStyle(2, red)
skinnyborder=LineStyle(2, blue)
brickborder=LineStyle(2, black)

def crazy(digit, decimal):
    randomout = round((random.random())*(10**digit), decimal)
    return randomout

class Background(Sprite):
    asset = ImageAsset("images/Metal_floor_by_goeshadow13-d5zy027.jpg")
    height= 100
    width= 100

    def __init__(self, position):
        super().__init__(Background.asset, position)

class player1(Sprite):
    asset = RectangleAsset(130, 20, thinborder, blue)

    def __init__(self, position):
        super().__init__(player1.asset, position)
        self.Movex=0
        self.rectangularCollisionModel()
        BrickBreaker.listenKeyEvent("keydown", "left arrow", self.Left)
        BrickBreaker.listenKeyEvent("keyup", "left arrow", self.Leftstop)
        BrickBreaker.listenKeyEvent("keydown", "right arrow", self.Right)
        BrickBreaker.listenKeyEvent("keyup", "right arrow", self.Rightstop)

    def step(self):
        self.x += self.Movex
        if self.x<30:
            self.x=30
        if self.x>1580:
            self.x=1580

    def Left(self, event):
        self.Movex=-12
    def Leftstop(self, event):
        self.Movex=0
    def Right(self, event):
        self.Movex=12
    def Rightstop(self, event):
        self.Movex=0

class ball(Sprite):
    asset = CircleAsset(10, skinnyborder, green)

    def __init__(self, position):
        super().__init__(ball.asset, position)
        self.avy = 0
        self.avx = 0
        self.circularCollisionModel()
        self.randomx = 0
        self.randomy = 0
        self.fxcenter = self.fycenter = 0.5
        self.randomx = crazy(0, 3)*-6
        if self.randomx<6:
            self.randomx=6
        self.randomy = (crazy(0, 3)*-1)*6
        if self.randomy<4:
            self.randomy=4
        if self.randomy>6:
            self.randomy=6
        self.avx = self.randomx
        self.avy = self.randomy

    def step(self):
        if self.collidingWithSprites(player1):
            self.avy = self.avy*-1
        if self.collidingWithSprites(Brick):
            self.avy = self.avy*-1
        if self.x>1710:
            self.avx = self.avx*-1
        if self.x<30:
            self.avx = self.avx*-1
        if self.y<30:
            self.avy = self.avy*-1
        if self.y>840:
            self.visible=False
        self.x += self.avx
        self.y += self.avy
class Brick(Sprite):
    asset = RectangleAsset(1000, 400, brickborder, red)

    def __init__(self, position):
        super().__init__(Brick.asset, position)
        self.rectangularCollisionModel()
    def step(self):
        if self.visible==True:
            if self.collidingWithSprites(ball):
                self.visible=False

class BrickBreaker(App):
    def __init__(self, width, height):
        super().__init__(width, height)
        for x in range(self.width//Background.width + 1):
            for y in range(self.height//Background.height + 1):
                Background((x*Background.width, y*Background.height))
        Background((0, 0))
        player1((800, 820))
        ball((860, 410))
        Brick((0,0))
    def step(self):
        for ship in self.getSpritesbyClass(player1):
            ship.step()
        for ship in self.getSpritesbyClass(ball):
            ship.step()
        for ship in self.getSpritesbyClass(Brick):
            ship.step()

app = BrickBreaker(0, 0)
app.run()<commit_msg>Updated from Brython Server: 12/13/2016 1:45:50 PM<commit_after>"""
Author: Bauti Gallino
Credit: Vinzent, Liam S
Background Image: http://img4.wikia.nocookie.net/__cb20140610025745/ravens-talon/images/a/af/Metal_floor_by_goeshadow13-d5zy027.jpg
"""
from ggame import App, Sprite, ImageAsset, Frame
from ggame import SoundAsset, Sound, TextAsset, Color
from ggame import App, Color, LineStyle, Sprite
from ggame import RectangleAsset, CircleAsset, EllipseAsset, PolygonAsset
import math
import random

red = Color(0xff0000, 1.0)
green = Color(0x00ff00, 1.0)
blue = Color(0x0000ff, 1.0)
black = Color(0x000000, 1.0)

thinborder=LineStyle(2, red)
skinnyborder=LineStyle(2, blue)
brickborder=LineStyle(2, black)

def crazy(digit, decimal):
    randomout = round((random.random())*(10**digit), decimal)
    return randomout

class Background(Sprite):
    asset = ImageAsset("images/Metal_floor_by_goeshadow13-d5zy027.jpg")
    height= 100
    width= 100

    def __init__(self, position):
        super().__init__(Background.asset, position)

class player1(Sprite):
    asset = RectangleAsset(130, 20, thinborder, blue)

    def __init__(self, position):
        super().__init__(player1.asset, position)
        self.Movex=0
        self.rectangularCollisionModel()
        BrickBreaker.listenKeyEvent("keydown", "left arrow", self.Left)
        BrickBreaker.listenKeyEvent("keyup", "left arrow", self.Leftstop)
        BrickBreaker.listenKeyEvent("keydown", "right arrow", self.Right)
        BrickBreaker.listenKeyEvent("keyup", "right arrow", self.Rightstop)

    def step(self):
        self.x += self.Movex
        if self.x<30:
            self.x=30
        if self.x>1580:
            self.x=1580

    def Left(self, event):
        self.Movex=-12
    def Leftstop(self, event):
        self.Movex=0
    def Right(self, event):
        self.Movex=12
    def Rightstop(self, event):
        self.Movex=0

class ball(Sprite):
    asset = CircleAsset(10, skinnyborder, green)

    def __init__(self, position):
        super().__init__(ball.asset, position)
        self.avy = 0
        self.avx = 0
        self.circularCollisionModel()
        self.randomx = 0
        self.randomy = 0
        self.fxcenter = self.fycenter = 0.5
        self.randomx = crazy(0, 3)*-6
        if self.randomx<6:
            self.randomx=6
        self.randomy = (crazy(0, 3)*-1)*6
        if self.randomy<4:
            self.randomy=4
        if self.randomy>6:
            self.randomy=6
        self.avx = self.randomx
        self.avy = self.randomy

    def step(self):
        if self.collidingWithSprites(player1):
            self.avy = self.avy*-1
        if self.collidingWithSprites(Brick):
            self.avy = self.avy*-1
        if self.x>1710:
            self.avx = self.avx*-1
        if self.x<30:
            self.avx = self.avx*-1
        if self.y<30:
            self.avy = self.avy*-1
        if self.y>840:
            self.visible=False
        self.x += self.avx
        self.y += self.avy
class Brick(Sprite):
    asset = RectangleAsset(1000, 400, brickborder, red)

    def __init__(self, position):
        super().__init__(Brick.asset, position)
        self.rectangularCollisionModel()
    def step(self):
        if self.collidingWithSprites(ball):
            self.visible=False

class BrickBreaker(App):
    def __init__(self, width, height):
        super().__init__(width, height)
        for x in range(self.width//Background.width + 1):
            for y in range(self.height//Background.height + 1):
                Background((x*Background.width, y*Background.height))
        Background((0, 0))
        player1((800, 820))
        ball((860, 410))
        Brick((0,0))
    def step(self):
        for ship in self.getSpritesbyClass(player1):
            ship.step()
        for ship in self.getSpritesbyClass(ball):
            ship.step()
        for ship in self.getSpritesbyClass(Brick):
            ship.step()

app = BrickBreaker(0, 0)
app.run()<|endoftext|>


<commit_before>import random

items = (
	{"name" : "une Fee" , "rate" : 4 , "type" : 0 , "Modifier" : 6},
	{"name" : "un Coeur" , "rate" : 10 , "type" : 0 , "Modifier" : 1},
	{"name" : "une Galette Saucisse" , "rate" : 9 , "type" : 0 , "Modifier" : 2},
	{"name" : "des Abats" , "rate" : 10 , "type" : 0 , "Modifier" : 1},
	{"name" : "des Jelly Babies" , "rate" : 7 , "type" : 0 , "Modifier" : 4},
	{"name" : "une Fleu de Feu" , "rate" : 7 , "type" : 1 , "Modifier" : 1},
	{"name" : "un Arc" , "rate" : 7 , "type" : 1 , "Modifier" : 1},
	{"name" : "une Arbalette" , "rate" : 5 , "type" : 1 , "Modifier" : 3},
	{"name" : "un Morgenstern" , "rate" : 5 , "type" : 1 , "Modifier" : 3},
	{"name" : "une Masse" , "rate" : 7 , "type" : 1 , "Modifier" : 2},
	{"name" : "la Master Sword" , "rate" : 2 , "type" : 1 , "Modifier" : 6},
	{"name" : "un Lance-Pierre" , "rate" : 7 , "type" : 1 , "Modifier" : 1},
	{"name" : "une Catapulte" , "rate" : 3 , "type" : 1 , "Modifier" : 5},
	{"name" : "une Claymore" , "rate" : 5 , "type" : 1 , "Modifier" : 3},
	{"name" : "un Katana" , "rate" : 6 , "type" : 1 , "Modifier" : 2},
	{"name" : "un Tromblon" , "rate" : 6 , "type" : 1 , "Modifier" : 2})

def addRandom():
	item = random.randint(1,100)
	rate = items[0]["rate"]
	index = 0
	while rate < item:
		print rate, item, index
		rate += items[index + 1]["rate"]
		index += 1
	return items[index]
<commit_msg>removed dev line<commit_after>import random

items = (
	{"name" : "une Fee" , "rate" : 4 , "type" : 0 , "Modifier" : 6},
	{"name" : "un Coeur" , "rate" : 10 , "type" : 0 , "Modifier" : 1},
	{"name" : "une Galette Saucisse" , "rate" : 9 , "type" : 0 , "Modifier" : 2},
	{"name" : "des Abats" , "rate" : 10 , "type" : 0 , "Modifier" : 1},
	{"name" : "des Jelly Babies" , "rate" : 7 , "type" : 0 , "Modifier" : 4},
	{"name" : "une Fleu de Feu" , "rate" : 7 , "type" : 1 , "Modifier" : 1},
	{"name" : "un Arc" , "rate" : 7 , "type" : 1 , "Modifier" : 1},
	{"name" : "une Arbalette" , "rate" : 5 , "type" : 1 , "Modifier" : 3},
	{"name" : "un Morgenstern" , "rate" : 5 , "type" : 1 , "Modifier" : 3},
	{"name" : "une Masse" , "rate" : 7 , "type" : 1 , "Modifier" : 2},
	{"name" : "la Master Sword" , "rate" : 2 , "type" : 1 , "Modifier" : 6},
	{"name" : "un Lance-Pierre" , "rate" : 7 , "type" : 1 , "Modifier" : 1},
	{"name" : "une Catapulte" , "rate" : 3 , "type" : 1 , "Modifier" : 5},
	{"name" : "une Claymore" , "rate" : 5 , "type" : 1 , "Modifier" : 3},
	{"name" : "un Katana" , "rate" : 6 , "type" : 1 , "Modifier" : 2},
	{"name" : "un Tromblon" , "rate" : 6 , "type" : 1 , "Modifier" : 2})

def addRandom():
	item = random.randint(1,100)
	rate = items[0]["rate"]
	index = 0
	while rate < item:
		rate += items[index + 1]["rate"]
		index += 1
	return items[index]
<|endoftext|>


<commit_before>import pygame, sys, math
from Player  import *
from Wall import *
from Meatball import *
from specialmeatball import *
class Level():
    def __init__(self, levelFile, tileSize=40):
        self.walls = []
        self.meatballs = []
        self.tileSize = tileSize
        self.player = None
        self.loadLevel(levelFile)

    def unloadLevel(self):
        self.walls = []

    def loadLevel(self, levelFile):
        f = open("rsc/levels/"+levelFile, 'r')
        lines = f.readlines()
        f.close()

        """
        print lines
        print "________________________"

        for line in lines:
            print line
        print "________________________"
        """
        newlines = []
        for line in lines:
            newline = ""
            for c in line:
                if c != '\n':
                    newline += c
            newlines += [newline]

        lines = newlines

        for line in lines:
            print line
        print "________________________"

        for y,line in enumerate(lines):
            for x,c in enumerate(line):
                if c == '#':
                    self.walls += [Wall([x*self.tileSize + self.tileSize/15,
                                        y*self.tileSize + self.tileSize/15],
                                       self.tileSize)
                                  ]

                if c == "b":
                    self.player2 = Player (100,
                                        [x*self.tileSize + self.tileSize/.5,
                                         y*self.tileSize + self.tileSize/.5])
                if c == "p":
                    self.player = Player (5,
                                        [x*self.tileSize + self.tileSize/.5,
                                         y*self.tileSize + self.tileSize/.5])
                if c == "o":
                    self.meatballs += [Meatball("regular.png",
                                          [0,0],
                                          [x*self.tileSize + self.tileSize/2,
                                           y*self.tileSize + self.tileSize/2],
                                          self.tileSize)
                                ]
                if c == "s":
                    self.meatballs += [Specialmeatball("spicy.png",
                                          [0,0],
                                          [x*self.tileSize + self.tileSize/2,
                                           y*self.tileSize + self.tileSize/2],
                                          self.tileSize)
                                ]
                    print "MEAATBALL!!!!"

#Level("level1.lvl")
<commit_msg>Arrg! Im a Pirate<commit_after>import pygame, sys, math
from Player  import *
from Wall import *
from Meatball import *
from specialmeatball import *
class Level():
    def __init__(self, levelFile, tileSize=40):
        self.walls = []
        self.meatballs = []
        self.tileSize = tileSize
        self.player = None
        self.loadLevel(levelFile)

    def unloadLevel(self):
        self.walls = []

    def loadLevel(self, levelFile):
        f = open("rsc/levels/"+levelFile, 'r')
        lines = f.readlines()
        f.close()

        """
        print lines
        print "________________________"

        for line in lines:
            print line
        print "________________________"
        """
        newlines = []
        for line in lines:
            newline = ""
            for c in line:
                if c != '\n':
                    newline += c
            newlines += [newline]

        lines = newlines

        for line in lines:
            print line
        print "________________________"

        for y,line in enumerate(lines):
            for x,c in enumerate(line):
                if c == '#':
                    self.walls += [Wall([x*self.tileSize + self.tileSize/15,
                                        y*self.tileSize + self.tileSize/15],
                                       self.tileSize)
                                  ]

                if c == "b":
                    self.player2 = Player (5,
                                        [x*self.tileSize + self.tileSize/.5,
                                         y*self.tileSize + self.tileSize/.5])
                if c == "p":
                    self.player = Player (5,
                                        [x*self.tileSize + self.tileSize/.5,
                                         y*self.tileSize + self.tileSize/.5])
                if c == "o":
                    self.meatballs += [Meatball("regular.png",
                                          [0,0],
                                          [x*self.tileSize + self.tileSize/2,
                                           y*self.tileSize + self.tileSize/2],
                                          self.tileSize)
                                ]
                if c == "s":
                    self.meatballs += [Specialmeatball("spicy.png",
                                          [0,0],
                                          [x*self.tileSize + self.tileSize/2,
                                           y*self.tileSize + self.tileSize/2],
                                          self.tileSize)
                                ]
                    print "MEAATBALL!!!!"

#Level("level1.lvl")
<|endoftext|>


<commit_before>import os

from django.contrib.gis.geos import MultiPolygon, Polygon, GEOSGeometry

from countries.storage import polyfile_location

def get_polyfile_name_to_file_mapping():
    polyfile_mapping = {}
    for possible_polyfile in os.listdir(polyfile_location):
        if possible_polyfile.endswith('.poly'):
            name = possible_polyfile.split('.poly')[0]
            polyfile_mapping[name] = possible_polyfile
    return polyfile_mapping

def polyfile_to_geos_geometry(relative_polygon_file, simplify_tolerance=None):
    with open(os.path.join(polyfile_location, relative_polygon_file)) as poly_file:
        poly = GEOSGeometry(parse_poly(poly_file.readlines()))
        if simplify_tolerance:
            poly.simplify(tolerance=simplify_tolerance)
    return poly

def parse_poly(lines):
    """
    Python3 adaptation of http://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Python_Parsing_Geodjango

        directly stemming from: http://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Python_Parsing_Geodjango
        Parse an Osmosis polygon filter file.
        Accept a sequence of lines from a polygon file, return a django.contrib.gis.geos.MultiPolygon object.
        http://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Format
        Adapted from http://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Python_Parsing
    """
    in_ring = False
    coords = []

    for (index, line) in enumerate(lines):
        if index == 0:
            # first line is junk.
            continue

        elif in_ring and line.strip() == 'END':
            # we are at the end of a ring, perhaps with more to come.
            in_ring = False

        elif in_ring:
            # we are in a ring and picking up new coordinates.
            ring.append([val for val in map(float, line.split())])  # noqa: this is too complicated for flake to understand that ring will be defined if we reach this point

        elif not in_ring and line.strip() == 'END':
            # we are at the end of the whole polygon.
            break

        elif not in_ring and line.startswith('!'):
            # we are at the start of a polygon part hole.
            coords[-1].append([])
            ring = coords[-1][-1]
            in_ring = True

        elif not in_ring:
            # we are at the start of a polygon part.
            coords.append([[]])
            ring = coords[-1][0]  # noqa: it is in fact used in the next iteration.
            in_ring = True

    return MultiPolygon(*(Polygon(*polycoords) for polycoords in coords))
<commit_msg>simplification on load<commit_after>import os

from django.contrib.gis.geos import MultiPolygon, Polygon, GEOSGeometry

from countries.storage import polyfile_location

def get_polyfile_name_to_file_mapping():
    polyfile_mapping = {}
    for possible_polyfile in os.listdir(polyfile_location):
        if possible_polyfile.endswith('.poly'):
            name = possible_polyfile.split('.poly')[0]
            polyfile_mapping[name] = possible_polyfile
    return polyfile_mapping

def polyfile_to_geos_geometry(relative_polygon_file, simplify_tolerance=None):
    with open(os.path.join(polyfile_location, relative_polygon_file)) as poly_file:
        poly = parse_poly(poly_file.readlines())
        if simplify_tolerance:
            poly = poly.simplify(tolerance=simplify_tolerance, preserve_topology=True)
            # simplifying can lead to a polygon. Assure, it stays a multipolygon.
            if poly and isinstance(poly, Polygon):
                poly = MultiPolygon(poly)
    return GEOSGeometry(poly)

def parse_poly(lines):
    """
    Python3 adaptation of http://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Python_Parsing_Geodjango

        directly stemming from: http://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Python_Parsing_Geodjango
        Parse an Osmosis polygon filter file.
        Accept a sequence of lines from a polygon file, return a django.contrib.gis.geos.MultiPolygon object.
        http://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Format
        Adapted from http://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Python_Parsing
    """
    in_ring = False
    coords = []

    for (index, line) in enumerate(lines):
        if index == 0:
            # first line is junk.
            continue

        elif in_ring and line.strip() == 'END':
            # we are at the end of a ring, perhaps with more to come.
            in_ring = False

        elif in_ring:
            # we are in a ring and picking up new coordinates.
            ring.append([val for val in map(float, line.split())])  # noqa: this is too complicated for flake to understand that ring will be defined if we reach this point

        elif not in_ring and line.strip() == 'END':
            # we are at the end of the whole polygon.
            break

        elif not in_ring and line.startswith('!'):
            # we are at the start of a polygon part hole.
            coords[-1].append([])
            ring = coords[-1][-1]
            in_ring = True

        elif not in_ring:
            # we are at the start of a polygon part.
            coords.append([[]])
            ring = coords[-1][0]  # noqa: it is in fact used in the next iteration.
            in_ring = True

    return MultiPolygon(*(Polygon(*polycoords) for polycoords in coords))
<|endoftext|>


<commit_before>"""
Functions for reading a file or file-like object as a series of entries
delimited by a given string or regex.  Each function returns a generator of the
entries, optionally with the delimiters included.  The file-like object may
produce either bytes or characters; the type of the separator should match, and
the same type will be returned.

The three functions differ in how a delimiter immediately at the beginning or
end of the file is treated, as well as in how retained delimiters are returned:

``read_preceded`` is for delimiters that mark the beginning of an entry; when
retained, the delimiters are placed at the beginning of each entry (except
possibly the first).  A delimiter at the beginning of a file does not produce
an entry for the empty string before it, and a delimiter at the end of a file
indicates an empty entry.

``read_separated`` is for delimiters that separate one entry from another; when
retained, the delimiters are included in the returned generator as separate
elements.  A delimiter at the start or end of a file produces an entry for the
empty string before or after it.

``read_terminated`` is for delimiters that mark the end of an entry; when
retained, the delimiters are placed at the end of each entry (except possibly
the last).  A delimiter at the start of a file indicates an empty entry, and a
delimiter at the end of a file does not produce an entry for the empty string
after it.
"""

__version__      = '1.0.0'
__author__       = 'John Thorvald Wodder II'
__author_email__ = 'linesep@varonathe.org'
__license__      = 'MIT'
__url__          = 'https://github.com/jwodder/linesep'

import re

def read_preceded(fp, sep, retain=True, size=512):
    """
    Read from a file-like object ``fp`` containing entries starting
    with/preceded by the string or compiled regex ``sep`` and return a
    generator of the entries.  An empty file will always produce an empty
    generator.

    If ``retain`` is `True`, each entry (except possibly the first) will have
    its leading delimiter prepended to it.  If ``sep`` is a compiled regex, the
    delimiter will be the string matched by the regex at that point; any
    capturing subgroups will be ignored.

    If ``retain`` is `False`, leading delimiters will not be present in the
    output entries.

    ``size`` specifies how many bytes or characters to read from ``fp`` at a
    time.
    """
    # Omits empty leading entry
    entries = read_separated(fp, sep, retain=retain, size=size)
    e = next(entries)
    if e:
        yield e
    if retain:
        entries = _join_pairs(entries)
    for e in entries:
        yield e

def read_separated(fp, sep, retain=True, size=512):
    """
    Read from a file-like object ``fp`` containing entries separated by the
    string or compiled regex ``sep`` and return a generator of the entries.  An
    empty file will always produce a generator with one element, the empty
    string.

    If ``retain`` is `True`, the separators will be included in the output: the
    generator will contain an odd number of elements, alternating between
    entries and separators, starting with a (possibly empty) entry.  If ``sep``
    is a compiled regex, the separator elements will be the strings matched by
    the regex; any capturing subgroups will be ignored.

    If ``retain`` is `False`, separators will not be present in the output.

    ``size`` specifies how many bytes or characters to read from ``fp`` at a
    time.
    """
    if not isinstance(sep, re.RegexObject):
        sep = re.compile(re.escape(sep))
    buff = ''
    for chunk in iter(lambda: fp.read(size), ''):
        buff += chunk
        lastend = 0
        for m in sep.finditer(buff):
            yield buff[lastend:m.start()]
            if retain:
                yield m.group()
            lastend = m.end()
        buff = buff[lastend:]
    yield buff

def read_terminated(fp, sep, retain=True, size=512):
    """
    Read from a file-like object ``fp`` containing entries terminated by the
    string or compiled regex ``sep`` and return a generator of the entries.  An
    empty file will always produce an empty generator.

    If ``retain`` is `True`, each entry (except possibly the last) will have
    its terminator appended to it.  If ``sep`` is a compiled regex, the
    terminator will be the string matched by the regex at that point; any
    capturing subgroups will be ignored.

    If ``retain`` is `False`, terminators will not be present in the output
    entries.

    ``size`` specifies how many bytes or characters to read from ``fp`` at a
    time.
    """
    # Omits empty trailing entry
    prev = None
    entries = read_separated(fp, sep, retain=retain, size=size)
    if retain:
        entries = _join_pairs(entries)
    for e in entries:
        if prev is not None:
            yield prev
        prev = e
    if prev:
        yield prev

def _join_pairs(iterable):
    """
    Concatenate each pair of consecutive strings in ``iterable``.  If there are
    an odd number of items in ``iterable``, the last one will be returned
    unmodified.
    """
    while True:
        a = next(iterable)
        try:
            b = next(iterable)
        except StopIteration:
            yield a
            return
        else:
            yield a + b
<commit_msg>Fix regex object type test<commit_after>"""
Functions for reading a file or file-like object as a series of entries
delimited by a given string or regex.  Each function returns a generator of the
entries, optionally with the delimiters included.  The file-like object may
produce either bytes or characters; the type of the separator should match, and
the same type will be returned.

The three functions differ in how a delimiter immediately at the beginning or
end of the file is treated, as well as in how retained delimiters are returned:

``read_preceded`` is for delimiters that mark the beginning of an entry; when
retained, the delimiters are placed at the beginning of each entry (except
possibly the first).  A delimiter at the beginning of a file does not produce
an entry for the empty string before it, and a delimiter at the end of a file
indicates an empty entry.

``read_separated`` is for delimiters that separate one entry from another; when
retained, the delimiters are included in the returned generator as separate
elements.  A delimiter at the start or end of a file produces an entry for the
empty string before or after it.

``read_terminated`` is for delimiters that mark the end of an entry; when
retained, the delimiters are placed at the end of each entry (except possibly
the last).  A delimiter at the start of a file indicates an empty entry, and a
delimiter at the end of a file does not produce an entry for the empty string
after it.
"""

__version__      = '1.0.0'
__author__       = 'John Thorvald Wodder II'
__author_email__ = 'linesep@varonathe.org'
__license__      = 'MIT'
__url__          = 'https://github.com/jwodder/linesep'

import re

def read_preceded(fp, sep, retain=True, size=512):
    """
    Read from a file-like object ``fp`` containing entries starting
    with/preceded by the string or compiled regex ``sep`` and return a
    generator of the entries.  An empty file will always produce an empty
    generator.

    If ``retain`` is `True`, each entry (except possibly the first) will have
    its leading delimiter prepended to it.  If ``sep`` is a compiled regex, the
    delimiter will be the string matched by the regex at that point; any
    capturing subgroups will be ignored.

    If ``retain`` is `False`, leading delimiters will not be present in the
    output entries.

    ``size`` specifies how many bytes or characters to read from ``fp`` at a
    time.
    """
    # Omits empty leading entry
    entries = read_separated(fp, sep, retain=retain, size=size)
    e = next(entries)
    if e:
        yield e
    if retain:
        entries = _join_pairs(entries)
    for e in entries:
        yield e

def read_separated(fp, sep, retain=True, size=512):
    """
    Read from a file-like object ``fp`` containing entries separated by the
    string or compiled regex ``sep`` and return a generator of the entries.  An
    empty file will always produce a generator with one element, the empty
    string.

    If ``retain`` is `True`, the separators will be included in the output: the
    generator will contain an odd number of elements, alternating between
    entries and separators, starting with a (possibly empty) entry.  If ``sep``
    is a compiled regex, the separator elements will be the strings matched by
    the regex; any capturing subgroups will be ignored.

    If ``retain`` is `False`, separators will not be present in the output.

    ``size`` specifies how many bytes or characters to read from ``fp`` at a
    time.
    """
    # http://stackoverflow.com/a/7054512/744178
    if not hasattr(sep, 'match'):
        sep = re.compile(re.escape(sep))
    buff = ''
    for chunk in iter(lambda: fp.read(size), ''):
        buff += chunk
        lastend = 0
        for m in sep.finditer(buff):
            yield buff[lastend:m.start()]
            if retain:
                yield m.group()
            lastend = m.end()
        buff = buff[lastend:]
    yield buff

def read_terminated(fp, sep, retain=True, size=512):
    """
    Read from a file-like object ``fp`` containing entries terminated by the
    string or compiled regex ``sep`` and return a generator of the entries.  An
    empty file will always produce an empty generator.

    If ``retain`` is `True`, each entry (except possibly the last) will have
    its terminator appended to it.  If ``sep`` is a compiled regex, the
    terminator will be the string matched by the regex at that point; any
    capturing subgroups will be ignored.

    If ``retain`` is `False`, terminators will not be present in the output
    entries.

    ``size`` specifies how many bytes or characters to read from ``fp`` at a
    time.
    """
    # Omits empty trailing entry
    prev = None
    entries = read_separated(fp, sep, retain=retain, size=size)
    if retain:
        entries = _join_pairs(entries)
    for e in entries:
        if prev is not None:
            yield prev
        prev = e
    if prev:
        yield prev

def _join_pairs(iterable):
    """
    Concatenate each pair of consecutive strings in ``iterable``.  If there are
    an odd number of items in ``iterable``, the last one will be returned
    unmodified.
    """
    while True:
        a = next(iterable)
        try:
            b = next(iterable)
        except StopIteration:
            yield a
            return
        else:
            yield a + b
<|endoftext|>