def _set_taxonomy_from_category(dataset_metadata, elem):

    taxonomy_tags = ckanapiutils.get_all_tags_from_tag_vocabulary(
        {'vocabulary_id': config['taxonomy_tag_vocab']})

    dataset_metadata['taxonomy'] = []
    for category in elem.findall('category'):
        category_name = script_utils._prepare_string_for_ckan_tag_name(
            category.text)
        category_name = script_utils._str_capitalize_underscore_replaced_with_space(
            category_name)
        if category_name in taxonomies:
            mapped = taxonomies[category_name]
            for tag in mapped:
                tag_name = script_utils._prepare_string_for_ckan_tag_name(tag)
                tag_name = script_utils._str_capitalize_underscore_replaced_with_space(
                    tag_name)
        else:
            tag_name = script_utils._prepare_string_for_ckan_tag_name(
                category_name)
            tag_name = script_utils._str_capitalize_underscore_replaced_with_space(
                category_name)

        if tag_name not in dataset_metadata['taxonomy']:
            if tag_name in taxonomy_tags:
                dataset_metadata['taxonomy'].append(tag_name)

    return dataset_metadata
def _map_geoserver_feature_to_ckan_dataset(feature_namespace,feature_name,feature_title,taxonomy_tags,config):

  # First, extract the information from the layer (Title, Abstract, Tags)
  params_dict = {}

  # Specify the package_type
  params_dict['type'] = config.package_type

  params_dict['maintainer'] = config.IMPORTER_NAME
  params_dict['maintainer_email'] = config.IMPORTER_EMAIL

  # The dataset id will be set when we find or create it
  params_dict['state'] = 'active'

  # Extract title (Mandatory)
  params_dict['title'] = feature_title

  # Extract name (Mandatory, lowcase and without characters except _-')
  params_dict['name'] = script_utils._prepare_string_for_ckan_name(feature_name)

  # Notes / Description / Abstract
  params_dict['notes'] = 'Imported Geoserver Layer: '+params_dict['title'] + '.'

  params_dict['taxonomy'] = []
  category_name = script_utils._prepare_string_for_ckan_tag_name(feature_namespace)
  category_name =  script_utils._str_capitalize_underscore_replaced_with_space(category_name)
  if (category_name in taxonomy_tags):
    params_dict['taxonomy'].append(category_name)

  return params_dict
def _set_taxonomy_from_category(dataset_metadata,elem):

  taxonomy_tags = ckanapiutils.get_all_tags_from_tag_vocabulary({'vocabulary_id': config['taxonomy_tag_vocab']})

  dataset_metadata['taxonomy'] = []
  for category in elem.findall('category'):
    category_name = script_utils._prepare_string_for_ckan_tag_name(category.text)
    category_name = script_utils._str_capitalize_underscore_replaced_with_space(category_name)
    if category_name in taxonomies:
      mapped = taxonomies[category_name]
      for tag in mapped:
        tag_name = script_utils._prepare_string_for_ckan_tag_name(tag)
        tag_name = script_utils._str_capitalize_underscore_replaced_with_space(tag_name)
    else:
      tag_name = script_utils._prepare_string_for_ckan_tag_name(category_name)
      tag_name = script_utils._str_capitalize_underscore_replaced_with_space(category_name)

    if tag_name not in dataset_metadata['taxonomy']:
      if tag_name in taxonomy_tags:
        dataset_metadata['taxonomy'].append(tag_name)

  return dataset_metadata
def _add_taxonomy_tag(taxonomy_tag_vocabulary,tag_name):

  tag_name = script_utils._prepare_string_for_ckan_tag_name(tag_name)

  tag = {'name': tag_name}

  # Avoid duplicates
  tag_exists = False
  for existing_tag in taxonomy_tag_vocabulary['tags']:
    if ( existing_tag['name'] == tag_name):
      tag_exists = True
      break;

  if (tag_exists == False):
    taxonomy_tag_vocabulary['tags'].append(dict(tag))
def _map_geoserver_feature_to_ckan_dataset(feature_namespace, feature_name,
                                           feature_title, taxonomy_tags,
                                           config):

    # First, extract the information from the layer (Title, Abstract, Tags)
    params_dict = {}

    # Specify the package_type
    params_dict['type'] = config.package_type

    params_dict['maintainer'] = config.IMPORTER_NAME
    params_dict['maintainer_email'] = config.IMPORTER_EMAIL

    # The dataset id will be set when we find or create it
    params_dict['state'] = 'active'

    # Extract title (Mandatory)
    params_dict['title'] = feature_title

    # Extract name (Mandatory, lowcase and without characters except _-')
    params_dict['name'] = script_utils._prepare_string_for_ckan_name(
        feature_name)

    # Notes / Description / Abstract
    params_dict[
        'notes'] = 'Imported Geoserver Layer: ' + params_dict['title'] + '.'

    params_dict['taxonomy'] = []
    category_name = script_utils._prepare_string_for_ckan_tag_name(
        feature_namespace)
    category_name = script_utils._str_capitalize_underscore_replaced_with_space(
        category_name)
    if (config.DEBUG):
        print(category_name)
    if (category_name in taxonomy_tags):
        params_dict['taxonomy'].append(category_name)

    return params_dict
  def run(self):

    print("S3_import_taxonomy_term_translations run")

    added_term_translations = []

    # Obtain JSON File from Github containing the different translations
    locales = config['supported_langs']
    term_lists = {}
    terms_to_import = []

    # Generate term lists for each one of the supported locales
    for locale in locales:

      # init list
      term_lists[locale] = []

      try:

        # Obtain the translation_dict from github
        translation_dict = taxonomyutils.get_taxonomy_for_locale(locale)

        # Call utility function
        script_utils._inspect_json_dict_fill_list(translation_dict,term_lists[locale])

      except (urllib2.HTTPError) as e:

        if config.DEBUG:
          print("File for locale " + locale +" not found. Check your config and make sure that the file is available on the odm-localization repository.")

    # Now loop through the term_lists
    for locale_origin in locales:

      # Set counter
      term_position = 0
      other_locales = list(locales)
      other_locales.remove(locale_origin)

      # Now loop through the terms of the particular locale
      for term in term_lists[locale_origin]:

        # For each term, we add a term translation of each of the other languages
        for locale_destination in other_locales:

          try:

            orig_term = term_lists[locale_origin][term_position]
            dest_term = term_lists[locale_destination][term_position]

            if orig_term != dest_term:

              # Add term translation locale_origin -> locale_destination
              params1 = {'term':script_utils._prepare_string_for_ckan_tag_name(orig_term),'term_translation':dest_term,'lang_code':locale_destination}
              terms_to_import.append(dict(params1))

              print('Translating ' + params1['term'].encode("utf-8") + ' ('+ locale_origin + ') as ' + params1['term_translation'].encode("utf-8") + ' (' +  locale_destination + ')')

              # Add term translation locale_origin -> locale_destination
              params2 = {'term':script_utils._prepare_string_for_ckan_tag_name(dest_term),'term_translation':orig_term,'lang_code':locale_origin}
              terms_to_import.append(dict(params2))

              print('Translating ' + params2['term'].encode("utf-8") + ' ('+ locale_destination + ') as ' + params2['term_translation'].encode("utf-8") + ' (' +  locale_origin + ')')

          except IndexError:

            break

        term_position = term_position + 1

    count = 0
    if len(terms_to_import) > 0:
      result = ckanapiutils.add_term_translation_many(dict({'data':terms_to_import}))
      count = result["success"]

    return "COMPLETED import_taxonomy_term_translations " + str(count) + " terms imported successfully."