<commit_before># ----------------------------------------------------------------------------
# cocos2d
# Copyright (c) 2008 Daniel Moisset, Ricardo Quesada, Rayentray Tappa, Lucio Torre
# 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 cocos2d 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 OWNER 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.
# ----------------------------------------------------------------------------
'''Grid data structure'''

__docformat__ = 'restructuredtext'

import pyglet
from pyglet import image
from pyglet.gl import *
from euclid import Point2, Point3

from director import director
import framegrabber

__all__ = ['GridBase',
           'Grid3D',
           'TiledGrid3D',
            ]

class GridBase(object):
    """
    A Scene that takes two scenes and makes a transition between them
    """
    texture = None

    def __init__(self):
        super(GridBase, self).__init__()
        self._active = False
        self.reuse_grid = 0     #! Number of times that this grid will be reused

    def init( self, grid ):
        '''Initializes the grid creating both a vertex_list for an independent-tiled grid
        and creating also a vertex_list_indexed for a "united" (non independent tile) grid.

        :Parameters:
            `grid` : euclid.Point2
                size of a 2D grid
        '''

        #: size of the grid. (rows, columns)
        self.grid = grid

        width, height = director.get_window_size()

        if self.texture is None:
            self.texture = image.Texture.create_for_size(
                    GL_TEXTURE_2D, width,
                    height, GL_RGBA)

        self.grabber = framegrabber.TextureGrabber()
        self.grabber.grab(self.texture)

        #: x pixels between each vertex (float)
        self.x_step = width / self.grid.x
        #: y pixels between each vertex (float)
        self.y_step = height / self.grid.y

        self._init()

    def before_draw( self ):
        '''Binds the framebuffer to a texture
        and set a 2d projection before binding
        to prevent calculating a new texture
        '''

        self._set_2d_projection()

        # capture before drawing
        self.grabber.before_render(self.texture)

    def after_draw( self, camera ):
        '''Called by CocosNode when the texture is already grabbed.
        The FrameBuffer will be unbound and the texture will be drawn

        :Parameters:
            `camera` : `Camera`
                The target's camera object.
        '''

        # capture after drawing
        self.grabber.after_render(self.texture)

        # after unbinding
        # set a 3d projection
        self._set_3d_projection()

        # and center the camera
        camera.locate( force=True )

        # blit
        glEnable(self.texture.target)
        glBindTexture(self.texture.target, self.texture.id)

        glPushAttrib(GL_COLOR_BUFFER_BIT)

        self._blit()

        glPopAttrib()
        glDisable(self.texture.target)

    def _set_active(self, bool):
        if self._active == bool:
            return
        self._active = bool
        if self._active == True:
            pass
        elif self._active == False:
            self.vertex_list.delete()
            # to restore the camera to default position
            director.set_projection()
        else:
            raise Exception("Invalid value for GridBase.active")

    def _get_active(self):
        return self._active

    active = property(_get_active, _set_active,
                      doc='''Determines whether the grid is active or not
                     :type: bool
                     ''')
    def _init(self):
        raise NotImplementedError('abstract')

    def _blit(self):
        raise NotImplementedError('abstract')

    def _on_resize(self):
        raise NotImplementedError('abstract')

    @classmethod
    def _set_3d_projection(cls):
        width, height = director.window.width, director.window.height

        glViewport(0, 0, width, height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(60, 1.0*width/height, 0.1, 3000.0)
        glMatrixMode(GL_MODELVIEW)

    @classmethod
    def _set_2d_projection(cls):

#        director.set_2d_projection()
        width, height = director.get_window_size()
        glLoadIdentity()
        glViewport(0, 0, width, height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(0, width, 0, height, -100, 100)
        glMatrixMode(GL_MODELVIEW)

class Grid3D(GridBase):
    '''`Grid3D` is a 3D grid implementation. Each vertex has 3 dimensions: x,y,z

    The vindexed ertex array will be built with::

        self.vertex_list.vertices: x,y,z (floats)
        self.vertex_list.tex_coords: x,y,z (floats)
        self.vertex_list.colors: RGBA, with values from 0 - 255
    '''

    def _init( self ):
        # calculate vertex, textures depending on screen size
        idx_pts, ver_pts_idx, tex_pts_idx = self._calculate_vertex_points()

        #: indexed vertex array that can be transformed.
        #: it has these attributes:
        #:
        #:    - vertices
        #:    - colors
        #:    - tex_coords
        #:
        #: for more information refer to pyglet's documentation: pyglet.graphics.vertex_list_indexed
        self.vertex_list = pyglet.graphics.vertex_list_indexed( (self.grid.x+1) * (self.grid.y+1),
                            idx_pts, "t2f", "v3f/stream","c4B")

        #: original vertex array of the grid. (read-only)
        self.vertex_points = ver_pts_idx[:]
        self.vertex_list.vertices = ver_pts_idx
        self.vertex_list.tex_coords = tex_pts_idx
        self.vertex_list.colors = (255,255,255,255) * (self.grid.x+1) * (self.grid.y+1)

    def _blit(self ):
        self.vertex_list.draw(pyglet.gl.GL_TRIANGLES)

    def _calculate_vertex_points(self):
        w = float(self.texture.width)
        h = float(self.texture.height)

        index_points = []
        vertex_points_idx = []
        texture_points_idx = []

        for x in xrange(0,self.grid.x+1):
            for y in xrange(0,self.grid.y+1):
                vertex_points_idx += [-1,-1,-1]
                texture_points_idx += [-1,-1]

        for x in xrange(0, self.grid.x):
            for y in xrange(0, self.grid.y):
                x1 = x * self.x_step
                x2 = x1 + self.x_step
                y1 = y * self.y_step
                y2 = y1 + self.y_step

                #  d <-- c
                #        ^
                #        |
                #  a --> b
                a = x * (self.grid.y+1) + y
                b = (x+1) * (self.grid.y+1) + y
                c = (x+1) * (self.grid.y+1) + (y+1)
                d = x * (self.grid.y+1) + (y+1)

                # 2 triangles: a-b-d, b-c-d
                index_points += [ a, b, d, b, c, d]    # triangles

                l1 = ( a*3, b*3, c*3, d*3 )
                l2 = ( Point3(x1,y1,0), Point3(x2,y1,0), Point3(x2,y2,0), Point3(x1,y2,0) )

                #  building the vertex
                for i in xrange( len(l1) ):
                    vertex_points_idx[ l1[i] ] = l2[i].x
                    vertex_points_idx[ l1[i] + 1 ] = l2[i].y
                    vertex_points_idx[ l1[i] + 2 ] = l2[i].z

                # building the texels
                tex1 = ( a*2, b*2, c*2, d*2 )
                tex2 = ( Point2(x1,y1), Point2(x2,y1), Point2(x2,y2), Point2(x1,y2) )

                for i in xrange( len(tex1)):
                    texture_points_idx[ tex1[i] ] = tex2[i].x / w
                    texture_points_idx[ tex1[i] + 1 ] = tex2[i].y / h

        return ( index_points, vertex_points_idx, texture_points_idx )

    def get_vertex( self, x, y):
        '''Get the current vertex coordinate

        :Parameters:
            `x` : int
               x-vertex
            `y` : int
               y-vertex

        :rtype: (float, float, float)
        '''
        idx = (x * (self.grid.y+1) + y) * 3
        x = self.vertex_list.vertices[idx]
        y = self.vertex_list.vertices[idx+1]
        z = self.vertex_list.vertices[idx+2]
        return (x,y,z)

    def get_original_vertex( self, x, y):
        '''Get the original vertex coordinate.
        The original vertices are the ones weren't modified by the current action.

        :Parameters:
            `x` : int
               x-vertex
            `y` : int
               y-vertex

        :rtype: (float, float, float)
        '''
        idx = (x * (self.grid.y+1) + y) * 3
        x = self.vertex_points[idx]
        y = self.vertex_points[idx+1]
        z = self.vertex_points[idx+2]

        return (x,y,z)

    def set_vertex( self, x, y, v):
        '''Set a vertex point is a certain value

        :Parameters:
            `x` : int
               x-vertex
            `y` : int
               y-vertex
            `v` : (float, float, float)
                tuple value for the vertex
        '''
        idx = (x * (self.grid.y+1) + y) * 3
        self.vertex_list.vertices[idx] = int(v[0])
        self.vertex_list.vertices[idx+1] = int(v[1])
        self.vertex_list.vertices[idx+2] = int(v[2])

class TiledGrid3D(GridBase):
    '''`TiledGrid3D` is a 3D grid implementation. It differs from `Grid3D` in that
    the tiles can be separated from the grid.

    The vertex array will be built with::

        self.vertex_list.vertices: x,y,z (floats)
        self.vertex_list.tex_coords: x,y (floats)
        self.vertex_list.colors: RGBA, with values from 0 - 255
    '''
    def _init( self ):
        # calculate vertex, textures depending on screen size
        ver_pts, tex_pts = self._calculate_vertex_points()

        #: vertex array that can be transformed.
        #: it has these attributes:
        #:
        #:    - vertices
        #:    - colors
        #:    - tex_coords
        #:
        #: for more information refer to pyglet's documentation: pyglet.graphics.vertex_list
        self.vertex_list = pyglet.graphics.vertex_list(self.grid.x * self.grid.y * 4,
                            "t2f", "v3f/stream","c4B")
        #: original vertex array of the grid. (read-only)
        self.vertex_points = ver_pts[:]
        self.vertex_list.vertices = ver_pts
        self.vertex_list.tex_coords = tex_pts
        self.vertex_list.colors = (255,255,255,255) * self.grid.x * self.grid.y * 4

    def _blit(self ):
        self.vertex_list.draw(pyglet.gl.GL_QUADS)

    def _calculate_vertex_points(self):
        w = float(self.texture.width)
        h = float(self.texture.height)

        vertex_points = []
        texture_points = []

        for x in xrange(0, self.grid.x):
            for y in xrange(0, self.grid.y):
                x1 = x * self.x_step
                x2 = x1 + self.x_step
                y1 = y * self.y_step
                y2 = y1 + self.y_step

                # Building the tiles' vertex and texture points
                vertex_points += [x1, y1, 0, x2, y1, 0, x2, y2, 0, x1, y2, 0 ]
                texture_points += [x1/w, y1/h, x2/w, y1/h, x2/w, y2/h, x1/w, y2/h]

        # Generates a quad for each tile, to perform tiles effect
        return (vertex_points, texture_points)

    def set_tile(self, x, y, coords):
        '''Set the 4 tile coordinates

        Coordinates positions::

            3 <-- 2
                  ^
                  |
            0 --> 1

        :Parameters:
            `x` : int
                x coodinate of the tile
            `y` : int
                y coordinate of the tile
            `coords` : [ float, float, float, float, float, float, float, float, float, float, float, float ]
                The 4 coordinates in the format (x0, y0, z0, x1, y1, z1,..., x3, y3, z3)
        '''
        idx = (self.grid.y * x + y) * 4 * 3
        self.vertex_list.vertices[idx:idx+12] = coords

    def get_original_tile(self, x, y):
        '''Get the 4-original tile coordinates.

        Coordinates positions::

            3 <-- 2
                  ^
                  |
            0 --> 1

        :Parameters:
            `x` : int
                x coordinate of the tile
            `y` : int
                y coordinate of the tile

        :rtype: [ float, float, float, float, float, float, float, float, float, float, float, float ]
        :returns: The 4 coordinates with the following order: x0, y0, z0, x1, y1, z1,...,x3, y3, z3
        '''
        idx = (self.grid.y * x + y) * 4 * 3
        return self.vertex_points[idx:idx+12]

    def get_tile(self, x, y):
        '''Get the current tile coordinates.

        Coordinates positions::

            3 <-- 2
                  ^
                  |
            0 --> 1

        :Parameters:
            `x` : int
                x coordinate of the tile
            `y` : int
                y coordinate of the tile

        :rtype: [ float, float, float, float, float, float, float, float, float, float, float, float ]
        :returns: The 4 coordinates with the following order: x0, y0, z0, x1, y1, z1,...,x3, y3, z3
        '''
        idx = (self.grid.y * x + y) * 4 * 3
        return self.vertex_list.vertices[idx:idx+12]
<commit_msg>fix issue 152: tilegid actions view wrong when resized<commit_after># ----------------------------------------------------------------------------
# cocos2d
# Copyright (c) 2008 Daniel Moisset, Ricardo Quesada, Rayentray Tappa, Lucio Torre
# 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 cocos2d 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 OWNER 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.
# ----------------------------------------------------------------------------
'''Grid data structure'''

__docformat__ = 'restructuredtext'

import pyglet
from pyglet import image
from pyglet.gl import *
from euclid import Point2, Point3

from director import director
import framegrabber

__all__ = ['GridBase',
           'Grid3D',
           'TiledGrid3D',
            ]

class GridBase(object):
    """
    A Scene that takes two scenes and makes a transition between them
    """
    texture = None

    def __init__(self):
        super(GridBase, self).__init__()
        self._active = False
        self.reuse_grid = 0     #! Number of times that this grid will be reused

    def init( self, grid ):
        '''Initializes the grid creating both a vertex_list for an independent-tiled grid
        and creating also a vertex_list_indexed for a "united" (non independent tile) grid.

        :Parameters:
            `grid` : euclid.Point2
                size of a 2D grid
        '''

        #: size of the grid. (rows, columns)
        self.grid = grid

        width, height = director.get_window_size()

        if self.texture is None:
            self.texture = image.Texture.create_for_size(
                    GL_TEXTURE_2D, width,
                    height, GL_RGBA)

        self.grabber = framegrabber.TextureGrabber()
        self.grabber.grab(self.texture)

        #: x pixels between each vertex (float)
        self.x_step = width / self.grid.x
        #: y pixels between each vertex (float)
        self.y_step = height / self.grid.y

        self._init()

    def before_draw( self ):
        '''Binds the framebuffer to a texture
        and set a 2d projection before binding
        to prevent calculating a new texture
        '''

        self._set_2d_projection()

        # capture before drawing
        self.grabber.before_render(self.texture)

    def after_draw( self, camera ):
        '''Called by CocosNode when the texture is already grabbed.
        The FrameBuffer will be unbound and the texture will be drawn

        :Parameters:
            `camera` : `Camera`
                The target's camera object.
        '''

        # capture after drawing
        self.grabber.after_render(self.texture)

        # after unbinding
        # set a 3d projection
        self._set_3d_projection()

        # and center the camera
        camera.locate( force=True )

        # blit
        glEnable(self.texture.target)
        glBindTexture(self.texture.target, self.texture.id)

        glPushAttrib(GL_COLOR_BUFFER_BIT)

        self._blit()

        glPopAttrib()
        glDisable(self.texture.target)

    def _set_active(self, bool):
        if self._active == bool:
            return
        self._active = bool
        if self._active == True:
            pass
        elif self._active == False:
            self.vertex_list.delete()
            # to restore the camera to default position
            director.set_projection()
        else:
            raise Exception("Invalid value for GridBase.active")

    def _get_active(self):
        return self._active

    active = property(_get_active, _set_active,
                      doc='''Determines whether the grid is active or not
                     :type: bool
                     ''')
    def _init(self):
        raise NotImplementedError('abstract')

    def _blit(self):
        raise NotImplementedError('abstract')

    def _on_resize(self):
        raise NotImplementedError('abstract')

    @classmethod
    def _set_3d_projection(cls):
        glViewport(director._offset_x, director._offset_y, director._usable_width, director._usable_height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(60, 1.0*director._usable_width/director._usable_height, 0.1, 3000.0)
        glMatrixMode(GL_MODELVIEW)

    @classmethod
    def _set_2d_projection(cls):

#        director.set_2d_projection()
        width, height = director.get_window_size()
        glLoadIdentity()
        glViewport(0, 0, width, height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(0, width, 0, height, -100, 100)
        glMatrixMode(GL_MODELVIEW)

class Grid3D(GridBase):
    '''`Grid3D` is a 3D grid implementation. Each vertex has 3 dimensions: x,y,z

    The vindexed ertex array will be built with::

        self.vertex_list.vertices: x,y,z (floats)
        self.vertex_list.tex_coords: x,y,z (floats)
        self.vertex_list.colors: RGBA, with values from 0 - 255
    '''

    def _init( self ):
        # calculate vertex, textures depending on screen size
        idx_pts, ver_pts_idx, tex_pts_idx = self._calculate_vertex_points()

        #: indexed vertex array that can be transformed.
        #: it has these attributes:
        #:
        #:    - vertices
        #:    - colors
        #:    - tex_coords
        #:
        #: for more information refer to pyglet's documentation: pyglet.graphics.vertex_list_indexed
        self.vertex_list = pyglet.graphics.vertex_list_indexed( (self.grid.x+1) * (self.grid.y+1),
                            idx_pts, "t2f", "v3f/stream","c4B")

        #: original vertex array of the grid. (read-only)
        self.vertex_points = ver_pts_idx[:]
        self.vertex_list.vertices = ver_pts_idx
        self.vertex_list.tex_coords = tex_pts_idx
        self.vertex_list.colors = (255,255,255,255) * (self.grid.x+1) * (self.grid.y+1)

    def _blit(self ):
        self.vertex_list.draw(pyglet.gl.GL_TRIANGLES)

    def _calculate_vertex_points(self):
        w = float(self.texture.width)
        h = float(self.texture.height)

        index_points = []
        vertex_points_idx = []
        texture_points_idx = []

        for x in xrange(0,self.grid.x+1):
            for y in xrange(0,self.grid.y+1):
                vertex_points_idx += [-1,-1,-1]
                texture_points_idx += [-1,-1]

        for x in xrange(0, self.grid.x):
            for y in xrange(0, self.grid.y):
                x1 = x * self.x_step
                x2 = x1 + self.x_step
                y1 = y * self.y_step
                y2 = y1 + self.y_step

                #  d <-- c
                #        ^
                #        |
                #  a --> b
                a = x * (self.grid.y+1) + y
                b = (x+1) * (self.grid.y+1) + y
                c = (x+1) * (self.grid.y+1) + (y+1)
                d = x * (self.grid.y+1) + (y+1)

                # 2 triangles: a-b-d, b-c-d
                index_points += [ a, b, d, b, c, d]    # triangles

                l1 = ( a*3, b*3, c*3, d*3 )
                l2 = ( Point3(x1,y1,0), Point3(x2,y1,0), Point3(x2,y2,0), Point3(x1,y2,0) )

                #  building the vertex
                for i in xrange( len(l1) ):
                    vertex_points_idx[ l1[i] ] = l2[i].x
                    vertex_points_idx[ l1[i] + 1 ] = l2[i].y
                    vertex_points_idx[ l1[i] + 2 ] = l2[i].z

                # building the texels
                tex1 = ( a*2, b*2, c*2, d*2 )
                tex2 = ( Point2(x1,y1), Point2(x2,y1), Point2(x2,y2), Point2(x1,y2) )

                for i in xrange( len(tex1)):
                    texture_points_idx[ tex1[i] ] = tex2[i].x / w
                    texture_points_idx[ tex1[i] + 1 ] = tex2[i].y / h

        return ( index_points, vertex_points_idx, texture_points_idx )

    def get_vertex( self, x, y):
        '''Get the current vertex coordinate

        :Parameters:
            `x` : int
               x-vertex
            `y` : int
               y-vertex

        :rtype: (float, float, float)
        '''
        idx = (x * (self.grid.y+1) + y) * 3
        x = self.vertex_list.vertices[idx]
        y = self.vertex_list.vertices[idx+1]
        z = self.vertex_list.vertices[idx+2]
        return (x,y,z)

    def get_original_vertex( self, x, y):
        '''Get the original vertex coordinate.
        The original vertices are the ones weren't modified by the current action.

        :Parameters:
            `x` : int
               x-vertex
            `y` : int
               y-vertex

        :rtype: (float, float, float)
        '''
        idx = (x * (self.grid.y+1) + y) * 3
        x = self.vertex_points[idx]
        y = self.vertex_points[idx+1]
        z = self.vertex_points[idx+2]

        return (x,y,z)

    def set_vertex( self, x, y, v):
        '''Set a vertex point is a certain value

        :Parameters:
            `x` : int
               x-vertex
            `y` : int
               y-vertex
            `v` : (float, float, float)
                tuple value for the vertex
        '''
        idx = (x * (self.grid.y+1) + y) * 3
        self.vertex_list.vertices[idx] = int(v[0])
        self.vertex_list.vertices[idx+1] = int(v[1])
        self.vertex_list.vertices[idx+2] = int(v[2])

class TiledGrid3D(GridBase):
    '''`TiledGrid3D` is a 3D grid implementation. It differs from `Grid3D` in that
    the tiles can be separated from the grid.

    The vertex array will be built with::

        self.vertex_list.vertices: x,y,z (floats)
        self.vertex_list.tex_coords: x,y (floats)
        self.vertex_list.colors: RGBA, with values from 0 - 255
    '''
    def _init( self ):
        # calculate vertex, textures depending on screen size
        ver_pts, tex_pts = self._calculate_vertex_points()

        #: vertex array that can be transformed.
        #: it has these attributes:
        #:
        #:    - vertices
        #:    - colors
        #:    - tex_coords
        #:
        #: for more information refer to pyglet's documentation: pyglet.graphics.vertex_list
        self.vertex_list = pyglet.graphics.vertex_list(self.grid.x * self.grid.y * 4,
                            "t2f", "v3f/stream","c4B")
        #: original vertex array of the grid. (read-only)
        self.vertex_points = ver_pts[:]
        self.vertex_list.vertices = ver_pts
        self.vertex_list.tex_coords = tex_pts
        self.vertex_list.colors = (255,255,255,255) * self.grid.x * self.grid.y * 4

    def _blit(self ):
        self.vertex_list.draw(pyglet.gl.GL_QUADS)

    def _calculate_vertex_points(self):
        w = float(self.texture.width)
        h = float(self.texture.height)

        vertex_points = []
        texture_points = []

        for x in xrange(0, self.grid.x):
            for y in xrange(0, self.grid.y):
                x1 = x * self.x_step
                x2 = x1 + self.x_step
                y1 = y * self.y_step
                y2 = y1 + self.y_step

                # Building the tiles' vertex and texture points
                vertex_points += [x1, y1, 0, x2, y1, 0, x2, y2, 0, x1, y2, 0 ]
                texture_points += [x1/w, y1/h, x2/w, y1/h, x2/w, y2/h, x1/w, y2/h]

        # Generates a quad for each tile, to perform tiles effect
        return (vertex_points, texture_points)

    def set_tile(self, x, y, coords):
        '''Set the 4 tile coordinates

        Coordinates positions::

            3 <-- 2
                  ^
                  |
            0 --> 1

        :Parameters:
            `x` : int
                x coodinate of the tile
            `y` : int
                y coordinate of the tile
            `coords` : [ float, float, float, float, float, float, float, float, float, float, float, float ]
                The 4 coordinates in the format (x0, y0, z0, x1, y1, z1,..., x3, y3, z3)
        '''
        idx = (self.grid.y * x + y) * 4 * 3
        self.vertex_list.vertices[idx:idx+12] = coords

    def get_original_tile(self, x, y):
        '''Get the 4-original tile coordinates.

        Coordinates positions::

            3 <-- 2
                  ^
                  |
            0 --> 1

        :Parameters:
            `x` : int
                x coordinate of the tile
            `y` : int
                y coordinate of the tile

        :rtype: [ float, float, float, float, float, float, float, float, float, float, float, float ]
        :returns: The 4 coordinates with the following order: x0, y0, z0, x1, y1, z1,...,x3, y3, z3
        '''
        idx = (self.grid.y * x + y) * 4 * 3
        return self.vertex_points[idx:idx+12]

    def get_tile(self, x, y):
        '''Get the current tile coordinates.

        Coordinates positions::

            3 <-- 2
                  ^
                  |
            0 --> 1

        :Parameters:
            `x` : int
                x coordinate of the tile
            `y` : int
                y coordinate of the tile

        :rtype: [ float, float, float, float, float, float, float, float, float, float, float, float ]
        :returns: The 4 coordinates with the following order: x0, y0, z0, x1, y1, z1,...,x3, y3, z3
        '''
        idx = (self.grid.y * x + y) * 4 * 3
        return self.vertex_list.vertices[idx:idx+12]
<|endoftext|>


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

"""
This module combines variant cDNA sequences collected from a BAM file with
the reading frames of annotated reference transcripts to create candidate
translations.
"""

from __future__ import print_function, division, absolute_import

from .logging import get_logger
from .translation_key import TranslationKey

logger = get_logger(__name__)

class Translation(TranslationKey):
    """
    Translated amino acid sequence of a VariantORF for a particular
    ReferenceContext and VariantSequence.
    """
    __slots__ = [
        "untrimmed_variant_sequence",
        "reference_context",
        "variant_orf"
    ]

    def __init__(
            self,
            amino_acids,
            contains_mutation,
            mutation_start_idx,
            mutation_end_idx,
            ends_with_stop_codon,
            frameshift,
            untrimmed_variant_sequence,
            reference_context,
            variant_orf):
        """
        Parameters
        ----------
        amino_acids : str
            Amino acid sequence

        contains_mutation : bool
            Does the amino acid sequence contain a mutation?

        mutant_interval_start : int
            Start of half-open interval for variant amino acids
            in the translated sequence

        mutant_interval_end : int
            End of half-open interval for variant amino acids
            in the translated sequence

        ends_with_stop_codon : bool
            Did the amino acid sequence end due to a stop codon or did we
            just run out of sequence context around the variant?

        frameshift : bool
            Was the variant a frameshift relative to the reference sequence?
        """
        # TODO:
        #  get rid of untrimmed_variant_sequence by making
        #  VariantORF keep track of its inputs
        self.amino_acids = amino_acids
        self.contains_mutation = contains_mutation
        self.mutation_start_idx = mutation_start_idx
        self.mutation_end_idx = mutation_end_idx
        self.ends_with_stop_codon = ends_with_stop_codon
        self.frameshift = frameshift
        # this variant sequence might differ from the one
        # in variant_orf due to trimming required to match the reference
        self.untrimmed_variant_sequence = untrimmed_variant_sequence
        self.reference_context = reference_context
        self.variant_orf = variant_orf

    @property
    def reads(self):
        """
        RNA reads which were used to construct the coding sequence
        from which we translated these amino acids.
        """
        return self.untrimmed_variant_sequence.reads

    @property
    def reference_cdna_sequence_before_variant(self):
        """

        Returns str
        """
        return (
            self.variant_orf.reference_cdna_sequence_before_variant)

    @property
    def num_mismatches_before_variant(self):
        """
        Number of nucleotides in the variant cDNA sequence which
        don't match the ReferenceContext transcript sequence at
        positions before the variant locus.

        Returns int
        """
        return self.variant_orf.num_mismatches_before_variant

    @property
    def num_mismatches_after_variant(self):
        """
        Number of nucleotides in the variant cDNA sequence which
        don't match the ReferenceContext transcript sequence at
        positions after the variant locus.

        Returns int
        """
        return self.variant_orf.num_mismatches_after_variant

    @property
    def cdna_sequence(self):
        """
        cDNA sequence assembled from variant supporting reads

        Returns str
        """
        return self.variant_orf.cdna_sequence

    @property
    def offset_to_first_complete_codon(self):
        """
        Offset to first complete codon in the cDNA sequence

        Returns int in {0, 1, 2}
        """
        return self.variant_orf.offset_to_first_complete_codon

    @property
    def variant_cdna_interval_start(self):
        """
        Interbase start coordinate of variant interval in the cDNA sequence
        """
        return self.variant_orf.variant_cdna_interval_start

    @property
    def variant_cdna_interval_end(self):
        """
        Interbase end coordinate of variant interval in the cDNA sequence
        """
        return self.variant_orf.variant_cdna_interval_end

    def as_translation_key(self):
        """
        Project Translation object or any other derived class into just a
        TranslationKey, which has fewer fields and can be used as a
        dictionary key.
        """
        return TranslationKey(**{
            name: getattr(self, name)
            for name in TranslationKey._fields})

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

"""
This module combines variant cDNA sequences collected from a BAM file with
the reading frames of annotated reference transcripts to create candidate
translations.
"""

from __future__ import print_function, division, absolute_import

from .logging import get_logger
from .translation_key import TranslationKey

logger = get_logger(__name__)

class Translation(TranslationKey):
    """
    Translated amino acid sequence of a VariantORF for a particular
    ReferenceContext and VariantSequence.
    """
    __slots__ = [
        "untrimmed_variant_sequence",
        "reference_context",
        "variant_orf"
    ]

    def __init__(
            self,
            amino_acids,
            contains_mutation,
            mutation_start_idx,
            mutation_end_idx,
            ends_with_stop_codon,
            frameshift,
            untrimmed_variant_sequence,
            reference_context,
            variant_orf):
        """
        Parameters
        ----------
        amino_acids : str
            Amino acid sequence

        contains_mutation : bool
            Does the amino acid sequence contain a mutation?

        mutation_start_idx : int
            Start of half-open interval for variant amino acids
            in the translated sequence

        mutation_end_idx : int
            End of half-open interval for variant amino acids
            in the translated sequence

        ends_with_stop_codon : bool
            Did the amino acid sequence end due to a stop codon or did we
            just run out of sequence context around the variant?

        frameshift : bool
            Was the variant a frameshift relative to the reference sequence?

        untrimmed_variant_sequence : VariantSequence

        reference_context : ReferenceContext

        variant_orf : VariantORF
        """

        # TODO:
        #  get rid of untrimmed_variant_sequence by making
        #  VariantORF keep track of its inputs

        self.amino_acids = amino_acids
        self.contains_mutation = contains_mutation
        self.mutation_start_idx = mutation_start_idx
        self.mutation_end_idx = mutation_end_idx
        self.ends_with_stop_codon = ends_with_stop_codon
        self.frameshift = frameshift
        # this variant sequence might differ from the one
        # in variant_orf due to trimming required to match the reference
        self.untrimmed_variant_sequence = untrimmed_variant_sequence
        self.reference_context = reference_context
        self.variant_orf = variant_orf

    @property
    def reads(self):
        """
        RNA reads which were used to construct the coding sequence
        from which we translated these amino acids.
        """
        return self.untrimmed_variant_sequence.reads

    @property
    def reference_cdna_sequence_before_variant(self):
        """

        Returns str
        """
        return (
            self.variant_orf.reference_cdna_sequence_before_variant)

    @property
    def num_mismatches_before_variant(self):
        """
        Number of nucleotides in the variant cDNA sequence which
        don't match the ReferenceContext transcript sequence at
        positions before the variant locus.

        Returns int
        """
        return self.variant_orf.num_mismatches_before_variant

    @property
    def num_mismatches_after_variant(self):
        """
        Number of nucleotides in the variant cDNA sequence which
        don't match the ReferenceContext transcript sequence at
        positions after the variant locus.

        Returns int
        """
        return self.variant_orf.num_mismatches_after_variant

    @property
    def cdna_sequence(self):
        """
        cDNA sequence assembled from variant supporting reads

        Returns str
        """
        return self.variant_orf.cdna_sequence

    @property
    def offset_to_first_complete_codon(self):
        """
        Offset to first complete codon in the cDNA sequence

        Returns int in {0, 1, 2}
        """
        return self.variant_orf.offset_to_first_complete_codon

    @property
    def variant_cdna_interval_start(self):
        """
        Interbase start coordinate of variant interval in the cDNA sequence
        """
        return self.variant_orf.variant_cdna_interval_start

    @property
    def variant_cdna_interval_end(self):
        """
        Interbase end coordinate of variant interval in the cDNA sequence
        """
        return self.variant_orf.variant_cdna_interval_end

    def as_translation_key(self):
        """
        Project Translation object or any other derived class into just a
        TranslationKey, which has fewer fields and can be used as a
        dictionary key.
        """
        return TranslationKey(**{
            name: getattr(self, name)
            for name in TranslationKey._fields})

<|endoftext|>


<commit_before>from __future__ import absolute_import
import re
import inspect
import itertools
import functools
from .functools import compose
from .exceptions import throws_exception

def substitution(old, new):
	"""
	Return a function that will perform a substitution on a string
	"""
	return lambda s: s.replace(old, new)

def multi_substitution(*substitutions):
	"""
	Take a sequence of pairs specifying substitutions, and create
	a function that performs those substitutions.

	>>> multi_substitution(('foo', 'bar'), ('bar', 'baz'))('foo')
	'baz'
	"""
	substitutions = itertools.starmap(substitution, substitutions)
	# compose function applies last function first, so reverse the
	#  substitutions to get the expected order.
	substitutions = reversed(tuple(substitutions))
	return compose(*substitutions)

class FoldedCase(str):
	"""
	A case insensitive string class; behaves just like str
	except compares equal when the only variation is case.
	>>> s = FoldedCase('hello world')

	>>> s == 'Hello World'
	True

	>>> 'Hello World' == s
	True

	>>> s.index('O')
	4

	>>> s.split('O')
	['hell', ' w', 'rld']

	>>> sorted(map(FoldedCase, ['GAMMA', 'alpha', 'Beta']))
	['alpha', 'Beta', 'GAMMA']
	"""
	def __lt__(self, other):
		return self.lower() < other.lower()
	def __gt__(self, other):
		return self.lower() > other.lower()
	def __eq__(self, other):
		return self.lower() == other.lower()
	def __hash__(self):
		return hash(self.lower())
	# cache lower since it's likely to be called frequently.
	def lower(self):
		self._lower = super(FoldedCase, self).lower()
		self.lower = lambda: self._lower
		return self._lower

	def index(self, sub):
		return self.lower().index(sub.lower())

	def split(self, splitter=' ', maxsplit=0):
		pattern = re.compile(re.escape(splitter), re.I)
		return pattern.split(self, maxsplit)

def local_format(string):
	"""
	format the string using variables in the caller's local namespace.

	>>> a = 3
	>>> local_format("{a:5}")
	'    3'
	"""
	return string.format(**inspect.currentframe().f_back.f_locals)

def is_decodable(value):
	"""
	Return True if the supplied value is decodable (using the 'unicode'
	constructor and thus the default encoding).
	"""
	return not throws_exception(functools.partial(unicode, value),
		UnicodeDecodeError)

def is_binary(value):
	"""
	Return True if the value appears to be binary (that is, it's a byte
	string and isn't decodable).
	"""
	return isinstance(value, bytes) and not is_decodable(value)
<commit_msg>Minor cleanup<commit_after>from __future__ import absolute_import

import re
import inspect
import itertools
import functools

from .functools import compose
from .exceptions import throws_exception

def substitution(old, new):
	"""
	Return a function that will perform a substitution on a string
	"""
	return lambda s: s.replace(old, new)

def multi_substitution(*substitutions):
	"""
	Take a sequence of pairs specifying substitutions, and create
	a function that performs those substitutions.

	>>> multi_substitution(('foo', 'bar'), ('bar', 'baz'))('foo')
	'baz'
	"""
	substitutions = itertools.starmap(substitution, substitutions)
	# compose function applies last function first, so reverse the
	#  substitutions to get the expected order.
	substitutions = reversed(tuple(substitutions))
	return compose(*substitutions)

class FoldedCase(str):
	"""
	A case insensitive string class; behaves just like str
	except compares equal when the only variation is case.
	>>> s = FoldedCase('hello world')

	>>> s == 'Hello World'
	True

	>>> 'Hello World' == s
	True

	>>> s.index('O')
	4

	>>> s.split('O')
	['hell', ' w', 'rld']

	>>> sorted(map(FoldedCase, ['GAMMA', 'alpha', 'Beta']))
	['alpha', 'Beta', 'GAMMA']
	"""
	def __lt__(self, other):
		return self.lower() < other.lower()
	def __gt__(self, other):
		return self.lower() > other.lower()
	def __eq__(self, other):
		return self.lower() == other.lower()
	def __hash__(self):
		return hash(self.lower())
	# cache lower since it's likely to be called frequently.
	def lower(self):
		self._lower = super(FoldedCase, self).lower()
		self.lower = lambda: self._lower
		return self._lower

	def index(self, sub):
		return self.lower().index(sub.lower())

	def split(self, splitter=' ', maxsplit=0):
		pattern = re.compile(re.escape(splitter), re.I)
		return pattern.split(self, maxsplit)

def local_format(string):
	"""
	format the string using variables in the caller's local namespace.

	>>> a = 3
	>>> local_format("{a:5}")
	'    3'
	"""
	return string.format(**inspect.currentframe().f_back.f_locals)

def is_decodable(value):
	"""
	Return True if the supplied value is decodable (using the 'unicode'
	constructor and thus the default encoding).
	"""
	return not throws_exception(functools.partial(unicode, value),
		UnicodeDecodeError)

def is_binary(value):
	"""
	Return True if the value appears to be binary (that is, it's a byte
	string and isn't decodable).
	"""
	return isinstance(value, bytes) and not is_decodable(value)
<|endoftext|>


<commit_before>#!/opt/local/bin/python

from lxml import etree
import re
import os
import multiprocessing
import sys
import datetime
import numpy

start = 0

# Print iterations progress
def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100, eta=True):
    """
    Call in a loop to create terminal progress bar
    @params:
        iteration   - Required  : current iteration (Int)
        total       - Required  : total iterations (Int)
        prefix      - Optional  : prefix string (Str)
        suffix      - Optional  : suffix string (Str)
        decimals    - Optional  : positive number of decimals in percent complete (Int)
        barLength   - Optional  : character length of bar (Int)
    """
    global start
    start = datetime.datetime.now() if start == 0 else start
    format_str = "{0:." + str(decimals) + "f}"
    percents = format_str.format(100 * (iteration / float(total)))
    filled_length = int(round(bar_length * iteration / float(total)))
    bar = '█' * filled_length + '-' * (bar_length - filled_length)
    eta_ts = (start + (datetime.datetime.now() - start) * total / iteration) if iteration > 0 else 0
    eta = 'ETA ' + eta_ts.strftime('%H:%M:%S') if eta and iteration > 0 else ''
    sys.stdout.write('\r%s |%s| %s%s %s %s' % (prefix, bar, percents, '%', suffix, eta)),
    if iteration == total:
        sys.stdout.write('\n')
    sys.stdout.flush()

law_complexities = dict()

def get_complexity(fname):
    global law_complexities

    if len(law_complexities.values()) == 0:
        with open("complexities.txt") as cf:
            ls = cf.readlines()

        for line in ls:
            cs = line.split("\t")
            law_complexities[cs[0]] = float(cs[7])

    return law_complexities.get(fname, 0)

def namespace(element):
    ns_m = re.match('\{.*\}', element.tag)
    return ns_m.group(0) if m else ''

def get_match(fname, ta):
    m_r = etree.parse(fname).getroot()
    ns = namespace(m_r)
    t = m_r.find('.//{0}pealkiri'.format(ns)).text
    return ta, re.compile(t + '\w*?', re.I)

def calculate(task):
    ca, cs, t = task
    return ca, len(re.findall(cs, t))

def get_links(fname, s_list):
    l_r = etree.parse(fname).getroot()
    ns = namespace(l_r)
    txt = ""

    for child in l_r.findall('.//{0}tavatekst'.format(ns)):
        if child.text:
            txt = txt + "\t" + child.text

    PROCESSES = 8
    TASKS = [(t_act, t_str, txt) for (t_act, t_str) in s_list]

    matches = dict()
    with multiprocessing.Pool(PROCESSES) as pool:
        for ma, c in pool.map(calculate, TASKS):
            matches[ma] = c

    return matches

# Get the titles of acts per month and compile a list of REs to match them
r = re.compile("(\d+)\.(\d+)\.(\d+)-(.*)\.xml", re.I)
d = dict()
bCount = dict()
l = os.listdir('.')
i = 0
print('Extract titles and complexities')
for filename in l:
    print_progress(i, len(l), prefix='Progress:', suffix='Complete', bar_length=50)
    i += 1

    m = re.match(r, filename)
    if m:
        k = m.group(2) + "-" + m.group(3)
        if k not in d:
            d[k] = []
            bCount[k] = dict()

        bCount[k][m.group(4)] = get_complexity(filename)

        d[k].append(get_match(filename, m.group(4)))

print('\nTitle extraction done')

matrices = dict()
print('Starting to find links')

i = 0
for filename in os.listdir('.'):
    print_progress(i, len(l), prefix='Progress:', suffix='Complete', bar_length=50)
    i += 1

    m = re.match(r, filename)
    if m:
        k = m.group(2) + "-" + m.group(3)
    else:
        continue

    act = m.group(4)
    if k not in matrices:
        matrices[k] = dict()

    matrices[k][act] = get_links(filename, d[k])

print('\nLinks done')

# for mk in matrices:
#     m = matrices[mk]
#     header = ""
#     f = open("x-" + mk + ".txt", "w")
#     for k in sorted(m.keys()):
#         header = header + "\t" + k
#     f.write("%s\n" % header)
#
#     for a in sorted(m.keys()):
#         line = a
#         for k in sorted(m.keys()):
#             bc = max(bCount[mk][a], bCount[mk][k])
#             if bc == 0:
#                 print("%s %s %s" % (mk, a, k))
#             s = str(m[a][k]/bc) if bc > 0 else "0"
#             line = line + "\t" + s
#         f.write("%s\n" % line)
#
#     f.close()
#
f = open("q_complexities.txt", "w")
for mk in matrices:
    m = matrices[mk]
    matrix = []
    s_keys = sorted(m.keys())
    for a in s_keys:
        matrix.append([m[a][b] / max(bCount[mk][a], bCount[mk][b]) for b in s_keys])

    c1 = sum(bCount[mk].values())
    c2 = sum(list(map(lambda x: sum(x), matrix)))

    matrix = numpy.array(matrix)

    numpy.fill_diagonal(matrix, 1)
    matrix = matrix + matrix.T
    matrix[matrix > 1] = 1

    c3 = sum(numpy.linalg.eigvals(matrix))
    complexity = c1 + c2 * c3

    mks = mk.split("-")
    f.write("%s\t%s\t%f\t%f\t%f\t%f\n" % (mks[0], mks[1], c1, c2, c3.real, complexity.real))
f.close()
<commit_msg>Minor big fix<commit_after>#!/opt/local/bin/python

from lxml import etree
import re
import os
import multiprocessing
import sys
import datetime
import numpy

start = 0

# Print iterations progress
def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100, eta=True):
    """
    Call in a loop to create terminal progress bar
    @params:
        iteration   - Required  : current iteration (Int)
        total       - Required  : total iterations (Int)
        prefix      - Optional  : prefix string (Str)
        suffix      - Optional  : suffix string (Str)
        decimals    - Optional  : positive number of decimals in percent complete (Int)
        barLength   - Optional  : character length of bar (Int)
    """
    global start
    start = datetime.datetime.now() if start == 0 else start
    format_str = "{0:." + str(decimals) + "f}"
    percents = format_str.format(100 * (iteration / float(total)))
    filled_length = int(round(bar_length * iteration / float(total)))
    bar = '█' * filled_length + '-' * (bar_length - filled_length)
    eta_ts = (start + (datetime.datetime.now() - start) * total / iteration) if iteration > 0 else 0
    eta = 'ETA ' + eta_ts.strftime('%H:%M:%S') if eta and iteration > 0 else ''
    sys.stdout.write('\r%s |%s| %s%s %s %s' % (prefix, bar, percents, '%', suffix, eta)),
    if iteration == total:
        sys.stdout.write('\n')
    sys.stdout.flush()

law_complexities = dict()

def get_complexity(fname):
    global law_complexities

    if len(law_complexities.values()) == 0:
        with open("complexities.txt") as cf:
            ls = cf.readlines()

        for line in ls:
            cs = line.split("\t")
            law_complexities[cs[0]] = float(cs[7])

    return law_complexities.get(fname, 0)

def namespace(element):
    ns_m = re.match('\{.*\}', element.tag)
    return ns_m.group(0) if ns_m else ''

def get_match(fname, ta):
    m_r = etree.parse(fname).getroot()
    ns = namespace(m_r)
    t = m_r.find('.//{0}pealkiri'.format(ns)).text
    return ta, re.compile(t + '\w*?', re.I)

def calculate(task):
    ca, cs, t = task
    return ca, len(re.findall(cs, t))

def get_links(fname, s_list):
    l_r = etree.parse(fname).getroot()
    ns = namespace(l_r)
    txt = ""

    for child in l_r.findall('.//{0}tavatekst'.format(ns)):
        if child.text:
            txt = txt + "\t" + child.text

    PROCESSES = 8
    TASKS = [(t_act, t_str, txt) for (t_act, t_str) in s_list]

    matches = dict()
    with multiprocessing.Pool(PROCESSES) as pool:
        for ma, c in pool.map(calculate, TASKS):
            matches[ma] = c

    return matches

# Get the titles of acts per month and compile a list of REs to match them
r = re.compile("(\d+)\.(\d+)\.(\d+)-(.*)\.xml", re.I)
d = dict()
bCount = dict()
l = os.listdir('.')
i = 0
print('Extract titles and complexities')
for filename in l:
    print_progress(i, len(l), prefix='Progress:', suffix='Complete', bar_length=50)
    i += 1

    m = re.match(r, filename)
    if m:
        k = m.group(2) + "-" + m.group(3)
        if k not in d:
            d[k] = []
            bCount[k] = dict()

        bCount[k][m.group(4)] = get_complexity(filename)

        d[k].append(get_match(filename, m.group(4)))

print('\nTitle extraction done')

matrices = dict()
print('Starting to find links')

i = 0
for filename in os.listdir('.'):
    print_progress(i, len(l), prefix='Progress:', suffix='Complete', bar_length=50)
    i += 1

    m = re.match(r, filename)
    if m:
        k = m.group(2) + "-" + m.group(3)
    else:
        continue

    act = m.group(4)
    if k not in matrices:
        matrices[k] = dict()

    matrices[k][act] = get_links(filename, d[k])

print('\nLinks done')

f = open("q_complexities.txt", "w")
for mk in matrices:
    m = matrices[mk]
    matrix = []
    s_keys = sorted(m.keys())
    for a in s_keys:
        matrix.append([m[a][b] / max(bCount[mk][a], bCount[mk][b]) if a != b else 0 for b in s_keys])

    c1 = sum(bCount[mk].values())
    c2 = sum(list(map(lambda x: sum(x), matrix)))

    matrix = numpy.array(matrix)

    numpy.fill_diagonal(matrix, 1)
    matrix = matrix + matrix.T
    matrix[matrix > 1] = 1

    c3 = sum(numpy.linalg.eigvals(matrix))
    complexity = c1 + c2 * c3

    mks = mk.split("-")
    f.write("%s\t%s\t%f\t%f\t%f\t%f\n" % (mks[0], mks[1], c1, c2, c3.real, complexity.real))
f.close()
<|endoftext|>


<commit_before># ERPNext - web based ERP (http://erpnext.com)
# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals
import webnotes
from webnotes.utils import flt, getdate
from webnotes.model.doc import make_autoname
from webnotes.model.wrapper import getlist

class DocType:
	def __init__(self,d,dl):
		self.doc, self.doclist = d,dl

	def autoname(self):
		self.doc.name = make_autoname(self.doc.naming_series + '.#####')

	def validate(self):
		"""Validate invoice that c-form is applicable
			and no other c-form is received for that"""

		for d in getlist(self.doclist, 'invoice_details'):
			inv = webnotes.conn.sql("""select c_form_applicable, c_form_no from
				`tabSales Invoice` where name = %s""", d.invoice_no)

			if not inv:
				webnotes.msgprint("Invoice: %s is not exists in the system, please check." %
					d.invoice_no, raise_exception=1)

			elif inv[0][0] != 'Yes':
				webnotes.msgprint("C-form is not applicable for Invoice: %s" %
					d.invoice_no, raise_exception=1)

			elif inv[0][1] and inv[0][1] != self.doc.name:
				webnotes.msgprint("""Invoice %s is tagged in another C-form: %s.
					If you want to change C-form no for this invoice,
					please remove invoice no from the previous c-form and then try again""" %
					(d.invoice_no, inv[0][1]), raise_exception=1)

	def on_update(self):
		"""	Update C-Form No on invoices"""
		inv = [d.invoice_no for d in getlist(self.doclist, 'invoice_details')]
		if inv:
			webnotes.conn.sql("""update `tabSales Invoice` set c_form_no=%s, modified=%s
				where name in (%s)""" % ('%s', '%s', ', '.join(['%s'] * len(inv))),
				tuple([self.doc.name, self.doc.modified] + inv), debug=1)

			webnotes.conn.sql("""update `tabSales Invoice` set c_form_no = '', modified = %s
				where name not in (%s) and ifnull(c_form_no, '') = %s""" %
				('%s', ', '.join(['%s'*len(inv)]), '%s'),
				tuple([self.doc.modified] + inv + [self.doc.name]), debug=1)
		else:
			webnotes.msgprint("Please enter atleast 1 invoice in the table", raise_exception=1)

		self.set_total_invoiced_amount()

	def set_total_invoiced_amount(self):
		total = sum([flt(d.total) for d in getlist(self.doclist, 'invoice_details')])
		webnotes.conn.set(self.doc, 'total_invoiced_amount', total)

	def get_invoice_details(self, invoice_no):
		"""	Pull details from invoices for referrence """

		inv = webnotes.conn.sql("""select posting_date, territory, net_total, grand_total
			from `tabSales Invoice` where name = %s""", invoice_no)
		return {
			'invoice_date' : inv and getdate(inv[0][0]).strftime('%Y-%m-%d') or '',
			'territory'    : inv and inv[0][1] or '',
			'net_total'    : inv and flt(inv[0][2]) or '',
			'grand_total'  : inv and flt(inv[0][3]) or ''
		}<commit_msg>fixes in c-form<commit_after># ERPNext - web based ERP (http://erpnext.com)
# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals
import webnotes
from webnotes.utils import flt, getdate
from webnotes.model.doc import make_autoname
from webnotes.model.wrapper import getlist

class DocType:
	def __init__(self,d,dl):
		self.doc, self.doclist = d,dl

	def autoname(self):
		self.doc.name = make_autoname(self.doc.naming_series + '.#####')

	def validate(self):
		"""Validate invoice that c-form is applicable
			and no other c-form is received for that"""

		for d in getlist(self.doclist, 'invoice_details'):
			inv = webnotes.conn.sql("""select c_form_applicable, c_form_no from
				`tabSales Invoice` where name = %s""", d.invoice_no)

			if not inv:
				webnotes.msgprint("Invoice: %s is not exists in the system, please check." %
					d.invoice_no, raise_exception=1)

			elif inv[0][0] != 'Yes':
				webnotes.msgprint("C-form is not applicable for Invoice: %s" %
					d.invoice_no, raise_exception=1)

			elif inv[0][1] and inv[0][1] != self.doc.name:
				webnotes.msgprint("""Invoice %s is tagged in another C-form: %s.
					If you want to change C-form no for this invoice,
					please remove invoice no from the previous c-form and then try again""" %
					(d.invoice_no, inv[0][1]), raise_exception=1)

	def on_update(self):
		"""	Update C-Form No on invoices"""
		inv = [d.invoice_no for d in getlist(self.doclist, 'invoice_details')]
		if inv:
			webnotes.conn.sql("""update `tabSales Invoice` set c_form_no=%s, modified=%s
				where name in (%s)""" % ('%s', '%s', ', '.join(['%s'] * len(inv))),
				tuple([self.doc.name, self.doc.modified] + inv), debug=1)

			webnotes.conn.sql("""update `tabSales Invoice` set c_form_no = '', modified = %s
				where name not in (%s) and ifnull(c_form_no, '') = %s""" %
				('%s', ', '.join(['%s']*len(inv)), '%s'),
				tuple([self.doc.modified] + inv + [self.doc.name]), debug=1)
		else:
			webnotes.msgprint("Please enter atleast 1 invoice in the table", raise_exception=1)

		self.set_total_invoiced_amount()

	def set_total_invoiced_amount(self):
		total = sum([flt(d.total) for d in getlist(self.doclist, 'invoice_details')])
		webnotes.conn.set(self.doc, 'total_invoiced_amount', total)

	def get_invoice_details(self, invoice_no):
		"""	Pull details from invoices for referrence """

		inv = webnotes.conn.sql("""select posting_date, territory, net_total, grand_total
			from `tabSales Invoice` where name = %s""", invoice_no)
		return {
			'invoice_date' : inv and getdate(inv[0][0]).strftime('%Y-%m-%d') or '',
			'territory'    : inv and inv[0][1] or '',
			'net_total'    : inv and flt(inv[0][2]) or '',
			'grand_total'  : inv and flt(inv[0][3]) or ''
		}<|endoftext|>


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

# BSD 3-Clause License
#
# Copyright (c) 2017, ColoredInsaneAsylums
# 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 copyright holder 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.

# CREDITS
# Creator: Nitin Verma (nitin dot verma at utexas dot edu)
# Update: Milind Siddhanti (milindsiddhanti at utexas dot edu)
#

import metadatautilspkg.globalvars as globalvars

# ERROR CODES
ERROR_INVALID_ARGUMENT_STRING = {"code": "e01", "message": "Invalid (number of) arguments specified on the command-line."}
ERROR_CANNOT_OPEN_CSV_FILE = {"code": "e02", "message": "Cannot open the CSV batch file '{}'.".format(globalvars.csvFile)}
ERROR_CANNOT_WRITE_CSV_FILE = {"code": "e03", "message": "Could not write CSV file for errors encountered during transfers."}
ERROR_CANNOT_READ_DBCONF_FILE = {"code": "e04", "message": "Cannot read the DB configuration file."}
ERROR_INVALID_HEADER_ROW = {"code": "e05", "message": "The header in the input CSV file is invalid."}
ERROR_CANNOT_CONNECT_TO_DB = {"code": "e06", "message": "Cannot connect to the DB."}
ERROR_CANNOT_AUTHENTICATE_DB_USER = {"code": "e07", "message": "Cannot authenticate DB user specified."}
ERROR_CANNOT_INSERT_INTO_DB = {"code": "e08", "message": "Cannot insert record into the DB."}
ERROR_CANNOT_REMOVE_FILE = {"code": "e09", "message": "Cannot remove file from directory."}
ERROR_CANNOT_REMOVE_RECORD_FROM_DB = {"code": "e10", "message": "Cannot remove record from DB."}
ERROR_CANNOT_READ_LABELS_FILE = {"code": "e11", "message": "Cannot read the labels file '{}'.".format(globalvars.labelsFileName)}
ERROR_INVALID_JSON_IN_LABELS_FILE = {"code": "e12", "message": "The file '{}' is not a valid JSON file. Please check the file for formatting errors.".format(globalvars.labelsFileName)}
ERROR_CANNOT_READ_VOCAB_FILE = {"code": "e13", "message": "Cannot read the vocab file '{}'.".format(globalvars.vocabFileName)}
ERROR_INVALID_JSON_IN_VOCAB_FILE = {"code": "e14", "message": "The file '{}' is not a valid JSON file. Please check the file for formatting errors.".format(globalvars.vocabFileName)}
ERROR_CANNOT_CREATE_DESTINATION_DIRECTORY = {"code": "e15", "message": "Cannot create destination directory '{}'. Skipping to next transfer."}
ERROR_CANNOT_FIND_DIRECTORY = {"code": "e16", "message": "Cannot find the filepath directory. '{}'."}
ERROR_CANNOT_FIND_DOCUMENT = {"code": "e17", "message": "Document not found in the database. '{}'."}
ERROR_CANNOT_FILE_EXTENSION = {"code": "e18", "message": "File Extension does not match. '{}'."}
ERROR_INSTALL_IMAGEMAGICK = {"code": "e19", "message": "Image Magick is not installed."}
ERROR_CANNOT_UPDATE_DB = {"code": "e20", "message": "Updation of the record failed."}
ERROR_EXT_ARGUMENT = {"code": "e21", "message": "Required argument '-e' not passed in the command line."}
ERROR_FILE_ARGUMENT = {"code": "e22", "message": "Required argument '-f' not passed in the command line."}
ERROR_TECH_UPDATED = {"code": "e23", "message": "The technical property profile for the file '{}' has been already updated."}
ERROR_COM_UPDATED = {"code": "e24", "message": "The compliance metadata profile has been already updated."}
<commit_msg>changes to the errorcodes.py to include admin errors<commit_after># -*- coding: utf-8 -*-

# BSD 3-Clause License
#
# Copyright (c) 2017, ColoredInsaneAsylums
# 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 copyright holder 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.

# CREDITS
# Creator: Nitin Verma (nitin dot verma at utexas dot edu)
# Update: Milind Siddhanti (milindsiddhanti at utexas dot edu)
#

import metadatautilspkg.globalvars as globalvars

# ERROR CODES
ERROR_INVALID_ARGUMENT_STRING = {"code": "e01", "message": "Invalid (number of) arguments specified on the command-line."}
ERROR_CANNOT_OPEN_CSV_FILE = {"code": "e02", "message": "Cannot open the CSV batch file '{}'.".format(globalvars.csvFile)}
ERROR_CANNOT_WRITE_CSV_FILE = {"code": "e03", "message": "Could not write CSV file for errors encountered during transfers."}
ERROR_CANNOT_READ_DBCONF_FILE = {"code": "e04", "message": "Cannot read the DB configuration file."}
ERROR_INVALID_HEADER_ROW = {"code": "e05", "message": "The header in the input CSV file is invalid."}
ERROR_CANNOT_CONNECT_TO_DB = {"code": "e06", "message": "Cannot connect to the DB."}
ERROR_CANNOT_AUTHENTICATE_DB_USER = {"code": "e07", "message": "Cannot authenticate DB user specified."}
ERROR_CANNOT_INSERT_INTO_DB = {"code": "e08", "message": "Cannot insert record into the DB."}
ERROR_CANNOT_REMOVE_FILE = {"code": "e09", "message": "Cannot remove file from directory."}
ERROR_CANNOT_REMOVE_RECORD_FROM_DB = {"code": "e10", "message": "Cannot remove record from DB."}
ERROR_CANNOT_READ_LABELS_FILE = {"code": "e11", "message": "Cannot read the labels file '{}'.".format(globalvars.labelsFileName)}
ERROR_INVALID_JSON_IN_LABELS_FILE = {"code": "e12", "message": "The file '{}' is not a valid JSON file. Please check the file for formatting errors.".format(globalvars.labelsFileName)}
ERROR_CANNOT_READ_VOCAB_FILE = {"code": "e13", "message": "Cannot read the vocab file '{}'.".format(globalvars.vocabFileName)}
ERROR_INVALID_JSON_IN_VOCAB_FILE = {"code": "e14", "message": "The file '{}' is not a valid JSON file. Please check the file for formatting errors.".format(globalvars.vocabFileName)}
ERROR_CANNOT_CREATE_DESTINATION_DIRECTORY = {"code": "e15", "message": "Cannot create destination directory '{}'. Skipping to next transfer."}
ERROR_CANNOT_FIND_DIRECTORY = {"code": "e16", "message": "Cannot find the filepath directory. '{}'."}
ERROR_CANNOT_FIND_DOCUMENT = {"code": "e17", "message": "Document not found in the database. '{}'."}
ERROR_CANNOT_FILE_EXTENSION = {"code": "e18", "message": "File Extension does not match. '{}'."}
ERROR_INSTALL_IMAGEMAGICK = {"code": "e19", "message": "Image Magick is not installed '{}'."}
ERROR_CANNOT_UPDATE_DB = {"code": "e20", "message": "Updation of the record failed."}
ERROR_EXT_ARGUMENT = {"code": "e21", "message": "Required argument '-e' not passed in the command line."}
ERROR_FILE_ARGUMENT = {"code": "e22", "message": "Required argument '-f' not passed in the command line."}
ERROR_TECH_UPDATED = {"code": "e23", "message": "The technical property profile for the file '{}' has been already updated."}
ERROR_COM_UPDATED = {"code": "e24", "message": "The compliance metadata has been already updated."}
ERROR_ADMIN_UPDATED = {"code": "e25", "message": "The admin metadata has been already updated."}
<|endoftext|>


<commit_before>import os
import fiona
from matplotlib import pyplot as plt

def print_a_few_lines(input_file):
	with fiona.open(input_file) as src:
		f = src.next()
		print (f["geometry"]["type"])
		print (f["properties"])

def map_it(input_file):
	plt.figure(figsize=(6,6))
	with fiona.open(input_file) as src:
		f = src.next()
		for coords in f["geometry"]["coordinates"]:
			x, y = zip(*coords[0])
			plt.plot(x, y)
		plt.gca().set_aspect("equal")
		plt.show()

if __name__ == "__main__":
	input_file = "/Users/Emily/Desktop/SciPy-Tutorial-2015/examples/nybb_15b/nybb.shp"

	# print_a_few_lines(input_file)

	map_it(input_file)<commit_msg>Adding shapely tutorial<commit_after>import os
import fiona
from matplotlib import pyplot as plt
from shapely.geometry import Point, LineString, Polygon, MultiPoint, GeometryCollection

def print_a_few_lines(input_file):
	with fiona.open(input_file) as src:
		f = src.next()
		print (f["geometry"]["type"])
		print (f["properties"])

def map_it(input_file):
	plt.figure(figsize=(6,6))
	with fiona.open(input_file) as src:
		f = src.next()
		for coords in f["geometry"]["coordinates"]:
			x, y = zip(*coords[0])
			plt.plot(x, y)
		plt.gca().set_aspect("equal")
		plt.show()

def shapely_exercise():
	l1 = [0, 1, 2, 0, 1, 2]
	l2 = [0, 0, 0, 1, 1, 1]
	pts = [Point(x, y) for x, y in zip(l1, l2)]
	MultiPoint(pts)

if __name__ == "__main__":
	input_file = "/Users/Emily/Desktop/SciPy-Tutorial-2015/examples/nybb_15b/nybb.shp"

	# print_a_few_lines(input_file)

	# map_it(input_file)

	# shapely_exercise()
	l1 = [0, 1, 2, 0, 1, 2]
	l2 = [0, 0, 0, 1, 1, 1]
	pts = [Point(x, y) for x, y in zip(l1, l2)]
	MultiPoint(pts)<|endoftext|>


<commit_before>from tvrenamr_core import TvRenamr
from optparse import OptionParser
import os

parser = OptionParser()
parser.add_option("-w", "--working", dest="working_dir", help="The working DIRECTORY to run tvrenamr in. Required!")
parser.add_option("-r", "--renamed", dest="renamed_dir", help="The DIRECTORY to move renamed files to. Mutually exclusive to -a")
parser.add_option("-a", "--auto", dest="auto_move", help="Automatically move renamed files to the appropriate directory under the DIRECTORY specified. Mutually exclusive to -r")
(options, args) = parser.parse_args()

working_dir = options.working_dir
renamed_dir = options.renamed_dir
auto_move = options.auto_move

tv = TvRenamr(working_dir)
for fn in os.listdir(working_dir):
    if auto_move != None:
        print "rename and auto move"
        tv.rename_and_auto_move(fn, auto_move)
    elif renamed_dir != None:
        print "rename and move"
        tv.rename_and_move(fn, renamed_dir)
    elif auto_move == None and renamed_dir == None:
        try:
            tv.rename(fn)
        except Exception, e:
            print e
            continue
    else:
        print "incorrect options!"<commit_msg>added exception handling and changed method calls to reflect core changes<commit_after>from tvrenamr_core import TvRenamr
from optparse import OptionParser
import os

parser = OptionParser()
parser.add_option("-w", "--working", dest="working_dir", help="The working DIRECTORY to run tvrenamr in. Required!")
parser.add_option("-r", "--renamed", dest="renamed_dir", help="The DIRECTORY to move renamed files to. Mutually exclusive to -a")
parser.add_option("-a", "--auto", dest="auto_move", help="Automatically move renamed files to the appropriate directory under the DIRECTORY specified. Mutually exclusive to -r")
parser.add_option("-l", "--log", dest="is_logging", help="")
(options, args) = parser.parse_args()

working_dir = options.working_dir
renamed_dir = options.renamed_dir
auto_move = options.auto_move
logging = options.is_logging

tv = TvRenamr(working_dir)
for fn in os.listdir(working_dir):
    if auto_move != None:
        #rename and auto move
        try:
            tv.rename(fn, auto_move=auto_move)
        except Exception, e:
            print e
            continue
    elif renamed_dir != None:
        #rename and move
        try:
            tv.rename(fn, renamed_dir)
        except Exception, e:
            print e
            continue
    elif auto_move == None and renamed_dir == None:
        #rename
        try:
            tv.rename(fn)
        except Exception, e:
            print e
            continue
    else:
        print "incorrect options!"<|endoftext|>


<commit_before>from tvrenamr_core import TvRenamr
from optparse import OptionParser
import os

parser = OptionParser()
parser.add_option("-a", "--auto", dest="auto_move", help="Automatically move renamed files to the appropriate directory under the directory specified. Mutually exclusive to -r")
parser.add_option("-l", "--log", dest="logging", help="Set the location for logs to be created in")
parser.add_option("-n", "--name", dest="name", help="Set the show name for renaming")
parser.add_option("-r", "--renamed", dest="renamed_dir", help="The directory to move renamed files to. Mutually exclusive to -a")
parser.add_option("--regex", dest="regex", help="the regular expression to set the format of files being renamed. Use %n to specify the show name, %s for the season number and %e for the episode number. All spaces are converted to periods before the regex is run")
parser.add_option("-s", "--season", dest="season")
parser.add_option("-w", "--working", dest="working_dir", help="The working directory to run tvrenamr in. Required!")
(options, args) = parser.parse_args()

working_dir = options.working_dir
renamed_dir = options.renamed_dir
auto_move = options.auto_move
logging = options.logging
regex = options.regex
#working_dir = "/Users/madnashua/Projects/tvrenamr/core/testcase"
#regex = "%n\.\-\.[Ss]?%s\.E%e"

tv = TvRenamr(working_dir)
for fn in os.listdir(working_dir):
    if auto_move != None:
        #rename and auto move
        try:
            tv.rename(fn, auto_move=auto_move, regex=regex)
        except Exception, e:
            print e
            continue
    elif renamed_dir != None:
        #rename and move
        try:
            tv.rename(fn, renamed_dir, regex=regex)
        except Exception, e:
            print e
            continue
    elif auto_move == None and renamed_dir == None:
        #rename
        try:
            tv.rename(fn, regex=regex)
        except Exception, e:
            print e
            continue
    else:
        print "incorrect options!"<commit_msg>updated to conform to the new cores api<commit_after>from tvrenamr_core import TvRenamr
from optparse import OptionParser
import os

parser = OptionParser()
parser.add_option("-a", "--auto", dest="auto_move", help="Automatically move renamed files to the appropriate directory under the directory specified. Mutually exclusive to -r")
parser.add_option("-l", "--log", dest="logging", help="Set the location for logs to be created in")
parser.add_option("-n", "--name", dest="name", help="Set the show name for renaming")
parser.add_option("-r", "--renamed", dest="renamed_dir", help="The directory to move renamed files to. Mutually exclusive to -a")
parser.add_option("--regex", dest="regex", help="the regular expression to set the format of files being renamed. Use %n to specify the show name, %s for the season number and %e for the episode number. All spaces are converted to periods before the regex is run")
parser.add_option("-s", "--season", dest="season")
parser.add_option("-w", "--working", dest="working_dir", help="The working directory to run tvrenamr in. Required!")
(options, args) = parser.parse_args()

working_dir = options.working_dir
renamed_dir = options.renamed_dir
auto_move = options.auto_move
logging = options.logging
regex = options.regex
#working_dir = "/Users/madnashua/Projects/tvrenamr/core/tests/data/files"
#regex = "%n\.\-\.[Ss]?%s\.E%e"

tv = TvRenamr(working_dir)
for fn in os.listdir(working_dir):
    if auto_move != None:
        #rename and auto move
        try:
            tv.rename(fn, auto_move=auto_move, regex=regex)
        except Exception, e:
            print e
            continue
    elif renamed_dir != None:
        #rename and move
        try:
            tv.rename(fn, renamed_dir, regex=regex)
        except Exception, e:
            print e
            continue
    elif auto_move == None and renamed_dir == None:
        #rename
        try:
            details = tv.extract_episode_details_from_file(fn, user_regex=regex)
            name = tv.retrieve_episode_name(details[0],details[1],details[2])
            path = tv.build_path(details,name)
            tv.rename(fn,path)
        except Exception, e:
            print e
            continue
    else:
        print "incorrect options!"<|endoftext|>


<commit_before>import tweepy
import schedule
import time
from auth import consumer_key, consumer_secret, key, secret
from time import strftime, localtime

auth = tweepy.OAuthHandler(consumer_key,  consumer_secret)
auth.set_access_token(key, secret)

api=tweepy.API(auth)
#All the code relating to twitter lies here. Before you make your own changes,
#make sure you've added your own API keys.

#positive and negative reflect the possible responses to vote with.
#These can be changed as needed/desired.
positive = 'yes', 'yep', 'yepp', 'yeppers', 'yup', 'yupp', 'yuuup', 'ye', 'yee', 'ofc', 'yass'
negative = 'no', 'nope', 'nop', 'not on your nelly', 'no way', 'not a chance', 'hell no', 'no please', 'nu', 'nein'
#where is the image we're uploading going to be at? this case: it's nested. heavily.
photo ='/usr/src/app/pi.png'
global lastDayWatered
lastDayWatered = "N/A"

def startPeriod(): #this is the tweet that signifies the start of the voting period.
    timeNow = strftime('%a, %b %d %Y', localtime())
    global timeStartPeriod
    timeStartPeriod = strftime('%Y-%m-%d', localtime())
    #Your message. We need this to update every time we tweet, hence why it's here.
    #If you don't change anything (add/remove search terms), keep the piplant.cu.cc link, it's there to help users on what they can tweet.
    message = "It's {} now! Last watered on {}. Vote using #waterbecky and a word like YES or NO! Learn more @ piplant.cu.cc".format(timeNow, lastDayWatered)
    api.update_with_media(filename=photo, status=message)
    print(message)

def pollPeriod(): #this is the hard part: searching for relevant tweets, comparing the users of found tweets to sets to ensure they don't get counted for multiple votes, etc.
    #the basis of pollPeriod(). if yes>no, we water. if no>yes, we don't. if yes=no, we issue a tiebreaker.
    yes=0
    no=0
    #sets an end time should tweepy's since and until tags work correctly
    timeEndPeriod = strftime('%Y-%m-%d', localtime())
    ignoreUserY = set()
    ignoreUserN = set()

    #runs the search queries to gather the votes for positive and negative votes, adds 1 to their respective variable (positive adds to yes, negative adds to no)
    #and if the user has been caught voting already, counts only the most recent vote.
    print('Yes from:')
    for votes in positive:
        votesPos = api.search(q='waterbecky {}'.format(votes))
        for content in votesPos:
            #print(content.author._json['screen_name'])
            username = content.author._json['screen_name']
            if username not in ignoreUserY:
                print(username)
                ignoreUserY.add(username)
                yes += 1
            else:
                yes = yes
    print('No from:')
    for votes in negative:
        votesNeg = api.search(q='watertaila {}'.format(votes))
        for content in votesNeg:
            #print(content.author._json['screen_name'])
            username = content.author._json['screen_name']
            if username not in ignoreUserN:
                print(username)
                ignoreUserN.add(username)
                no += 1
            else:
                no = no

    print()
    print('=====Total Votes=====')
    print('{} to water.'.format(yes))
    print('{} to not water.'.format(no))

    if yes > no:
        print("\nWe're watering today!")
        api.update_status(status="We had more votes to water than to not. We're watering today!")
        lastDayWatered = strftime("%a")
    if no > yes:
        print("\nWe're not watering today!")
        api.update_status(status="We had more votes to not water than to. We're not watering today!")
    if yes == no:
        print("\nWe had equal votes today.")
        random.seed(version=2)
        tiebreaker = random.randrange(0, 1)
        if tiebreaker == 1:
            print("\nWe're watering today!")
            api.update_status(status="We had more votes to water than to not. We're watering today!")
            lastDayWatered = strftime("%a")
        if tiebreaker == 0:
            print("\nWe're not watering today!")
            api.update_status(status="We had more votes to not water than to. We're not watering today!")

schedule.every().day.at("11:00").do(pollPeriod)
schedule.every().day.at("23:00").do(startPeriod)
#schedule.every().day.at("01:25").do(startPeriod)
#schedule.every().day.at("01:27").do(pollPeriod)

while True:
    schedule.run_pending()
    time.sleep(1)
<commit_msg>Once Upon A Commit<commit_after>import tweepy
import schedule
import time
from auth import consumer_key, consumer_secret, key, secret
from time import strftime, localtime

auth = tweepy.OAuthHandler(consumer_key,  consumer_secret)
auth.set_access_token(key, secret)

api=tweepy.API(auth)
#All the code relating to twitter lies here. Before you make your own changes,
#make sure you've added your own API keys.

#positive and negative reflect the possible responses to vote with.
#These can be changed as needed/desired.
positive = 'yes', 'yep', 'yepp', 'yeppers', 'yup', 'yupp', 'yuuup', 'ye', 'yee', 'ofc', 'yass'
negative = 'no', 'nope', 'nop', 'not on your nelly', 'no way', 'not a chance', 'hell no', 'no please', 'nu', 'nein'
#where is the image we're uploading going to be at? this case: it's nested. heavily.
photo ='/usr/src/app/pi.png'
global lastDayWatered
lastDayWatered = "N/A"

def startPeriod(): #this is the tweet that signifies the start of the voting period.
    timeNow = strftime('%a, %b %d %Y', localtime())
    global timeStartPeriod
    timeStartPeriod = strftime('%Y-%m-%d', localtime())
    #Your message. We need this to update every time we tweet, hence why it's here.
    #If you don't change anything (add/remove search terms), keep the piplant.cu.cc link, it's there to help users on what they can tweet.
    message = "It's {} now! Last watered on {}. Vote using #waterbecky and a word like YES or NO! Learn more @ piplant.cu.cc".format(timeNow, lastDayWatered)
    api.update_with_media(filename=photo, status=message)
    print(message)

def pollPeriod(): #this is the hard part: searching for relevant tweets, comparing the users of found tweets to sets to ensure they don't get counted for multiple votes, etc.
    #the basis of pollPeriod(). if yes>no, we water. if no>yes, we don't. if yes=no, we issue a tiebreaker.
    yes=0
    no=0
    #sets an end time should tweepy's since and until tags work correctly
    timeEndPeriod = strftime('%Y-%m-%d', localtime())
    ignoreUserY = set()
    ignoreUserN = set()

    #runs the search queries to gather the votes for positive and negative votes, adds 1 to their respective variable (positive adds to yes, negative adds to no)
    #and if the user has been caught voting already, counts only the most recent vote.
    print('Yes from:')
    for votes in positive:
        votesPos = api.search(q='waterbecky {}'.format(votes))
        for content in votesPos:
            #print(content.author._json['screen_name'])
            username = content.author._json['screen_name']
            if username not in ignoreUserY:
                print(username)
                ignoreUserY.add(username)
                yes += 1
            else:
                yes = yes
    print('No from:')
    for votes in negative:
        votesNeg = api.search(q='watertaila {}'.format(votes))
        for content in votesNeg:
            #print(content.author._json['screen_name'])
            username = content.author._json['screen_name']
            if username not in ignoreUserN:
                print(username)
                ignoreUserN.add(username)
                no += 1
            else:
                no = no

    print()
    print('=====Total Votes=====')
    print('{} to water.'.format(yes))
    print('{} to not water.'.format(no))

    if yes > no:
        print("\nWe're watering today!")
        api.update_status(status="We had more votes to water than to not. We're watering today!")
        lastDayWatered = strftime("%a")
    if no > yes:
        print("\nWe're not watering today!")
        api.update_status(status="We had more votes to not water than to. We're not watering today!")
    if yes == no:
        print("\nWe had equal votes today.")
        random.seed(version=2)
        tiebreaker = random.randrange(0, 1)
        if tiebreaker == 1:
            print("\nWe're watering today!")
            api.update_status(status="We had more votes to water than to not. We're watering today!")
            lastDayWatered = strftime("%a")
        if tiebreaker == 0:
            print("\nWe're not watering today!")
            api.update_status(status="We had more votes to not water than to. We're not watering today!")

schedule.every().day.at("03:00").do(pollPeriod)
schedule.every().day.at("07:00").do(startPeriod)
schedule.every().day.at("03:45").do(startPeriod)
schedule.every().day.at("03:47").do(pollPeriod)

while True:
    schedule.run_pending()
    time.sleep(1)
<|endoftext|>