Skip to content

Commit

Permalink
Skript, das Gruppen und Untergruppen anlegt.
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Marienfeld committed Nov 21, 2012
1 parent e296425 commit 4b1fa5b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/group-creator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/python
# -*- coding: utf8 -*-
import traceback,pprint,sys
import ckanclient #developed with ckanclient 0.10
reload(sys)
sys.setdefaultencoding('utf-8')

import ckanclient
import json
import argparse
import groupmap

pp=pprint.PrettyPrinter(indent=4)
parser = argparse.ArgumentParser(description='Create group/subgroup structur.')
parser.add_argument('--ckanurl', help='ckan url', required=True)
parser.add_argument('--apikey', help='ckan api key, eg localhost:5000/api')
parser.add_argument('--domain', help='ID for a group of domains like "iso" or "berlin".', required=True)
args = parser.parse_args()


# Instantiate the CKAN client.
ckan = ckanclient.CkanClient(base_location=args.ckanurl, api_key=args.apikey)
pp=pprint.PrettyPrinter(indent=4)

filename = "../kategorien/" + args.domain + ".json"
groups = json.loads( open(filename, 'r').read())
transl = groupmap.Translator('bremen')

for gid in groups.iterkeys():
group_entity = {
'name': gid,
'title': groups[gid]
}
if not 'deutschland' == args.domain:
group_entity['type'] = 'subgroup'

try:
ckan.group_register_post(group_entity)
except:
traceback.print_exc()

if not 'deutschland' == args.domain:
for supergroupname in transl.translate(gid):
supergroup = ckan.group_entity_get(supergroupname)
supergroup['groups'] = [{'name':gid}] + supergroup['groups']
try:
ckan.group_register_post(supergroup)
except:
traceback.print_exc()
7 changes: 7 additions & 0 deletions lib/groupmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def __init__(self, sourceportal):
self.map = json.loads( open(filename, 'r').read())

def translate(self, categories=[]):
if not isinstance(categories, list):
categories = [categories]
out = []
if not categories:
return out
Expand All @@ -29,6 +31,11 @@ def test_b2d(self):
print translated
self.assertEqual(translated, ['transport_verkehr','politik_wahlen'])

def test_atom(self):
translated = self.u.translate('verkehr')
print translated
self.assertEqual(translated, ['transport_verkehr'])

def test_missing(self):
translated = self.u.translate(['verkehr','xyz','wahl'])
print translated
Expand Down

0 comments on commit 4b1fa5b

Please sign in to comment.