Ejemplo n.º 1
0
def jurisdictions_for_selector(selector_uri):
    """
    Find out all the jurisdictions relevant to a selector/licenseclass

    That is, find all the unique jurisdictions that licenses in this
    selector have.

    Returns:
      A list of jurisdiction URIs
    """
    if JURISDICTIONS_FOR_SELECTOR_CACHE.has_key(selector_uri):
        return JURISDICTIONS_FOR_SELECTOR_CACHE[selector_uri]

    qstring = """
       PREFIX cc: <http://creativecommons.org/ns#>

       SELECT ?jurisdiction
       WHERE {
         ?license cc:licenseClass <%s> .
         ?license cc:jurisdiction ?jurisdiction . }"""
    query = RDF.Query(qstring % selector_uri, query_language='sparql')
    results = set([
        str(result['jurisdiction'].uri) for result in query.execute(ALL_MODEL)
    ])

    JURISDICTIONS_FOR_SELECTOR_CACHE[selector_uri] = results

    return results
Ejemplo n.º 2
0
    def __init__(self, uri):
        # XXX do this as a dict later?
        self._uri = uri
        self._lclass = None
        self._titles = None
        self._descriptions = None
        self._superseded_by = None
        self._version = None
        self._jurisdiction = None
        self._deprecated = None
        self._superseded = None
        self._permits = None
        self._requires = None
        self._prohibits = None
        self._code = None
        self._logos = None
        self._legalcodes = {}

        # make sure the license actually exists
        qstring = """
                  PREFIX cc: <http://creativecommons.org/ns#>
                  PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                
                  ASK { <%s> rdf:type cc:License . }"""
        query = RDF.Query(qstring % self.uri, query_language='sparql')
        uri_exists = query.execute(rdf_helper.ALL_MODEL).get_boolean()
        if not uri_exists:
            raise ExistentialException, \
                  "License <%(uri)s> does not exist in model given." % {
                              'uri': self.uri }
Ejemplo n.º 3
0
def active_languages():
    """Return a sequence of dicts, where each element consists of the
    following keys:

    * code: the language code
    * name: the translated name of this language

    for each available language."""
    global _ACTIVE_LANGUAGES
    if _ACTIVE_LANGUAGES:
        return _ACTIVE_LANGUAGES

    # get a list of avaialable translations
    domain = base.queryUtility(ITranslationDomain,
                               ccorg_i18n_setup.I18N_DOMAIN)
    lang_codes = set(domain.getCatalogsInfo().keys())

    # determine the intersection of available translations and
    # launched jurisdiction locales
    launched_locales = set()
    jurisdictions = cclicense_functions.get_valid_jurisdictions()

    for jurisdiction in jurisdictions:
        query_string = ('PREFIX dc: <http://purl.org/dc/elements/1.1/> '
                        'SELECT ?lang WHERE {'
                        '  <%s> dc:language ?lang}') % jurisdiction

        query = RDF.Query(str(query_string), query_language='sparql')
        this_juri_locales = set([
            locale_to_lower_upper(str(result['lang']))
            for result in query.execute(rdf_helper.JURI_MODEL)
        ])

        # Append those locales that are applicable to this domain
        launched_locales.update(lang_codes.intersection(this_juri_locales))

    # XXX: Have to hack in Esperanto here because it's technically an
    # "Unported" language but there is no unported RDF jurisdiction in
    # jurisdictions.rdf..
    launched_locales.add('eo')

    # make our sequence have a predictable order
    launched_locales = list(launched_locales)

    # this loop is long hand for clarity; it's only done once, so
    # the additional performance cost should be negligible
    result = []
    for code in launched_locales:

        if code == 'test': continue

        gettext = ugettext_for_locale(negotiate_locale(code))
        name = gettext(mappers.LANG_MAP[code])
        result.append(dict(code=code, name=name))

    result = sorted(result, key=lambda lang: lang['name'].lower())

    _ACTIVE_LANGUAGES = result

    return result
