<commit_before>"""This module stores the CadImporter class, which is used to load CAD parts."""

import collections
import math
import os
import pdb
# needed to prevent dxfgrabber from crashing on import
os.environ['DXFGRABBER_CYTHON'] = 'OFF'
import dxfgrabber # needed for dxf files
import subprocess # needed to run gmsh to make geo files

from . import geometry
from . import partmodule

class CadImporter(object):
    """Makes an object which can import cad parts.

    Args:
        feamodel (FeaModel): model that we want to import parts into
        layer (int): layer to import, all entities will be flattened
            to one plane. -1 mean all layers, any other number is a specific layer
        swapxy (bool): True rotates the part from x axial to y axial
        scale (str): string telling the unit conversion scalar

            * Any option of 'fromunit-tounit' using the below units
            * mm, m, in, ft
            * Examples 'mm-m' 'm-in' 'ft-mm' 'in-ft'
            * Default value is '' and does not apply a scale factor

    Attributes:
        __fea (FeaModel): model
        __fname (str): file we want to import
        __layer (int): layer to import
        __swapxy (bool): If true, swap part from xy to yx orientation
    """

    def __init__(self, feamodel, fname='', layer=-1, swapxy=False, scale=''):
        self.__fea = feamodel
        self.__fname = fname
        self.__layer = layer
        self.__swapxy = swapxy
        self.__scale = scale

    def load(self):
        """Loads the self.__fname cad file

        Returns:
            list: list of Part
        """
        if self.__fname == '':
            print('You must pass in a file name to load!')
            return []

        fname_list = self.__fname.split('.')
        ext = fname_list[1]
        first_pt = None
        if len(self.__fea.points) > 0:
            first_pt = self.__fea.points[0]
        if ext == 'dxf':
            parts = self.__load_dxf()
        elif ext in ['brep', 'brp', 'iges', 'igs', 'step', 'stp']:
            self.__make_geo()
            parts = self.__load_geo()
        last_pt = None
        if first_pt != None:
            if len(self.__fea.points) > 2:
                last_pt = self.__fea.points[-1]
        if self.__scale != '':
            # call scale
            pass
        return parts

    def __fix_point(self, point):
        """Adjusts the point to be in the right plane (yx)"""
        if self.__swapxy:
            return geometry.Point(point.y, point.x)
        return point

    def __get_pt(self, points, point):
        """Returns a point if it is within accuracy of point in points"""
        for realpoint in points:
            dist = point - realpoint
            dist = dist.length()
            if dist < geometry.ACC:
                return realpoint
        return point

    def __get_pts_lines(self, lines, arcs):
        """Returns a set of points, and a list of Lines and Arcs

        Args:
            lines (dxfgrabber LINE list): dxf lines
            arcs (dxfgrabber ARC list): dxf arcs

        Returns:
            list: [list of points, list of Line and Arc]
        """
        # store unique points
        point_set = set()
        all_lines = []
        for ind, line in enumerate(lines):
            start = geometry.Point(line.start[0], line.start[1])
            end = geometry.Point(line.end[0], line.end[1])
            start, end = self.__fix_point(start), self.__fix_point(end)
            point_set.update([start, end])
            line = geometry.Line(start, end)
            all_lines.append(line)
        for ind, arc in enumerate(arcs):
            # dxfgrabber arcs are stored ccw when looking at xy plane
            # x horizontal
            # y vertical
            center = geometry.Point(arc.center[0], arc.center[1])
            sign = 1
            if self.__swapxy == False:
                sign = -1
            center = self.__fix_point(center)
            point_set.add(center)
            startangle = arc.start_angle*sign
            endangle = arc.end_angle*sign
            angle = endangle - startangle
            if arc.end_angle < arc.start_angle:
                angle = angle + 360*sign
            """
            print('---------------------------------------')
            print('| ARC')
            print('center: %s' % center)
            print('startangle: %f' % startangle)
            print('endangle: %f' % endangle)
            print('traversed_angle: %f' % angle)
            """
            start_vect = geometry.Point(0, arc.radius)
            if self.__swapxy == False:
                start_vect = geometry.Point(arc.radius, 0)
            start_vect.rot_ccw_deg(arc.start_angle*sign)
            end_vect = geometry.Point(0, arc.radius)
            if self.__swapxy == False:
                end_vect = geometry.Point(arc.radius, 0)
            end_vect.rot_ccw_deg(arc.end_angle*sign)
            start = center + start_vect
            end = center + end_vect
            start = self.__get_pt(point_set, start)
            end = self.__get_pt(point_set, end)
            point_set.update([start, end])
            rvect = start - center
            if abs(angle) <= 90:
                arc = geometry.Arc(start, end, center)
                all_lines.append(arc)
                print('1 arc made')
                continue
                #print(' %s' % arc)
            pieces = math.ceil(abs(angle)/90)
            print('%i arcs being made' % pieces)
            points = [start, end]
            # 2 pieces need 3 points, we have start + end already --> 1 pt
            inserts = pieces + 1 - 2
            piece_ang = angle/pieces
            #print('piece_ang = %f' % piece_ang)
            while inserts > 0:
                rvect.rot_ccw_deg(piece_ang)
                point = center + rvect
                points.insert(-1, point)
                inserts = inserts - 1
            for ind in range(len(points)-1):
                point_set.update([points[ind], points[ind+1]])
                arc = geometry.Arc(points[ind], points[ind+1], center)
                #print(' %s' % arc)
                all_lines.append(arc)
        for line in all_lines:
            line.save_to_points()
        return [list(point_set), all_lines]

    def __make_geo(self):
        """Makes a gmsh geo file given a step, iges, or brep input"""
        # gmsh freecad_part.iges -o out_iges.geo -0
        fname_list = self.__fname.split('.')
        geo_file = fname_list[0]+'.geo'
        runstr = "%s %s -o %s -0" % (environment.GMSH, self.__fname, geo_file)
        print(runstr)
        subprocess.call(runstr, shell=True)
        print('Wrote file: %s' % geo_file)

    def __load_geo(self):
        """Loads in a gmsh geo file and returns a list of parts

        Returns:
            list: list of Part
        """
        pass
        # process any splines? and turn them into arcs
        # http://www.mathopenref.com/constcirclecenter.html
        # find max dist between points
        # double it
        # select two segments
        # draw normal lines
        # find intersections, that is the center

    @staticmethod
    def __dangling_points(all_points):
        return [point for point in all_points
                if len(point.lines) == 1 and not point.arc_center]

    def __load_dxf(self):
        """Loads in a dxf file and returns a list of parts

        Returns:
            list: list of Part
        """
        # pdb.set_trace()
        print('Loading file: %s' % self.__fname)
        dwg = dxfgrabber.readfile(self.__fname)
        lines = [item for item in dwg.entities if item.dxftype == 'LINE']
        arcs = [item for item in dwg.entities if item.dxftype == 'ARC']
        if self.__layer > -1:
            lines = [item for item in lines if item.layer == self.__layer]
            arcs = [item for item in arcs if item.layer == self.__layer]
        print('File read.')
        print('Loaded %i lines' % len(lines))
        print('Loaded %i arcs' % len(arcs))

        # get all points and Line and Arc using pycalculix entities
        print('Converting to pycalculix lines arcs and points ...')
        all_points, all_lines = self.__get_pts_lines(lines, arcs)
        print('Loaded %i line segments, lines or arcs' % len(all_lines))
        print('Loaded %i points' % len(all_points))
        for point in all_points:
            print('%s %s' % (point, point.lines))

        # remove all lines that are not part of areas
        dangling_points = self.__dangling_points(all_points)
        # pdb.set_trace()
        pruned_geometry = bool(dangling_points)
        while dangling_points:
            for point in dangling_points:
                all_points.remove(point)
                print('Removed point= %s' % point)
                dangling_line = list(point.lines)[0]
                point.unset_line(dangling_line)
                if dangling_line in all_lines:
                    all_lines.remove(dangling_line)
                    print('Removed line= %s' % dangling_line)
            dangling_points = self.__dangling_points(all_points)
        if pruned_geometry:
            print('Remaining line segments: %i' % len(all_lines))
            print('Remaining points: %i' % len(all_points))

        # make line loops now
        loops = []
        line = all_lines[0]
        this_loop = geometry.LineLoop()
        while len(all_lines) > 0:
            this_loop.append(line)
            all_lines.remove(line)
            if this_loop.closed == True:
                loops.append(this_loop)
                this_loop = geometry.LineLoop()
                if all_lines:
                    line = all_lines[0]
                continue
            point = line.pt(1)
            other_lines = point.lines - set([line])
            if len(other_lines) > 1:
                # note: one could exclude connected segment nodes
                # make disconnected line loops, then have another
                # loop to connect thos disconnected line loops
                print('One point was connected to > 2 lines.')
                print('Only import simple part loops, or surfaces.')
                raise Exception('Import geometry is too complex')
            next_line = list(other_lines)[0]
            if line.pt(1) != next_line.pt(0):
                next_line.reverse()
            line = next_line

        # check loops to see if one is inside another
        # if no loops inside self, then self is a part
        # each part in parts is a list of line loops [exterior, hole1, hole2]
        parts = []
        for ind, loop in enumerate(loops):
            other_loops = loops[ind+1:]
            is_exterior = True
            for other_loop in other_loops:
                if loop.inside(other_loop):
                    is_exterior = False
                    break
            if is_exterior:
                # exterior must be clockwise
                if loop.ccw == True:
                    loop.reverse()
                parts.append(loop)
        # remove the exterior loops from our loops list
        for loop in parts:
            loops.remove(loop)
        # now place the child loops under the part exterior loops
        for ind in range(len(parts)):
            exterior_loop = parts[ind]
            holes = []
            # find child holes
            for loop in loops:
                if loop.inside(exterior_loop):
                    # holes must be ccw
                    loop.hole = True
                    if loop.ccw == False:
                        loop.reverse()
                    holes.append(loop)
            loop_list = [exterior_loop] + holes
            parts[ind] = loop_list
            # remove child holes from loop list
            for hole in holes:
                loops.remove(hole)

        # make parts
        parts_list = []
        for part_loops in parts:
            this_part = partmodule.Part(self.__fea)
            for ind, loop in enumerate(part_loops):
                is_hole = loop.hole
                start = loop[0].pt(0)
                this_part.goto(start.x, start.y, is_hole)
                for item in loop:
                    if isinstance(item, geometry.Line):
                        end = item.pt(1)
                        this_part.draw_line_to(end.x, end.y)
                    elif isinstance(item, geometry.Arc):
                        end = item.pt(1)
                        center = item.actr
                        this_part.draw_arc(end.x, end.y, center.x, center.y)
            parts_list.append(this_part)
        print('Parts created: %i' % len(parts_list))
        return parts_list
