Пример #1
0
def test_yaml_list(root_context: LDContext):
    """Update Octiron graph from a YAML file."""
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test_yaml/list.yaml',
        local_iri=LOCAL_IRI,
    )

    assert (
        URIRef(RDF),
        LOCAL.prefix,
        Literal('rdf'),
    ) in octiron.graph

    triples = set(octiron.graph.triples((None, RDFS.label, None)))
    assert triples == {
        (BNode('local:test.yaml/b0'), RDFS.label, Literal('boo')),
        (BNode('local:test.yaml/b1'), RDFS.label, Literal('foo')),
    }
Пример #2
0
def test_markdown_with_dollar_sign(root_context: LDContext):
    """Update Octiron graph from a Markdown file with a $context."""
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test_dollar_id.md',
        local_iri=LOCAL_IRI,
    )

    assert set(octiron.graph.quads()).issuperset({
        (
            LOCAL_IRI,
            RDFS.label,
            Literal('Hey, I am a test!'),
            Graph(identifier=LOCAL_IRI),
        ),
        (
            LOCAL_IRI,
            RDFS.domain,
            LOCAL.UnitTesting,
            Graph(identifier=LOCAL_IRI),
        ),
        (
            LOCAL_IRI,
            RDF.type,
            OCTA.Page,
            Graph(identifier=LOCAL_IRI),
        ),
    })
Пример #3
0
def test_file_in_docs_directory(root_context: LDContext):
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_directory = Path(temp_dir)

        docs_directory = Path(temp_directory) / 'docs'
        docs_directory.mkdir()
        (docs_directory / 'context.yaml').write_text(data='')

        octiron = Octiron(
            root_directory=docs_directory,
            root_context=root_context,
        )
        assert list(octiron._find_context_files(docs_directory / 'posts')) == [
            temp_directory / 'docs/context.yaml',
        ]
Пример #4
0
def foundational_classes(octiron: Octiron) -> Graph:
    """Select the foundational classes of the website."""
    return octiron.query('''
        CONSTRUCT {
            ?s ?p ?o .
            
            ?s rdfs:label ?s_label .
            ?o rdfs:label ?o_label .
        } WHERE {
            ?p
                rdfs:domain ?s ;
                rdfs:range ?o .
            
            OPTIONAL {
                ?s rdfs:label ?s_label .
            }
            
            OPTIONAL {
                ?o rdfs:label ?o_label .
            }

            FILTER(?s IN (<local:Violation>, <local:ViolationPage>, <local:Flake8Plugin>, <local:Flake8PluginVersion>))
            FILTER(?o IN (<local:Violation>, <local:ViolationPage>, <local:Flake8Plugin>, <local:Flake8PluginVersion>))
        }
    ''')
Пример #5
0
def test_file_in_upper_directory(root_context: LDContext):
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_directory = Path(temp_dir)

        # Create a file that will not be accessible to the finder
        # because it is above the root_directory in structure
        (temp_directory / 'context.yaml').write_text(data='')

        docs_directory = Path(temp_directory) / 'docs'
        docs_directory.mkdir()

        octiron = Octiron(
            root_directory=docs_directory,
            root_context=root_context,
        )
        assert not list(octiron._find_context_files(docs_directory / 'posts'))
Пример #6
0
def sidebar_property(
    octiron: Octiron,
    iri: URIRef,
    environment: URIRef,
) -> str:
    """Render name of the property of the ADR page."""
    rows = octiron.query(
        '''
        SELECT * WHERE {
            ?property rdfs:label ?label .

            OPTIONAL {
                ?property octa:symbol ?symbol .
            }
        } LIMIT 1
        ''',
        property=iri,
    )

    try:
        row = first(rows)
    except ValueError as err:
        raise PropertyNotRenderable(iri=iri) from err

    label = row['label']
    if symbol := row.get('symbol'):
        label = f'{symbol} {label}'
Пример #7
0
def retrieve_properties_by_page(octiron: Octiron, iri: URIRef):
    about_this_page = octiron.query(
        '''
        SELECT ?property ?value WHERE {
            ?page ?property ?value .

            ?property a adr:ADRProperty .

            OPTIONAL {
                ?property octa:position ?explicit_position .
            }

            BIND(COALESCE(?explicit_position, 0) as ?position)
        } ORDER BY ?position ?property
        ''',
        page=iri,
    )

    groups = itertools.groupby(
        about_this_page,
        key=operator.itemgetter('property'),
    )

    return dict({
        grouper: list(map(
            operator.itemgetter('value'),
            group_items,
        ))
        for grouper, group_items in groups
    })