Ejemplo n.º 4
0
def get_deprecated(uri, model=ALL_MODEL):
    qstring = """
              PREFIX cc: <http://creativecommons.org/ns#>
              PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                
              ASK { <%s> cc:deprecatedOn ?date . }"""
    query = RDF.Query(qstring % uri, query_language='sparql')
    return query.execute(model).get_boolean()
Ejemplo n.º 5
0
def make_query(rdf, query):
    model = RDF.Model()
    parser = RDF.Parser()
    parser.parse_into_model(model, rdf)
    sparql = """
    %s
    %s""" % (PREFIX, query)
    q = RDF.Query(sparql, query_language="sparql")
    return q.execute(model)
Ejemplo n.º 6
0
 def make_query(self, rdf, query):
     """Make sparql query."""
     model = RDF.Model()
     parser = RDF.Parser()
     parser.parse_into_model(model, rdf)
     sparql = """
     %s
     %s""" % (self.rdf_prefix, query)
     q = RDF.Query(sparql, query_language="sparql")
     return q.execute(model)
Ejemplo n.º 7
0
def get_requires(uri, model=ALL_MODEL):
    qstring = """
              PREFIX cc: <http://creativecommons.org/ns#>

              SELECT ?requirement
              WHERE {
                     <%s> cc:requires ?requirement .
              }
              """
    query = RDF.Query(qstring % uri, query_language='sparql')
    return tuple(str(p['requirement'].uri) for p in query.execute(model))
Ejemplo n.º 8
0
def get_logos(uri, model=ALL_MODEL):
    qstring = """
              PREFIX foaf: <http://xmlns.com/foaf/0.1/>

              SELECT ?img
              WHERE {
                     <%s> foaf:logo ?img .
              }
              """
    query = RDF.Query(qstring % uri, query_language='sparql')
    return tuple(str(s['img'].uri) for s in query.execute(model))
Ejemplo n.º 9
0
    def get_complete_metadata(self, user_uri, work_uri, format='json'):
        work = Work.from_model(self._model, work_uri, user_uri)

        if not self._can_access('read', work):
            raise EntryAccessError("Can't access work {0}".format(work_uri))

        if format not in ('ntriples', 'rdfxml', 'json'):
            raise ParamError('invalid RDF format: {0}'.format(format))

        query_format = """
            PREFIX dc: <http://purl.org/dc/elements/1.1/>
            PREFIX catalog: <http://catalog.commonsmachinery.se/ns#>
            PREFIX rem3: <http://scam.sf.net/schema#>

            CONSTRUCT {
                ?s ?p ?o .
                ?work dc:source ?sourceWork .
            }
            WHERE
            {
                BIND (<%s> AS ?work)
                BIND (<%s> AS ?user)

                ?work catalog:creator ?creator .
                ?work catalog:visible ?visible .
                ?work rem3:metadata ?workMetadata .
                ?work catalog:source ?sourceRef .
                ?sourceRef rem3:resource ?sourceWork .

                { ?sourceWork rem3:metadata ?sourceMetadata . }
                UNION
                { ?sourceRef rem3:cachedExternalMetadata ?sourceMetadata . }

                GRAPH ?g { ?s ?p ?o . }

                FILTER((?g = ?workMetadata || ?g = ?sourceMetadata) &&
                       ((?visible = "public") ||
                        (?visible = "private") && (?creator = ?user)))
            }
        """

        query_string = query_format % (work_uri, user_uri)
        query = RDF.Query(query_string)

        query_results = query.execute(self._model)

        # TODO: use results.to_string() with proper format URIs
        temp_model = RDF.Model(RDF.MemoryStorage())

        for statement in query_results.as_stream():
            temp_model.append(statement)

        result = temp_model.to_string(name=format, base_uri=None)
        return result