<commit_msg>Adds point and line printing<commit_after>"""This module stores the CadImporter class, which is used to load CAD parts."""

import collections
import math
import os
import pdb
# needed to prevent dxfgrabber from crashing on import
os.environ['DXFGRABBER_CYTHON'] = 'OFF'
import dxfgrabber # needed for dxf files
import subprocess # needed to run gmsh to make geo files

from . import geometry
from . import partmodule

class CadImporter(object):
    """Makes an object which can import cad parts.

    Args:
        feamodel (FeaModel): model that we want to import parts into
        layer (int): layer to import, all entities will be flattened
            to one plane. -1 mean all layers, any other number is a specific layer
        swapxy (bool): True rotates the part from x axial to y axial
        scale (str): string telling the unit conversion scalar

            * Any option of 'fromunit-tounit' using the below units
            * mm, m, in, ft
            * Examples 'mm-m' 'm-in' 'ft-mm' 'in-ft'
            * Default value is '' and does not apply a scale factor

    Attributes:
        __fea (FeaModel): model
        __fname (str): file we want to import
        __layer (int): layer to import
        __swapxy (bool): If true, swap part from xy to yx orientation
    """

    def __init__(self, feamodel, fname='', layer=-1, swapxy=False, scale=''):
        self.__fea = feamodel
        self.__fname = fname
        self.__layer = layer
        self.__swapxy = swapxy
        self.__scale = scale

    def load(self):
        """Loads the self.__fname cad file

        Returns:
            list: list of Part
        """
        if self.__fname == '':
            print('You must pass in a file name to load!')
            return []

        fname_list = self.__fname.split('.')
        ext = fname_list[1]
        first_pt = None
        if len(self.__fea.points) > 0:
            first_pt = self.__fea.points[0]
        if ext == 'dxf':
            parts = self.__load_dxf()
        elif ext in ['brep', 'brp', 'iges', 'igs', 'step', 'stp']:
            self.__make_geo()
            parts = self.__load_geo()
        last_pt = None
        if first_pt != None:
            if len(self.__fea.points) > 2:
                last_pt = self.__fea.points[-1]
        if self.__scale != '':
            # call scale
            pass
        return parts

    def __fix_point(self, point):
        """Adjusts the point to be in the right plane (yx)"""
        if self.__swapxy:
            return geometry.Point(point.y, point.x)
        return point

    def __get_pt(self, points, point):
        """Returns a point if it is within accuracy of point in points"""
        for realpoint in points:
            dist = point - realpoint
            dist = dist.length()
            if dist < geometry.ACC:
                return realpoint
        return point

    def __get_pts_lines(self, lines, arcs):
        """Returns a set of points, and a list of Lines and Arcs

        Args:
            lines (dxfgrabber LINE list): dxf lines
            arcs (dxfgrabber ARC list): dxf arcs

        Returns:
            list: [list of points, list of Line and Arc]
        """
        # store unique points
        point_set = set()
        all_lines = []
        for ind, line in enumerate(lines):
            start = geometry.Point(line.start[0], line.start[1])
            end = geometry.Point(line.end[0], line.end[1])
            start, end = self.__fix_point(start), self.__fix_point(end)
            point_set.update([start, end])
            line = geometry.Line(start, end)
            all_lines.append(line)
        for ind, arc in enumerate(arcs):
            # dxfgrabber arcs are stored ccw when looking at xy plane
            # x horizontal
            # y vertical
            center = geometry.Point(arc.center[0], arc.center[1])
            sign = 1
            if self.__swapxy == False:
                sign = -1
            center = self.__fix_point(center)
            point_set.add(center)
            startangle = arc.start_angle*sign
            endangle = arc.end_angle*sign
            angle = endangle - startangle
            if arc.end_angle < arc.start_angle:
                angle = angle + 360*sign
            """
            print('---------------------------------------')
            print('| ARC')
            print('center: %s' % center)
            print('startangle: %f' % startangle)
            print('endangle: %f' % endangle)
            print('traversed_angle: %f' % angle)
            """
            start_vect = geometry.Point(0, arc.radius)
            if self.__swapxy == False:
                start_vect = geometry.Point(arc.radius, 0)
            start_vect.rot_ccw_deg(arc.start_angle*sign)
            end_vect = geometry.Point(0, arc.radius)
            if self.__swapxy == False:
                end_vect = geometry.Point(arc.radius, 0)
            end_vect.rot_ccw_deg(arc.end_angle*sign)
            start = center + start_vect
            end = center + end_vect
            start = self.__get_pt(point_set, start)
            end = self.__get_pt(point_set, end)
            point_set.update([start, end])
            rvect = start - center
            if abs(angle) <= 90:
                arc = geometry.Arc(start, end, center)
                all_lines.append(arc)
                print('1 arc made')
                continue
                #print(' %s' % arc)
            pieces = math.ceil(abs(angle)/90)
            print('%i arcs being made' % pieces)
            points = [start, end]
            # 2 pieces need 3 points, we have start + end already --> 1 pt
            inserts = pieces + 1 - 2
            piece_ang = angle/pieces
            #print('piece_ang = %f' % piece_ang)
            while inserts > 0:
                rvect.rot_ccw_deg(piece_ang)
                point = center + rvect
                points.insert(-1, point)
                inserts = inserts - 1
            for ind in range(len(points)-1):
                point_set.update([points[ind], points[ind+1]])
                arc = geometry.Arc(points[ind], points[ind+1], center)
                #print(' %s' % arc)
                all_lines.append(arc)
        for line in all_lines:
            line.save_to_points()
        return [list(point_set), all_lines]

    def __make_geo(self):
        """Makes a gmsh geo file given a step, iges, or brep input"""
        # gmsh freecad_part.iges -o out_iges.geo -0
        fname_list = self.__fname.split('.')
        geo_file = fname_list[0]+'.geo'
        runstr = "%s %s -o %s -0" % (environment.GMSH, self.__fname, geo_file)
        print(runstr)
        subprocess.call(runstr, shell=True)
        print('Wrote file: %s' % geo_file)

    def __load_geo(self):
        """Loads in a gmsh geo file and returns a list of parts

        Returns:
            list: list of Part
        """
        pass
        # process any splines? and turn them into arcs
        # http://www.mathopenref.com/constcirclecenter.html
        # find max dist between points
        # double it
        # select two segments
        # draw normal lines
        # find intersections, that is the center

    @staticmethod
    def __dangling_points(all_points):
        return [point for point in all_points
                if len(point.lines) == 1 and not point.arc_center]

    def __load_dxf(self):
        """Loads in a dxf file and returns a list of parts

        Returns:
            list: list of Part
        """
        # pdb.set_trace()
        print('Loading file: %s' % self.__fname)
        dwg = dxfgrabber.readfile(self.__fname)
        lines = [item for item in dwg.entities if item.dxftype == 'LINE']
        arcs = [item for item in dwg.entities if item.dxftype == 'ARC']
        if self.__layer > -1:
            lines = [item for item in lines if item.layer == self.__layer]
            arcs = [item for item in arcs if item.layer == self.__layer]
        print('File read.')
        print('Loaded %i lines' % len(lines))
        print('Loaded %i arcs' % len(arcs))

        # get all points and Line and Arc using pycalculix entities
        print('Converting to pycalculix lines arcs and points ...')
        all_points, all_lines = self.__get_pts_lines(lines, arcs)
        print('Loaded %i line segments, lines or arcs' % len(all_lines))
        print('Loaded %i points' % len(all_points))
        for point in all_points:
            print('%s %s' % (point, point.lines))
        for line in all_lines:
            print('%s %s' % (line, point.points))

        # remove all lines that are not part of areas
        dangling_points = self.__dangling_points(all_points)
        # pdb.set_trace()
        pruned_geometry = bool(dangling_points)
        while dangling_points:
            for point in dangling_points:
                all_points.remove(point)
                print('Removed point= %s' % point)
                dangling_line = list(point.lines)[0]
                point.unset_line(dangling_line)
                if dangling_line in all_lines:
                    all_lines.remove(dangling_line)
                    print('Removed line= %s' % dangling_line)
            dangling_points = self.__dangling_points(all_points)
        if pruned_geometry:
            print('Remaining line segments: %i' % len(all_lines))
            print('Remaining points: %i' % len(all_points))

        # make line loops now
        loops = []
        line = all_lines[0]
        this_loop = geometry.LineLoop()
        while len(all_lines) > 0:
            this_loop.append(line)
            all_lines.remove(line)
            if this_loop.closed == True:
                loops.append(this_loop)
                this_loop = geometry.LineLoop()
                if all_lines:
                    line = all_lines[0]
                continue
            point = line.pt(1)
            other_lines = point.lines - set([line])
            if len(other_lines) > 1:
                # note: one could exclude connected segment nodes
                # make disconnected line loops, then have another
                # loop to connect thos disconnected line loops
                print('One point was connected to > 2 lines.')
                print('Only import simple part loops, or surfaces.')
                raise Exception('Import geometry is too complex')
            next_line = list(other_lines)[0]
            if line.pt(1) != next_line.pt(0):
                next_line.reverse()
            line = next_line

        # check loops to see if one is inside another
        # if no loops inside self, then self is a part
        # each part in parts is a list of line loops [exterior, hole1, hole2]
        parts = []
        for ind, loop in enumerate(loops):
            other_loops = loops[ind+1:]
            is_exterior = True
            for other_loop in other_loops:
                if loop.inside(other_loop):
                    is_exterior = False
                    break
            if is_exterior:
                # exterior must be clockwise
                if loop.ccw == True:
                    loop.reverse()
                parts.append(loop)
        # remove the exterior loops from our loops list
        for loop in parts:
            loops.remove(loop)
        # now place the child loops under the part exterior loops
        for ind in range(len(parts)):
            exterior_loop = parts[ind]
            holes = []
            # find child holes
            for loop in loops:
                if loop.inside(exterior_loop):
                    # holes must be ccw
                    loop.hole = True
                    if loop.ccw == False:
                        loop.reverse()
                    holes.append(loop)
            loop_list = [exterior_loop] + holes
            parts[ind] = loop_list
            # remove child holes from loop list
            for hole in holes:
                loops.remove(hole)

        # make parts
        parts_list = []
        for part_loops in parts:
            this_part = partmodule.Part(self.__fea)
            for ind, loop in enumerate(part_loops):
                is_hole = loop.hole
                start = loop[0].pt(0)
                this_part.goto(start.x, start.y, is_hole)
                for item in loop:
                    if isinstance(item, geometry.Line):
                        end = item.pt(1)
                        this_part.draw_line_to(end.x, end.y)
                    elif isinstance(item, geometry.Arc):
                        end = item.pt(1)
                        center = item.actr
                        this_part.draw_arc(end.x, end.y, center.x, center.y)
            parts_list.append(this_part)
        print('Parts created: %i' % len(parts_list))
        return parts_list
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Created on Sun Mar 06 00:27:21 2016
@author: maheshwa
"""

from datetime import date, timedelta
import requests, time, binascii, hashlib, json, urllib #adding hashlib
import pandas as pd

# Authentication
class AdobeAnalytics:

    def __init__(self, user_name, shared_secret, endpoint='', debug = False):
        """
        Entry point for making authenticated API calls to the Adobe Report API's
        """
        self.__user_name = user_name
        self.__shared_secret = shared_secret
        self.__company = self.__user_name.split(":")[1]
        self.__debug = debug

        #If user doesn't specify their own endpoint, call API to get proper endpoint
        #Most users should never do this, but some have requested capability in RSiteCatalyst
        self.__api_url = 'https://api.omniture.com/admin/1.4/rest/'
        if endpoint != '':
            self.__api_url = endpoint
        else:
            self.__api_url = self.GetEndpoint(company = self.__company)

    def __buildheader(self):
        """
        Returns required header for authenticating API calls. This is an internal method to be used
        by other public method calls.
        """
        nonce = str(time.time())
        base64nonce = binascii.b2a_base64(binascii.a2b_qp(nonce))
        created_date = time.strftime("%Y-%m-%dT%H:%M:%SZ",  time.gmtime())
        sha_object = hashlib.sha1(nonce + created_date + '%s' % (self.__shared_secret))
        password_64 = binascii.b2a_base64(sha_object.digest())
        X_str = 'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"' % ('%s:%s' % (self.__user_name, self.__company), password_64.strip(), base64nonce.strip(), created_date)
        return {'X-WSSE':X_str}

    #Core Method, Actually runs the JSON request
    def __callapi(self, endpoint, verb = "POST", **kwargs):
        header = self.__buildheader()
        if verb == "GET":
            req = requests.get('%s?method=%s' % (self.__api_url, endpoint), params=json.dumps(kwargs), headers=header)
            if self.__debug:
                print json.dumps(kwargs)
        else:
            req = requests.post('%s?method=%s' % (self.__api_url, endpoint), data=json.dumps(kwargs), headers=header)
            if self.__debug:
                print json.dumps(kwargs)
        return req.json()

    def GetActivation(self, rsid_list):
        """
        Returns the activation date for the report suite(s) specified.

        Keyword arguments:
        rsid_list -- Report suites to evaluate
        """
        return self.__callapi('ReportSuite.GetActivation', rsid_list = rsid_list)

    def GetAxleStartDate(self):
        return self.__callapi('ReportSuite.GetAxleStartDate', )

    def GetBaseCurrency(self, ):
        return self.__callapi('ReportSuite.GetBaseCurrency', )

    def GetBaseURL(self, ):
        return self.__callapi('ReportSuite.GetBaseURL', )

    def GetBookmarks(self, ):
        return self.__callapi('Bookmark.GetBookmarks', )

    def GetCalculatedMetrics(self, ):
        return self.__callapi('ReportSuite.GetCalculatedMetrics', )

    def GetClassifications(self, ):
        return self.__callapi('ReportSuite.GetClassifications', )

    def GetCustomCalendar(self, ):
        return self.__callapi('ReportSuite.GetCustomCalendar', )

    def GetDashboards(self, ):
        return self.__callapi('Bookmark.GetDashboards', )

    def GetDataWarehouseDisplay(self, ):
        return self.__callapi('ReportSuite.GetDataWarehouseDisplay', )

    def GetDefaultPage(self, ):
        return self.__callapi('ReportSuite.GetDefaultPage', )

    def GetDiscoverEnabled(self, ):
        return self.__callapi('ReportSuite.GetDiscoverEnabled', )

    def GetEcommerce(self, ):
        return self.__callapi('ReportSuite.GetEcommerce', )

    def GetElements(self, ):
        return self.__callapi('Report.GetElements', )

    def GetEndpoint(self, company):
        """
        Calls Company.GetEndpoint to determine the appropriate endpoint for a give company

        Keyword arguments:
        company -- Company to retrieve endpoint for
        """
        return self.__callapi('Company.GetEndpoint', "GET", company = company)

    def GetEvars(self, ):
        return self.__callapi('ReportSuite.GetEvars', )

    def GetFeed(self, ):
        return self.__callapi('DataFeed.GetFeed', )

    def GetFeeds(self, ):
        return self.__callapi('DataFeed.GetFeeds', )

    def GetFunctions(self, ):
        return self.__callapi('CalculatedMetrics.GetFunctions', )

    def GetGeoSegmentation(self, ):
        return self.__callapi('ReportSuite.GetGeoSegmentation', )

    def GetGroups(self, ):
        return self.__callapi('Permissions.GetGroups', )

    def GetInternalURLFilters(self, ):
        return self.__callapi('ReportSuite.GetInternalURLFilters', )

    def GetIPAddressExclusions(self, ):
        return self.__callapi('ReportSuite.GetIPAddressExclusions', )

    def GetIPObfuscation(self, ):
        return self.__callapi('ReportSuite.GetIPObfuscation', )

    def GetKeyVisitors(self, ):
        return self.__callapi('ReportSuite.GetKeyVisitors', )

    def GetListVariables(self, ):
        return self.__callapi('ReportSuite.GetListVariables', )

    def GetLocalization(self, ):
        return self.__callapi('ReportSuite.GetLocalization', )

    def GetLogin(self, ):
        return self.__callapi('Permissions.GetLogin', )

    def GetLogins(self, ):
        return self.__callapi('Permissions.GetLogins', )

    def GetMarketingChannelExpiration(self, ):
        return self.__callapi('ReportSuite.GetMarketingChannelExpiration', )

    def GetMarketingChannelRules(self, ):
        return self.__callapi('ReportSuite.GetMarketingChannelRules', )

    def GetMarketingChannels(self, ):
        return self.__callapi('ReportSuite.GetMarketingChannels', )

    def GetMetrics(self, ):
        return self.__callapi('Report.GetMetrics', )

    def GetMobileAppReporting(self, ):
        return self.__callapi('ReportSuite.GetMobileAppReporting', )

    def GetPaidSearchDetection(self, ):
        return self.__callapi('ReportSuite.GetPaidSearchDetection', )

    def GetPrivacySettings(self, ):
        return self.__callapi('ReportSuite.GetPrivacySettings', )

    def GetProps(self, ):
        return self.__callapi('ReportSuite.GetProps', )

    def GetQueue(self, ):
        return self.__callapi('Report.GetQueue', )

    def GetRealTimeReport(self, ):
        return self.__callapi('Company.GetReportSuites', )

    def GetRealTimeSettings(self, ):
        return self.__callapi('ReportSuite.GetRealTimeSettings', )

    def GetReportDescription(self, ):
        return self.__callapi('Bookmark.GetReportDescription', )

    def GetReportSuites(self):
        """Returns all report suites available to user from a given company."""
        return self.__callapi('Company.GetReportSuites')

    def GetScheduledSpike(self, ):
        return self.__callapi('ReportSuite.GetScheduledSpike', )

    def GetSegments(self, ):
        return self.__callapi('ReportSuite.GetSegments', )

    def GetSiteTitle(self, ):
        return self.__callapi('ReportSuite.GetSiteTitle', )

    def GetEvents(self, ):
        return self.__callapi('ReportSuite.GetEvents', )

    def GetTemplate(self, ):
        return self.__callapi('ReportSuite.GetTemplate', )

    def GetTimeZone(self, ):
        return self.__callapi('ReportSuite.GetTimeZone', )

    def GetTrackingServer(self, ):
        return self.__callapi('Company.GetTrackingServer', )

    def GetTransactionEnabled(self, ):
        return self.__callapi('ReportSuite.GetTransactionEnabled', )

    def GetUniqueVisitorVariable(self, ):
        return self.__callapi('ReportSuite.GetUniqueVisitorVariable', )

    def GetVersionAccess(self, ):
        return self.__callapi('Company.GetVersionAccess', )

    def GetVideoSettings(self, ):
        return self.__callapi('ReportSuite.GetVideoSettings', )

<commit_msg>Add arguments to selected functions<commit_after># -*- coding: utf-8 -*-
"""
Created on Sun Mar 06 00:27:21 2016
@author: maheshwa
"""

from datetime import date, timedelta
import requests, time, binascii, hashlib, json, urllib #adding hashlib
import pandas as pd

# Authentication
class AdobeAnalytics:

    def __init__(self, user_name, shared_secret, endpoint='', debug = False):
        """
        Entry point for making authenticated API calls to the Adobe Report API's
        """
        self.__user_name = user_name
        self.__shared_secret = shared_secret
        self.__company = self.__user_name.split(":")[1]
        self.__debug = debug

        #If user doesn't specify their own endpoint, call API to get proper endpoint
        #Most users should never do this, but some have requested capability in RSiteCatalyst
        self.__api_url = 'https://api.omniture.com/admin/1.4/rest/'
        if endpoint != '':
            self.__api_url = endpoint
        else:
            self.__api_url = self.GetEndpoint(company = self.__company)

    def __buildheader(self):
        """
        Returns required header for authenticating API calls. This is an internal method to be used
        by other public method calls.
        """
        nonce = str(time.time())
        base64nonce = binascii.b2a_base64(binascii.a2b_qp(nonce))
        created_date = time.strftime("%Y-%m-%dT%H:%M:%SZ",  time.gmtime())
        sha_object = hashlib.sha1(nonce + created_date + '%s' % (self.__shared_secret))
        password_64 = binascii.b2a_base64(sha_object.digest())
        X_str = 'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"' % ('%s:%s' % (self.__user_name, self.__company), password_64.strip(), base64nonce.strip(), created_date)
        return {'X-WSSE':X_str}

    def __callapi(self, endpoint, verb = "POST", **kwargs):
        """
        Calls the Adobe Analytics API at a given endpoint and variable arguments
        """
        header = self.__buildheader()
        if verb == "GET":
            req = requests.get('%s?method=%s' % (self.__api_url, endpoint), params=json.dumps(kwargs), headers=header)
            if self.__debug:
                print json.dumps(kwargs)
        else:
            req = requests.post('%s?method=%s' % (self.__api_url, endpoint), data=json.dumps(kwargs), headers=header)
            if self.__debug:
                print json.dumps(kwargs)
        return req.json()

    def GetActivation(self, rsid_list):
        """
        Returns the activation date for the report suite(s) specified.

        Keyword arguments:
        rsid_list -- Report suites to evaluate
        """
        return self.__callapi('ReportSuite.GetActivation', rsid_list = rsid_list)

    def GetAxleStartDate(self, rsid_list):
        return self.__callapi('ReportSuite.GetAxleStartDate', rsid_list = rsid_list)

    def GetBaseCurrency(self, rsid_list):
        return self.__callapi('ReportSuite.GetBaseCurrency', rsid_list = rsid_list)

    def GetBaseURL(self, rsid_list):
        return self.__callapi('ReportSuite.GetBaseURL', rsid_list = rsid_list)

    def GetBookmarks(self, ):
        return self.__callapi('Bookmark.GetBookmarks', )

    def GetCalculatedMetrics(self, rsid_list):
        return self.__callapi('ReportSuite.GetCalculatedMetrics', rsid_list = rsid_list)

    def GetClassifications(self, ):
        return self.__callapi('ReportSuite.GetClassifications', )

    def GetCustomCalendar(self, rsid_list):
        return self.__callapi('ReportSuite.GetCustomCalendar', rsid_list = rsid_list)

    def GetDashboards(self, ):
        return self.__callapi('Bookmark.GetDashboards', )

    def GetDataWarehouseDisplay(self, rsid_list):
        return self.__callapi('ReportSuite.GetDataWarehouseDisplay', rsid_list = rsid_list)

    def GetDefaultPage(self, rsid_list):
        return self.__callapi('ReportSuite.GetDefaultPage', rsid_list = rsid_list)

    def GetDiscoverEnabled(self, rsid_list):
        return self.__callapi('ReportSuite.GetDiscoverEnabled', rsid_list = rsid_list)

    def GetEcommerce(self, rsid_list):
        return self.__callapi('ReportSuite.GetEcommerce', rsid_list = rsid_list)

    def GetElements(self, ):
        return self.__callapi('Report.GetElements', )

    def GetEndpoint(self, company):
        """
        Calls Company.GetEndpoint to determine the appropriate endpoint for a give company

        Keyword arguments:
        company -- Company to retrieve endpoint for
        """
        return self.__callapi('Company.GetEndpoint', "GET", company = company)

    def GetEvars(self, rsid_list):
        return self.__callapi('ReportSuite.GetEvars', rsid_list = rsid_list)

    def GetFeed(self, ):
        return self.__callapi('DataFeed.GetFeed', )

    def GetFeeds(self, ):
        return self.__callapi('DataFeed.GetFeeds', )

    def GetFunctions(self, ):
        return self.__callapi('CalculatedMetrics.GetFunctions', )

    def GetGeoSegmentation(self, rsid_list):
        return self.__callapi('ReportSuite.GetGeoSegmentation', rsid_list = rsid_list)

    def GetGroups(self, ):
        return self.__callapi('Permissions.GetGroups', )

    def GetInternalURLFilters(self, rsid_list):
        return self.__callapi('ReportSuite.GetInternalURLFilters', rsid_list = rsid_list)

    def GetIPAddressExclusions(self, rsid_list):
        return self.__callapi('ReportSuite.GetIPAddressExclusions', rsid_list = rsid_list)

    def GetIPObfuscation(self, rsid_list):
        return self.__callapi('ReportSuite.GetIPObfuscation', rsid_list = rsid_list)

    def GetKeyVisitors(self, rsid_list):
        return self.__callapi('ReportSuite.GetKeyVisitors', rsid_list = rsid_list)

    def GetListVariables(self, rsid_list):
        return self.__callapi('ReportSuite.GetListVariables', rsid_list = rsid_list)

    def GetLocalization(self, rsid_list):
        return self.__callapi('ReportSuite.GetLocalization', rsid_list = rsid_list)

    def GetLogin(self, ):
        return self.__callapi('Permissions.GetLogin', )

    def GetLogins(self, ):
        return self.__callapi('Permissions.GetLogins', )

    def GetMarketingChannelExpiration(self, rsid_list):
        return self.__callapi('ReportSuite.GetMarketingChannelExpiration', rsid_list = rsid_list)

    def GetMarketingChannelRules(self, rsid_list):
        return self.__callapi('ReportSuite.GetMarketingChannelRules', rsid_list = rsid_list)

    def GetMarketingChannels(self, rsid_list):
        return self.__callapi('ReportSuite.GetMarketingChannels',  rsid_list = rsid_list)

    def GetMetrics(self, ):
        return self.__callapi('Report.GetMetrics', )

    def GetMobileAppReporting(self, rsid_list):
        return self.__callapi('ReportSuite.GetMobileAppReporting', rsid_list = rsid_list)

    def GetPaidSearchDetection(self, rsid_list):
        return self.__callapi('ReportSuite.GetPaidSearchDetection', rsid_list = rsid_list)

    def GetPrivacySettings(self, rsid_list):
        return self.__callapi('ReportSuite.GetPrivacySettings', rsid_list = rsid_list)

    def GetProps(self, rsid_list):
        return self.__callapi('ReportSuite.GetProps', rsid_list = rsid_list)

    def GetQueue(self ):
        return self.__callapi('Report.GetQueue')

    def GetRealTimeReport(self, ):
        return self.__callapi('Company.GetReportSuites', )

    def GetRealTimeSettings(self, rsid_list):
        return self.__callapi('ReportSuite.GetRealTimeSettings', rsid_list = rsid_list)

    def GetReportDescription(self, ):
        return self.__callapi('Bookmark.GetReportDescription', )

    def GetReportSuites(self):
        """Returns all report suites available to user from a given company."""
        return self.__callapi('Company.GetReportSuites')

    def GetScheduledSpike(self, rsid_list):
        return self.__callapi('ReportSuite.GetScheduledSpike', rsid_list = rsid_list)

    def GetSegments(self, rsid_list):
        return self.__callapi('ReportSuite.GetSegments', rsid_list = rsid_list)

    def GetSiteTitle(self, rsid_list):
        return self.__callapi('ReportSuite.GetSiteTitle', rsid_list = rsid_list)

    def GetEvents(self, rsid_list):
        return self.__callapi('ReportSuite.GetEvents', rsid_list = rsid_list)

    def GetTemplate(self, rsid_list):
        return self.__callapi('ReportSuite.GetTemplate', rsid_list = rsid_list)

    def GetTimeZone(self, rsid_list):
        return self.__callapi('ReportSuite.GetTimeZone', rsid_list = rsid_list)

    def GetTrackingServer(self, rsid_list):
        return self.__callapi('Company.GetTrackingServer', rsid_list = rsid_list)

    def GetTransactionEnabled(self, rsid_list):
        return self.__callapi('ReportSuite.GetTransactionEnabled', rsid_list = rsid_list)

    def GetUniqueVisitorVariable(self, rsid_list):
        return self.__callapi('ReportSuite.GetUniqueVisitorVariable', rsid_list = rsid_list)

    def GetVersionAccess(self):
        return self.__callapi('Company.GetVersionAccess', )

    def GetVideoSettings(self, rsid_list):
        return self.__callapi('ReportSuite.GetVideoSettings', rsid_list = rsid_list)

<|endoftext|>


<commit_before>import pandas as pd
from utils import get_template, compile_pdf_from_template

def get_options_from_file(path):
    """Read a parameters configuration file. The pattern in the file is '<key>=<value>'
    """
    with open(path, encoding='utf-8') as f:
        content = f.read()
        keys = re.findall(r"(.*?)=.*", content)
        values = re.findall(r".*=(.*?)\s+", content)

    options = dict(zip(keys, values))
    return options

def get_cover_abstract_data(path):
    data = pd.read_excel(path, index_col='index')
    data['icons list'] = data['icons list'].str.split(',')
    data['icons list'] = data['icons list'].fillna('[]')

    ### For Cover page
    blue_items = data[data.color == 'blue'][['item name']].T.to_dict()
    yellow_items = data[data.color == 'yellow'][['item name']].T.to_dict()

    ### For abstract
    categories = data.category.unique()
    abstract_list = {k:data[data.category == k].T.to_dict() for k in categories}

    return {'blue_items': blue_items, 'yellow_items': yellow_items, 'abstract_list': abstract_list}

def get_main_data(path):
    def filter_items(key):
        items = data[data.index == key]
        items_dict = items[['item name', 'item alias', 'brief', 'definition']].iloc[0].to_dict()
        items_dict['subentries'] = list(items.reset_index()[['start', 'icons short list', 'specific description', 'collapse', 'rate']].T.to_dict().values())
        return items_dict

    def filter_categories(key):
        items = data[data.category == key]
        new_keys = items.index.unique()
        return {k:filter_items(k) for k in new_keys}

    data = pd.read_excel(path, index_col='index')
    data['icons short list'] = data['icons short list'].str.split(',')
    data['icons short list'] = data['icons short list'].fillna('[]')
    data['brief'] = data['brief'].fillna('')
    data['definition'] = data['definition'].fillna('')

    ### For main
    categories = data.category.unique()
    items = data.index.unique()
    main_list = {k:filter_categories(k) for k in categories}

    return {'main_list': main_list}

all_variables = {}

customer_data = get_options_from_file('./parameters_config.txt')
all_variables =  {**all_variables, **customer_data}

cover_abstract_data = get_cover_abstract_data('./cover+abstract.xlsx')
all_variables = {**all_variables, **cover_abstract_data}

main_data = get_main_data('./main.xlsx')
all_variables = {**all_variables, **main_data}

compile_pdf_from_template('./template/everything.tex', insert_variables=all_variables, out_path='./out.pdf')
<commit_msg>pls make a detailed comments on function get_main_data(path), confused<commit_after>import pandas as pd
from utils import get_template, compile_pdf_from_template

def get_options_from_file(path):
    """Read a parameters configuration file. The pattern in the file is '<key>=<value>'
    """
    with open(path, encoding='utf-8') as f:
        content = f.read()
        keys = re.findall(r"(.*?)=.*", content)
        values = re.findall(r".*=(.*?)\s+", content)

    options = dict(zip(keys, values))
    return options

def get_cover_abstract_data(path):
    data = pd.read_excel(path, index_col='index')
    data['icons list'] = data['icons list'].str.split(',')
    data['icons list'] = data['icons list'].fillna('[]')

    ### For Cover page
    blue_items = data[data.color == 'blue'][['item name']].T.to_dict()
    yellow_items = data[data.color == 'yellow'][['item name']].T.to_dict()

    ### For abstract
    categories = data.category.unique()
    abstract_list = {k:data[data.category == k].T.to_dict() for k in categories}

    return {'blue_items': blue_items, 'yellow_items': yellow_items, 'abstract_list': abstract_list}

def get_main_data(path):
    def filter_items(key):
        items = data[data.index == key]
        items_dict = items[['item name', 'item alias', 'brief', 'definition']].iloc[0].to_dict()
        items_dict['subentries'] = list(items.reset_index()[['start', 'icons short list', 'specific description', 'collapse', 'rate']].T.to_dict().values())
        return items_dict

    def filter_categories(key):
        items = data[data.category == key]
        new_keys = items.index.unique()
        return {k:filter_items(k) for k in new_keys}

    data = pd.read_excel(path, index_col='index')
    data['icons short list'] = data['icons short list'].str.split(',')
    data['icons short list'] = data['icons short list'].fillna('[]')
    data['brief'] = data['brief'].fillna('')
    data['definition'] = data['definition'].fillna('')

    ### For main
    categories = data.category.unique()
    # when checked with Lint, line 53 treated as useless. Get the same layout when removing line 53
    items = data.index.unique()
    main_list = {k:filter_categories(k) for k in categories}

    return {'main_list': main_list}

all_variables = {}

customer_data = get_options_from_file('./parameters_config.txt')
all_variables =  {**all_variables, **customer_data}

cover_abstract_data = get_cover_abstract_data('./cover+abstract.xlsx')
all_variables = {**all_variables, **cover_abstract_data}

main_data = get_main_data('./main.xlsx')
all_variables = {**all_variables, **main_data}

compile_pdf_from_template('./template/everything.tex', insert_variables=all_variables, out_path='./out.pdf')
<|endoftext|>


<commit_before># generic solver

# from .SolutionChecker import SolutionChecker
# from .VM import VM

from .. import Tx, TxIn, TxOut
from ...key import Key
from ...ui import standard_tx_out_script

class Atom(object):
    def __init__(self, name):
        self.name = name

    def dependencies(self):
        return frozenset([self.name])

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

class Operator(Atom):
    def __init__(self, op_name, *args):
        self._op_name = op_name
        self._args = args

    def dependencies(self):
        s = set()
        for a in self._args:
            if hasattr(a, "dependencies"):
                s.update(a.dependencies())
        return frozenset(s)

    def __repr__(self):
        return "(%s %s)" % (self._op_name, ' '.join(repr(a) for a in self._args))

def make_traceback_f(constraints):
    def traceback_f(*args):
        opcode, data, pc, vm = args
        if vm.pc == 0:
            # reset stack
            vm.stack = vm.Stack(reversed([Atom("x_%d" % i) for i in range(10)]))
        stack = vm.stack
        altstack = vm.altstack
        if len(altstack) == 0:
            altstack = ''
        # print("%s %s\n  %3x  %s" % (vm.stack, altstack, vm.pc, vm.disassemble_for_opcode_data(opcode, data)))
        import pdb
        # pdb.set_trace()
        if opcode == vm.OP_HASH160 and not isinstance(vm.stack[-1], bytes):
            def my_op_hash160(vm):
                t = vm.stack.pop()
                t = Operator('HASH160', t)
                vm.stack.append(t)
            return my_op_hash160
        if opcode == vm.OP_EQUALVERIFY and any(not isinstance(v, bytes) for v in vm.stack[-2:]):
            def my_op_equalverify(vm):
                t1 = vm.stack.pop()
                t2 = vm.stack.pop()
                c = Operator('EQUAL', t1, t2)
                constraints.append(c)
            return my_op_equalverify
        if opcode == vm.OP_CHECKSIG:
            def my_op_checksig(vm):
                t1 = vm.stack.pop()
                t2 = vm.stack.pop()
                t = Operator('CHECKSIG', t1, t2)
                constraints.append(Operator('IS_PUBKEY', t1))
                constraints.append(Operator('IS_SIGNATURE', t2))
                vm.stack.append(t)
                if pc >= len(vm.script):
                    constraints.append(Operator('IS_TRUE', vm.stack[-1]))
                    if len(vm.stack) > 1:
                        constraints.append(Operator('STACK_EMPTY_AFTER', vm.stack[-2]))
                    vm.stack = vm.Stack([vm.VM_TRUE])
            return my_op_checksig
    return traceback_f

def solve(tx, tx_in_idx, **kwargs):
    constraints = []
    tx.check_solution(tx_in_idx, traceback_f=make_traceback_f(constraints))
    for c in constraints:
        print(c, sorted(c.dependencies()))
    solutions = []
    for c in constraints:
        s = solution_for_constraint(c)
        # s = (solution_f, target atom, dependency atom list)
        if s is not None:
            solutions.append(s)
    max_stack_size = 2  # BRAIN DAMAGE
    solved_values = dict((Atom("x_%d" % i), None) for i in range(max_stack_size))
    progress = True
    while progress and any(v is None for v in solved_values.values()):
        progress = False
        for solution, target, dependencies in solutions:
            if any(solved_values[d] is None for d in dependencies):
                continue
            solved_values[target] = solution(solved_values, **kwargs)
            progress = True
    print(solved_values)

class CONSTANT(object):
    def __init__(self, name):
        self._name = name

class VAR(object):
    def __init__(self, name):
        self._name = name

def solution_for_constraint(c):
    # given a constraint c
    # return None or
    # a solution (solution_f, target atom, dependency atom list)
    # where solution_f take list of solved values
    m = constraint_matches(c, ('EQUAL', CONSTANT("0"), ('HASH160', VAR("1"))))
    if m:
        pass
    # (EQUAL K (HASH160 <x_0>))

def constraint_matches(c, m):
    if isinstance(m, tuple):
        if not isinstance(c, Operator):
            return False
        if c._op_name != m[0]:
            return False
        if len(c.args) != len(m[1:]):
            return False
        pass

def test():
    key = Key(1)
    previous_hash = b'\1' * 32
    txs_in = [TxIn(previous_hash, 0)]
    txs_out = [TxOut(1000, standard_tx_out_script(key.address()))]
    version, lock_time = 1, 0
    tx = Tx(version, txs_in, txs_out, lock_time)
    tx.set_unspents(txs_out)
    print(tx)
    solve(tx, 0)
    return tx

test()

"""
WE REQUIRE: b'u\x1ev\xe8\x19\x91\x96\xd4T\x94\x1cE\xd1\xb3\xa3#\xf1C;\xd6' == hash160(<X_0>)
WE REQUIRE: <X_0> to be a public key
WE REQUIRE: <X_1> to be a signature
WE REQUIRE: checksig(<X_0>, <X_1>) be true
hash160(x0) == K
for x0_candidates = public_keys()
for x0 in invhash160(k, x0_candidates):
   for x1 in invchecksig(x0, private_keys):

