<commit_before>#!/usr/bin/env python
"""This tool builds or repacks the client binaries.

This handles invocations for the build across the supported platforms including
handling Visual Studio, pyinstaller and other packaging mechanisms.
"""

import os
import platform
import sys

# pylint: disable=unused-import
from grr.client import client_plugins
# pylint: enable=unused-import

from grr.lib import build
from grr.lib import builders
from grr.lib import config_lib
from grr.lib import flags
from grr.lib import startup

parser = flags.PARSER

if "32 bit" in sys.version:
  default_arch = "i386"
else:
  default_arch = "amd64"

parser.add_argument(
    "--platform", choices=["darwin", "linux", "windows"],
    default=platform.system().lower(),
    help="The platform to build or repack for. This will default to "
    "the current platform: %s." % platform.system())

parser.add_argument(
    "--arch", choices=["amd64", "i386"],
    default=default_arch,
    help="The architecture to build or repack for.")

parser.add_argument(
    "--package_format", choices=["deb", "rpm"],
    default="deb",
    help="The packaging format to use when building a Linux client.")

# Initialize sub parsers and their arguments.
subparsers = parser.add_subparsers(
    title="subcommands", dest="subparser_name", description="valid subcommands")

# Build arguments.
parser_build = subparsers.add_parser(
    "build", help="Build a client from source.")

parser_repack = subparsers.add_parser(
    "repack", help="Repack a zip file into an installer (Only useful when "
    "signing).")

parser_repack.add_argument("--template", default=None,
                           help="The template zip file to repack.")

parser_repack.add_argument("--output", default=None,
                           help="The path to write the output installer.")

parser_repack.add_argument("--outputdir", default="",
                           help="The directory to which we should write the "
                           "output installer. Installers will be named "
                           "automatically from config options. Incompatible"
                           " with --output")

parser_repack.add_argument("--debug_build", action="store_true", default=False,
                           help="Create a debug client.")

parser_repack.add_argument("-p", "--plugins", default=[], nargs="+",
                           help="Additional python files that will be loaded "
                           "as custom plugins.")

parser_deploy = subparsers.add_parser(
    "deploy", help="Build a deployable self installer from a package.")

parser_deploy.add_argument("--template", default=None,
                           help="The template zip file to deploy.")

parser_deploy.add_argument("--templatedir", default="",
                           help="Directory containing template zip files to "
                           "repack. Incompatible with --template")

parser_deploy.add_argument("--output", default=None,
                           help="The path to write the output installer.")

parser_deploy.add_argument("--outputdir", default="",
                           help="The directory to which we should write the "
                           "output installer. Installers will be named "
                           "automatically from config options. Incompatible"
                           " with --output")

parser_deploy.add_argument("-p", "--plugins", default=[], nargs="+",
                           help="Additional python files that will be loaded "
                           "as custom plugins.")

parser_deploy.add_argument("--debug_build", action="store_true", default=False,
                           help="Create a debug client.")

args = parser.parse_args()

def GetBuilder(context):
  """Get the appropriate builder based on the selected flags."""
  try:
    if args.platform == "darwin":
      context = ["Platform:Darwin"] + context
      builder_obj = builders.DarwinClientBuilder

    elif args.platform == "windows":
      context = ["Platform:Windows"] + context
      builder_obj = builders.WindowsClientBuilder

    elif args.platform == "linux":
      if args.package_format == "deb":
        context = ["Platform:Linux"] + context
        builder_obj = builders.LinuxClientBuilder
      else:
        context = ["Platform:Linux", "Target:LinuxRpm"] + context
        builder_obj = builders.CentosClientBuilder

    else:
      parser.error("Unsupported build platform: %s" % args.platform)

  except AttributeError:
    raise RuntimeError("Unable to build for platform %s when running "
                       "on current platform." % args.platform)

  return builder_obj(context=context)

def GetDeployer(context):
  """Get the appropriate client deployer based on the selected flags."""
  if args.platform == "darwin":
    context = ["Platform:Darwin"] + context
    deployer_obj = build.DarwinClientDeployer

  elif args.platform == "windows":
    context = ["Platform:Windows"] + context
    deployer_obj = build.WindowsClientDeployer

  elif args.platform == "linux":
    if args.package_format == "deb":
      context = ["Platform:Linux"] + context
      deployer_obj = build.LinuxClientDeployer
    else:
      context = ["Platform:Linux", "Target:LinuxRpm"] + context
      deployer_obj = build.CentosClientDeployer

  else:
    parser.error("Unsupported build platform: %s" % args.platform)

  return deployer_obj(context=context)

def TemplateInputFilename(context):
  """Build template file name from config."""
  if args.templatedir:
    filename = config_lib.CONFIG.Get("PyInstaller.template_filename",
                                     context=context)
    return os.path.join(args.templatedir, filename)
  return None

def main(_):
  """Launch the appropriate builder."""
  config_lib.CONFIG.AddContext(
      "ClientBuilder Context",
      "Context applied when we run the client builder script.")

  startup.ClientInit()

  # The following is used to change the identity of the builder based on the
  # target platform.
  context = flags.FLAGS.context
  if args.arch == "amd64":
    context.append("Arch:amd64")
  else:
    context.append("Arch:i386")

  if args.subparser_name == "build":
    builder_obj = GetBuilder(context)
    builder_obj.MakeExecutableTemplate()

  elif args.subparser_name == "repack":
    if args.plugins:
      config_lib.CONFIG.Set("Client.plugins", args.plugins)

    if args.debug_build:
      context += ["DebugClientBuild Context"]

    deployer = GetDeployer(context)
    output_filename = os.path.join(
        args.outputdir, config_lib.CONFIG.Get(
            "ClientBuilder.output_filename", context=deployer.context))

    deployer.RepackInstaller(open(args.template, "rb").read(), args.output or
                             output_filename)

  elif args.subparser_name == "deploy":
    if args.plugins:
      config_lib.CONFIG.Set("Client.plugins", args.plugins)

    if args.debug_build:
      context += ["DebugClientBuild Context"]

    deployer = GetDeployer(context)
    template_path = (args.template or TemplateInputFilename(deployer.context) or
                     config_lib.CONFIG.Get("ClientBuilder.template_path",
                                           context=deployer.context))

    # If output filename isn't specified, write to args.outputdir with a
    # .deployed extension so we can distinguish it from repacked binaries.
    filename = ".".join(
        (config_lib.CONFIG.Get("ClientBuilder.output_filename",
                               context=deployer.context), "deployed"))

    deployer.MakeDeployableBinary(template_path, args.output or os.path.join(
        args.outputdir, filename))

if __name__ == "__main__":
  flags.StartMain(main)
<commit_msg>Fix issue when deploying linux client with no output parameter<commit_after>#!/usr/bin/env python
"""This tool builds or repacks the client binaries.

This handles invocations for the build across the supported platforms including
handling Visual Studio, pyinstaller and other packaging mechanisms.
"""

import os
import platform
import sys

# pylint: disable=unused-import
from grr.client import client_plugins
# pylint: enable=unused-import

from grr.lib import build
from grr.lib import builders
from grr.lib import config_lib
from grr.lib import flags
from grr.lib import startup

parser = flags.PARSER

if "32 bit" in sys.version:
  default_arch = "i386"
else:
  default_arch = "amd64"

parser.add_argument(
    "--platform", choices=["darwin", "linux", "windows"],
    default=platform.system().lower(),
    help="The platform to build or repack for. This will default to "
    "the current platform: %s." % platform.system())

parser.add_argument(
    "--arch", choices=["amd64", "i386"],
    default=default_arch,
    help="The architecture to build or repack for.")

parser.add_argument(
    "--package_format", choices=["deb", "rpm"],
    default="deb",
    help="The packaging format to use when building a Linux client.")

# Initialize sub parsers and their arguments.
subparsers = parser.add_subparsers(
    title="subcommands", dest="subparser_name", description="valid subcommands")

# Build arguments.
parser_build = subparsers.add_parser(
    "build", help="Build a client from source.")

parser_repack = subparsers.add_parser(
    "repack", help="Repack a zip file into an installer (Only useful when "
    "signing).")

parser_repack.add_argument("--template", default=None,
                           help="The template zip file to repack.")

parser_repack.add_argument("--output", default=None,
                           help="The path to write the output installer.")

parser_repack.add_argument("--outputdir", default="",
                           help="The directory to which we should write the "
                           "output installer. Installers will be named "
                           "automatically from config options. Incompatible"
                           " with --output")

parser_repack.add_argument("--debug_build", action="store_true", default=False,
                           help="Create a debug client.")

parser_repack.add_argument("-p", "--plugins", default=[], nargs="+",
                           help="Additional python files that will be loaded "
                           "as custom plugins.")

parser_deploy = subparsers.add_parser(
    "deploy", help="Build a deployable self installer from a package.")

parser_deploy.add_argument("--template", default=None,
                           help="The template zip file to deploy.")

parser_deploy.add_argument("--templatedir", default="",
                           help="Directory containing template zip files to "
                           "repack. Incompatible with --template")

parser_deploy.add_argument("--output", default=None,
                           help="The path to write the output installer.")

parser_deploy.add_argument("--outputdir", default="",
                           help="The directory to which we should write the "
                           "output installer. Installers will be named "
                           "automatically from config options. Incompatible"
                           " with --output")

parser_deploy.add_argument("-p", "--plugins", default=[], nargs="+",
                           help="Additional python files that will be loaded "
                           "as custom plugins.")

parser_deploy.add_argument("--debug_build", action="store_true", default=False,
                           help="Create a debug client.")

args = parser.parse_args()

def GetBuilder(context):
  """Get the appropriate builder based on the selected flags."""
  try:
    if args.platform == "darwin":
      context = ["Platform:Darwin"] + context
      builder_obj = builders.DarwinClientBuilder

    elif args.platform == "windows":
      context = ["Platform:Windows"] + context
      builder_obj = builders.WindowsClientBuilder

    elif args.platform == "linux":
      if args.package_format == "deb":
        context = ["Platform:Linux"] + context
        builder_obj = builders.LinuxClientBuilder
      else:
        context = ["Platform:Linux", "Target:LinuxRpm"] + context
        builder_obj = builders.CentosClientBuilder

    else:
      parser.error("Unsupported build platform: %s" % args.platform)

  except AttributeError:
    raise RuntimeError("Unable to build for platform %s when running "
                       "on current platform." % args.platform)

  return builder_obj(context=context)

