# Path: services/orcid/pyorcid/constants.py
# ORCID_PUBLIC_BASE_URL = 'http://pub.orcid.org/'
# 
# Path: services/orcid/pyorcid/utils.py
# def dictmapper(typename, mapping):
#     """
#     A factory to create `namedtuple`-like classes from a field-to-dict-path
#     mapping::
# 
#         Person = dictmapper({'person':('person','name')})
#         example_dict = {'person':{'name':'John'}}
#         john = Person(example_dict)
#         assert john.name == 'John'
# 
#     If a function is specified as a mapping value instead of a dict "path", it
#     will be run with the backing dict as its first argument.
#     """
#     def init(self, d, *args, **kwargs):
#         """
#         Initialize `dictmapper` classes with a dict to back getters.
#         """
#         self._original_dict = d
# 
#     def getter_from_dict_path(path):
#         if not callable(path) and len(path) < 1:
#             raise ValueError('Dict paths should be iterables with at least one'
#                              ' key or callable objects that take one argument.')
#         def getter(self):
#             cur_dict = self._original_dict
#             if callable(path):
#                 return path(cur_dict)
#             return dict_value_from_path(cur_dict, path)
#         return getter
# 
#     prop_mapping = dict((k, property(getter_from_dict_path(v))) 
#                         for k, v in mapping.iteritems())
#     prop_mapping['__init__'] = init
#     return type(typename, tuple(), prop_mapping)
# 
# class MappingRule(object):
#     def __init__(self, path, further_func = lambda x : x):
#         self.path = path
#         self.further_func = further_func
# 
#     def __call__(self, d):
#         return self.further_func(dict_value_from_path(d, self.path))
# 
# Path: services/orcid/pyorcid/exceptions.py
# class NotFoundException(ORCIDException):
#     pass


import requests
import json
import logging
from .constants import ORCID_PUBLIC_BASE_URL
from .utils import dictmapper, MappingRule as to
from .exceptions import NotFoundException
    'url':['url','value'],
    'citation': to(['work-citation'], lambda d: Citation(d) if d is not None else None),
    'external_ids':to(['work-external-identifiers','work-external-identifier'],
                      lambda l: map(ExternalID, l) if l is not None else None),
    'publicationyear': ['publication-date', 'year', 'value'],
})

class Publication(PublicationBase):
    def __repr__(self):
        return '<%s "%s">' % (type(self).__name__, self.title)

WORKS_PATH = ['orcid-profile', 'orcid-activities','orcid-works',]

def _parse_publications(l):
    if l is not None:
        #logger.debug(json.dumps(l, sort_keys=True, indent=4, separators=(',', ': ')))
        return [Publication(d) for d in l]
    return []

Works = dictmapper('Works', {
    'publications':to(WORKS_PATH + ['orcid-work'], _parse_publications),
})

AuthorBase = dictmapper('AuthorBase', {
    'orcid':['orcid-profile','orcid-identifier','path'],
    'family_name':PERSONAL_DETAILS_PATH + ['family-name','value'],
    'given_name':PERSONAL_DETAILS_PATH + ['given-names','value'],
    'biography':BIO_PATH + ['biography',],
    'keywords':to(BIO_PATH + ['keywords'], _parse_keywords),
    #'keywords':to(BIO_PATH + ['keywords', 'keyword'], _parse_keywords),

# Path: services/orcid/pyorcid/constants.py
# ORCID_PUBLIC_BASE_URL = 'http://pub.orcid.org/'
# 
# Path: services/orcid/pyorcid/utils.py
# def dictmapper(typename, mapping):
#     """
#     A factory to create `namedtuple`-like classes from a field-to-dict-path
#     mapping::
# 
#         Person = dictmapper({'person':('person','name')})
#         example_dict = {'person':{'name':'John'}}
#         john = Person(example_dict)
#         assert john.name == 'John'
# 
#     If a function is specified as a mapping value instead of a dict "path", it
#     will be run with the backing dict as its first argument.
#     """
#     def init(self, d, *args, **kwargs):
#         """
#         Initialize `dictmapper` classes with a dict to back getters.
#         """
#         self._original_dict = d
# 
#     def getter_from_dict_path(path):
#         if not callable(path) and len(path) < 1:
#             raise ValueError('Dict paths should be iterables with at least one'
#                              ' key or callable objects that take one argument.')
#         def getter(self):
#             cur_dict = self._original_dict
#             if callable(path):
#                 return path(cur_dict)
#             return dict_value_from_path(cur_dict, path)
#         return getter
# 
#     prop_mapping = dict((k, property(getter_from_dict_path(v))) 
#                         for k, v in mapping.iteritems())
#     prop_mapping['__init__'] = init
#     return type(typename, tuple(), prop_mapping)
# 
# class MappingRule(object):
#     def __init__(self, path, further_func = lambda x : x):
#         self.path = path
#         self.further_func = further_func
# 
#     def __call__(self, d):
#         return self.further_func(dict_value_from_path(d, self.path))
# 
# Path: services/orcid/pyorcid/exceptions.py
# class NotFoundException(ORCIDException):
#     pass


# Path: services/orcid/pyorcid/rest.py
import requests
import json
import logging
from .constants import ORCID_PUBLIC_BASE_URL
from .utils import dictmapper, MappingRule as to
from .exceptions import NotFoundException

    'url':['url','value'],
    'citation': to(['work-citation'], lambda d: Citation(d) if d is not None else None),
    'external_ids':to(['work-external-identifiers','work-external-identifier'],
                      lambda l: map(ExternalID, l) if l is not None else None),
    'publicationyear': ['publication-date', 'year', 'value'],
})

class Publication(PublicationBase):
    def __repr__(self):
        return '<%s "%s">' % (type(self).__name__, self.title)

WORKS_PATH = ['orcid-profile', 'orcid-activities','orcid-works',]

def _parse_publications(l):
    if l is not None:
        #logger.debug(json.dumps(l, sort_keys=True, indent=4, separators=(',', ': ')))
        return [Publication(d) for d in l]
    return []

Works = dictmapper('Works', {
    'publications':to(WORKS_PATH + ['orcid-work'], _parse_publications),
})

AuthorBase = dictmapper('AuthorBase', {
    'orcid':['orcid-profile','orcid-identifier','path'],
    'family_name':PERSONAL_DETAILS_PATH + ['family-name','value'],
    'given_name':PERSONAL_DETAILS_PATH + ['given-names','value'],
    'biography':BIO_PATH + ['biography',],
    'keywords':to(BIO_PATH + ['keywords'], _parse_keywords),
    #'keywords':to(BIO_PATH + ['keywords', 'keyword'], _parse_keywords),

    'researcher_urls':to(BIO_PATH + ['researcher-urls','researcher-url'],