def layer_creator(ctx, asset, configfile):
  @retries(10)
  def create_layer(ctx, config):
    return ctx.service().layers().create(body=config, process=True).execute()

  @retries(10)
  def publish_layer(ctx, layer_id):
    return ctx.service().layers().publish(id=layer_id).execute()

  with open(configfile, "r") as f:
    config = json.load(f)

  for layer in config["layers"]:
    layer["projectId"] = asset["projectId"]
    layer["datasources"] = [{"id": asset["id"]}]

    # Patch layers.json config with the optional styleFile and infoWindowFile parameters
    if "styleFile" in layer:
      styleFile = os.path.join(os.path.dirname(configfile.name), layer["styleFile"])
      if os.path.exists(styleFile):
        with open(styleFile) as f:
          layer["style"] = json.load(f)
        del layer["styleFile"]

    if "infoWindowFile" in layer:
      if "style" in layer:
        infoWindowFile = os.path.join(os.path.dirname(configfile.name), layer["infoWindowFile"])
        if os.path.exists(infoWindowFile):
          with open(infoWindowFile) as f:
            if "featureInfo" not in layer["style"]:
              layer["style"]["featureInfo"] = {}
            layer["style"]["featureInfo"]["content"] = f.read()
      del layer["infoWindowFile"]

    # Create layer
    layer = create_layer(ctx, layer)
    ctx.log("Layer '%s' created with id %s" % (layer['name'], layer['id']))

    # Process and publish
    poll_asset_processing(ctx, layer['id'], ctx.service().layers())
    publish_layer(ctx, layer['id'])
    poll_layer_publishing(ctx, layer['id'])
Creates layers from an asset according to user-supplied config.
dataset/ETHPy150Open keithmoss/Hodor/hodor/commands/cmd_create.py/layer_creator