def GetDeployer(context):
  """Get the appropriate client deployer based on the selected flags."""
  if args.platform == "darwin":
    context = ["Platform:Darwin"] + context
    deployer_obj = build.DarwinClientDeployer

  elif args.platform == "windows":
    context = ["Platform:Windows"] + context
    deployer_obj = build.WindowsClientDeployer

  elif args.platform == "linux":
    if args.package_format == "deb":
      context = ["Platform:Linux"] + context
      deployer_obj = build.LinuxClientDeployer
    else:
      context = ["Platform:Linux", "Target:LinuxRpm"] + context
      deployer_obj = build.CentosClientDeployer

  else:
    parser.error("Unsupported build platform: %s" % args.platform)

  return deployer_obj(context=context)

def TemplateInputFilename(context):
  """Build template file name from config."""
  if args.templatedir:
    filename = config_lib.CONFIG.Get("PyInstaller.template_filename",
                                     context=context)
    return os.path.join(args.templatedir, filename)
  return None

def main(_):
  """Launch the appropriate builder."""
  config_lib.CONFIG.AddContext(
      "ClientBuilder Context",
      "Context applied when we run the client builder script.")

  startup.ClientInit()

  # The following is used to change the identity of the builder based on the
  # target platform.
  context = flags.FLAGS.context
  if args.arch == "amd64":
    context.append("Arch:amd64")
  else:
    context.append("Arch:i386")

  if args.subparser_name == "build":
    builder_obj = GetBuilder(context)
    builder_obj.MakeExecutableTemplate()

  elif args.subparser_name == "repack":
    if args.plugins:
      config_lib.CONFIG.Set("Client.plugins", args.plugins)

    if args.debug_build:
      context += ["DebugClientBuild Context"]

    deployer = GetDeployer(context)
    output_filename = os.path.join(
        args.outputdir, config_lib.CONFIG.Get(
            "ClientBuilder.output_filename", context=deployer.context))

    deployer.RepackInstaller(open(args.template, "rb").read(), args.output or
                             output_filename)

  elif args.subparser_name == "deploy":
    if args.plugins:
      config_lib.CONFIG.Set("Client.plugins", args.plugins)

    if args.debug_build:
      context += ["DebugClientBuild Context"]

    deployer = GetDeployer(context)
    template_path = (args.template or TemplateInputFilename(deployer.context) or
                     config_lib.CONFIG.Get("ClientBuilder.template_path",
                                           context=deployer.context))

    # If neither output filename or output directory is specified,
    # use the default location from the config file.
    output = None
    if args.output:
      output = args.output
    elif args.outputdir:
      # If output filename isn't specified, write to args.outputdir with a
      # .deployed extension so we can distinguish it from repacked binaries.
      filename = ".".join(
          (config_lib.CONFIG.Get("ClientBuilder.output_filename",
                                 context=deployer.context), "deployed"))
      output = os.path.join(args.outputdir, filename)

    deployer.MakeDeployableBinary(template_path, output)

if __name__ == "__main__":
  flags.StartMain(main)
<|endoftext|>


<commit_before>from pydojo import *

# inizializzo
pygame.init()
width = 800
height = 600
SCREEN(width, height)

bianco = [255, 255, 255]

gamepad_control = False
try:
    # creo un oggetto Joystick
    pad = pygame.joystick.Joystick(0)
    # inizializzo il joystick
    pad.init()
    gamepad_control = True
except:
    print("no GamePad found...")

gufo = Actor('example_library/gufo.png')
gufo.goto(width/2, height/2)

uno = Arduino('/dev/rfcomm0')
#uno = Arduino('/dev/ttyACM0')

rightPwm = uno.get_pin('d:5:p')
leftPwm = uno.get_pin('d:10:p')
RF = uno.get_pin('d:7:o')
RB = uno.get_pin('d:6:o')
LF = uno.get_pin('d:9:o')
LB = uno.get_pin('d:8:o')

RF.write(True)
RB.write(False)
LF.write(True)
LB.write(False)

direction = "forward"

def shutdown():
    RF.write(False)
    RB.write(False)
    LF.write(False)
    LB.write(False)
    rightPwm.write(0)
    leftPwm.write(0)

while True:

    # KEYBOARD CONTROL
    if keydown(ESCAPE):
        shutdown()
        quit()

    if keydown(RIGHT):
        if direction == "forward":
            RF.write(True)
            RB.write(False)
        elif direction == "back":
            RF.write(False)
            RB.write(True)
        rightPwm.write(1)
    if keyup(RIGHT):
        rightPwm.write(0)
        RF.write(False)
        RB.write(False)

    if keydown(LEFT):
        if direction == "forward":
            LF.write(True)
            LB.write(False)
        elif direction == "back":
            LF.write(False)
            LB.write(True)
        leftPwm.write(1)
    if keyup(LEFT):
        leftPwm.write(0)
        LF.write(False)
        LB.write(False)

    if keydown(UP):
        direction = "forward"
    if keydown(DOWN):
        direction = "back"

    # GAMEPAD CONTROL
    if gamepad_control:
        horizontal = pad.get_axis(0)

        if horizontal > 0.2:
            rightPwm.write(0.9)
            gufo.x += int(horizontal * 10)
        if horizontal < -0.2:
            leftPwm.write(0.9)
            gufo.x += int(horizontal * 10)

    fill(bianco)
    gufo.draw()
    sleep(0.01)
    UPDATE()
<commit_msg>bind bluetooth port command<commit_after>from pydojo import *

# inizializzo
pygame.init()
width = 800
height = 600
SCREEN(width, height)

bianco = [255, 255, 255]

gamepad_control = False
try:
    # creo un oggetto Joystick
    pad = pygame.joystick.Joystick(0)
    # inizializzo il joystick
    pad.init()
    gamepad_control = True
except:
    print("no GamePad found...")

gobo = Actor('example_library/gobo.png')
gobo.goto(width/2, height/2)

#os.call('sudo rfcomm bind /dev/rfcomm0  00:15:83:35:86:A2 1')
uno = Arduino('/dev/rfcomm0')
#uno = Arduino('/dev/ttyACM0')

rightPwm = uno.get_pin('d:5:p')
leftPwm = uno.get_pin('d:10:p')
RF = uno.get_pin('d:7:o')
RB = uno.get_pin('d:6:o')
LF = uno.get_pin('d:9:o')
LB = uno.get_pin('d:8:o')

RF.write(True)
RB.write(False)
LF.write(True)
LB.write(False)

direction = "forward"

def shutdown():
    RF.write(False)
    RB.write(False)
    LF.write(False)
    LB.write(False)
    rightPwm.write(0)
    leftPwm.write(0)

while True:

    # KEYBOARD CONTROL
    if keydown(ESCAPE):
        shutdown()
        quit()

    if keydown(RIGHT):
        if direction == "forward":
            RF.write(True)
            RB.write(False)
        elif direction == "back":
            RF.write(False)
            RB.write(True)
        rightPwm.write(1)
    if keyup(RIGHT):
        rightPwm.write(0)
        RF.write(False)
        RB.write(False)

    if keydown(LEFT):
        if direction == "forward":
            LF.write(True)
            LB.write(False)
        elif direction == "back":
            LF.write(False)
            LB.write(True)
        leftPwm.write(1)
    if keyup(LEFT):
        leftPwm.write(0)
        LF.write(False)
        LB.write(False)

    if keydown(UP):
        direction = "forward"
    if keydown(DOWN):
        direction = "back"

    # GAMEPAD CONTROL
    if gamepad_control:
        #print pad.get_numbuttons()
        horizontal = pad.get_axis(0)

        for event in pygame.event.get():
            print event

        if horizontal > 0.2:
            rightPwm.write(0.9)
        if horizontal < -0.2:
            leftPwm.write(0.9)

    gobo.draw()
    sleep(0.01)
    UPDATE()
<|endoftext|>


<commit_before>import copy
from scipy import spatial

from PySide6 import QtCore

class Connect:
    """docstring"""

    def __init__(self, progdialog):

        # get MainWindow instance (overcomes handling parents)
        self.mainwindow = QtCore.QCoreApplication.instance().mainwindow

        self.progdialog = progdialog

    def getVertices(self, block):
        """Make a list of point tuples from a BlockMesh object

        Args:
            block (BlockMesh): BlockMesh object

        Returns:
            list: list of point tuples
                  # [(x1, y1), (x2, y2), (x3, y3), ... , (xn, yn)]
        """
        vertices = list()
        for uline in block.getULines():
            vertices += uline
        return vertices

    def getConnectivity(self, block):

        connectivity = list()

        U, V = block.getDivUV()
        up = U + 1
        for u in range(U):
            for v in range(V):
                p1 = v * up + u
                p2 = p1 + up
                p3 = p2 + 1
                p4 = p1 + 1
                connectivity.append((p1, p2, p3, p4))

        return connectivity

    def getMinMaxConnectivityIDs(self, connectivity):
        id_min = 1e10
        id_max = -1
        for cell in connectivity:
            for id in cell:
                id_min = min(id_min, id)
                id_max = max(id_max, id)
        return id_min, id_max

    def getNearestNeighboursPairs(self, vertices, radius=1.e-6):
        tree = spatial.cKDTree(vertices)
        pairs = tree.query_pairs(radius, p=2., eps=0)
        return pairs

    def getNearestNeighboursBiDirectional(d1, d2, radius=1.e-6):
        """Get all indices ofts in d1 which are within distance r to d2"""
        tree_1 = spatial.cKDTree(d1)
        tree_2 = spatial.cKDTree(d2)
        idx1 = tree_2.query_ball_tree(tree_1, radius, p=2., eps=0)
        idx2 = tree_1.query_ball_tree(tree_2, radius, p=2., eps=0)
        matching = [e[0] for e in idx1 if e]
        opposite = [e[0] for e in idx2 if e]
        return matching, opposite

    def getNearestNeighbours(self, vertices, neighbours, radius=1.e-6):
        """Get the nearest neighbours to each vertex in a list of vertices
        uses Scipy kd-tree for quick nearest-neighbor lookup

        Args:
            vertices (list of tuples): Vertices for which nearest neighbours
                                       should be searched
            neighbours (list of tuples): These are the neighbours which
                                         are being searched
            radius (float, optional): Search neighbours within this radius

        Returns:
            vertex_and_neighbours(dictionary): Contains vertices searched
                                               as key and a list of nearest
                                               neighbours as values
        """

        # setup k-dimensional tree
        tree = spatial.cKDTree(neighbours)

        vertex_and_neighbours = dict()
        for vertex_id, vertex in enumerate(vertices):
            vertex_and_neighbours[vertex_id] = \
                tree.query_ball_point(vertex, radius)

        return vertex_and_neighbours

    def shiftConnectivity(self, connectivity, shift):

        if shift == 0:
            return connectivity

        connectivity_shifted = list()
        for cell in connectivity:
            new_cell = [vertex + shift for vertex in cell]
            connectivity_shifted.append(new_cell)

        return connectivity_shifted

    def connectAllBlocks(self, blocks):

        # compile global vertex list and cell connectivity from all blocks
        vertices = list()
        connectivity = list()

        for i, block in enumerate(blocks):

            # accumulated number of vertices
            # for i = 0 shift is automatically 0
            # so the connectivity of the first block doesn't get shifted
            # thus, this variable must be set before 'vertices += ...'
            shift = len(vertices)
            print('Shift in block {}: {}'.format(i, shift))

            # concatenate vertices of all blocks
            # vertices += [vertex for vertex in self.getVertices(block)]
            vertices += self.getVertices(block)

            # shift the block connectivity by accumulated number of vertices
            # from all blocks before this one
            connectivity_block = \
                self.shiftConnectivity(self.getConnectivity(block), shift)
            connectivity += [tuple(cell) for cell in connectivity_block]

        self.progdialog.setValue(80)

        # FIXME
        # FIXME for some reason tuples need to be redefined
        # FIXME
        vertices = [(vertex[0], vertex[1]) for vertex in vertices]

        # search vertices of all blocks against themselves
        # finds itself AND multiple connections very fast
        # uses Scipy kd-tree for quick nearest-neighbor lookup
        # the distance tolerance is specified via the radius variable
        vertex_and_neighbours = self.getNearestNeighbours(vertices,
                                                          vertices,
                                                          radius=1.e-6)

        # substitute vertex ids in connectivity at block connections
        connectivity_connected = list()
        for cell in connectivity:
            cell_new = list()
            for node in cell:
                # if there is only one cell in vertex_and_neighbours,
                # then it is taken as it is
                # if there is more than one cell,
                # then the minimum vertex index is used
                # so a few vertices remain unused and need to be removed later
                node_new = min(vertex_and_neighbours[node])
                cell_new.append(node_new)
            connectivity_connected.append(cell_new)

        # DEBUG
        with open('vertices.dat', 'w') as f:
            for i, vertex in enumerate(vertices):
                f.write('{:8d} {:10.6f} {:10.6f}\n'.format(i, vertex[0], vertex[1]))
        with open('connectivity.dat', 'w') as f:
            for i, cell in enumerate(connectivity):
                f.write('{:8d}'.format(i) + ' '.join(['{:8d}'.format(c) for c in cell]) + '\n')
        with open('connectivity_connected.dat', 'w') as f:
            for i, cell in enumerate(connectivity_connected):
                f.write('{:8d}'.format(i) + ' '.join(['{:8d}'.format(c) for c in cell]) + '\n')

        self.progdialog.setValue(90)

        connectivity_clean = copy.deepcopy(connectivity_connected)

        return (vertices, connectivity, self.progdialog)