build a list of Constraints for each variable

x0 :
  is a public key
  has hash160 of K

x1 :
  is a signature with PK x0

public_key_candidates
x0 = hashes_to_k(K)
x1 = sign(x0, sig_type)

"""
<commit_msg>Solving P2PK and P2PKH.<commit_after># generic solver

import pdb

from .. import Tx, TxIn, TxOut
from ...key import Key
from ...ui import standard_tx_out_script

from pycoin import ecdsa
from pycoin.tx.script import der
from pycoin.intbytes import bytes_from_int
from pycoin.tx.script.VM import VM

from pycoin.tx.pay_to import ScriptPayToPublicKey

class Atom(object):
    def __init__(self, name):
        self.name = name

    def dependencies(self):
        return frozenset([self.name])

    def __eq__(self, other):
        if isinstance(other, Atom):
            return self.name == other.name
        return False

    def __hash__(self):
        return self.name.__hash__()

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

class Operator(Atom):
    def __init__(self, op_name, *args):
        self._op_name = op_name
        self._args = tuple(args)
        s = set()
        for a in self._args:
            if hasattr(a, "dependencies"):
                s.update(a.dependencies())
        self._dependencies = frozenset(s)

    def __hash__(self):
        return self._args.__hash__()

    def __eq__(self, other):
        if isinstance(other, Operator):
            return self._op_name, self._args == other._op_name, other._args
        return False

    def dependencies(self):
        return self._dependencies

    def __repr__(self):
        return "(%s %s)" % (self._op_name, ' '.join(repr(a) for a in self._args))

def make_traceback_f(constraints):
    def traceback_f(*args):
        opcode, data, pc, vm = args
        if vm.pc == 0:
            # reset stack
            vm.stack = vm.Stack(reversed([Atom("x_%d" % i) for i in range(10)]))
        stack = vm.stack
        altstack = vm.altstack
        if len(altstack) == 0:
            altstack = ''
        # print("%s %s\n  %3x  %s" % (vm.stack, altstack, vm.pc, vm.disassemble_for_opcode_data(opcode, data)))
        if opcode == vm.OP_HASH160 and not isinstance(vm.stack[-1], bytes):
            def my_op_hash160(vm):
                t = vm.stack.pop()
                t = Operator('HASH160', t)
                vm.stack.append(t)
            return my_op_hash160
        if opcode == vm.OP_EQUALVERIFY and any(not isinstance(v, bytes) for v in vm.stack[-2:]):
            def my_op_equalverify(vm):
                t1 = vm.stack.pop()
                t2 = vm.stack.pop()
                c = Operator('EQUAL', t1, t2)
                constraints.append(c)
            return my_op_equalverify
        if opcode == vm.OP_CHECKSIG:
            def my_op_checksig(vm):
                t1 = vm.stack.pop()
                t2 = vm.stack.pop()
                t = Operator('CHECKSIG', t1, t2)
                constraints.append(Operator('IS_PUBKEY', t1))
                constraints.append(Operator('IS_SIGNATURE', t2))
                vm.stack.append(t)
                if pc >= len(vm.script):
                    constraints.append(Operator('IS_TRUE', vm.stack[-1]))
                    if len(vm.stack) > 1:
                        constraints.append(Operator('STACK_EMPTY_AFTER', vm.stack[-2]))
                    vm.stack = vm.Stack([vm.VM_TRUE])
            return my_op_checksig
    return traceback_f

def determine_constraints(tx, tx_in_idx):
    constraints = []
    tx.check_solution(tx_in_idx, traceback_f=make_traceback_f(constraints))
    return constraints

def solve(tx, tx_in_idx, **kwargs):
    constraints = determine_constraints(tx, tx_in_idx)
    for c in constraints:
        print(c, sorted(c.dependencies()))
    solutions = []
    for c in constraints:
        s = solution_for_constraint(c)
        # s = (solution_f, target atom, dependency atom list)
        if s is not None:
            solutions.append(s)
    max_stack_size = kwargs["max_stack_size"]  # BRAIN DAMAGE
    solved_values = dict((Atom("x_%d" % i), None) for i in range(max_stack_size))
    progress = True
    while progress and any(v is None for v in solved_values.values()):
        progress = False
        for solution, target, dependencies in solutions:
            if solved_values.get(target) is not None:
                continue
            if any(solved_values[d] is None for d in dependencies):
                continue
            solved_values[target] = solution(solved_values, **kwargs)
            progress = True

    solution_list = [solved_values.get(Atom("x_%d" % i)) for i in reversed(range(max_stack_size))]
    return VM.bin_script(solution_list)

class CONSTANT(object):
    def __init__(self, name):
        self._name = name

class VAR(object):
    def __init__(self, name):
        self._name = name

def solution_for_constraint(c):
    # given a constraint c
    # return None or
    # a solution (solution_f, target atom, dependency atom list)
    # where solution_f take list of solved values

    def lookup_solved_value(solved_values, item):
        if isinstance(item, Atom):
            return solved_values[item]
        return item

    def filtered_dependencies(*args):
        return [a for a in args if isinstance(a, Atom)]

    m = constraint_matches(c, ('EQUAL', CONSTANT("0"), ('HASH160', VAR("1"))))
    if m:
        the_hash = m["0"]

        def f(solved_values, **kwargs):
            return kwargs["pubkey_for_hash"](the_hash)

        return (f, m["1"], ())

    m = constraint_matches(c, (('IS_TRUE', ('CHECKSIG', VAR("0"), VAR("1")))))
    if m:

        def f(solved_values, **kwargs):
            pubkey = lookup_solved_value(solved_values, m["0"])
            pdb.set_trace()
            privkey = kwargs["privkey_for_pubkey"](pubkey)
            signature = kwargs["signature_for_secret_exponent"](privkey)
            return signature
        return (f, m["1"], filtered_dependencies(m["0"]))

    return None

def constraint_matches(c, m):
    """
    Return False or dict with indices the substitution values
    """
    d = {}
    if isinstance(m, tuple):
        if not isinstance(c, Operator):
            return False
        if c._op_name != m[0]:
            return False
        if len(c._args) != len(m[1:]):
            return False
        for c1, m1 in zip(c._args, m[1:]):
            if isinstance(m1, tuple) and isinstance(c1, Operator):
                d1 = constraint_matches(c1, m1)
                if d1 is False:
                    return False
                d.update(d1)
                continue
            if isinstance(m1, CONSTANT):
                if isinstance(c1, bytes):
                    d[m1._name] = c1
                    continue
            if isinstance(m1, VAR):
                if isinstance(c1, (bytes, Atom)):
                    d[m1._name] = c1
                    continue
            if c1 == m1:
                continue
            return False
        return d

def test_solve(tx, tx_in_idx, **kwargs):
    solution_script = solve(tx, 0, **kwargs)
    print(VM.disassemble(solution_script))

    tx.txs_in[tx_in_idx].script = solution_script
    tx.check_solution(0)

def make_test_tx(input_script):
    previous_hash = b'\1' * 32
    txs_in = [TxIn(previous_hash, 0)]
    txs_out = [TxOut(1000, standard_tx_out_script(Key(1).address()))]
    version, lock_time = 1, 0
    tx = Tx(version, txs_in, txs_out, lock_time)
    unspents = [TxOut(1000, input_script)]
    tx.set_unspents(unspents)
    return tx

def test_tx(incoming_script, max_stack_size):
    key = Key(1)
    tx = make_test_tx(incoming_script)
    tx_in_idx = 0

    def pubkey_for_hash(the_hash):
        if the_hash == key.hash160():
            return key.sec()

    def privkey_for_pubkey(pubkey):
        if pubkey == key.sec():
            return key.secret_exponent()

    def signature_for_secret_exponent(secret_exponent):
        signature_type = 1  # BRAIN DAMAGE

        def signature_for_hash_type_f(hash_type, script):
            return tx.signature_hash(script, tx_in_idx, hash_type)

        script_to_hash = incoming_script
        sign_value = signature_for_hash_type_f(signature_type, script_to_hash)
        order = ecdsa.generator_secp256k1.order()
        r, s = ecdsa.sign(ecdsa.generator_secp256k1, secret_exponent, sign_value)
        if s + s > order:
            s = order - s
        return der.sigencode_der(r, s) + bytes_from_int(signature_type)

    kwargs = dict(pubkey_for_hash=pubkey_for_hash,
                  privkey_for_pubkey=privkey_for_pubkey,
                  signature_for_secret_exponent=signature_for_secret_exponent,
                  max_stack_size=max_stack_size)

    test_solve(tx, tx_in_idx, **kwargs)

def test_p2pkh():
    key = Key(1)
    test_tx(standard_tx_out_script(key.address()), 2)

def test_p2pk():
    key = Key(1)
    test_tx(ScriptPayToPublicKey.from_key(key).script(), 1)

def main():
    test_p2pkh()
    test_p2pk()

if __name__ == '__main__':
    main()

"""
WE REQUIRE: b'u\x1ev\xe8\x19\x91\x96\xd4T\x94\x1cE\xd1\xb3\xa3#\xf1C;\xd6' == hash160(<X_0>)
WE REQUIRE: <X_0> to be a public key
WE REQUIRE: <X_1> to be a signature
WE REQUIRE: checksig(<X_0>, <X_1>) be true
hash160(x0) == K
for x0_candidates = public_keys()
for x0 in invhash160(k, x0_candidates):
   for x1 in invchecksig(x0, private_keys):

