Module margo_parser.api

The main API for Margo Parser

Expand source code
"""The main API for Margo Parser"""

from .classes.MargoBlock import MargoBlock
from .classes.MargoStatement import MargoStatement, MargoStatementTypes
from .classes.MargoDirective import MargoDirective
from .classes.MargoAssignment import MargoAssignment
from .classes.MargoPythonCellPreambleBlock import MargoPythonCellPreambleBlock
from .classes.MargoMarkdownCellPrambleBlock import (
    MargoMarkdownCellPreambleBlock,
)
from ..exceptions import MargoParseException

__all__ = [
    'MargoBlock',
    'MargoStatement',
    'MargoStatementTypes',
    'MargoDirective',
    'MargoAssignment',
    'MargoPythonCellPreambleBlock',
    'MargoMarkdownCellPreambleBlock',
    'MargoParseException',
]

Sub-modules

margo_parser.api.classes
margo_parser.api.utils

Classes

class MargoAssignment (name: str, value)

A Margo statement

:param statement_type: MargoStatementTypes.DECLARATION or MargoStatementTypes.DIRECTIVE :param name: the name of the statement :param value: the value of the statement @raises MargoLangException if parameters are invalid

Expand source code
class MargoAssignment(MargoStatement):
    def __init__(self, name: str, value):
        super().__init__("DECLARATION", name, value=value)

    @property
    def type(self) -> str:
        return "DECLARATION"

Ancestors

Inherited members

class MargoBlock (source: str)

A collection of Margo statements.

Parses source immediately, raising an exception if parsing fails. :param source: The source code string :raises: MargoParseException if the source string cannot be parsed :raises: MargoLangException if there's some other error

Expand source code
class MargoBlock:
    """A collection of Margo statements."""

    def __init__(self, source: str):
        """Parses source immediately, raising an exception if parsing
        fails.
        :param source: The source code string
        :raises: MargoParseException if the source string cannot be parsed
        :raises: MargoLangException if there's some other error"""

        # This is what raises the MargoParseException if it fails
        parsed = tokenize(source)
        self.__statements = []
        # TODO - Test statement for valid structure
        for statement in parsed["BODY"]:
            # statement_type = statement["TYPE"]
            statement_name = statement["NAME"]
            if "VALUE" in statement:
                statement_value = statement["VALUE"]
                statement = MargoAssignment(statement_name, statement_value)
            else:
                statement = MargoDirective(statement_name)

            self.__statements.append(statement)

    @property
    def statements(self) -> List[MargoStatement]:
        """List of Margo statements"""
        return self.__statements

Subclasses

Instance variables

var statements : List[MargoStatement]

List of Margo statements

Expand source code
@property
def statements(self) -> List[MargoStatement]:
    """List of Margo statements"""
    return self.__statements
class MargoDirective (name: str)

A Margo statement

:param statement_type: MargoStatementTypes.DECLARATION or MargoStatementTypes.DIRECTIVE :param name: the name of the statement :param value: the value of the statement @raises MargoLangException if parameters are invalid

Expand source code
class MargoDirective(MargoStatement):
    def __init__(self, name: str):
        super().__init__("DIRECTIVE", name)

    @property
    def type(self) -> str:
        return "DIRECTIVE"

    @property
    def value(self) -> any:
        return None

Ancestors

Inherited members

class MargoMarkdownCellPreambleBlock (source: str)

A helper to process just the Margo preamble (if any) of a Markdown cell. Instead of using MargoBlock directly, which requires the source string to only be valid Margo, this will extract the preamble from the cell contents.

:param source: The entire source of a Markdown cell

Expand source code
class MargoMarkdownCellPreambleBlock(MargoBlock):
    """A helper to process just the Margo preamble (if any) of a Markdown
    cell. Instead of using MargoBlock directly, which requires the source
    string to only be valid Margo, this will extract the preamble from the
    cell contents.
    """

    def __init__(self, source: str):
        """
        :param source: The entire source of a Markdown cell
        """

        preamble_source = get_markdown_preamble_source(source)
        super().__init__(preamble_source)

