Verified Commit adf3cdf4 authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Add setup.py and entrypoints

parent 194452a3
Loading
Loading
Loading
Loading

rpcc/__init__.py

0 → 100644
+0 −0

Empty file added.

rpcc/__main__.py

0 → 100644
+59 −0
Original line number Diff line number Diff line
import argparse
import sys

from rpcc.version import __version__ as rpcc_version
from rpcc.parser import Parser
from pathlib import Path


class PrintVersion(argparse.Action):
    def __init__(self, option_strings, dest, const=None, default=None,
                 required=False, help=None, metavar=None):
        super().__init__(option_strings=option_strings,
                         dest=dest,
                         nargs=0,
                         const=const,
                         default=default,
                         required=required,
                         help=help)

    def __call__(self, parser, namespace, values, option_string=None):
        setattr(namespace, self.dest, self.const)
        print(f"rpcc {rpcc_version}")
        sys.exit(0)


def main(args=None):
    """"The main routine"""

    if args is None:
        args = sys.argv[1:]

    parser = argparse.ArgumentParser(prog="rpcc",
                                     description="Parse RPC_PROTO_FILE and generate C++ output based on the options "
                                                 "given.")
    parser.add_argument(
        "rpc_proto_file",
        type=Path,
        metavar="RPC_PROTO_FILE",
        help="The file containing the specifications for the RPCs."
    )

    parser.add_argument(
        "--version",
        "-V",
        action=PrintVersion,
        help="Display rpcc version information."
    )
    args = parser.parse_args()

    # try:
    ast = Parser(args.rpc_proto_file).parse()
    print(ast.pretty())

    # except Exception:
    #    return 1


if __name__ == "__main__":
    sys.exit(main())

rpcc/parser.py

0 → 100644
+49 −0
Original line number Diff line number Diff line
from lark import Lark
from loguru import logger

GRAMMAR = r"""
    start           : ( rpc )+
                    | empty_statement
    rpc             : "rpc" rpc_name rpc_body
    rpc_name        : ident
    rpc_body        : "{" ( rpc_args | rpc_return | empty_statement )+ "};"
    rpc_args        : "arguments" rpc_args_body
    rpc_args_body   : "{" ( arg | empty_statement )+ "};"

    arg             : type ident ";"

    type            : "double"         -> double
                    | "float"          -> float
                    | "int32"          -> int32
                    | "uint32"         -> uint32
                    | "string"         -> string
                    | "exposed_buffer" -> exposed_buffer
    rpc_return      : "returns" rpc_return_body
    rpc_return_body : "{" ( arg | empty_statement ) "};"
    field_name      : ident
    ?ident          : CNAME
    empty_statement : ";"*
    COMMENT         : /#+[^\n]*/

    %import common.CNAME
    %import common.WS
    %ignore WS
    %ignore COMMENT
"""


class Parser:

    def __init__(self, input_file: str):
        self.input_file = input_file
        self.parser = Lark(GRAMMAR, parser='lalr')

    def parse(self):
        try:
            file = open(self.input_file, "r", encoding="utf-8")
        except (FileNotFoundError, EnvironmentError) as err:
            logger.error(f"Error parsing file: {err}")
            raise
        else:
            with file:
                return self.parser.parse(file.read())

rpcc/version.py

0 → 100644
+1 −0
Original line number Diff line number Diff line
__version__ = "0.1.0"

setup.py

0 → 100644
+19 −0
Original line number Diff line number Diff line
from setuptools import setup

# import __version__ from version.py into this namespace
with open("rpcc/version.py") as f:
    exec(f.read())

setup(
    name="rpcc",
    version=__version__,
    packages=["rpcc"],
    author="Alberto Miranda",
    author_email="alberto.miranda@bsc.es",
    license="GPLv3",
    entry_points={
        "console_scripts": [
            "rpcc = rpcc.__main__:main"
        ]
    },
)