build a list of Constraints for each variable

x0 :
  is a public key
  has hash160 of K

x1 :
  is a signature with PK x0

public_key_candidates
x0 = hashes_to_k(K)
x1 = sign(x0, sig_type)

"""
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
    PyValitron
    ~~~~~~
    Python Inputs Validation Library
    :copyright: (c) 2016 by Clivern (hello@clivern.com).
    :license: MIT, see LICENSE for more details.
"""
from __future__ import print_function
from pyvalitron.validator import Validator
from pyvalitron.sanitizer import Sanitizer
from pyvalitron.form import Form
from flask import Flask
import unittest

app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello World!"

class MyValidator(Validator):

    def username(self):
        if not isinstance(self._input, (str)):
            return False
        current_input = self._input.strip()
        if len(current_input) > 5 and current_input.isalpha():
            return True
        return False

class MySanitizer(Sanitizer):

    def clear_spaces(self):
        if not isinstance(self._input, (str)):
            self._sinput = str(self._input)
        else:
            self._sinput = self._input

        self._sinput = self._sinput.replace(" ", "")
        return self._sinput

    def lower_case(self):
        if not isinstance(self._input, (str)):
            self._sinput = str(self._input)
        else:
            self._sinput = self._input
        self._sinput = self._sinput.lower()
        return self._sinput

