Ejemplo n.º 1
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)
        ])
Ejemplo n.º 2
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
    })
Ejemplo n.º 3
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>))
        }
    ''')
Ejemplo n.º 4
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)
Ejemplo n.º 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}'
Ejemplo n.º 7
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))
Ejemplo n.º 8
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}
Ejemplo n.º 9
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),
            ),
        ),
    )
Ejemplo n.º 10
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,
        ),
    )
Ejemplo n.º 11
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]')
Ejemplo n.º 12
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
    }
Ejemplo n.º 13
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),
            ),
        ),
    )
Ejemplo n.º 14
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',
    )
Ejemplo n.º 15
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'] })"
Ejemplo n.º 16
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}'
Ejemplo n.º 17
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']})")
Ejemplo n.º 18
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}'