Ejemplo n.º 10
0
def get_prohibits(uri, model=ALL_MODEL):
    qstring = """
              PREFIX cc: <http://creativecommons.org/ns#>

              SELECT ?prohibition
              WHERE {
                     <%s> cc:prohibits ?prohibition .
              }
              """
    query = RDF.Query(qstring % uri, query_language='sparql')
    return tuple(str(p['prohibition'].uri) for p in query.execute(model))
Ejemplo n.º 11
0
def get_selector_id(uri):
    qstring = """
              PREFIX cc: <http://creativecommons.org/ns#>

              SELECT ?lcode
              WHERE {
                     <%s> cc:licenseCode ?lcode .
                    }
              """
    query = RDF.Query(qstring % uri, query_language='sparql')
    solns = [i for i in query.execute(SEL_MODEL)]
    return str(solns[0]['lcode'].literal_value['string'])
Ejemplo n.º 12
0
def get_license_code(uri, model=ALL_MODEL):
    qstring = """
              PREFIX dc: <http://purl.org/dc/elements/1.1/>

              SELECT ?code
              WHERE {
                     <%s> dc:identifier ?code .
              }
              """
    query = RDF.Query(qstring % uri, query_language='sparql')
    solns = [i for i in query.execute(model)]
    return str(solns[0]['code'].literal_value['string'])
Ejemplo n.º 13
0
def get_license_class(uri, model=ALL_MODEL):
    qstring = """
              PREFIX cc: <http://creativecommons.org/ns#>

              SELECT ?lclassuri
              WHERE {
                     <%s> cc:licenseClass ?lclassuri .
              }
              """
    query = RDF.Query(qstring % uri, query_language='sparql')
    solns = [i for i in query.execute(model)]
    return str(solns[0]['lclassuri'].uri)
Ejemplo n.º 14
0
def get_all_license_urls():
    qstring = """
              PREFIX cc: <http://creativecommons.org/ns#>
              PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

              SELECT ?luri
              WHERE {
                     ?luri rdf:type cc:License .
                    }
              """
    query = RDF.Query(qstring, query_language='sparql')
    return tuple(
        str(s['luri'].uri) for s in query.execute(rdf_helper.ALL_MODEL))
Ejemplo n.º 15
0
def selector_has_license(selector_uri, license_uri, model=ALL_MODEL):
    qstring = """
              PREFIX cc: <http://creativecommons.org/ns#>

              ASK { <%s> cc:licenseClass <%s> . }"""
    # can't interpolate empty strings
    if selector_uri == '':
        selector_uri = 'NONE'
    if license_uri == '':
        license_uri = 'NONE'
    query = RDF.Query(qstring % (license_uri, selector_uri),
                      query_language='sparql')
    return query.execute(model).get_boolean()
Ejemplo n.º 16
0
def get_license_uris(selector_uri, model=ALL_MODEL):
    # FIXME: This function is never hit by any unit tests.
    qstring = """
              PREFIX cc: <http://creativecommons.org/ns#>
              PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

              SELECT ?luri
              WHERE {
                     ?luri cc:licenseClass <%s> .
                    }
              """
    query = RDF.Query(qstring % selector_uri, query_language='sparql')
    return tuple(str(s['luri'].uri) for s in query.execute(model))
Ejemplo n.º 17
0
def get_selector_uris():
    """Returns a list of LicenseSelector URIs."""
    qstring = """
              PREFIX cc: <http://creativecommons.org/ns#>
              PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

              SELECT ?uri
              WHERE {
                     ?uri rdf:type cc:LicenseSelector .
                    }
              """
    query = RDF.Query(qstring, query_language='sparql')
    return [str(s['uri'].uri) for s in query.execute(SEL_MODEL)]