class TestFormMethods(unittest.TestCase):

    def test_form(self):
        form = Form({
            'user_email': {
                'value': '',
                'validate': {
                    'not_empty': {
                        'error': 'User email must be provided'
                    },
                    'email': {
                        'error': 'User email is invalid'
                    }
                }
            }
        }, 'values')
        form.process()
        errors = form.get_errors()
        self.assertEqual(2, len(errors['user_email']))
        self.assertEqual(True, 'User email must be provided' in errors['user_email'])
        self.assertEqual(True, 'User email is invalid' in errors['user_email'])

    def test_form_validation_with_param(self):
        form = Form({
            'test_field1': {
                'value': 'Hello World',
                'validate': {
                    'length_between': {
                        'param': [1, 12],
                        'error': 'Input lenght must be between 1 and 12 characters'
                    }
                }
            },
            'test_field2': {
                'value': 'Hello World',
                'validate': {
                    'length_between': {
                        'param': [1, 9],
                        'error': 'Input lenght must be between 1 and 12 characters'
                    }
                }
            }
        }, 'values')
        form.process()
        errors = form.get_errors()
        self.assertEqual(0, len(errors['test_field1']))
        self.assertEqual(1, len(errors['test_field2']))
        self.assertEqual(True, 'Input lenght must be between 1 and 12 characters' in errors['test_field2'])

    def test_custom_validator(self):
        form = Form({
            'user_name': {
                'value': '',
                'validate': {
                    'username': {
                        'error': 'Invalid Username'
                    }
                }
            }
        }, 'values')
        form.add_validator(MyValidator())
        form.process()
        errors = form.get_errors()
        self.assertEqual(1, len(errors['user_name']))
        self.assertEqual(True, 'Invalid Username' in errors['user_name'])

    def test_custom_sanitizer(self):
        form = Form({
            'test_field': {
                'value': 'Hello World',
                'sanitize': {
                    'clear_spaces': {},
                    'lower_case': {}
                }
            }
        }, 'values')
        form.add_sanitizer(MySanitizer())
        form.process()
        inputs = form.get_inputs()
        self.assertEqual('helloworld', inputs['test_field']['svalue'])

    def test_web_client(self):
        self.app = app.test_client()
        self.app.testing = True
        result = self.app.get('/')

        self.assertEqual(result.status_code, 200)
        self.assertEqual(result.data, "Hello World!")

