當前位置: 首頁>>代碼示例>>Python>>正文


Python Status.record方法代碼示例

本文整理匯總了Python中status.Status.record方法的典型用法代碼示例。如果您正苦於以下問題:Python Status.record方法的具體用法?Python Status.record怎麽用?Python Status.record使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在status.Status的用法示例。


在下文中一共展示了Status.record方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: sort_out_tags

# 需要導入模塊: from status import Status [as 別名]
# 或者: from status.Status import record [as 別名]
def sort_out_tags(source_ckan_uri,
                  dest_ckan_uri, dest_api_key,
                  ):
    ckan1 = ckanclient.CkanClient(base_location=source_ckan_uri)
    ckan2 = ckanclient.CkanClient(base_location=dest_ckan_uri,
                                  api_key=dest_api_key)

    # ensure group exists
    group = 'country-ca'
    assert group in set(ckan2.group_register_get())
    group_to_change = 'canadagov'

    # work out tag mappings
    tag_status = Status('tag mapping')
    tag_replace_map = {}
    source_tags = ckan1.tag_register_get()
    for tag in source_tags:
        mangled_tag = re.sub('[-._]', '', tag)
        replacement_tag = tag
        # Change underscores to hyphens
        replacement_tag = replacement_tag.replace('_', '-')
        # Remove trailing punctuation
        if replacement_tag[-1] in '_-.':
            replacement_tag = replacement_tag[:-1]
        if replacement_tag[0] in '_-.':
            replacement_tag = replacement_tag[1:]
        if mangled_tag == replacement_tag:
            tag_status.record('Unchanged', mangled_tag, do_print=False)
            continue
        if mangled_tag in tag_replace_map and tag_replace_map[mangled_tag] != replacement_tag:
            print 'Warning - can\'t differentiate %s : %s / %s' % \
                  (mangled_tag, tag_replace_map[mangled_tag], replacement_tag)
        tag_status.record('Mapping added', '%s:%s' % (mangled_tag, replacement_tag), do_print=False)
        tag_replace_map[mangled_tag] = replacement_tag
    example_map = tag_replace_map.items()[0]
    print tag_status

    # Custom mappings
    tag_replace_map['metaimportedfromcackannet'] = 'meta.imported-from-ca-ckan-net'

    # edit packages
    pkg_status = Status('Packages')
    pkgs = ckan2.group_entity_get(group)['packages']
    print 'Packages in the group: %i' % len(pkgs)
    for pkg_name in pkgs:
        pkg = ckan2.package_entity_get(pkg_name)
        original_pkg = copy.deepcopy(pkg)

        # Change tags
        edited_tags = [tag_replace_map.get(tag, tag) for tag in pkg['tags']]
        if 'canada' in edited_tags:
            edited_tags.remove('canada')

        if group_to_change in pkg['groups']:
            pkg['groups'].remove(group_to_change)
            edited_tags.append('canada-gov')

        if set(pkg['tags']) != set(edited_tags):
            pkg['tags'] = edited_tags
            print '%s: %r -> %r' % (pkg_name, sorted(original_pkg['tags']), sorted(edited_tags))

        if pkg == original_pkg:
            pkg_status.record('Unchanged', pkg_name)
            continue

        try:
            ckan2.package_entity_put(pkg)
        except ckanclient.CkanApiError, e:
            pkg_status.record('Error: %r' % e.args, pkg_name)
            continue
        
        pkg_status.record('Successfully changed', pkg_name)
開發者ID:1sha1,項目名稱:ckan,代碼行數:74,代碼來源:canada.py


注:本文中的status.Status.record方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。