Ejemplo n.º 18
0
def validate(g):
    print >>sys.stderr, 'Validating domains/ranges ...',
    for domain_constraint in RDF.Query('SELECT ?s ?o WHERE (?s <http://www.w3.org/2000/01/rdf-schema#domain> ?o)').execute(g):
        if not domain_constraint['s'].is_resource() or not domain_constraint['o'].is_resource():
            continue # for now
        if domain_constraint['o'] in DOMAIN_OBJECT_EXCEPTIONS:
            continue
        for x in RDF.Query('SELECT ?s WHERE (?s <%s> ?o)' % domain_constraint['s'].uri).execute(g):
            if domain_constraint['o'] not in _types(g, x['s']):
                raise ValueError('property %s on %s violates rdfs:domain constraint of %s (found %r)' % (domain_constraint['s'], x['s'], domain_constraint['o'], [str(x) for x in _types(g, x['s'])]))
    for range_constraint in RDF.Query('SELECT ?s ?o WHERE (?s <http://www.w3.org/2000/01/rdf-schema#range> ?o)').execute(g):
        if not range_constraint['s'].is_resource() or not range_constraint['o'].is_resource():
            continue # for now
        if range_constraint['s'] in RANGE_PROPERTY_EXCEPTIONS or range_constraint['o'] in RANGE_OBJECT_EXCEPTIONS:
            continue
        for x in RDF.Query('SELECT ?o WHERE (?s <%s> ?o)' % range_constraint['s'].uri).execute(g):
            if range_constraint['o'] in (RDF_NS.Literal, RDFS_NS.Literal):
                if not x['o'].is_literal():
                    raise ValueError('property %s to %s violates rdfs:range constraint of %s (not is_literal())' % (range_constraint['s'], x['o'], range_constraint['o']))
            else:
                if range_constraint['o'] not in _types(g, x['o']):
                    raise ValueError('property %s to %s violates rdfs:range constraint of %s (found %r)' % (range_constraint['s'], x['o'], range_constraint['o'], [str(x) for x in _types(g, x['o'])]))
    print >>sys.stderr, 'done'
Ejemplo n.º 19
0
def get_license_legalcodes(license_uri):
    """
    Return a list of [(legalcode_uri, legalcode_lang)] for license_uri

    If this is a single-legalcode option, it'll probably return
    [(legalcode_uri, None)]
    """
    qstring = """
       PREFIX cc: <http://creativecommons.org/ns#>

       SELECT ?legalcode
       WHERE {
         <%s> cc:legalcode ?legalcode }"""
    query = RDF.Query(qstring % license_uri, query_language='sparql')
    results = set()

    for result in query.execute(ALL_MODEL):
        legalcode_uri = str(result['legalcode'].uri)

        qstring = """
           PREFIX dcq: <http://purl.org/dc/terms/>

           SELECT ?lang
           WHERE {
             <%s> dcq:language ?lang }"""
        query = RDF.Query(qstring % legalcode_uri, query_language='sparql')

        lang_results = [
            str(result['lang']) for result in query.execute(ALL_MODEL)
        ]

        if lang_results:
            results.add((legalcode_uri, str(lang_results[0])))
        else:
            results.add((legalcode_uri, None))

    return results
Ejemplo n.º 20
0
def get_version(uri, model=ALL_MODEL):
    qstring = """
              PREFIX dcq: <http://purl.org/dc/terms/>

              SELECT ?version
              WHERE {
                     <%s> dcq:hasVersion ?version .
                    }
              """
    query = RDF.Query(qstring % uri, query_language='sparql')
    solns = [i for i in query.execute(model)]
    if len(solns) == 0:
        return ''
    else:
        return solns[0]['version'].literal_value['string']
Ejemplo n.º 21
0
def get_jurisdiction(uri, model=ALL_MODEL):
    qstring = """
              PREFIX cc: <http://creativecommons.org/ns#>

              SELECT ?jurisdiction
              WHERE {
                     <%s> cc:jurisdiction ?jurisdiction .
              }
              """

    query = RDF.Query(qstring % uri, query_language='sparql')
    solns = [i for i in query.execute(model)]
    if len(solns) == 0:
        return cc.license.Jurisdiction('')  # empty string makes 'Unported'
    else:
        return cc.license.Jurisdiction(str(solns[0]['jurisdiction'].uri))
