In [1]:
from marple.datatypes import Domain
from marple.schema import Datatype
from marple.csv import CsvFile
from marple.utils import isNaN
import csvkit as csv
import os
from copy import deepcopy
import pprint
pp = pprint.PrettyPrinter(indent=2)

In [2]:
# Get from settings, should point to root folder
DATATYPES_DIR = ".."
# Get from request
LANG = "en"
# Get from request
DOMAIN = "http://marple-datatypes.herokuapp.com"

# Points to root folder
RELATIONS_CSV_PATH = "../relations.csv"

# Use this object for getting labels etc
# Get item by id: ALL_DOMAINS.row("Stockholms kommun")
# Get label for item: ALL_DOMAINS.label("Stockholms kommun", lang="en")
# Get parent for item: ALL_DOMAINS.parent("Stockholms kommun")
# Get children for item: ALL_DOMAINS.children("Stockholms kommun")

ALL_DOMAINS = Domain("*/*", datatypes_dir=DATATYPES_DIR)

In [3]:
def jsonify_item(item_id, lang, domain):
    """ 
    """
    return {
        "id": item_id,
        "label": ALL_DOMAINS.label(item_id, lang=lang),
        "path": u"{}/item/{}".format(domain, item_id),
    }

/GET /datatype


In [4]:
data = []

file_path = os.path.join(DATATYPES_DIR, "datatypes.csv")
csv_file = CsvFile(file_path)
# TODO: Add path
pp.pprint(csv_file.to_dictlist()[:5])


[ { u'allowed_values': u'misc/age_groups',
    u'description': None,
    'id': u'age_group',
    u'value_type': u'str'},
  { u'allowed_values': u'misc/crime_codes',
    u'description': u'A crime code (brottskod) used by the Swedish police',
    'id': u'crime_code',
    u'value_type': u'str'},
  { u'allowed_values': u'misc/crime_types',
    u'description': u'A crime type (brottstyp) used by the Swedish police. Higher level than brottskod.',
    'id': u'crime_type',
    u'value_type': u'str'},
  { u'allowed_values': u'misc/foreignborn',
    u'description': u'Is the subject born outside Sweden?',
    'id': u'foreignborn',
    u'value_type': u'str'},
  { u'allowed_values': u'misc/genders',
    u'description': u'A subjects gender',
    'id': u'gender',
    u'value_type': u'str'}]

/GET /datatype/{datatype_id}


In [5]:
datatype_id = "gender"
datatype = Datatype(datatype_id, datatypes_dir=DATATYPES_DIR)
allowed_values = []

data = {
    "id": datatype_id,
    "allowed_values": []
}

# Get lang and domain from request!
for allowed_value_id in datatype.allowed_values:
    data["allowed_values"].append(jsonify_item(allowed_value_id, LANG, DOMAIN))
    
pp.pprint(data)


{ 'allowed_values': [ { 'id': u'male',
                        'label': u'Men',
                        'path': u'http://marple-datatypes.herokuapp.com/item/male'},
                      { 'id': u'female',
                        'label': u'Women',
                        'path': u'http://marple-datatypes.herokuapp.com/item/female'},
                      { 'id': u'other',
                        'label': u'Other',
                        'path': u'http://marple-datatypes.herokuapp.com/item/other'},
                      { 'id': u'unknown',
                        'label': u'Unknown',
                        'path': u'http://marple-datatypes.herokuapp.com/item/unknown'},
                      { 'id': u'all genders',
                        'label': u'Both men and women',
                        'path': u'http://marple-datatypes.herokuapp.com/item/all genders'}],
  'id': 'gender'}

/GET /item/{item_id}


In [7]:
def remove_nan(obj): 
    """Remove all NaN values
    """
    for key, value in obj.items():
        if isNaN(value):
            obj.pop(key, None)
    return obj

item_id = "Stockholms kommun"
# TODO: Use "**/*" to fetch all files in all subfolders once glob2 in place
data = ALL_DOMAINS.row(item_id)
# This is something of a hack. The Domain class will include a lot of empty columns/propeties defined in other files
# Here we clean up those
data = remove_nan(data)

# Translate label to selected language
data["label"] = ALL_DOMAINS.label(item_id,lang=LANG)

# Populate relational properties (parent, neighbours etc)
# These relations are defined in relations.csv in the root folder
relations_csv = CsvFile(RELATIONS_CSV_PATH)
# Ie {u'neighbours': u'one_to_many', u'parent': u'one_to_one'}
relational_columns = dict(relations_csv.data.to_records())

for column, relation_type in relational_columns.iteritems():
    if column not in data:
        continue
    if relation_type == "one_to_one":
        related_item_id = data[column] # ie the parent id
        data[column] = jsonify_item(related_item_id, LANG, DOMAIN)
        
    elif relation_type == "one_to_many":
        related_item_ids = data[column].split(",") #ie neighbours
        data[column] = []
        for related_item_id in related_item_ids:
            data[column].append(jsonify_item(related_item_id, LANG, DOMAIN))