if __name__ == '__main__':
    unittest.main()
<commit_msg>py3 compat issue<commit_after># -*- coding: utf-8 -*-
"""
    PyValitron
    ~~~~~~
    Python Inputs Validation Library
    :copyright: (c) 2016 by Clivern (hello@clivern.com).
    :license: MIT, see LICENSE for more details.
"""
from __future__ import print_function
from pyvalitron.validator import Validator
from pyvalitron.sanitizer import Sanitizer
from pyvalitron.form import Form
from flask import Flask
import unittest

app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello World!"

class MyValidator(Validator):

    def username(self):
        if not isinstance(self._input, (str)):
            return False
        current_input = self._input.strip()
        if len(current_input) > 5 and current_input.isalpha():
            return True
        return False

class MySanitizer(Sanitizer):

    def clear_spaces(self):
        if not isinstance(self._input, (str)):
            self._sinput = str(self._input)
        else:
            self._sinput = self._input

        self._sinput = self._sinput.replace(" ", "")
        return self._sinput

    def lower_case(self):
        if not isinstance(self._input, (str)):
            self._sinput = str(self._input)
        else:
            self._sinput = self._input
        self._sinput = self._sinput.lower()
        return self._sinput

class TestFormMethods(unittest.TestCase):

    def test_form(self):
        form = Form({
            'user_email': {
                'value': '',
                'validate': {
                    'not_empty': {
                        'error': 'User email must be provided'
                    },
                    'email': {
                        'error': 'User email is invalid'
                    }
                }
            }
        }, 'values')
        form.process()
        errors = form.get_errors()
        self.assertEqual(2, len(errors['user_email']))
        self.assertEqual(True, 'User email must be provided' in errors['user_email'])
        self.assertEqual(True, 'User email is invalid' in errors['user_email'])

    def test_form_validation_with_param(self):
        form = Form({
            'test_field1': {
                'value': 'Hello World',
                'validate': {
                    'length_between': {
                        'param': [1, 12],
                        'error': 'Input lenght must be between 1 and 12 characters'
                    }
                }
            },
            'test_field2': {
                'value': 'Hello World',
                'validate': {
                    'length_between': {
                        'param': [1, 9],
                        'error': 'Input lenght must be between 1 and 12 characters'
                    }
                }
            }
        }, 'values')
        form.process()
        errors = form.get_errors()
        self.assertEqual(0, len(errors['test_field1']))
        self.assertEqual(1, len(errors['test_field2']))
        self.assertEqual(True, 'Input lenght must be between 1 and 12 characters' in errors['test_field2'])

    def test_custom_validator(self):
        form = Form({
            'user_name': {
                'value': '',
                'validate': {
                    'username': {
                        'error': 'Invalid Username'
                    }
                }
            }
        }, 'values')
        form.add_validator(MyValidator())
        form.process()
        errors = form.get_errors()
        self.assertEqual(1, len(errors['user_name']))
        self.assertEqual(True, 'Invalid Username' in errors['user_name'])

    def test_custom_sanitizer(self):
        form = Form({
            'test_field': {
                'value': 'Hello World',
                'sanitize': {
                    'clear_spaces': {},
                    'lower_case': {}
                }
            }
        }, 'values')
        form.add_sanitizer(MySanitizer())
        form.process()
        inputs = form.get_inputs()
        self.assertEqual('helloworld', inputs['test_field']['svalue'])

    def test_web_client(self):
        self.app = app.test_client()
        self.app.testing = True
        result = self.app.get('/')

        self.assertEqual(result.status_code, 200)
        self.assertEqual(result.data, b"Hello World!")

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


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