<commit_msg>continue fixing connect algorithm<commit_after>import copy

import numpy as np
from scipy import spatial

from PySide6 import QtCore

class Connect:
    """docstring"""

    def __init__(self, progdialog):

        # get MainWindow instance (overcomes handling parents)
        self.mainwindow = QtCore.QCoreApplication.instance().mainwindow

        self.progdialog = progdialog

    def getVertices(self, block):
        """Make a list of point tuples from a BlockMesh object

        Args:
            block (BlockMesh): BlockMesh object

        Returns:
            list: list of point tuples
                  # [(x1, y1), (x2, y2), (x3, y3), ... , (xn, yn)]
        """
        vertices = list()
        for uline in block.getULines():
            vertices += uline
        return vertices

    def getConnectivity(self, block):

        connectivity = list()

        U, V = block.getDivUV()
        up = U + 1
        for u in range(U):
            for v in range(V):
                p1 = v * up + u
                p2 = p1 + up
                p3 = p2 + 1
                p4 = p1 + 1
                connectivity.append((p1, p2, p3, p4))

        return connectivity

    def getMinMaxConnectivityIDs(self, connectivity):
        id_min = 1e10
        id_max = -1
        for cell in connectivity:
            for id in cell:
                id_min = min(id_min, id)
                id_max = max(id_max, id)
        return id_min, id_max

    def getNearestNeighboursPairs(self, vertices, radius=1.e-6):
        tree = spatial.cKDTree(vertices)
        pairs = tree.query_pairs(radius, p=2., eps=0)
        return pairs

    def getNearestNeighboursBiDirectional(d1, d2, radius=1.e-6):
        """Get all indices ofts in d1 which are within distance r to d2"""
        tree_1 = spatial.cKDTree(d1)
        tree_2 = spatial.cKDTree(d2)
        idx1 = tree_2.query_ball_tree(tree_1, radius, p=2., eps=0)
        idx2 = tree_1.query_ball_tree(tree_2, radius, p=2., eps=0)
        matching = [e[0] for e in idx1 if e]
        opposite = [e[0] for e in idx2 if e]
        return matching, opposite

    def getNearestNeighbours(self, vertices, neighbours, radius=1.e-6):
        """Get the nearest neighbours to each vertex in a list of vertices
        uses Scipy kd-tree for quick nearest-neighbor lookup

        Args:
            vertices (list of tuples): Vertices for which nearest neighbours
                                       should be searched
            neighbours (list of tuples): These are the neighbours which
                                         are being searched
            radius (float, optional): Search neighbours within this radius

        Returns:
            vertex_and_neighbours(dictionary): Contains vertices searched
                                               as key and a list of nearest
                                               neighbours as values
        """

        # setup k-dimensional tree
        tree = spatial.cKDTree(neighbours)

        vertex_and_neighbours = dict()
        for vertex_id, vertex in enumerate(vertices):
            vertex_and_neighbours[vertex_id] = \
                tree.query_ball_point(vertex, radius)

        return vertex_and_neighbours

    def shiftConnectivity(self, connectivity, shift):

        if shift == 0:
            return connectivity

        connectivity_shifted = list()
        for cell in connectivity:
            new_cell = [vertex + shift for vertex in cell]
            connectivity_shifted.append(new_cell)

        return connectivity_shifted

    def connectAllBlocks(self, blocks):

        # compile global vertex list and cell connectivity from all blocks
        vertices = list()
        connectivity = list()

        for i, block in enumerate(blocks):

            # accumulated number of vertices
            # for i = 0 shift is automatically 0
            # so the connectivity of the first block doesn't get shifted
            # thus, this variable must be set before 'vertices += ...'
            shift = len(vertices)
            print('Shift in block {}: {}'.format(i, shift))

            # concatenate vertices of all blocks
            # vertices += [vertex for vertex in self.getVertices(block)]
            vertices += self.getVertices(block)

            # shift the block connectivity by accumulated number of vertices
            # from all blocks before this one
            connectivity_block = \
                self.shiftConnectivity(self.getConnectivity(block), shift)
            connectivity += [tuple(cell) for cell in connectivity_block]

        self.progdialog.setValue(80)

        # FIXME
        # FIXME for some reason tuples need to be redefined
        # FIXME
        vertices = [(vertex[0], vertex[1]) for vertex in vertices]

        # search vertices of all blocks against themselves
        # finds itself AND multiple connections very fast
        # uses Scipy kd-tree for quick nearest-neighbor lookup
        # the distance tolerance is specified via the radius variable
        vertex_and_neighbours = self.getNearestNeighbours(vertices,
                                                          vertices,
                                                          radius=1.e-6)

        # substitute vertex ids in connectivity at block connections
        connectivity_connected = list()
        for cell in connectivity:
            cell_new = list()
            for node in cell:
                # if there is only one cell in vertex_and_neighbours,
                # then it is taken as it is
                # if there is more than one cell,
                # then the minimum vertex index is used
                # so a few vertices remain unused and need to be removed later
                node_new = min(vertex_and_neighbours[node])
                cell_new.append(node_new)
            connectivity_connected.append(cell_new)

        # DEBUG
        with open('vertices.dat', 'w') as f:
            for i, vertex in enumerate(vertices):
                f.write('{:8d} {:10.6f} {:10.6f}\n'.format(i, vertex[0], vertex[1]))
        with open('connectivity.dat', 'w') as f:
            for i, cell in enumerate(connectivity):
                f.write('{:8d}'.format(i) + ' '.join(['{:8d}'.format(c) for c in cell]) + '\n')
        with open('connectivity_connected.dat', 'w') as f:
            for i, cell in enumerate(connectivity_connected):
                f.write('{:8d}'.format(i) + ' '.join(['{:8d}'.format(c) for c in cell]) + '\n')

        connectivity_array = np.array(connectivity)
        cconnectivity_connected_array = np.array(connectivity_connected)

        self.progdialog.setValue(90)

        connectivity_clean = copy.deepcopy(connectivity_connected)

        return (vertices, connectivity, self.progdialog)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

# https://github.com/wechat-python-sdk/wechat-python-sdk/blob/master/wechat_sdk/reply.py

import time

from .msg_template import (TEXT_TEMPLATE, IMAGE_TEMPLATE,
                           VOICE_TEMPLATE, VIDEO_TEMPLATE,
                           THUM_MUSIC_TEMPLATE, NOTHUM_MUSIC_TEMPLATE,
                           ARITICLE_TEMPLATE, ARITICLE_ITEM_TEMPLATE)

class Article(object):
    def __init__(self, title=None, description=None, picurl=None, url=None):
        self.title = title or ''
        self.description = description or ''
        self.picurl = picurl or ''
        self.url = url or ''

class WXReply(object):

    def __init__(self, to_user=None, from_user=None,
                 create_time=None, **kwargs):
        '''
        MsgType: text|image|voice|video|music|news
        '''
        kwargs['to_user'] = to_user
        kwargs['from_user'] = from_user
        kwargs['create_time'] = create_time or int(time.time())

        self.params = {k: v for k, v in kwargs.items() if kwargs[k]}

    def render(self):
        raise NotImplementedError()

class TextReply(WXReply):
    """
    回复文字消息
    """
    TEMPLATE = TEXT_TEMPLATE

    def __init__(self, content, *args, **kwargs):
        """
        :param content: 文字回复内容
        """
        super(TextReply, self).__init__(content=content, *args, **kwargs)

    def render(self):
        return self.TEMPLATE.format(**self.params)

class ImageReply(WXReply):
    """
    回复图片消息
    """
    TEMPLATE = IMAGE_TEMPLATE

    def __init__(self, media_id):
        """
        :param media_id: 图片的 MediaID
        """
        super(ImageReply, self).__init__(media_id=media_id)

    def render(self):
        return self.TEMPLATE.format(**self.params)

class VoiceReply(WXReply):
    """
    回复语音消息
    """
    TEMPLATE = VOICE_TEMPLATE

    def __init__(self, media_id):
        """
        :param media_id: 语音的 MediaID
        """
        super(VoiceReply, self).__init__(media_id=media_id)

    def render(self):
        return self.TEMPLATE.format(**self.params)