data["children"] = []             
for child_id in ALL_DOMAINS.children(item_id):
    data["children"].append(jsonify_item(child_id, LANG, DOMAIN))

pp.pprint(data)


{ '_category': 'municipalities',
  '_file': '../regions/municipalities.csv',
  u'br\xe5_id': u'8510',
  'children': [ { 'id': u'Bromma stadsdelsomr\xe5de',
                  'label': u'Bromma stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Bromma stadsdelsomr\xe5de'},
                { 'id': u'Enskede-\xc5rsta-Vant\xf6rs stadsdelsomr\xe5de',
                  'label': u'Enskede-\xc5rsta-Vant\xf6rs stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Enskede-\xc5rsta-Vant\xf6rs stadsdelsomr\xe5de'},
                { 'id': u'Farsta stadsdelsomr\xe5de',
                  'label': u'Farsta stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Farsta stadsdelsomr\xe5de'},
                { 'id': u'H\xe4gersten-Liljeholmens stadsdelsomr\xe5de',
                  'label': u'H\xe4gersten-Liljeholmens stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/H\xe4gersten-Liljeholmens stadsdelsomr\xe5de'},
                { 'id': u'H\xe4sselby-V\xe4llingby stadsdelsomr\xe5de',
                  'label': u'H\xe4sselby-V\xe4llingby stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/H\xe4sselby-V\xe4llingby stadsdelsomr\xe5de'},
                { 'id': u'Kungsholmens stadsdelsomr\xe5de',
                  'label': u'Kungsholmens stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Kungsholmens stadsdelsomr\xe5de'},
                { 'id': u'Norrmalms stadsdelsomr\xe5de',
                  'label': u'Norrmalms stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Norrmalms stadsdelsomr\xe5de'},
                { 'id': u'Rinkeby-Kista stadsdelsomr\xe5de',
                  'label': u'Rinkeby-Kista stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Rinkeby-Kista stadsdelsomr\xe5de'},
                { 'id': u'Skarpn\xe4cks stadsdelsomr\xe5de',
                  'label': u'Skarpn\xe4cks stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Skarpn\xe4cks stadsdelsomr\xe5de'},
                { 'id': u'Sk\xe4rholmens stadsdelsomr\xe5de',
                  'label': u'Sk\xe4rholmens stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Sk\xe4rholmens stadsdelsomr\xe5de'},
                { 'id': u'Sp\xe5nga-Tensta stadsdelsomr\xe5de',
                  'label': u'Sp\xe5nga-Tensta stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Sp\xe5nga-Tensta stadsdelsomr\xe5de'},
                { 'id': u'S\xf6dermalms stadsdelsomr\xe5de',
                  'label': u'S\xf6dermalms stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/S\xf6dermalms stadsdelsomr\xe5de'},
                { 'id': u'\xc4lvsj\xf6 stadsdelsomr\xe5de',
                  'label': u'\xc4lvsj\xf6 stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/\xc4lvsj\xf6 stadsdelsomr\xe5de'},
                { 'id': u'\xd6stermalms stadsdelsomr\xe5de',
                  'label': u'\xd6stermalms stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/\xd6stermalms stadsdelsomr\xe5de'},
                { 'id': u'Enskede stadsdelsomr\xe5de',
                  'label': u'Enskede stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Enskede stadsdelsomr\xe5de'},
                { 'id': u'Hammarby stadsdelsomr\xe5de',
                  'label': u'Hammarby stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Hammarby stadsdelsomr\xe5de'},
                { 'id': u'Hornstulls stadsdelsomr\xe5de',
                  'label': u'Hornstulls stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Hornstulls stadsdelsomr\xe5de'},
                { 'id': u'H\xe4gerstens stadsdelsomr\xe5de',
                  'label': u'H\xe4gerstens stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/H\xe4gerstens stadsdelsomr\xe5de'},
                { 'id': u'H\xe4sselby stadsdelsomr\xe5de',
                  'label': u'H\xe4sselby stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/H\xe4sselby stadsdelsomr\xe5de'},
                { 'id': u'Katarina-Sofia stadsdelsomr\xe5de',
                  'label': u'Katarina-Sofia stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Katarina-Sofia stadsdelsomr\xe5de'},
                { 'id': u'Kista stadsdelsomr\xe5de',
                  'label': u'Kista stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Kista stadsdelsomr\xe5de'},
                { 'id': u'Liljeholmens stadsdelsomr\xe5de',
                  'label': u'Liljeholmens stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Liljeholmens stadsdelsomr\xe5de'},
                { 'id': u'Maria-Gamla stans stadsdelsomr\xe5de',
                  'label': u'Maria-Gamla stans stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Maria-Gamla stans stadsdelsomr\xe5de'},
                { 'id': u'Rinkeby stadsdelsomr\xe5de',
                  'label': u'Rinkeby stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Rinkeby stadsdelsomr\xe5de'},
                { 'id': u'S\xf6derleds stadsdelsomr\xe5de',
                  'label': u'S\xf6derleds stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/S\xf6derleds stadsdelsomr\xe5de'},
                { 'id': u'Vant\xf6rs stadsdelsomr\xe5de',
                  'label': u'Vant\xf6rs stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Vant\xf6rs stadsdelsomr\xe5de'},
                { 'id': u'V\xe4llingby stadsdelsomr\xe5de',
                  'label': u'V\xe4llingby stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/V\xe4llingby stadsdelsomr\xe5de'},
                { 'id': u'V\xe4sterleds stadsdelsomr\xe5de',
                  'label': u'V\xe4sterleds stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/V\xe4sterleds stadsdelsomr\xe5de'},
                { 'id': u'\xc5rsta stadsdelsomr\xe5de',
                  'label': u'\xc5rsta stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/\xc5rsta stadsdelsomr\xe5de'},
                { 'id': u'Enskede-\xc5rsta stadsdelsomr\xe5de',
                  'label': u'Enskede-\xc5rsta stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Enskede-\xc5rsta stadsdelsomr\xe5de'},
                { 'id': u'H\xe4sselby-V\xe4llingby stadsdelsomr\xe5de f\xf6re 2007',
                  'label': u'H\xe4sselby-V\xe4llingby stadsdelsomr\xe5de',
                  'path': u'http://marple-datatypes.herokuapp.com/item/H\xe4sselby-V\xe4llingby stadsdelsomr\xe5de f\xf6re 2007'},
                { 'id': u'Inre staden',
                  'label': u'Inre staden',
                  'path': u'http://marple-datatypes.herokuapp.com/item/Inre staden'},
                { 'id': u'S\xf6derort',
                  'label': u'S\xf6derort',
                  'path': u'http://marple-datatypes.herokuapp.com/item/S\xf6derort'},
                { 'id': u'V\xe4sterort',
                  'label': u'V\xe4sterort',
                  'path': u'http://marple-datatypes.herokuapp.com/item/V\xe4sterort'}],
  u'end': u'9',
  'label': u'Stockholms kommun',
  u'label_short': u'Stockholm',
  u'metro': u'Storstockholm',
  u'neighbours': [ { 'id': u'Sollentuna kommun',
                     'label': u'Sollentuna kommun',
                     'path': u'http://marple-datatypes.herokuapp.com/item/Sollentuna kommun'},
                   { 'id': u'J\xe4rf\xe4lla kommun',
                     'label': u'J\xe4rf\xe4lla kommun',
                     'path': u'http://marple-datatypes.herokuapp.com/item/J\xe4rf\xe4lla kommun'},
                   { 'id': u'Sundbybergs kommun',
                     'label': u'Sundbybergs kommun',
                     'path': u'http://marple-datatypes.herokuapp.com/item/Sundbybergs kommun'},
                   { 'id': u'Eker\xf6 kommun',
                     'label': u'Eker\xf6 kommun',
                     'path': u'http://marple-datatypes.herokuapp.com/item/Eker\xf6 kommun'},
                   { 'id': u'Liding\xf6 kommun',
                     'label': u'Liding\xf6 kommun',
                     'path': u'http://marple-datatypes.herokuapp.com/item/Liding\xf6 kommun'},
                   { 'id': u'Nacka kommun',
                     'label': u'Nacka kommun',
                     'path': u'http://marple-datatypes.herokuapp.com/item/Nacka kommun'},
                   { 'id': u'Tyres\xf6 kommun',
                     'label': u'Tyres\xf6 kommun',
                     'path': u'http://marple-datatypes.herokuapp.com/item/Tyres\xf6 kommun'},
                   { 'id': u'Huddinge kommun',
                     'label': u'Huddinge kommun',
                     'path': u'http://marple-datatypes.herokuapp.com/item/Huddinge kommun'},
                   { 'id': u'Danderyds kommun',
                     'label': u'Danderyds kommun',
                     'path': u'http://marple-datatypes.herokuapp.com/item/Danderyds kommun'},
                   { 'id': u'Solna kommun',
                     'label': u'Solna kommun',
                     'path': u'http://marple-datatypes.herokuapp.com/item/Solna kommun'}],
  u'parent': { 'id': u'Stockholms l\xe4n',
               'label': u'Stockholms l\xe4n',
               'path': u'http://marple-datatypes.herokuapp.com/item/Stockholms l\xe4n'},
  u'region_level': u'municipality',
  u'scb': u'180',
  u'start': u'0',
  u'wikidata': u'Q506250'}

In [ ]:


In [ ]: