# Path: commands/wmcommons/wmcommons.py
# COMMONS_ENDPOINT = "http://commons.wikimedia.org"
# PAGE_ENDPOINT = COMMONS_ENDPOINT + "/wiki/File:%s"
# API_ENDPOINT = COMMONS_ENDPOINT + "/w/api.php";
# RESIZE_ENDPOINT = COMMONS_ENDPOINT + "/wiki/Special:Redirect/file/%s"
# FILE_PREFIX = "File:"
# def parse_imageinfo(data):
# def request(params):
# def imagepage(filename):
# def imageresize(filename, width = None):
# def imageinfo(args):
# 
# Path: commands/wikidata/entity_ld.py
# class WikidataEntityLD:
#     def __init__(self, qid, languages = ""):
#         self.qid = qid
#         self.languages = languages.split(",")
#         self.rdf = self.get_rdf()
# 
#     def _entityvalues(self, item, idns):
#         tree = {
#             "uri" : item["@id"]
#         }
# 
#         for ns in (NS_NAME, NS_DESCRIPTION):
#             if ns.uri in item:
#                 tree[ns.name] = { i["@language"]:i["@value"] for i in item[ns.uri] }
# 
#                 # Do we only want a selection of languages?
#                 if self.languages:
#                     tree[ns.name] = { k:v for k,v in tree[ns.name].items() if k in self.languages}
# 
#         return tree
# 
#     def _get_by_namespace(self, tree, namespace):
#         items = Knead(tree).filter(lambda i:i["@id"].startswith(namespace.uri))
#         return items.data()
# 
#     def _statementvalues(self, statement, uri):
#         prop = [
#             { "property_uri": key, "values" : val }
#             for key, val in statement.items() if key.startswith(uri)
#         ]
# 
#         if len(prop) == 1:
#             return prop[0]
#         else:
#             return None
# 
#     def get_rdf(self):
#         r = requests.get(RDF_ENDPOINT % self.qid)
#         return r.text
# 
#     def as_json_ld(self):
#         graph = Graph().parse(data=self.rdf, format='xml')
#         jsonld = graph.serialize(format='json-ld', indent=4)
#         return json.loads(jsonld)
# 
#     def as_json_ld_simplified(self):
#         tree = self.as_json_ld()
# 
#         entities = self._get_by_namespace(tree, NS_ENTITY)
#         entities = { i["@id"]:self._entityvalues(i, NS_ENTITY.uri) for i in entities }
# 
#         statements = self._get_by_namespace(tree, NS_ENTITY_STATEMENT)
#         statements = [self._statementvalues(s, NS_PROP_STATEMENT.uri) for s in statements]
# 
#         for statement in statements:
#             if statement:
#                 uri = statement["property_uri"].replace("prop/statement", "entity")
#                 statement["property"] = entities.get(uri, None)
# 
#                 for value in statement["values"]:
#                     if "@id" in value:
#                         value["entity"] = entities.get(value["@id"], None)
# 
#                     if "@value" in value:
#                         value["value"] = value["@value"]
# 
# 
#         return {
#             "entities" : entities,
#             "statements" : statements
#         }


import util, json, time, math, requests
from commands.wmcommons import wmcommons
from .entity_ld import WikidataEntityLD
            qid = "Q" + str(value["numeric-id"])
            val["value"] = qid
            self.entitycache[qid] = False
        elif datatype == "monolingualtext":
            # Probably a bit hacky really
            val["value"] = value["text"]
        else:
            val["value"] = value

        return val

    def get_claimvaluestring(self, val):
        datatype = val.get("datatype", None)
        value = val.get("value", False)

        if not value or not datatype:
            return ""

        if datatype == "wikibase-item":
            return val.get("value_labels", "")

        if datatype in ["string", "monolingualtext", "url"]:
            return value

        if datatype == "time":
            # HACK: This is really, pretty ugly
            # See < https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar >
            return value["time"][1:].lstrip("0")

        if datatype == "commonsMedia":