Ancestors

Inherited members

class MargoParseException (*args, **kwargs)

Parsing source code failed

Expand source code
class MargoParseException(Exception):
    """Parsing source code failed"""

    pass

Ancestors

  • builtins.Exception
  • builtins.BaseException
class MargoPythonCellPreambleBlock (source: str)

A helper to process just the Margo preamble (if any) of a Python cell. Instead of using MargoBlock directly, which requires the source string to only be valid Margo, this will extract the preamble from the cell contents.

:param source: The entire source of a Python cell

Expand source code
class MargoPythonCellPreambleBlock(MargoBlock):
    """A helper to process just the Margo preamble (if any) of a Python cell.
    Instead of using MargoBlock directly, which requires the source string to
    only be valid Margo, this will extract the preamble from the cell contents.
    """

    def __init__(self, source: str):
        """
        :param source: The entire source of a Python cell
        """

        preamble_source = get_preamble_source(source)
        super().__init__(preamble_source)

Ancestors

Inherited members

class MargoStatement (statement_type: str, name: str, value=None)

A Margo statement

:param statement_type: MargoStatementTypes.DECLARATION or MargoStatementTypes.DIRECTIVE :param name: the name of the statement :param value: the value of the statement @raises MargoLangException if parameters are invalid

Expand source code
class MargoStatement(ABC):

    """A Margo statement"""

    def __init__(self, statement_type: str, name: str, value=None):
        """
        :param statement_type: MargoStatementTypes.DECLARATION or
            MargoStatementTypes.DIRECTIVE
        :param name: the name of the statement
        :param value: the value of the statement
        @raises MargoLangException if parameters are invalid
        """

        if not MargoStatementTypes.is_valid_type(statement_type):
            raise MargoLangException(
                "Invalid Margo statement type: " + statement_type
            )
        self.__type = statement_type
        if type(name) != str:
            raise MargoLangException("Margo statement name type must be str")
        self.__name = name
        # Value can be anything, so don't validate
        if self.__type == MargoStatementTypes.DIRECTIVE and value is not None:
            raise MargoLangException("Cannot create a directive with value")
        self.__value = value

    @property
    def name(self) -> str:
        """The name of the statement"""
        return self.__name

    @property
    def type(self) -> str:
        """'DIRECTIVE' or 'DECLARATION'"""
        return self.__type

    @property
    def value(self) -> any:
        """Any value. Should be None for directives"""
        return self.__value

Ancestors

  • abc.ABC

Subclasses

Instance variables

var name : str

The name of the statement

Expand source code
@property
def name(self) -> str:
    """The name of the statement"""
    return self.__name
var type : str

'DIRECTIVE' or 'DECLARATION'

Expand source code
@property
def type(self) -> str:
    """'DIRECTIVE' or 'DECLARATION'"""
    return self.__type
var value

Any value. Should be None for directives

Expand source code
@property
def value(self) -> any:
    """Any value. Should be None for directives"""
    return self.__value
class MargoStatementTypes

Supported types of Margo statements

Expand source code
class MargoStatementTypes:

    """Supported types of Margo statements"""

    DECLARATION = "DECLARATION"
    DIRECTIVE = "DIRECTIVE"

    VALID_TYPES = [DECLARATION, DIRECTIVE]

    @staticmethod
    def is_valid_type(statement_type: str) -> bool:
        """Determine if a string is a valid margo statement type"""
        return statement_type in MargoStatementTypes.VALID_TYPES

Class variables

var DECLARATION
var DIRECTIVE
var VALID_TYPES

Static methods

def is_valid_type(statement_type: str) ‑> bool

Determine if a string is a valid margo statement type

Expand source code
@staticmethod
def is_valid_type(statement_type: str) -> bool:
    """Determine if a string is a valid margo statement type"""
    return statement_type in MargoStatementTypes.VALID_TYPES