Ejemplo n.º 22
0
def get_jurisdiction_licenses(uri, model=ALL_MODEL):
    # FIXME: This function is never hit by any unit tests.
    qstring = """
              PREFIX cc: <http://creativecommons.org/ns#>

              SELECT ?license
              WHERE {
                     ?license cc:jurisdiction <%s> .
              }
              """
    query = RDF.Query(qstring % uri, query_language='sparql')
    solns = [i for i in query.execute(model)]
    if len(solns) == 0:
        return []  # empty makes 'Unported'
    else:
        return [str(l['license'].uri) for l in solns]
Ejemplo n.º 23
0
def get_superseded(uri, model=ALL_MODEL):
    """Watch out: returns a tuple and not just a value."""
    qstring = """
              PREFIX dcq: <http://purl.org/dc/terms/>

              SELECT ?replacement
              WHERE {
                     <%s> dcq:isReplacedBy ?replacement .
                    }
              """
    query = RDF.Query(qstring % uri, query_language='sparql')
    solns = [i for i in query.execute(model)]
    if len(solns) == 0:
        return (False, None)
    else:
        superseded_by = str(solns[0]['replacement'].uri)
        return (True, superseded_by)
Ejemplo n.º 24
0
def getAggregatedIdentifiers(identifier):
    """Retrieves and parses the resource map with the specified identifier.

    Returns: List(str)
        List of identifiers.
    """

    if type(identifier) is not str or len(identifier) < 1:
        raise Exception("Bad identifier string passed to method.")

    model = RDF.Model()
    parser = RDF.Parser(name="rdfxml")

    base_url = "https://cn.dataone.org/cn/v1/object/"
    query_url = base_url + urllib.quote_plus(identifier)

    try:
        parser.parse_into_model(model, query_url)
    except RDF.RedlandError as e:
        print "Exception: Failed to parse RDF/XML at `%s`: %s" % (query_url, e)

    query = """
    SELECT ?s ?o
    WHERE {
        ?s <http://www.openarchives.org/ore/terms/aggregates> ?o
    }
    """

    q = RDF.Query(query)

    identifiers = []

    for result in q.execute(model):
        if 'o' not in result:
            continue

        object_node = result['o']

        if object_node.is_resource():
            object_node_str = str(object_node)
            identifier = extractIdentifierFromFullURL(object_node_str)

            if identifier is not None:
                identifiers.append(identifier)

    return identifiers
Ejemplo n.º 25
0
    def query_sparql(self, query_string=None, results_format="json"):
        if results_format not in ('json', 'n3', 'xml'):
            raise ParamError('invalid SPARQL result format: {0}'.format(results_format))

        query = RDF.Query(querystring=query_string, query_language="sparql")
        query_results = query.execute(self._model)
        if query.get_limit() < 0:
            query.set_limit(50)

        if results_format == "json":
            format_uri = "http://www.mindswap.org/%7Ekendall/sparql-results-json/"
        elif results_format == "n3":
            format_uri = "http://www.w3.org/TeamSubmission/turtle/"
        elif results_format == "html":
            format_uri = "http://www.w3.org/1999/xhtml/"
        else:
            format_uri = "http://www.w3.org/TR/2008/REC-rdf-sparql-XMLres-20080115/"
        return query_results.to_string(format_uri=format_uri)
Ejemplo n.º 26
0
def get_jurisdiction_default_language(juris_uri):
    qstring = """
              PREFIX dc: <http://purl.org/dc/elements/1.1/>
              PREFIX cc: <http://creativecommons.org/ns#>

              SELECT ?default_language
              WHERE {
                     <%s> cc:defaultLanguage ?default_language
                    }
              """
    query = RDF.Query(qstring % juris_uri, query_language='sparql')
    results = [
        str(result['default_language']) for result in query.execute(JURI_MODEL)
    ]
    if results:
        return results[0]
    else:
        return None