Пример #8
0
def latest_version_list(octiron: Octiron, iri: Node) -> str:
    """List latest version of every package as cards."""

    plugins = octiron.query(
        '''
        SELECT * WHERE {
            ?plugin
                a :Flake8Plugin ;
                :code-prefix ?prefix ;
                rdfs:label ?title .
        
            ?version
                a ?version_class ;
                rdfs:label ?version_number ;
                :plugin ?plugin .
        
            ?index_page
                a octa:IndexPage ;
                octa:isChildOf ?version ;
                octa:url ?url .
        } ORDER BY ?prefix
        ''',
        version_class=iri,
    )

    return create_template().render(plugins=plugins, )
def directory_list(octiron: Octiron, iri: Node) -> Union[html_tag, str]:
    """Render a list of subdirectories in given directory."""
    links = map(
        Prodict,
        octiron.query(
            '''
        SELECT * WHERE {
            ?index_page octa:isChildOf ?directory .
            
            ?directory
                octa:isParentOf /
                octa:isParentOf /
                octa:isParentOf ?link .
        
            ?link
                a octa:IndexPage ;
                octa:title ?title ;
                octa:url ?url .
        } ORDER BY ?title
        ''',
            index_page=iri,
        ))

    lis = [li(a(link.title, href=link.url)) for link in links]

    return ul(*lis)
Пример #10
0
    def moderate_files(self, files: Files, octiron: Octiron):
        """
        Exclude some files when building site locally.

        This helps reduce the site building time for the sake of local dev.
        """
        logging.warning(
            'A few files have been stripped from the site because local dev env'
            ' has been detected. Beware of 404 errors!', )
        directories = [
            row['version_directory'].toPython().replace('local:', '')
            for row in octiron.query(
                '''
                SELECT * WHERE {
                    ?version_directory a :Flake8PluginVersion .
                    
                    FILTER NOT EXISTS {
                        ?version_directory a :LatestVersion .
                    }
                }
                ''', )
        ]

        return Files([
            mkdocs_file for mkdocs_file in files if not any(
                mkdocs_file.src_path.startswith(directory)
                for directory in directories)
        ])
Пример #11
0
def status_class(octiron: Octiron, iri: URIRef):
    """Visualize all available status values as a table."""
    choices = octiron.query('''
        SELECT
            ?status ?label ?symbol
            ?defined_by_iri ?defined_by_url ?defined_by_label
        WHERE {
            ?status a adr:Status .

            GRAPH ?defined_by_iri {
                ?status rdfs:label ?label .

                OPTIONAL {
                   ?status octa:symbol ?symbol .
                }
            }

            OPTIONAL {
                ?defined_by_iri octa:url ?defined_by_url .
            }

            OPTIONAL {
                ?defined_by_iri rdfs:label ?defined_by_label .
            }
        } ORDER BY ?label
        ''')

    rows = map(build_table_row, choices)

    return table(thead(tr(
        th('Code'),
        th('Label'),
        th('Defined By'),
    )), tbody(*rows))
Пример #12
0
def cached_octiron(docs_dir: Path) -> Octiron:
    """Retrieve cached Octiron instance or create it if absent."""
    return Octiron(
        root_directory=docs_dir,
        root_context=construct_root_context(
            namespaces=DEFAULT_NAMESPACES,
        ),
    )
Пример #13
0
def app_by_property(octiron: Octiron) -> Dict[URIRef, URIRef]:
    """Find apps connected to properties."""
    pairs = octiron.query('''
        SELECT ?property ?app WHERE {
            ?property iolanta:facet ?facet .
            ?app iolanta:supports adr:sidebar .
        }
        ''')
    return {row['property']: row['facet'] for row in pairs}
Пример #14
0
def test_yaml_with_context(root_context: LDContext):
    """Update Octiron graph from a YAML file."""
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test_yaml/spo.yaml',
        local_iri=LOCAL_IRI,
    )

    assert (
        LOCAL_IRI,
        RDFS.label,
        Literal(
            'For any triple, its subject and object are Resources, and '
            'predicate is a Property.', ),
    ) in octiron.graph