class VideoReply(WXReply):
    """
    回复视频消息
    """

    TEMPLATE = VIDEO_TEMPLATE

    def __init__(self, media_id, title=None, description=None):
        """
        :param media_id: 视频的 MediaID
        :param title: 视频消息的标题
        :param description: 视频消息的描述
        """
        title = title or ''
        description = description or ''
        super(VideoReply, self).__init__(media_id=media_id, title=title,
                                         description=description)

    def render(self):
        return self.TEMPLATE.format(**self.params)

class MusicReply(WXReply):
    """
    回复音乐消息
    """
    TEMPLATE_THUMB = THUM_MUSIC_TEMPLATE
    TEMPLATE_NOTHUMB = NOTHUM_MUSIC_TEMPLATE

    def __init__(self, title='', description='', music_url='',
                 hq_music_url='', thumb_media_id=None):
        title = title or ''
        description = description or ''
        music_url = music_url or ''
        hq_music_url = hq_music_url or music_url
        super(MusicReply, self).__init__(
            title=title, description=description, music_url=music_url,
            hq_music_url=hq_music_url, thumb_media_id=thumb_media_id)

    def render(self):
        if self._args['thumb_media_id']:
            return self.TEMPLATE.format(**self.params)
        else:
            return self.TEMPLATE.format(**self.params)

class ArticleReply(WXReply):

    TEMPLATE = ARITICLE_TEMPLATE
    ITEM_TEMPLATE = ARITICLE_ITEM_TEMPLATE

    def __init__(self, **kwargs):
        super(ArticleReply, self).__init__(**kwargs)
        self._articles = []

    def add_article(self, article):
        if len(self._articles) >= 8:
            raise AttributeError(
                "Can't add more than 8 articles in an ArticleReply")
        else:
            self._articles.append(article)

    def render(self):
        items = []
        for article in self._articles:
            items.append(ArticleReply.ITEM_TEMPLATE.format(
                title=article.title,
                description=article.description,
                picurl=article.picurl,
                url=article.url,
            ))
        self.params["items"] = ''.join(items)
        self.params["count"] = len(items)
        return self.TEMPLATE.format(**self.params)

class WXCustomReply(object):

    def __init__(self, to_user=None, msgtype=None, **kwargs):
        '''
        MsgType: text|image|voice|video|music|news
        '''
        kwargs['to_user'] = to_user
        kwargs['msgtype'] = msgtype

        self.params = {k: v for k, v in kwargs.items() if kwargs[k]}

    def render(self):
        raise NotImplementedError()

class CustomTextReply(WXCustomReply):
    """
    回复文字消息
    """

    def __init__(self, content, *args, **kwargs):
        """
        :param content: 文字回复内容
        """
        super(CustomTextReply, self).__init__(msgtype='text',
                                              *args, **kwargs)
        self.content = content

    def render(self):
        self.params['text'] = {
            'content': self.content
        }
        return self.params

class CustomImageReply(WXCustomReply):
    """
    回复图片消息
    """

    def __init__(self, media_id):
        """
        :param media_id: 图片的 MediaID
        """
        super(CustomImageReply, self).__init__(msgtype='image')
        self.media_id = media_id

    def render(self):
        self.params['image'] = {
            'media_id': self.media_id
        }
        return self.params

class CustomVoiceReply(WXCustomReply):
    """
    回复语音消息
    """

    def __init__(self, media_id):
        """
        :param media_id: 语音的 MediaID
        """
        super(CustomVoiceReply, self).__init__(msgtype='voice')
        self.media_id = media_id

    def render(self):
        self.params['image'] = {
            'media_id': self.media_id
        }
        return self.params

class CustomVideoReply(WXCustomReply):
    """
    回复视频消息
    """

    def __init__(self, media_id, title=None, description=None):
        """
        :param media_id: 视频的 MediaID
        :param title: 视频消息的标题
        :param description: 视频消息的描述
        """
        super(CustomVideoReply, self).__init__(msgtype='music')
        self.media_id = media_id
        self.title = title or ''
        self.description = description or ''

    def render(self):
        self.params['video'] = {
            'media_id': self.media_id,
            'title': self.title,
            'description': self.description
        }
        return self.params

class CustomMusicReply(WXCustomReply):
    """
    回复音乐消息
    """

    def __init__(self, title='', description='', music_url='',
                 hq_music_url='', thumb_media_id=None):
        self.title = title or ''
        self.description = description or ''
        self.musicurl = music_url or ''
        self.hqmusicurl = hq_music_url or music_url
        self.thumb_media_id = thumb_media_id
        super(CustomMusicReply, self).__init__(msgtype='music')

    def render(self):
        self.params['music'] = {
            'title': self.title,
            'description': self.description,
            'musicurl': self.musicurl,
            'hqmusicurl': self.hqmusicurl,
            'thumb_media_id': self.thumb_media_id,
        }
        return self.params

class CustomArticleReply(WXCustomReply):

    def __init__(self, **kwargs):
        self._articles = []
        super(CustomArticleReply, self).__init__(msgtype='news', **kwargs)

    def add_article(self, article):
        '''
        :params article (dict)
        {
            "title":"Happy Day",
            "description":"Is Really A Happy Day",
            "url":"URL",
            "picurl":"PIC_URL"
        }

        '''
        if len(self._articles) >= 8:
            raise AttributeError(
                "Can't add more than 8 articles in an CustomArticleReply")
        else:
            self._articles.append(article)

    def render(self):
        items = []
        for article in self._articles:
            items.append(article)
        self.params['news'] = {'articles': items}
        return self.params
<commit_msg>fix reply bug<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

# https://github.com/wechat-python-sdk/wechat-python-sdk/blob/master/wechat_sdk/reply.py

import time

from .msg_template import (TEXT_TEMPLATE, IMAGE_TEMPLATE,
                           VOICE_TEMPLATE, VIDEO_TEMPLATE,
                           THUM_MUSIC_TEMPLATE, NOTHUM_MUSIC_TEMPLATE,
                           ARITICLE_TEMPLATE, ARITICLE_ITEM_TEMPLATE)

class Article(object):

    def __init__(self, title=None, description=None, picurl=None, url=None):
        self.title = title or ''
        self.description = description or ''
        self.picurl = picurl or ''
        self.url = url or ''

class WXReply(object):

    def __init__(self, to_user=None, from_user=None,
                 create_time=None, **kwargs):
        '''
        MsgType: text|image|voice|video|music|news
        '''
        kwargs['to_user'] = to_user
        kwargs['from_user'] = from_user
        kwargs['create_time'] = create_time or int(time.time())

        self.params = {k: v for k, v in kwargs.items() if kwargs[k]}

    def render(self):
        raise NotImplementedError()

class TextReply(WXReply):
    """
    回复文字消息
    """
    TEMPLATE = TEXT_TEMPLATE

    def __init__(self, content, *args, **kwargs):
        """
        :param content: 文字回复内容
        """
        super(TextReply, self).__init__(content=content, *args, **kwargs)

    def render(self):
        return self.TEMPLATE.format(**self.params)

class ImageReply(WXReply):
    """
    回复图片消息
    """
    TEMPLATE = IMAGE_TEMPLATE

    def __init__(self, media_id, *args, **kwargs):
        """
        :param media_id: 图片的 MediaID
        """
        super(ImageReply, self).__init__(media_id=media_id, *args, **kwargs)

    def render(self):
        return self.TEMPLATE.format(**self.params)

class VoiceReply(WXReply):
    """
    回复语音消息
    """
    TEMPLATE = VOICE_TEMPLATE

    def __init__(self, media_id, *args, **kwargs):
        """
        :param media_id: 语音的 MediaID
        """
        super(VoiceReply, self).__init__(media_id=media_id, *args, **kwargs)

    def render(self):
        return self.TEMPLATE.format(**self.params)

class VideoReply(WXReply):
    """
    回复视频消息
    """

    TEMPLATE = VIDEO_TEMPLATE

    def __init__(self, media_id, title=None, description=None,
                 *args, **kwargs):
        """
        :param media_id: 视频的 MediaID
        :param title: 视频消息的标题
        :param description: 视频消息的描述
        """
        title = title or ''
        description = description or ''
        super(VideoReply, self).__init__(media_id=media_id, title=title,
                                         description=description,
                                         *args, **kwargs)

    def render(self):
        return self.TEMPLATE.format(**self.params)

class MusicReply(WXReply):
    """
    回复音乐消息
    """
    TEMPLATE_THUMB = THUM_MUSIC_TEMPLATE
    TEMPLATE_NOTHUMB = NOTHUM_MUSIC_TEMPLATE

    def __init__(self, title='', description='', music_url='',
                 hq_music_url='', thumb_media_id=None, *args, **kwargs):
        self.title = title or ''
        self.description = description or ''
        self.music_url = music_url or ''
        self.hq_music_url = hq_music_url or music_url
        self.thumb_media_id = thumb_media_id or ''
        super(MusicReply, self).__init__(
            title=self.title, description=self.description,
            music_url=self.music_url,
            hq_music_url=self.hq_music_url,
            thumb_media_id=self.thumb_media_id)

    def render(self):
        if self.thumb_media_id:
            return self.TEMPLATE_THUMB.format(**self.params)
        else:
            return self.TEMPLATE_NOTHUMB.format(**self.params)

class ArticleReply(WXReply):

    TEMPLATE = ARITICLE_TEMPLATE
    ITEM_TEMPLATE = ARITICLE_ITEM_TEMPLATE

    def __init__(self, **kwargs):
        super(ArticleReply, self).__init__(**kwargs)
        self._articles = []

    def add_article(self, article):
        if len(self._articles) >= 8:
            raise AttributeError(
                "Can't add more than 8 articles in an ArticleReply")
        else:
            self._articles.append(article)

    def render(self):
        items = []
        for article in self._articles:
            items.append(ArticleReply.ITEM_TEMPLATE.format(
                title=article.title,
                description=article.description,
                picurl=article.picurl,
                url=article.url,
            ))
        self.params["items"] = ''.join(items)
        self.params["count"] = len(items)
        return self.TEMPLATE.format(**self.params)

class WXCustomReply(object):

    def __init__(self, to_user=None, msgtype=None, **kwargs):
        '''
        MsgType: text|image|voice|video|music|news
        '''
        kwargs['to_user'] = to_user
        kwargs['msgtype'] = msgtype

        self.params = {k: v for k, v in kwargs.items() if kwargs[k]}

    def render(self):
        raise NotImplementedError()