import bz2
from copy import deepcopy
import hashlib
import inspect
import os

try:
    import cPickle as pickle
except ImportError:
    import pickle

class Analysis(object):
    """
    An Analysis is a function whose code configuration and output can be
    serialized to disk. When it is invoked again, if it's code has not changed
    the serialized output will be used rather than executing the code again.

    Implements a promise-like API so that Analyses can depend on one another.
    If a parent analysis is invalidated then all it's children will be as well.
    """
    def __init__(self, func, cache_path='.agate'):
        self._name = func.__name__
        self._func = func
        self._cache_path = cache_path
        self._next_analyses = []

    def _fingerprint(self):
        """
        Generate a fingerprint for this analysis function.
        """
        fingerprint = hashlib.md5()
        fingerprint.update(inspect.getsource(self._func))

        return fingerprint.hexdigest()

    def _save_fingerprint(self):
        """
        Save the fingerprint of this analysis function to its cache.
        """
        path = os.path.join(self._cache_path, '%s.fingerprint' % self._name)

        if not os.path.exists(self._cache_path):
            os.makedirs(self._cache_path)

        with open(path, 'w') as f:
            f.write(self._fingerprint())

    def _load_fingerprint(self):
        """
        Load the fingerprint of this analysis function from its cache.
        """
        path = os.path.join(self._cache_path, '%s.fingerprint' % self._name)

        if not os.path.exists(path):
            return None

        with open(path) as f:
            fingerprint = f.read()

        return fingerprint

    def _save_data(self, data):
        """
        Save the output data for this analysis from its cache.
        """
        path = os.path.join(self._cache_path, '%s.data' % self._name)

        if not os.path.exists(self._cache_path):
            os.makedirs(self._cache_path)

        with open(path, 'w') as f:
            f.write(bz2.compress(pickle.dumps(data)))

    def _load_data(self):
        """
        Load the output data for this analysis from its cache.
        """
        path = os.path.join(self._cache_path, '%s.data' % self._name)

        if not os.path.exists(path):
            return None

        with open(path) as f:
            data = pickle.loads(bz2.decompress(f.read()))

        return data

    def then(self, next_func):
        """
        Create a new analysis which will run after this one has completed.

        :param func: The function to run. Must accept a `data` argument.
        """
        analysis = Analysis(next_func)

        self._next_analyses.append(analysis)

        return analysis

    def run(self, data={}, refresh=False):
        """
        Execute this analysis and its descendents. There are four possible
        execution scenarios:

        1. This analysis has never been run. Run it and cache the results.
        2. This analysis is the child of a parent analysis which was run, so it
           must be run because its inputs may have changed. Cache the result.
        3. This analysis has been run, its parents were loaded from cache and
           its fingerprints match. Load the cached result.
        4. This analysis has been run and its parents were loaded from cache,
           but its fingerprints do not match. Run it and cache updated results.

        :param data: The input "state" from the parent analysis, if any.
        :param refresh: Flag indicating if this analysis must refresh because
            its parents did.
        """
        if refresh:
            print('Refreshing: %s' % self._name)

            local_data = deepcopy(data)

            self._func(local_data)
            self._save_fingerprint()
            self._save_data(local_data)
        else:
            if self._fingerprint() != self._load_fingerprint():
                print('Running: %s' % self._name)

                local_data = deepcopy(data)

                self._func(local_data)
                self._save_fingerprint()
                self._save_data(local_data)

                refresh = True
            else:
                print('Loaded from cache: %s' % self._name)

                local_data = self._load_data()

        for analysis in self._next_analyses:
            analysis.run(local_data, refresh)