Пример #15
0
def test_clear_named_graph(root_context: LDContext):
    """Update Octiron graph from a YAML file."""
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test.yaml',
        local_iri=LOCAL_IRI,
    )

    assert set(octiron.graph) == {
        (BNode(f'{LOCAL_IRI}/b0'), RDF.subject, Literal('s')),
        (BNode(f'{LOCAL_IRI}/b1'), RDF.predicate, Literal('p')),
        (BNode(f'{LOCAL_IRI}/b2'), RDF.object, Literal('o')),
        (LOCAL_IRI, LOCAL.given, Literal('spo')),
        (LOCAL_IRI, LOCAL.infer, BNode(f'{LOCAL_IRI}/b0')),
        (LOCAL_IRI, LOCAL.infer, BNode(f'{LOCAL_IRI}/b1')),
        (LOCAL_IRI, LOCAL.infer, BNode(f'{LOCAL_IRI}/b2')),
        (URIRef(LOCAL), RDF.type, OCTA.Directory),
        (LOCAL_IRI, OCTA.isChildOf, URIRef(LOCAL)),
        (LOCAL_IRI, OCTA.fileName, Literal('test.yaml')),
        (LOCAL_IRI, OCTA.subjectOf, LOCAL_IRI),
    }

    octiron.clear_named_graph(local_iri=LOCAL_IRI, )

    assert (LOCAL_IRI, LOCAL.given, Literal('spo')) not in set(octiron.graph)
Пример #16
0
def test_markdown_with_context(root_context: LDContext):
    """Update Octiron graph from a Markdown file with a $context."""
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test_with_context.md',
        local_iri=LOCAL_IRI,
    )

    assert set(octiron.graph) == {
        (LOCAL_IRI, RDFS.label, Literal('Hey, I am a test!')),
        (LOCAL_IRI, RDF.type, OCTA.Page),
        (LOCAL_IRI, OCTA.fileName, Literal('test_with_context.md')),
        (LOCAL_IRI, OCTA.subjectOf, LOCAL_IRI),
        (LOCAL_IRI, OCTA.url, Literal('/test/')),
        (URIRef('local:test_with_context.md'), OCTA.isChildOf, URIRef(LOCAL)),
        (URIRef(LOCAL), RDF.type, OCTA.Directory),
    }
Пример #17
0
def test_yaml_date(root_context: LDContext):
    """
    Update Octiron graph from a YAML file with a date in it.

    Created for https://github.com/digitalbazaar/pyld/issues/146
    """
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test_yaml/publication_date.yaml',
        local_iri=LOCAL_IRI,
    )

    assert (
        LOCAL.foo,
        LOCAL.publicationDate,
        Literal('2020-11-16'),
    ) in octiron.graph
Пример #18
0
def test_markdown_without_id(root_context: LDContext):
    """Update Octiron graph from a Markdown file without a $id."""
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test_without_id.md',
        local_iri=LOCAL_IRI,
    )

    REAL_IRI = LOCAL['test_without_id.md']
    assert set(octiron.graph.triples((None, None, None))) == {
        (LOCAL_IRI, OCTA.title, Literal('Hey, I am a test!')),
        (LOCAL_IRI, RDF.type, OCTA.Page),
        (LOCAL_IRI, OCTA.url, Literal('/test/')),
        (LOCAL_IRI, OCTA.fileName, Literal('test_without_id.md')),
        (LOCAL_IRI, OCTA.subjectOf, LOCAL_IRI),
        (REAL_IRI, OCTA.isChildOf, URIRef(LOCAL)),
        (LOCAL.term(''), RDF.type, OCTA.Directory),
    }
Пример #19
0
def get_ordering(iri: URIRef, octiron: Octiron) -> List[URIRef]:
    """List of columns that we are ordering by."""
    # Idea: http://www.snee.com/bobdc.blog/2014/04/rdf-lists-and-sparql.html
    return list(
        map(
            operator.itemgetter('column'),
            octiron.query(
                query_text='''
                SELECT ?column WHERE {
                    ?iri table:ordering/rdf:rest*/rdf:first ?column .
                }
                ''',
                iri=URIRef(iri),
            ),
        ),
    )
Пример #20
0
def select_instances(
    iri: URIRef,
    octiron: Octiron,
) -> Iterable[URIRef]:
    """Select instances, or rows, for the table."""
    return map(
        operator.itemgetter('instance'),
        octiron.query(
            query_text='''
            SELECT ?instance WHERE {
                $iri table:class ?class .
                ?instance a ?class .
            }
            ''',
            iri=iri,
        ),
    )
Пример #21
0
def default(octiron: Octiron, node: Node) -> str:
    """
    Default facet to render an arbitrary object.

    Relies upon rdfs:label.
    """
    rows = octiron.query(
        'SELECT ?label WHERE { ?node rdfs:label ?label }',
        node=node,
    )

    labels = map(
        operator.itemgetter('label'),
        rows,
    )

    return first(labels, '[No Label]')