class CustomTextReply(WXCustomReply):
    """
    回复文字消息
    """

    def __init__(self, content, *args, **kwargs):
        """
        :param content: 文字回复内容
        """
        super(CustomTextReply, self).__init__(msgtype='text',
                                              *args, **kwargs)
        self.content = content

    def render(self):
        self.params['text'] = {
            'content': self.content
        }
        return self.params

class CustomImageReply(WXCustomReply):
    """
    回复图片消息
    """

    def __init__(self, media_id):
        """
        :param media_id: 图片的 MediaID
        """
        super(CustomImageReply, self).__init__(msgtype='image')
        self.media_id = media_id

    def render(self):
        self.params['image'] = {
            'media_id': self.media_id
        }
        return self.params

class CustomVoiceReply(WXCustomReply):
    """
    回复语音消息
    """

    def __init__(self, media_id):
        """
        :param media_id: 语音的 MediaID
        """
        super(CustomVoiceReply, self).__init__(msgtype='voice')
        self.media_id = media_id

    def render(self):
        self.params['image'] = {
            'media_id': self.media_id
        }
        return self.params

class CustomVideoReply(WXCustomReply):
    """
    回复视频消息
    """

    def __init__(self, media_id, title=None, description=None):
        """
        :param media_id: 视频的 MediaID
        :param title: 视频消息的标题
        :param description: 视频消息的描述
        """
        super(CustomVideoReply, self).__init__(msgtype='music')
        self.media_id = media_id
        self.title = title or ''
        self.description = description or ''

    def render(self):
        self.params['video'] = {
            'media_id': self.media_id,
            'title': self.title,
            'description': self.description
        }
        return self.params

class CustomMusicReply(WXCustomReply):
    """
    回复音乐消息
    """

    def __init__(self, title='', description='', music_url='',
                 hq_music_url='', thumb_media_id=None):
        self.title = title or ''
        self.description = description or ''
        self.musicurl = music_url or ''
        self.hqmusicurl = hq_music_url or music_url
        self.thumb_media_id = thumb_media_id
        super(CustomMusicReply, self).__init__(msgtype='music')

    def render(self):
        self.params['music'] = {
            'title': self.title,
            'description': self.description,
            'musicurl': self.musicurl,
            'hqmusicurl': self.hqmusicurl,
            'thumb_media_id': self.thumb_media_id,
        }
        return self.params

class CustomArticleReply(WXCustomReply):

    def __init__(self, **kwargs):
        self._articles = []
        super(CustomArticleReply, self).__init__(msgtype='news', **kwargs)

    def add_article(self, article):
        '''
        :params article (dict)
        {
            "title":"Happy Day",
            "description":"Is Really A Happy Day",
            "url":"URL",
            "picurl":"PIC_URL"
        }

        '''
        if len(self._articles) >= 8:
            raise AttributeError(
                "Can't add more than 8 articles in an CustomArticleReply")
        else:
            self._articles.append(article)

    def render(self):
        items = []
        for article in self._articles:
            items.append(article)
        self.params['news'] = {'articles': items}
        return self.params
<|endoftext|>


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

import lldb

# In the lldb shell, load with:
# command script import [Realm path]/tools/rlm_lldb.py --allow-reload
# To load automatically, add that line to your ~/.lldbinit file (which you will
# have to create if you have not set up any previous lldb scripts)

property_types = {
    0: 'int64_t',
    10: 'double',
    1: 'bool',
    9: 'float',
}

def cache_lookup(cache, key, generator):
    value = cache.get(key, None)
    if not value:
        value = generator(key)
        cache[key] = value
    return value

ivar_cache = {}
def get_ivar_info(obj, ivar):
    def get_offset(ivar):
        class_name, ivar_name = ivar.split('.')
        frame = obj.GetThread().GetSelectedFrame()
        ptr = frame.EvaluateExpression("&(({} *)0)->_{}".format(class_name, ivar_name))
        return (ptr.GetValueAsUnsigned(), ptr.deref.type, ptr.deref.size)

    return cache_lookup(ivar_cache, ivar, get_offset)

def get_ivar(obj, addr, ivar):
    offset, _, size = get_ivar_info(obj, ivar)
    if isinstance(addr, lldb.SBAddress):
        addr = int(str(addr), 16)
    return obj.GetProcess().ReadUnsignedFromMemory(addr + offset, size, lldb.SBError())

class SyntheticChildrenProvider(object):
    def _eval(self, expr):
        frame = self.obj.GetThread().GetSelectedFrame()
        return frame.EvaluateExpression(expr)

    def _get_ivar(self, addr, ivar):
        return get_ivar(self.obj, addr, ivar)

    def _to_str(self, val):
        return self.obj.GetProcess().ReadCStringFromMemory(val, 1024, lldb.SBError())

    def _value_from_ivar(self, ivar):
        offset, ivar_type, _ = get_ivar_info(self.obj, 'RLMObject.' + ivar)
        return self.obj.CreateChildAtOffset(ivar, offset, ivar_type)

schema_cache = {}
class RLMObject_SyntheticChildrenProvider(SyntheticChildrenProvider):
    def __init__(self, obj, _):
        self.obj = obj

        if not obj.GetAddress():
            self.props = []
            return

        self.bool_type = obj.GetTarget().FindFirstType('BOOL')
        self.realm_type = obj.GetTarget().FindFirstType('RLMRealm')
        self.object_schema_type = obj.GetTarget().FindFirstType('RLMObjectSchema')

        object_schema = self._get_ivar(self.obj.GetAddress(), 'RLMObject.objectSchema')

        def get_schema(object_schema):
            properties = self._get_ivar(object_schema, 'RLMObjectSchema.properties')
            count = self._eval("(NSUInteger)[((NSArray *){}) count]".format(properties)).GetValueAsUnsigned()
            return [self._get_prop(properties, i) for i in range(count)]

        self.props = cache_lookup(schema_cache, object_schema, get_schema)

    def num_children(self):
        return len(self.props) + 2

    def has_children(self):
        return True

    def get_child_index(self, name):
        if name == 'realm':
            return 0
        if name == 'objectSchema':
            return 1
        return next(i for i, (prop_name, _) in enumerate(self.props) if prop_name == name)

    def get_child_at_index(self, index):
        if index == 0:
            return self._value_from_ivar('realm')
        if index == 1:
            return self._value_from_ivar('objectSchema')

        name, getter = self.props[index - 2]
        value = self._eval(getter)
        return self.obj.CreateValueFromData(name, value.GetData(), value.GetType())

    def update(self):
        pass

    def _get_prop(self, props, i):
        prop = self._eval("(NSUInteger)[((NSArray *){}) objectAtIndex:{}]".format(props, i)).GetValueAsUnsigned()
        name = self._to_str(self._eval('[(NSString *){} UTF8String]'.format(self._get_ivar(prop, "RLMProperty.name"))).GetValueAsUnsigned())
        type = self._get_ivar(prop, 'RLMProperty.type')
        getter = "({})[(id){} {}]".format(property_types.get(type, 'id'), self.obj.GetAddress(), name)
        return name, getter

class_name_cache = {}
def get_object_class_name(frame, obj, addr, ivar):
    class_name_ptr = get_ivar(obj, addr, ivar)
    def get_class_name(ptr):
        utf8_addr = frame.EvaluateExpression('(const char *)[(NSString *){} UTF8String]'.format(class_name_ptr)).GetValueAsUnsigned()
        return obj.GetProcess().ReadCStringFromMemory(utf8_addr, 1024, lldb.SBError())

    return cache_lookup(class_name_cache, class_name_ptr, get_class_name)

def RLMArray_SummaryProvider(obj, _):
    frame = obj.GetThread().GetSelectedFrame()
    class_name = get_object_class_name(frame, obj, obj.GetAddress(), 'RLMArray.objectClassName')
    count = frame.EvaluateExpression('(NSUInteger)[(RLMArray *){} count]'.format(obj.GetAddress())).GetValueAsUnsigned()
    return "({}[{}])".format(class_name, count)

def RLMResults_SummaryProvider(obj, _):
    frame = obj.GetThread().GetSelectedFrame()
    addr = int(str(obj.GetAddress()), 16)
    class_name = get_object_class_name(frame, obj, addr, 'RLMResults.objectClassName')

    view_created = get_ivar(obj, addr, 'RLMResults.viewCreated')
    if not view_created:
        return 'Unevaluated query on ' + class_name

    count = frame.EvaluateExpression('(NSUInteger)[(RLMResults *){} count]'.format(obj.GetAddress())).GetValueAsUnsigned()
    return "({}[{}])".format(class_name, count)

class RLMArray_SyntheticChildrenProvider(SyntheticChildrenProvider):
    def __init__(self, valobj, _):
        self.obj = valobj
        self.addr = self.obj.GetAddress()

    def num_children(self):
        if not self.count:
            self.count = self._eval("(NSUInteger)[(RLMArray *){} count]".format(self.addr)).GetValueAsUnsigned()
        return self.count + 1

    def has_children(self):
        return True

    def get_child_index(self, name):
        if name == 'realm':
            return 0
        return int(name.lstrip('[').rstrip(']')) + 1

    def get_child_at_index(self, index):
        if index == 0:
            return self._value_from_ivar('realm')
        value = self._eval('(id)[(id){} objectAtIndex:{}]'.format(self.addr, index - 1))
        return self.obj.CreateValueFromData('[' + str(index - 1) + ']', value.GetData(), value.GetType())

    def update(self):
        self.count = None

def __lldb_init_module(debugger, _):
    debugger.HandleCommand('type summary add RLMArray -F rlm_lldb.RLMArray_SummaryProvider')
    debugger.HandleCommand('type summary add RLMArrayLinkView -F rlm_lldb.RLMArray_SummaryProvider')
    debugger.HandleCommand('type summary add RLMResults -F rlm_lldb.RLMResults_SummaryProvider')

    debugger.HandleCommand('type synthetic add RLMArray --python-class rlm_lldb.RLMArray_SyntheticChildrenProvider')
    debugger.HandleCommand('type synthetic add RLMArrayLinkView --python-class rlm_lldb.RLMArray_SyntheticChildrenProvider')
    debugger.HandleCommand('type synthetic add RLMResults --python-class rlm_lldb.RLMArray_SyntheticChildrenProvider')
    debugger.HandleCommand('type synthetic add -x RLMAccessor_.* --python-class rlm_lldb.RLMObject_SyntheticChildrenProvider')
<commit_msg>Add handling for deleted accessors<commit_after>##############################################################################
#
# Copyright 2014 Realm Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################

import lldb

# In the lldb shell, load with:
# command script import [Realm path]/tools/rlm_lldb.py --allow-reload
# To load automatically, add that line to your ~/.lldbinit file (which you will
# have to create if you have not set up any previous lldb scripts)

