Changeset 9dad293 in rtems-central


Ignore:
Timestamp:
Apr 28, 2020, 9:29:48 AM (6 months ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
master
Children:
b3f5b7e
Parents:
99da835
git-author:
Sebastian Huber <sebastian.huber@…> (04/28/20 09:29:48)
git-committer:
Sebastian Huber <sebastian.huber@…> (05/28/20 08:34:46)
Message:

items: Use generator functions for item links

This enables an easy access to link attributes.

Location:
rtemsqual
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • rtemsqual/applconfig.py

    r99da835 r9dad293  
    3434
    3535def _gather_groups(item: Item, groups: ItemMap) -> None:
    36     for child in item.children:
     36    for child in item.children():
    3737        _gather_groups(child, groups)
    3838    if item["type"] == "interface" and item[
     
    4242
    4343def _gather_options(item: Item, options: ItemMap) -> None:
    44     for child in item.children:
     44    for child in item.children():
    4545        _gather_options(child, options)
    4646    if item["type"] == "interface" and item[
  • rtemsqual/build.py

    r99da835 r9dad293  
    6464def _gather_source_files(item: Item, enabled: List[str],
    6565                         source_files: List[str]) -> None:
    66     for parent in item.parents:
     66    for parent in item.parents():
    6767        if parent["type"] == "build" and parent["build-type"] in [
    6868                "group", "objects", "start-file", "test-program"
  • rtemsqual/glossary.py

    r99da835 r9dad293  
    3636
    3737def _gather_glossary_groups(item: Item, glossary_groups: ItemMap) -> None:
    38     for child in item.children:
     38    for child in item.children():
    3939        _gather_glossary_groups(child, glossary_groups)
    4040    if item["type"] == "glossary" and item["glossary-type"] == "group":
     
    4343
    4444def _gather_glossary_terms(item: Item, glossary_terms: ItemMap) -> None:
    45     for child in item.children:
     45    for child in item.children():
    4646        _gather_glossary_terms(child, glossary_terms)
    4747    if item["type"] == "glossary" and item["glossary-type"] == "term":
  • rtemsqual/items.py

    r99da835 r9dad293  
    2828import pickle
    2929import stat
    30 from typing import Any, List, Dict
     30from typing import Any, Dict, Iterator, List
    3131import yaml
    3232
    3333from rtemsqual.content import Content
    3434
    35 ItemList = List["Item"]
    3635ItemMap = Dict[str, "Item"]
    3736
     
    8988
    9089
     90class Link:
     91    """ A link to an item. """
     92    def __init__(self, item: "Item", data: Any):
     93        self._item = item
     94        self._data = data
     95
     96    @classmethod
     97    def create(cls, link: "Link", item: "Item") -> "Link":
     98        """ Creates a link using an existing link with a new target item. """
     99        return cls(item, link._data)  # pylint: disable=protected-access
     100
     101    def __getitem__(self, name: str) -> Any:
     102        return self._data[name]
     103
     104    @property
     105    def item(self) -> "Item":
     106        """ The item referenced by this link. """
     107        return self._item
     108
     109
    91110class Item:
    92111    """ Objects of this class represent a specification item. """
     
    94113        self._uid = uid
    95114        self._data = data
    96         self._links = []  # type: ItemList
    97         self._children = []  # type: ItemList
     115        self._links_to_parents = []  # type: List[Link]
     116        self._links_to_children = []  # type: List[Link]
    98117
    99118    def __contains__(self, key: str) -> bool:
     
    118137            os.path.join(os.path.dirname(self.uid), abs_or_rel_uid))
    119138
    120     @property
    121     def parents(self) -> ItemList:
    122         """ Returns the list of parents of this items. """
    123         return self._links
    124 
    125     @property
    126     def children(self) -> ItemList:
    127         """ Returns the list of children of this items. """
    128         return self._children
     139    def links_to_parents(self) -> Iterator[Link]:
     140        """ Yields the links to the parents of this items. """
     141        yield from self._links_to_parents
     142
     143    def parents(self) -> Iterator["Item"]:
     144        """ Yields the parents of this items. """
     145        for link in self._links_to_parents:
     146            yield link.item
     147
     148    def links_to_children(self) -> Iterator[Link]:
     149        """ Yields the links to the children of this items. """
     150        yield from self._links_to_children
     151
     152    def children(self) -> Iterator["Item"]:
     153        """ Yields the children of this items. """
     154        for link in self._links_to_children:
     155            yield link.item
    129156
    130157    def init_parents(self, item_cache: "ItemCache"):
    131         """ Initializes the list of parents of this items. """
    132         for link in self._data["links"]:
    133             self._links.append(item_cache[self.to_abs_uid(link["uid"])])
    134 
    135     def add_child(self, child: "Item"):
    136         """ Adds a child to this item. """
    137         self._children.append(child)
     158        """ Initializes the list of links to parents of this items. """
     159        for data in self._data["links"]:
     160            link = Link(item_cache[self.to_abs_uid(data["uid"])], data)
     161            self._links_to_parents.append(link)
     162
     163    def add_link_to_child(self, link: Link):
     164        """ Adds a link to a child item of this items. """
     165        self._links_to_children.append(link)
    138166
    139167    def register_license_and_copyrights(self, content: Content):
     
    243271    def _init_children(self) -> None:
    244272        for item in self._items.values():
    245             for parent in item.parents:
    246                 parent.add_child(item)
     273            for link in item.links_to_parents():
     274                link.item.add_link_to_child(Link.create(link, item))
    247275
    248276    def _load_items(self, config: Any) -> None:
  • rtemsqual/tests/test_items_item.py

    r99da835 r9dad293  
    2828import pytest
    2929
    30 from rtemsqual.items import Item
     30from rtemsqual.items import Item, Link
    3131
    3232
     
    6060
    6161def test_children():
    62     c = Item("c", {})
    63     p = Item("p", {})
    64     p.add_child(c)
    65     x = p.children
    66     assert len(x) == 1
    67     assert c == x[0]
     62    child = Item("c", {})
     63    parent = Item("p", {})
     64    parent.add_link_to_child(Link(child, {"a": "b"}))
     65    children = [item for item in parent.children()]
     66    assert len(children) == 1
     67    assert children[0] == child
     68    links = [link for link in parent.links_to_children()]
     69    assert len(links) == 1
     70    assert links[0].item == child
     71    assert links[0]["a"] == "b"
    6872
    6973
  • rtemsqual/validation.py

    r99da835 r9dad293  
    277277def _gather_items(item: Item, source_files: Dict[str, SourceFile],
    278278                  test_programs: List[TestProgram]) -> None:
    279     for child in item.children:
     279    for child in item.children():
    280280        _gather_items(child, source_files, test_programs)
    281281    if item["type"] == "test-suite":
Note: See TracChangeset for help on using the changeset viewer.