# Path: commands/wmcommons/wmcommons.py
# COMMONS_ENDPOINT = "http://commons.wikimedia.org"
# PAGE_ENDPOINT = COMMONS_ENDPOINT + "/wiki/File:%s"
# API_ENDPOINT = COMMONS_ENDPOINT + "/w/api.php";
# RESIZE_ENDPOINT = COMMONS_ENDPOINT + "/wiki/Special:Redirect/file/%s"
# FILE_PREFIX = "File:"
# def parse_imageinfo(data):
# def request(params):
# def imagepage(filename):
# def imageresize(filename, width = None):
# def imageinfo(args):
# 
# Path: commands/wikidata/entity_ld.py
# class WikidataEntityLD:
#     def __init__(self, qid, languages = ""):
#         self.qid = qid
#         self.languages = languages.split(",")
#         self.rdf = self.get_rdf()
# 
#     def _entityvalues(self, item, idns):
#         tree = {
#             "uri" : item["@id"]
#         }
# 
#         for ns in (NS_NAME, NS_DESCRIPTION):
#             if ns.uri in item:
#                 tree[ns.name] = { i["@language"]:i["@value"] for i in item[ns.uri] }
# 
#                 # Do we only want a selection of languages?
#                 if self.languages:
#                     tree[ns.name] = { k:v for k,v in tree[ns.name].items() if k in self.languages}
# 
#         return tree
# 
#     def _get_by_namespace(self, tree, namespace):
#         items = Knead(tree).filter(lambda i:i["@id"].startswith(namespace.uri))
#         return items.data()
# 
#     def _statementvalues(self, statement, uri):
#         prop = [
#             { "property_uri": key, "values" : val }
#             for key, val in statement.items() if key.startswith(uri)
#         ]
# 
#         if len(prop) == 1:
#             return prop[0]
#         else:
#             return None
# 
#     def get_rdf(self):
#         r = requests.get(RDF_ENDPOINT % self.qid)
#         return r.text
# 
#     def as_json_ld(self):
#         graph = Graph().parse(data=self.rdf, format='xml')
#         jsonld = graph.serialize(format='json-ld', indent=4)
#         return json.loads(jsonld)
# 
#     def as_json_ld_simplified(self):
#         tree = self.as_json_ld()
# 
#         entities = self._get_by_namespace(tree, NS_ENTITY)
#         entities = { i["@id"]:self._entityvalues(i, NS_ENTITY.uri) for i in entities }
# 
#         statements = self._get_by_namespace(tree, NS_ENTITY_STATEMENT)
#         statements = [self._statementvalues(s, NS_PROP_STATEMENT.uri) for s in statements]
# 
#         for statement in statements:
#             if statement:
#                 uri = statement["property_uri"].replace("prop/statement", "entity")
#                 statement["property"] = entities.get(uri, None)
# 
#                 for value in statement["values"]:
#                     if "@id" in value:
#                         value["entity"] = entities.get(value["@id"], None)
# 
#                     if "@value" in value:
#                         value["value"] = value["@value"]
# 
# 
#         return {
#             "entities" : entities,
#             "statements" : statements
#         }


# Path: commands/wikidata/entity.py
import util, json, time, math, requests
from commands.wmcommons import wmcommons
from .entity_ld import WikidataEntityLD

            qid = "Q" + str(value["numeric-id"])
            val["value"] = qid
            self.entitycache[qid] = False
        elif datatype == "monolingualtext":
            # Probably a bit hacky really
            val["value"] = value["text"]
        else:
            val["value"] = value

        return val

    def get_claimvaluestring(self, val):
        datatype = val.get("datatype", None)
        value = val.get("value", False)

        if not value or not datatype:
            return ""

        if datatype == "wikibase-item":
            return val.get("value_labels", "")

        if datatype in ["string", "monolingualtext", "url"]:
            return value

        if datatype == "time":
            # HACK: This is really, pretty ugly
            # See < https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar >
            return value["time"][1:].lstrip("0")

        if datatype == "commonsMedia":

            return wmcommons.imagepage(value)