Пример #22
0
def construct_row(
    instance: URIRef,
    octiron: Octiron,
    columns: List[URIRef],
) -> Row:
    """Construct a table row."""
    formatted_columns = '({columns})'.format(
        columns=', '.join([
            f'<{column}>' for column in columns
        ]),
    )

    query_text = '''
    SELECT * WHERE {
        $instance ?column ?value .

        OPTIONAL {
            ?value octa:trustLevel ?trust_level .
        }

        FILTER(?column IN %s) .
    } ORDER BY ?column ?trust_level
    ''' % formatted_columns

    cells = octiron.query(
        query_text=query_text,
        instance=instance,
    )

    cells.append({
        'column': TABLE.self,
        'value': instance,
    })

    # This dictionary comprehension entails an implicit deduplication by
    # `cell['column']`, in which the last duplicate wins. Since we have sorted
    # the elements by `octa:trustLevel` this means we will prefer a higher trust
    # level over a lower one, or over an absence of defined trust level.
    return {
        cell['column']: instance if (
            cell['column'] == TABLE.self
        ) else cell['value']
        for cell in cells
    }
Пример #23
0
def list_columns(
    iri: URIRef,
    octiron: Octiron,
) -> List[URIRef]:
    """List of column IRIs for a table."""
    # Idea: http://www.snee.com/bobdc.blog/2014/04/rdf-lists-and-sparql.html
    return list(
        map(
            operator.itemgetter('column'),
            octiron.query(
                query_text='''
                SELECT ?column WHERE {
                    ?iri table:columns/rdf:rest*/rdf:first ?column .
                }
                ''',
                iri=URIRef(iri),
            ),
        ),
    )
Пример #24
0
def wps_constant(
    url: str,
    current_page: Page,
    octiron: Octiron,
):
    """Render a link to a WPS constant page."""
    constant = first(
        octiron.query(
            '''SELECT * WHERE {
            ?current_page_iri :version ?version .
            
            ?constant_iri
                :about ?python_iri ;
                :name ?name ;
                octa:url ?url ;
                :version ?version .
        }''',
            current_page_iri=current_page.iri,
            python_iri=URIRef(url + '/'),
        ))

    return f"[{constant['name']}]({constant['url'] })"
Пример #25
0
def violation_list(octiron: Octiron, iri: Node) -> html_tag:
    """Render a list of violations by directory."""
    violations = octiron.query(
        '''
        SELECT * WHERE {
            ?index_page octa:isChildOf ?violations_directory .
        
            ?page
                a :ViolationPage ;
                octa:isChildOf ?violations_directory ;
                octa:title ?title ;
                octa:url ?url ;
                :code ?code .
        } ORDER BY ?code
        ''',
        index_page=iri,
    )

    return ul(
        *construct_list_items(violations),
        style='column-count: 2',
    )
Пример #26
0
def default_property_facet(
    octiron: Octiron,
    property_iri: URIRef,
    property_values: List[Node],
) -> Optional[str]:
    """Default facet to render a property with its values."""
    label = first(
        map(
            operator.itemgetter('label'),
            octiron.query(
                '''
                SELECT ?label WHERE {
                    ?property rdfs:label ?label .
                } ORDER BY ?label LIMIT 1
                ''',
                property=property_iri,
            ),
        ),
        None,
    )

    if label is None:
        return

    if len(property_values) > 1:
        raise Exception('Too many values')

    property_value = property_values[0]

    if isinstance(property_value, Literal):
        return f'<strong>{label}</strong>: {property_value}'

    rendered_value = render(
        octiron=octiron,
        node=property_value,
    )
    return f'<strong>{label}</strong>: {rendered_value}'
Пример #27
0
def status(octiron: Octiron, iri: URIRef):
    """Render ADR document status."""
    meta = first(
        octiron.query(
            '''
            SELECT * WHERE {
                ?status rdfs:label ?label .
                
                OPTIONAL {
                    ?status octa:symbol ?symbol .
                }
            } ORDER BY DESC(?symbol)
            ''',
            status=iri,
        ),
        None,
    )

    if not meta:
        raise StatusNotFound(status=iri)

    label = meta['label']
    if symbol := meta.get('symbol'):
        label = f'{symbol} {label}'
Пример #28
0
def wps_violation(
    internal_name: str,
    current_page: Page,
    octiron: Octiron,
) -> str:
    """Render a link to a WPS Violation page."""
    violation = first(
        octiron.query(
            '''SELECT * WHERE {
            ?current_page_iri :version ?version .
        
            ?violation
                :internalName ?name ;
                :code ?code ;
                octa:title ?title ;
                octa:url ?url ;
                :version ?version .
        }''',
            name=Literal(internal_name),
            current_page_iri=current_page.iri,
        ))

    return (
        f"[**{violation['code']}** {violation['title']}]({violation['url']})")