<commit_msg>Tweaks. #171.<commit_after>#!/usr/bin/env python

import bz2
from copy import deepcopy
import hashlib
import inspect
import os

try:
    import cPickle as pickle
except ImportError:
    import pickle

class Analysis(object):
    """
    An Analysis is a function whose code configuration and output can be
    serialized to disk. When it is invoked again, if it's code has not changed
    the serialized output will be used rather than executing the code again.

    Implements a promise-like API so that Analyses can depend on one another.
    If a parent analysis is invalidated then all it's children will be as well.
    """
    def __init__(self, func, cache_path='.agate'):
        self._name = func.__name__
        self._func = func
        self._cache_path = cache_path
        self._next_analyses = []

    def _fingerprint(self):
        """
        Generate a fingerprint for this analysis function.
        """
        hasher = hashlib.md5()
        source = inspect.getsource(self._func)
        print(source)
        hasher.update(source)

        return hasher.hexdigest()

    def _save_fingerprint(self):
        """
        Save the fingerprint of this analysis function to its cache.
        """
        path = os.path.join(self._cache_path, '%s.fingerprint' % self._name)

        if not os.path.exists(self._cache_path):
            os.makedirs(self._cache_path)

        with open(path, 'w') as f:
            f.write(self._fingerprint())

    def _load_fingerprint(self):
        """
        Load the fingerprint of this analysis function from its cache.
        """
        path = os.path.join(self._cache_path, '%s.fingerprint' % self._name)

        if not os.path.exists(path):
            return None

        with open(path) as f:
            fingerprint = f.read()

        return fingerprint

    def _save_data(self, data):
        """
        Save the output data for this analysis from its cache.
        """
        path = os.path.join(self._cache_path, '%s.data' % self._name)

        if not os.path.exists(self._cache_path):
            os.makedirs(self._cache_path)

        with open(path, 'w') as f:
            f.write(bz2.compress(pickle.dumps(data)))

    def _load_data(self):
        """
        Load the output data for this analysis from its cache.
        """
        path = os.path.join(self._cache_path, '%s.data' % self._name)

        if not os.path.exists(path):
            return None

        with open(path) as f:
            data = pickle.loads(bz2.decompress(f.read()))

        return data

    def then(self, next_func):
        """
        Create a new analysis which will run after this one has completed.

        :param func: The function to run. Must accept a `data` argument.
        """
        analysis = Analysis(next_func)

        self._next_analyses.append(analysis)

        return analysis

    def run(self, data={}, refresh=False):
        """
        Execute this analysis and its descendents. There are four possible
        execution scenarios:

        1. This analysis has never been run. Run it and cache the results.
        2. This analysis is the child of a parent analysis which was run, so it
           must be run because its inputs may have changed. Cache the result.
        3. This analysis has been run, its parents were loaded from cache and
           its fingerprints match. Load the cached result.
        4. This analysis has been run and its parents were loaded from cache,
           but its fingerprints do not match. Run it and cache updated results.

        :param data: The input "state" from the parent analysis, if any.
        :param refresh: Flag indicating if this analysis must refresh because
            its parents did.
        """
        if refresh:
            print('Refreshing: %s' % self._name)

            local_data = deepcopy(data)

            self._func(local_data)
            self._save_fingerprint()
            self._save_data(local_data)
        else:
            if self._fingerprint() != self._load_fingerprint():
                print('Running: %s' % self._name)

                local_data = deepcopy(data)

                self._func(local_data)
                self._save_fingerprint()
                self._save_data(local_data)

                refresh = True
            else:
                print('Loaded from cache: %s' % self._name)

                local_data = self._load_data()

        for analysis in self._next_analyses:
            analysis.run(local_data, refresh)
<|endoftext|>


<commit_before>version = '0.60.3'
time = '2019-02-12 16:04:51'
<commit_msg>Update __version__.py<commit_after>version = '0.60.4'
time = '2019-02-12 16:04:51'
<|endoftext|>