Ejemplo n.º 27
0
def get_valid_jurisdictions(license_class='standard'):
    if _VALID_JURISDICTIONS_CACHE.has_key(license_class):
        return _VALID_JURISDICTIONS_CACHE[license_class]
    
    # TODO: use license_class here
    query = RDF.Query(
        str('PREFIX cc: <http://creativecommons.org/ns#> '
            'SELECT ?jurisdiction WHERE '
            '{ ?license cc:licenseClass <http://creativecommons.org/license/> .'
            '  ?license cc:jurisdiction ?jurisdiction }'),
        query_language="sparql")

    jurisdictions = set(
        [unicode(result['jurisdiction'].uri)
         for result in query.execute(rdf_helper.ALL_MODEL)])

    _VALID_JURISDICTIONS_CACHE[license_class] = jurisdictions

    return jurisdictions
Ejemplo n.º 28
0
def locales():
    """Returns a sequence of all locales possible.
       A locale is a string that represents the language of a jurisdiction.
       Note that locales are not the same as jurisdiction codes."""
    query_string = """
        PREFIX cc: <http://creativecommons.org/ns#>
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        PREFIX dc: <http://purl.org/dc/elements/1.1/>

        SELECT ?lang
        WHERE
         {
            ?x cc:defaultLanguage ?lang .
            ?x rdf:type cc:Jurisdiction .
         }
                  """
    query = RDF.Query(query_string, query_language='sparql')
    return [s['lang'].literal_value['string']
            for s in query.execute(rdf_helper.JURI_MODEL)]
Ejemplo n.º 29
0
def get_titles(uri, model=ALL_MODEL):
    """Given a URI for an RDF resource, return a dictionary of
       corresponding to its dc:title properties. The indices will
       be locale codes, and the values will be titles."""
    qstring = """
                     PREFIX dc: <http://purl.org/dc/elements/1.1/>

                     SELECT ?title
                     WHERE
                      {
                         <%s> dc:title ?title .
                      }
                  """
    # get the data back
    query = RDF.Query(qstring % uri, query_language='sparql')
    # parse the data
    _titles = {}
    for s in query.execute(model):
        tmp = s['title'].literal_value
        _titles[tmp['language']] = tmp['string']
    return _titles
Ejemplo n.º 30
0
def get_selector_jurisdictions(selector_name='standard'):
    """
    Get all of the launched jurisdictions that licenses in this
    selector are part of
    """
    if _SELECTOR_JURISDICTIONS_CACHE.has_key(selector_name):
        return _SELECTOR_JURISDICTIONS_CACHE[selector_name]

    selector = cc.license.selectors.choose(selector_name)
    qstring = "\n".join([
        "SELECT ?license",
        "WHERE (?license cc:licenseClass <%s>)" % str(selector.uri),
        "USING cc FOR <http://creativecommons.org/ns#>"
    ])
    query = RDF.Query(qstring, query_language="rdql")

    # This is so stupid, but if we add a WHERE clause for
    # jurisdictions in the query string it takes approximately 5
    # million years.
    licenses = [
        cc.license.by_uri(str(result['license'].uri))
        for result in query.execute(rdf_helper.ALL_MODEL)
    ]

    # We need to make sure jurisdictions are unique.  The easiest way
    # to do that is have a second set that keeps track of all the
    # codes added so far.
    code_check = set()
    jurisdictions = set()

    for license in licenses:
        jurisdiction = license.jurisdiction
        if jurisdiction.launched and not jurisdiction.code in code_check:
            jurisdictions.add(jurisdiction)
            code_check.add(jurisdiction.code)

    _SELECTOR_JURISDICTIONS_CACHE[selector_name] = jurisdictions

    return jurisdictions