Skip to content

fediverse_pasture.runner.entry

Entry dataclass

Represents an entry to generate support tables from.

Parameters:

Name Type Description Default
entry Dict[str, Dict]

Dictionary indexed by application with values the result

required
Source code in fediverse_pasture/runner/entry.py
@dataclass
class Entry:
    """Represents an entry to generate support tables from.

    :param entry: Dictionary indexed by application with values the result"""

    entry: Dict[str, Dict]

    @property
    def applications(self) -> Set[str]:
        """Returns the set of application names"""
        return set(self.entry.keys())

    @property
    def activity(self):
        return self.entry.get("activity")

    @property
    def object(self):
        activity = self.activity
        obj = activity.get("object", {})
        return {"@context": activity["@context"], **obj}

    def present_for(self, application):
        return application in self.entry

    def as_tabs(self, apps: List[str]) -> List[str]:
        """Renders the data for each application as tabbed markdown.

        If no data is present "no result" is shown.

        :param apps: List of applications to display"""
        lines = []
        for application_name in apps:
            lines.append(f"""=== "{application_name}"\n""")

            if application_name in self.entry:
                text = "```json\n"
                text += json.dumps(self.entry[application_name], indent=2)
                text += "\n```\n"
                lines += textwrap.indent(text, " " * 4).split("\n")
            else:
                lines.append("    no result")

            lines += [""]

        return [x + "\n" for x in lines]

    def apply_to(
        self, application: str, function: Callable[[Dict], List[str]]
    ) -> List[str]:
        """Applies the function to the entry for the given application.

        :param application: The application name
        :param function: extractor for the desired data"""
        try:
            return function(self.entry.get(application))
        except Exception as e:
            logger.info(repr(e))
            return ["-"]

    @staticmethod
    def from_result_list(result):
        val = {x["application_name"]: x for x in result}
        for x in val:
            del val[x]["application_name"]
        return Entry(val)

applications: Set[str] property

Returns the set of application names

apply_to(application, function)

Applies the function to the entry for the given application.

Parameters:

Name Type Description Default
application str

The application name

required
function Callable[[Dict], List[str]]

extractor for the desired data

required
Source code in fediverse_pasture/runner/entry.py
def apply_to(
    self, application: str, function: Callable[[Dict], List[str]]
) -> List[str]:
    """Applies the function to the entry for the given application.

    :param application: The application name
    :param function: extractor for the desired data"""
    try:
        return function(self.entry.get(application))
    except Exception as e:
        logger.info(repr(e))
        return ["-"]

as_tabs(apps)

Renders the data for each application as tabbed markdown.

If no data is present “no result” is shown.

Parameters:

Name Type Description Default
apps List[str]

List of applications to display

required
Source code in fediverse_pasture/runner/entry.py
def as_tabs(self, apps: List[str]) -> List[str]:
    """Renders the data for each application as tabbed markdown.

    If no data is present "no result" is shown.

    :param apps: List of applications to display"""
    lines = []
    for application_name in apps:
        lines.append(f"""=== "{application_name}"\n""")

        if application_name in self.entry:
            text = "```json\n"
            text += json.dumps(self.entry[application_name], indent=2)
            text += "\n```\n"
            lines += textwrap.indent(text, " " * 4).split("\n")
        else:
            lines.append("    no result")

        lines += [""]

    return [x + "\n" for x in lines]