property_types = {
    0: 'int64_t',
    10: 'double',
    1: 'bool',
    9: 'float',
}

def cache_lookup(cache, key, generator):
    value = cache.get(key, None)
    if not value:
        value = generator(key)
        cache[key] = value
    return value

ivar_cache = {}
def get_ivar_info(obj, ivar):
    def get_offset(ivar):
        class_name, ivar_name = ivar.split('.')
        frame = obj.GetThread().GetSelectedFrame()
        ptr = frame.EvaluateExpression("&(({} *)0)->{}".format(class_name, ivar_name))
        return (ptr.GetValueAsUnsigned(), ptr.deref.type, ptr.deref.size)

    return cache_lookup(ivar_cache, ivar, get_offset)

def get_ivar(obj, addr, ivar):
    offset, _, size = get_ivar_info(obj, ivar)
    if isinstance(addr, lldb.SBAddress):
        addr = int(str(addr), 16)
    return obj.GetProcess().ReadUnsignedFromMemory(addr + offset, size, lldb.SBError())

object_table_ptr_offset = None
def is_object_deleted(obj):
    addr = int(str(obj.GetAddress()), 16)
    global object_table_ptr_offset
    if not object_table_ptr_offset:
        row, _, _ = get_ivar_info(obj, 'RLMObject._row')
        table, _, _ = get_ivar_info(obj, 'tightdb::Row.m_table')
        ptr, _, _ = get_ivar_info(obj, 'tightdb::TableRef.m_ptr')
        object_table_ptr_offset = row + table + ptr

    ptr = obj.GetProcess().ReadUnsignedFromMemory(addr + object_table_ptr_offset,
            obj.target.addr_size, lldb.SBError())
    return ptr == 0

class SyntheticChildrenProvider(object):
    def _eval(self, expr):
        frame = self.obj.GetThread().GetSelectedFrame()
        return frame.EvaluateExpression(expr)

    def _get_ivar(self, addr, ivar):
        return get_ivar(self.obj, addr, ivar)

    def _to_str(self, val):
        return self.obj.GetProcess().ReadCStringFromMemory(val, 1024, lldb.SBError())

    def _value_from_ivar(self, ivar):
        offset, ivar_type, _ = get_ivar_info(self.obj, 'RLMObject._' + ivar)
        return self.obj.CreateChildAtOffset(ivar, offset, ivar_type)

def RLMObject_SummaryProvider(obj, _):
    if is_object_deleted(obj):
        return '[Deleted object]'
    return None

schema_cache = {}
class RLMObject_SyntheticChildrenProvider(SyntheticChildrenProvider):
    def __init__(self, obj, _):
        self.obj = obj

        if not obj.GetAddress() or is_object_deleted(obj):
            self.props = []
            return

        object_schema = self._get_ivar(self.obj.GetAddress(), 'RLMObject._objectSchema')

        self.bool_type = obj.GetTarget().FindFirstType('BOOL')
        self.realm_type = obj.GetTarget().FindFirstType('RLMRealm')
        self.object_schema_type = obj.GetTarget().FindFirstType('RLMObjectSchema')

        def get_schema(object_schema):
            properties = self._get_ivar(object_schema, 'RLMObjectSchema._properties')
            if not properties:
                return None
            count = self._eval("(NSUInteger)[((NSArray *){}) count]".format(properties)).GetValueAsUnsigned()
            return [self._get_prop(properties, i) for i in range(count)]

        self.props = cache_lookup(schema_cache, object_schema, get_schema)

    def num_children(self):
        return len(self.props) + 2

    def has_children(self):
        return not is_object_deleted(self.obj)

    def get_child_index(self, name):
        if name == 'realm':
            return 0
        if name == 'objectSchema':
            return 1
        return next(i for i, (prop_name, _) in enumerate(self.props) if prop_name == name)

    def get_child_at_index(self, index):
        if index == 0:
            return self._value_from_ivar('realm')
        if index == 1:
            return self._value_from_ivar('objectSchema')

        name, getter = self.props[index - 2]
        value = self._eval(getter)
        return self.obj.CreateValueFromData(name, value.GetData(), value.GetType())

    def update(self):
        pass

    def _get_prop(self, props, i):
        prop = self._eval("(NSUInteger)[((NSArray *){}) objectAtIndex:{}]".format(props, i)).GetValueAsUnsigned()
        name = self._to_str(self._eval('[(NSString *){} UTF8String]'.format(self._get_ivar(prop, "RLMProperty._name"))).GetValueAsUnsigned())
        type = self._get_ivar(prop, 'RLMProperty._type')
        getter = "({})[(id){} {}]".format(property_types.get(type, 'id'), self.obj.GetAddress(), name)
        return name, getter

class_name_cache = {}
def get_object_class_name(frame, obj, addr, ivar):
    class_name_ptr = get_ivar(obj, addr, ivar)
    def get_class_name(ptr):
        utf8_addr = frame.EvaluateExpression('(const char *)[(NSString *){} UTF8String]'.format(class_name_ptr)).GetValueAsUnsigned()
        return obj.GetProcess().ReadCStringFromMemory(utf8_addr, 1024, lldb.SBError())

    return cache_lookup(class_name_cache, class_name_ptr, get_class_name)

def RLMArray_SummaryProvider(obj, _):
    frame = obj.GetThread().GetSelectedFrame()
    class_name = get_object_class_name(frame, obj, obj.GetAddress(), 'RLMArray._objectClassName')
    count = frame.EvaluateExpression('(NSUInteger)[(RLMArray *){} count]'.format(obj.GetAddress())).GetValueAsUnsigned()
    return "({}[{}])".format(class_name, count)

def RLMResults_SummaryProvider(obj, _):
    frame = obj.GetThread().GetSelectedFrame()
    addr = int(str(obj.GetAddress()), 16)
    class_name = get_object_class_name(frame, obj, addr, 'RLMResults._objectClassName')

    view_created = get_ivar(obj, addr, 'RLMResults._viewCreated')
    if not view_created:
        return 'Unevaluated query on ' + class_name

    count = frame.EvaluateExpression('(NSUInteger)[(RLMResults *){} count]'.format(obj.GetAddress())).GetValueAsUnsigned()
    return "({}[{}])".format(class_name, count)

class RLMArray_SyntheticChildrenProvider(SyntheticChildrenProvider):
    def __init__(self, valobj, _):
        self.obj = valobj
        self.addr = self.obj.GetAddress()

    def num_children(self):
        if not self.count:
            self.count = self._eval("(NSUInteger)[(RLMArray *){} count]".format(self.addr)).GetValueAsUnsigned()
        return self.count + 1

    def has_children(self):
        return True

    def get_child_index(self, name):
        if name == 'realm':
            return 0
        return int(name.lstrip('[').rstrip(']')) + 1

    def get_child_at_index(self, index):
        if index == 0:
            return self._value_from_ivar('realm')
        value = self._eval('(id)[(id){} objectAtIndex:{}]'.format(self.addr, index - 1))
        return self.obj.CreateValueFromData('[' + str(index - 1) + ']', value.GetData(), value.GetType())

    def update(self):
        self.count = None

def __lldb_init_module(debugger, _):
    debugger.HandleCommand('type summary add RLMArray -F rlm_lldb.RLMArray_SummaryProvider')
    debugger.HandleCommand('type summary add RLMArrayLinkView -F rlm_lldb.RLMArray_SummaryProvider')
    debugger.HandleCommand('type summary add RLMResults -F rlm_lldb.RLMResults_SummaryProvider')
    debugger.HandleCommand('type summary add -x RLMAccessor_ -F rlm_lldb.RLMObject_SummaryProvider')

    debugger.HandleCommand('type synthetic add RLMArray --python-class rlm_lldb.RLMArray_SyntheticChildrenProvider')
    debugger.HandleCommand('type synthetic add RLMArrayLinkView --python-class rlm_lldb.RLMArray_SyntheticChildrenProvider')
    debugger.HandleCommand('type synthetic add RLMResults --python-class rlm_lldb.RLMArray_SyntheticChildrenProvider')
    debugger.HandleCommand('type synthetic add -x RLMAccessor_.* --python-class rlm_lldb.RLMObject_SyntheticChildrenProvider')
<|endoftext|>


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

import subprocess
import argparse

parser = argparse.ArgumentParser(description="QEMU run tool", formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("bin", help="Kernel binary image to load")
parser.add_argument("-debug", help="Enable QEMU GDB mode and halt execution after reset", default=False, action="store_true")
parser.add_argument("-memsize", help="RAM size", default="512M")
parser.add_argument("-cpu", help='''
                                Type of CPU. Select from:
                                - cortex-a53
                                - cortex-a57
                                - cortex-a72
                                ''', default="cortex-a53")
parser.add_argument("-cores", help="Number of cores to emulate", default=1)
parser.add_argument("-graphics", help="Enable graphical mode (i.e. display)", default=False, action="store_true")
parser.add_argument("-dtb", help="Use the given DeviceTree blob file", default="")

args = parser.parse_args()

qemu = ["qemu-system-aarch64", "-M", "virt,gic_version=3"]
qemu += ["-m", args.memsize]
qemu += ["-cpu", args.cpu]
qemu += ["-smp", "cores="+ str(args.cores)]
if not args.graphics:
    qemu += ["-nographic"]
if args.debug:
    qemu += ["-s", "-S"]
if args.dtb != "":
    qemu += ["-dtb", args.dtb]
qemu += ["-kernel", args.bin]

print("Running QEMU ARM Virt platform with command-line: " + ' '.join(qemu))
print("CPU: " + args.cpu + " CORES: " + str(args.cores))
print("RAM SIZE: " + args.memsize)
if args.graphics:
    print("Running with graphics enabled")
if args.debug:
    print("Running with GDB mode enabled. Execution halted by QEMU. You will need to use GDB to continue running the code.")

subprocess.call(qemu)
<commit_msg>tools: Update run-qemu.py script<commit_after>#!/usr/bin/env python3

import subprocess
import argparse

parser = argparse.ArgumentParser(description="QEMU run tool", formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("bin", help="Kernel binary image to load")
parser.add_argument("-debug", help="Enable QEMU GDB mode and halt execution after reset", default=False, action="store_true")
parser.add_argument("-memsize", help="RAM size", default="512M")
parser.add_argument("-cpu", help='''
                                Type of CPU. Select from:
                                - cortex-a53
                                - cortex-a57
                                - cortex-a72
                                ''', default="cortex-a53")
parser.add_argument("-cores", help="Number of cores to emulate", default=1)
parser.add_argument("-graphics", help="Enable graphical mode (i.e. display)", default=False, action="store_true")
parser.add_argument("-dtb", help="Use the given DeviceTree blob file", default="")

args = parser.parse_args()

qemu = ["qemu-system-aarch64", "-M", "virt,gic-version=3"]
qemu += ["-m", args.memsize]
qemu += ["-cpu", args.cpu]
qemu += ["-smp", "cores="+ str(args.cores)]
if not args.graphics:
    qemu += ["-nographic"]
if args.debug:
    qemu += ["-s", "-S"]
if args.dtb != "":
    qemu += ["-dtb", args.dtb]
qemu += ["-kernel", args.bin]

print("Running QEMU ARM Virt platform with command-line: " + ' '.join(qemu))
print("CPU: " + args.cpu + " CORES: " + str(args.cores))
print("RAM SIZE: " + args.memsize)
if args.graphics:
    print("Running with graphics enabled")
if args.debug:
    print("Running with GDB mode enabled. Execution halted by QEMU. You will need to use GDB to continue running the code.")

subprocess.call(qemu)
<|endoftext|>


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

# Copyright 2017 Google, Inc
#
# 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

from google.cloud import kms_v1
from google.cloud.kms_v1 import enums

# [START kms_create_keyring]
def create_key_ring(project_id, location_id, key_ring_id):
    """Creates a KeyRing in the given location (e.g. global)."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the location associated with the KeyRing.
    parent = client.location_path(project_id, location_id)

    # The keyring object template
    keyring_name = client.key_ring_path(project_id, location_id, key_ring_id)
    keyring = {'name': keyring_name}

    # Create a KeyRing
    response = client.create_key_ring(parent, key_ring_id, keyring)

    print('Created KeyRing {}.'.format(response.name))
    return response
# [END kms_create_keyring]

# [START kms_create_cryptokey]
def create_crypto_key(project_id, location_id, key_ring_id, crypto_key_id):
    """Creates a CryptoKey within a KeyRing in the given location."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the KeyRing associated with the CryptoKey.
    parent = client.key_ring_path(project_id, location_id, key_ring_id)

    # Create the CryptoKey object template
    purpose = enums.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT
    crypto_key = {'purpose': purpose}

    # Create a CryptoKey for the given KeyRing.
    response = client.create_crypto_key(parent, crypto_key_id, crypto_key)

    print('Created CryptoKey {}.'.format(response.name))
    return response
# [END kms_create_cryptokey]

# [START kms_encrypt]
def encrypt_symmetric(project_id, location_id, key_ring_id, crypto_key_id,
                      plaintext):
    """Encrypts input plaintext data using the provided symmetric CryptoKey."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the CryptoKey.
    name = client.crypto_key_path_path(project_id, location_id, key_ring_id,
                                       crypto_key_id)

    # Use the KMS API to encrypt the data.
    response = client.encrypt(name, plaintext)
    return response.ciphertext
# [END kms_encrypt]

# [START kms_decrypt]
def decrypt_symmetric(project_id, location_id, key_ring_id, crypto_key_id,
                      ciphertext):
    """Decrypts input ciphertext using the provided symmetric CryptoKey."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the CryptoKey.
    name = client.crypto_key_path_path(project_id, location_id, key_ring_id,
                                       crypto_key_id)
    # Use the KMS API to decrypt the data.
    response = client.decrypt(name, ciphertext)
    return response.plaintext
# [END kms_decrypt]

# [START kms_disable_cryptokey_version]
def disable_crypto_key_version(project_id, location_id, key_ring_id,
                               crypto_key_id, version_id):
    """Disables a CryptoKeyVersion associated with a given CryptoKey and
    KeyRing."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # Construct the resource name of the CryptoKeyVersion.
    name = client.crypto_key_version_path(project_id, location_id, key_ring_id,
                                          crypto_key_id, version_id)

    # Use the KMS API to disable the CryptoKeyVersion.
    new_state = enums.CryptoKeyVersion.CryptoKeyVersionState.DISABLED
    version = {'name': name, 'state': new_state}
    update_mask = {'paths': ["state"]}

    # Print results
    response = client.update_crypto_key_version(version, update_mask)
    print('CryptoKeyVersion {}\'s state has been set to {}.'.format(
        name, response.state))
# [END kms_disable_cryptokey_version]

# [START kms_enable_cryptokey_version]
def enable_crypto_key_version(project_id, location_id, key_ring_id,
                              crypto_key_id, version_id):
    """Enables a CryptoKeyVersion associated with a given CryptoKey and
    KeyRing."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # Construct the resource name of the CryptoKeyVersion.
    name = client.crypto_key_version_path(project_id, location_id, key_ring_id,
                                          crypto_key_id, version_id)

    # Use the KMS API to enable the CryptoKeyVersion.
    new_state = enums.CryptoKeyVersion.CryptoKeyVersionState.ENABLED
    version = {'name': name, 'state': new_state}
    update_mask = {'paths': ["state"]}

    # Print results
    response = client.update_crypto_key_version(version, update_mask)
    print('CryptoKeyVersion {}\'s state has been set to {}.'.format(
        name, response.state))
# [END kms_enable_cryptokey_version]

# [START kms_destroy_cryptokey_version]
def destroy_crypto_key_version(
        project_id, location_id, key_ring_id, crypto_key_id, version_id):
    """Schedules a CryptoKeyVersion associated with a given CryptoKey and
    KeyRing for destruction 24 hours in the future."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # Construct the resource name of the CryptoKeyVersion.
    name = client.crypto_key_version_path(project_id, location_id, key_ring_id,
                                          crypto_key_id, version_id)

    # Use the KMS API to mark the CryptoKeyVersion for destruction.
    response = client.destroy_crypto_key_version(name)

    # Print results
    print('CryptoKeyVersion {}\'s state has been set to {}.'.format(
        name, response.state))
# [END kms_destroy_cryptokey_version]

# [START kms_restore_cryptokey_version]
def restore_crypto_key_version(
        project_id, location_id, key_ring_id, crypto_key_id, version_id):
    """Restores a CryptoKeyVersion that is scheduled for destruction."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # Construct the resource name of the CryptoKeyVersion.
    name = client.crypto_key_version_path(project_id, location_id, key_ring_id,
                                          crypto_key_id, version_id)

    # Use the KMS API to restore the CryptoKeyVersion.
    response = client.restore_crypto_key_version(name)

    # Print results
    print('CryptoKeyVersion {}\'s state has been set to {}.'.format(
        name, response.state))

# [END kms_restore_cryptokey_version]

# [START kms_add_member_to_cryptokey_policy]
def add_member_to_crypto_key_policy(
        project_id, location_id, key_ring_id, crypto_key_id, member, role):
    """Adds a member with a given role to the Identity and Access Management
    (IAM) policy for a given CryptoKey associated with a KeyRing."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the CryptoKey.
    resource = client.crypto_key_path_path(project_id, location_id,
                                           key_ring_id, crypto_key_id)
    # Get the current IAM policy.
    policy = client.get_iam_policy(resource)

    # Add member
    policy.bindings.add(
        role=role,
        members=[member])

    # Update the IAM Policy.
    client.set_iam_policy(resource, policy)

    # Print results
    print('Member {} added with role {} to policy for CryptoKey {} \
           in KeyRing {}'.format(member, role, crypto_key_id, key_ring_id))
# [END kms_add_member_to_cryptokey_policy]

# [START kms_add_member_to_keyring_policy]
def add_member_to_key_ring_policy(
        project_id, location_id, key_ring_id, member, role):
    """Adds a member with a given role to the Identity and Access Management
    (IAM) policy for a given KeyRing."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the KeyRing.
    resource = client.key_ring_path(project_id, location_id, key_ring_id)

    # Get the current IAM policy.
    policy = client.get_iam_policy(resource)

    # Add member
    policy.bindings.add(
        role=role,
        members=[member])

    # Update the IAM Policy.
    client.set_iam_policy(resource, policy)

    # Print results
    print('Member {} added with role {} to policy in KeyRing {}'
          .format(member, role, key_ring_id))

# [END kms_add_member_to_keyring_policy]

# [START kms_remove_member_from_cryptokey_policy]
def remove_member_from_crypto_key_policy(
        project_id, location_id, key_ring_id, crypto_key_id, member, role):
    """Removes a member with a given role from the Identity and Access
    Management (IAM) policy for a given CryptoKey associated with a KeyRing."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the CryptoKey.
    resource = client.crypto_key_path_path(project_id, location_id,
                                           key_ring_id, crypto_key_id)
    # Get the current IAM policy.
    policy = client.get_iam_policy(resource)

    # Remove member
    for b in list(policy.bindings):
        if b.role == role and member in b.members:
            b.members.remove(member)

    # Update the IAM Policy.
    client.set_iam_policy(resource, policy)

    # Print results
    print('Member {} removed from role {} for CryptoKey in KeyRing {}'
          .format(member, role, crypto_key_id, key_ring_id))
# [END kms_remove_member_from_cryptokey_policy]

def remove_member_from_key_ring_policy(project_id, location_id, key_ring_id,
                                       member, role):
    """Removes a member with a given role from the Identity and Access
    Management (IAM) policy for a given KeyRing."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the KeyRing.
    resource = client.key_ring_path(project_id, location_id, key_ring_id)

    # Get the current IAM policy.
    policy = client.get_iam_policy(resource)

    # Remove member
    for b in list(policy.bindings):
        if b.role == role and member in b.members:
            b.members.remove(member)

    # Update the IAM Policy.
    client.set_iam_policy(resource, policy)

    # Print results
    print('Member {} removed from role {} for KeyRing {}'
          .format(member, role, key_ring_id))

# [START kms_get_keyring_policy]
def get_key_ring_policy(project_id, location_id, key_ring_id):
    """Gets the Identity and Access Management (IAM) policy for a given KeyRing
    and prints out roles and the members assigned to those roles."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the KeyRing.
    resource = client.key_ring_path(project_id, location_id, key_ring_id)

    # Get the current IAM policy.
    policy = client.get_iam_policy(resource)

    # Print results
    print('Printing IAM policy for resource {}:'.format(resource))
    for b in policy.bindings:
        for m in b.members:
            print('Role: {} Member: {}'.format(b.role, m))
    return policy
# [END kms_get_keyring_policy]

def get_crypto_key_policy(project_id, location_id, key_ring_id, crypto_key_id):
    """Gets the Identity and Access Management (IAM) policy for a given KeyRing
    and prints out roles and the members assigned to those roles."""

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the CryptoKey.
    resource = client.crypto_key_path_path(project_id, location_id,
                                           key_ring_id, crypto_key_id)

    # Get the current IAM policy.
    policy = client.get_iam_policy(resource)

    # Print results
    print('Printing IAM policy for resource {}:'.format(resource))
    for b in policy.bindings:
        for m in b.members:
            print('Role: {} Member: {}'.format(b.role, m))
    return policy
<commit_msg>Move import statements into tagged regions [(#2219)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/2219)<commit_after>#!/usr/bin/env python

# Copyright 2017 Google, Inc
#
# 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

# [START kms_create_keyring]
def create_key_ring(project_id, location_id, key_ring_id):
    """Creates a KeyRing in the given location (e.g. global)."""

    from google.cloud import kms_v1

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the location associated with the KeyRing.
    parent = client.location_path(project_id, location_id)

    # The keyring object template
    keyring_name = client.key_ring_path(project_id, location_id, key_ring_id)
    keyring = {'name': keyring_name}

    # Create a KeyRing
    response = client.create_key_ring(parent, key_ring_id, keyring)

    print('Created KeyRing {}.'.format(response.name))
    return response
# [END kms_create_keyring]

# [START kms_create_cryptokey]
def create_crypto_key(project_id, location_id, key_ring_id, crypto_key_id):
    """Creates a CryptoKey within a KeyRing in the given location."""

    from google.cloud import kms_v1
    from google.cloud.kms_v1 import enums

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the KeyRing associated with the CryptoKey.
    parent = client.key_ring_path(project_id, location_id, key_ring_id)

    # Create the CryptoKey object template
    purpose = enums.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT
    crypto_key = {'purpose': purpose}

    # Create a CryptoKey for the given KeyRing.
    response = client.create_crypto_key(parent, crypto_key_id, crypto_key)

    print('Created CryptoKey {}.'.format(response.name))
    return response
# [END kms_create_cryptokey]

# [START kms_encrypt]
def encrypt_symmetric(project_id, location_id, key_ring_id, crypto_key_id,
                      plaintext):
    """Encrypts input plaintext data using the provided symmetric CryptoKey."""

    from google.cloud import kms_v1

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the CryptoKey.
    name = client.crypto_key_path_path(project_id, location_id, key_ring_id,
                                       crypto_key_id)

    # Use the KMS API to encrypt the data.
    response = client.encrypt(name, plaintext)
    return response.ciphertext
# [END kms_encrypt]

# [START kms_decrypt]
def decrypt_symmetric(project_id, location_id, key_ring_id, crypto_key_id,
                      ciphertext):
    """Decrypts input ciphertext using the provided symmetric CryptoKey."""

    from google.cloud import kms_v1

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the CryptoKey.
    name = client.crypto_key_path_path(project_id, location_id, key_ring_id,
                                       crypto_key_id)
    # Use the KMS API to decrypt the data.
    response = client.decrypt(name, ciphertext)
    return response.plaintext
# [END kms_decrypt]

# [START kms_disable_cryptokey_version]
def disable_crypto_key_version(project_id, location_id, key_ring_id,
                               crypto_key_id, version_id):
    """Disables a CryptoKeyVersion associated with a given CryptoKey and
    KeyRing."""

    from google.cloud import kms_v1
    from google.cloud.kms_v1 import enums

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # Construct the resource name of the CryptoKeyVersion.
    name = client.crypto_key_version_path(project_id, location_id, key_ring_id,
                                          crypto_key_id, version_id)

    # Use the KMS API to disable the CryptoKeyVersion.
    new_state = enums.CryptoKeyVersion.CryptoKeyVersionState.DISABLED
    version = {'name': name, 'state': new_state}
    update_mask = {'paths': ["state"]}

    # Print results
    response = client.update_crypto_key_version(version, update_mask)
    print('CryptoKeyVersion {}\'s state has been set to {}.'.format(
        name, response.state))
# [END kms_disable_cryptokey_version]

# [START kms_enable_cryptokey_version]
def enable_crypto_key_version(project_id, location_id, key_ring_id,
                              crypto_key_id, version_id):
    """Enables a CryptoKeyVersion associated with a given CryptoKey and
    KeyRing."""

    from google.cloud import kms_v1
    from google.cloud.kms_v1 import enums

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # Construct the resource name of the CryptoKeyVersion.
    name = client.crypto_key_version_path(project_id, location_id, key_ring_id,
                                          crypto_key_id, version_id)

    # Use the KMS API to enable the CryptoKeyVersion.
    new_state = enums.CryptoKeyVersion.CryptoKeyVersionState.ENABLED
    version = {'name': name, 'state': new_state}
    update_mask = {'paths': ["state"]}

    # Print results
    response = client.update_crypto_key_version(version, update_mask)
    print('CryptoKeyVersion {}\'s state has been set to {}.'.format(
        name, response.state))
# [END kms_enable_cryptokey_version]

# [START kms_destroy_cryptokey_version]
def destroy_crypto_key_version(
        project_id, location_id, key_ring_id, crypto_key_id, version_id):
    """Schedules a CryptoKeyVersion associated with a given CryptoKey and
    KeyRing for destruction 24 hours in the future."""

    from google.cloud import kms_v1

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # Construct the resource name of the CryptoKeyVersion.
    name = client.crypto_key_version_path(project_id, location_id, key_ring_id,
                                          crypto_key_id, version_id)

    # Use the KMS API to mark the CryptoKeyVersion for destruction.
    response = client.destroy_crypto_key_version(name)

    # Print results
    print('CryptoKeyVersion {}\'s state has been set to {}.'.format(
        name, response.state))
# [END kms_destroy_cryptokey_version]

# [START kms_restore_cryptokey_version]
def restore_crypto_key_version(
        project_id, location_id, key_ring_id, crypto_key_id, version_id):
    """Restores a CryptoKeyVersion that is scheduled for destruction."""

    from google.cloud import kms_v1

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # Construct the resource name of the CryptoKeyVersion.
    name = client.crypto_key_version_path(project_id, location_id, key_ring_id,
                                          crypto_key_id, version_id)

    # Use the KMS API to restore the CryptoKeyVersion.
    response = client.restore_crypto_key_version(name)

    # Print results
    print('CryptoKeyVersion {}\'s state has been set to {}.'.format(
        name, response.state))

# [END kms_restore_cryptokey_version]

# [START kms_add_member_to_cryptokey_policy]
def add_member_to_crypto_key_policy(
        project_id, location_id, key_ring_id, crypto_key_id, member, role):
    """Adds a member with a given role to the Identity and Access Management
    (IAM) policy for a given CryptoKey associated with a KeyRing."""

    from google.cloud import kms_v1

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the CryptoKey.
    resource = client.crypto_key_path_path(project_id, location_id,
                                           key_ring_id, crypto_key_id)
    # Get the current IAM policy.
    policy = client.get_iam_policy(resource)

    # Add member
    policy.bindings.add(
        role=role,
        members=[member])

    # Update the IAM Policy.
    client.set_iam_policy(resource, policy)

    # Print results
    print('Member {} added with role {} to policy for CryptoKey {} \
           in KeyRing {}'.format(member, role, crypto_key_id, key_ring_id))
# [END kms_add_member_to_cryptokey_policy]

# [START kms_add_member_to_keyring_policy]
def add_member_to_key_ring_policy(
        project_id, location_id, key_ring_id, member, role):
    """Adds a member with a given role to the Identity and Access Management
    (IAM) policy for a given KeyRing."""

    from google.cloud import kms_v1

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the KeyRing.
    resource = client.key_ring_path(project_id, location_id, key_ring_id)

    # Get the current IAM policy.
    policy = client.get_iam_policy(resource)

    # Add member
    policy.bindings.add(
        role=role,
        members=[member])

    # Update the IAM Policy.
    client.set_iam_policy(resource, policy)

    # Print results
    print('Member {} added with role {} to policy in KeyRing {}'
          .format(member, role, key_ring_id))

# [END kms_add_member_to_keyring_policy]

# [START kms_remove_member_from_cryptokey_policy]
def remove_member_from_crypto_key_policy(
        project_id, location_id, key_ring_id, crypto_key_id, member, role):
    """Removes a member with a given role from the Identity and Access
    Management (IAM) policy for a given CryptoKey associated with a KeyRing."""

    from google.cloud import kms_v1

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the CryptoKey.
    resource = client.crypto_key_path_path(project_id, location_id,
                                           key_ring_id, crypto_key_id)
    # Get the current IAM policy.
    policy = client.get_iam_policy(resource)

    # Remove member
    for b in list(policy.bindings):
        if b.role == role and member in b.members:
            b.members.remove(member)

    # Update the IAM Policy.
    client.set_iam_policy(resource, policy)

    # Print results
    print('Member {} removed from role {} for CryptoKey in KeyRing {}'
          .format(member, role, crypto_key_id, key_ring_id))
# [END kms_remove_member_from_cryptokey_policy]

def remove_member_from_key_ring_policy(project_id, location_id, key_ring_id,
                                       member, role):
    """Removes a member with a given role from the Identity and Access
    Management (IAM) policy for a given KeyRing."""

    from google.cloud import kms_v1

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the KeyRing.
    resource = client.key_ring_path(project_id, location_id, key_ring_id)

    # Get the current IAM policy.
    policy = client.get_iam_policy(resource)

    # Remove member
    for b in list(policy.bindings):
        if b.role == role and member in b.members:
            b.members.remove(member)

    # Update the IAM Policy.
    client.set_iam_policy(resource, policy)

    # Print results
    print('Member {} removed from role {} for KeyRing {}'
          .format(member, role, key_ring_id))

# [START kms_get_keyring_policy]
def get_key_ring_policy(project_id, location_id, key_ring_id):
    """Gets the Identity and Access Management (IAM) policy for a given KeyRing
    and prints out roles and the members assigned to those roles."""

    from google.cloud import kms_v1

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the KeyRing.
    resource = client.key_ring_path(project_id, location_id, key_ring_id)

    # Get the current IAM policy.
    policy = client.get_iam_policy(resource)

    # Print results
    print('Printing IAM policy for resource {}:'.format(resource))
    for b in policy.bindings:
        for m in b.members:
            print('Role: {} Member: {}'.format(b.role, m))
    return policy
# [END kms_get_keyring_policy]

def get_crypto_key_policy(project_id, location_id, key_ring_id, crypto_key_id):
    """Gets the Identity and Access Management (IAM) policy for a given KeyRing
    and prints out roles and the members assigned to those roles."""

    from google.cloud import kms_v1

    # Creates an API client for the KMS API.
    client = kms_v1.KeyManagementServiceClient()

    # The resource name of the CryptoKey.
    resource = client.crypto_key_path_path(project_id, location_id,
                                           key_ring_id, crypto_key_id)

    # Get the current IAM policy.
    policy = client.get_iam_policy(resource)

    # Print results
    print('Printing IAM policy for resource {}:'.format(resource))
    for b in policy.bindings:
        for m in b.members:
            print('Role: {} Member: {}'.format(b.role, m))
    return policy
<|endoftext|>