Beispiel #1
0
def _i2c(this: str, shed: RST2PyI) -> str:
    shed.class_from_file(old=this, )
    shed.def_(
        old=r'.. class:: I2C(id, *, scl, sda, freq=400000)',
        new=[
            'def __init__(self, id: int, /, *, freq: int = 400_000)',
            'def __init__(self, id: int, /, *, scl: Pin, sda: Pin, freq: int = 400_000)',
        ],
    )
    shed.def_(
        old=r'.. method:: I2C.init(scl, sda, *, freq=400000)',
        new=[
            'def init(self, *, freq: int = 400_000) -> None',
            'def init(self, *, scl: Pin, sda: Pin, freq: int = 400_000) -> None',
        ],
    )
    shed.def_(
        old=r'.. method:: I2C.deinit()',
        new='def deinit(self) -> None',
    )
    primitive_docs_start = 'Primitive I2C operations'
    shed.def_(old=r'.. method:: I2C.scan()',
              new='def scan(self) -> List[int]',
              end=primitive_docs_start)
    primitive_docs = shed.extra_docs()
    shed.def_(old=r'.. method:: I2C.start()',
              new='def start(self) -> None',
              extra_docs=primitive_docs)
    shed.def_(old=r'.. method:: I2C.stop()',
              new='def stop(self) -> None',
              extra_docs=primitive_docs)
    shed.def_(
        old=r'.. method:: I2C.readinto(buf, nack=True, /)',
        new=
        'def readinto(self, buf: _AnyWritableBuf, nack: bool = True, /) -> None',
        extra_docs=primitive_docs)
    shed.def_(
        old=r'.. method:: I2C.write(buf)',
        new='def write(self, buf: _AnyReadableBuf, /) -> int',
        extra_docs=primitive_docs,
        end='Standard bus operations',
    )
    standard_docs = shed.extra_docs()
    shed.def_(
        old=r'.. method:: I2C.readfrom(addr, nbytes, stop=True, /)',
        new=
        'def readfrom(self, addr: int, nbytes: int, stop: bool = True, /) -> bytes',
        extra_docs=standard_docs)
    shed.def_(
        old=r'.. method:: I2C.readfrom_into(addr, buf, stop=True, /)',
        new=
        'def readfrom_into(self, addr: int, buf: _AnyWritableBuf, stop: bool = True, /) -> None',
        extra_docs=standard_docs)
    shed.def_(
        old=r'.. method:: I2C.writeto(addr, buf, stop=True, /)',
        new=
        'def writeto(self, addr: int, buf: _AnyWritableBuf, stop: bool = True, /) -> int',
        extra_docs=standard_docs)
    shed.def_(
        old=r'.. method:: I2C.writevto(addr, vector, stop=True, /)',
        new='''
def writevto(
   self, 
   addr: int, 
   vector: Sequence[_AnyReadableBuf], 
   stop: bool = True, 
   /
) -> int
''',
        extra_docs=standard_docs,
        end='Memory operations',
    )
    memory_docs = shed.extra_docs()
    shed.def_(
        old=
        r'.. method:: I2C.readfrom_mem(addr, memaddr, nbytes, *, addrsize=8)',
        new=
        'def readfrom_mem(self, addr: int, memaddr: int, nbytes: int, /, *, addrsize: int = 8) -> bytes',
        extra_docs=memory_docs)
    shed.def_(
        old=
        r'.. method:: I2C.readfrom_mem_into(addr, memaddr, buf, *, addrsize=8)',
        new='''
def readfrom_mem_into(
   self, 
   addr: int, 
   memaddr: int, 
   buf: _AnyWritableBuf, 
   /, 
   *, 
   addrsize: int = 8
) -> None
''',
        extra_docs=memory_docs)
    rtc = 'machine.RTC.rst'
    shed.def_(
        old=r'.. method:: I2C.writeto_mem(addr, memaddr, buf, *, addrsize=8',
        new=
        'def writeto_mem(self, addr: int, memaddr: int, buf: _AnyReadableBuf, /, *, addrsize: int = 8) -> None',
        extra_docs=memory_docs,
        end=rtc)
    return rtc
def uctypes(shed: RST2PyI) -> None:
    shed.module(
        name='uctypes',
        old='access binary data in a structured way',
        post_doc=f'''
from typing import Tuple, Union, TypeVar, Final

from uarray import array

{repdefs.AnyReadableBuf}
''',
        end=R'Module contents'
    )
    rst_name = R'.. class:: struct(addr, descriptor, layout_type=NATIVE, /)'
    shed.class_(
        pre_str='# noinspection PyPep8Naming',
        name='struct',
        end=rst_name,
    )
    shed.pyi.imports_vars_defs += [
        '_scalar_property = int',
        '_recursive_property = Tuple[int, "_property"]',
        '_array_property = Tuple[int, int]',
        '_array_of_aggregate_property = Tuple[int, int, "_property"]',
        '_pointer_to_a_primitive_property = Tuple[int, int]',
        '_pointer_to_an_aggregate_property = Tuple[int, "_property"]',
        '_bitfield_property = int',
        ('_property = Union['
         '_scalar_property, '
         '_recursive_property, '
         '_array_property, '
         '_array_of_aggregate_property, '
         '_pointer_to_a_primitive_property, '
         '_pointer_to_an_aggregate_property, '
         '_bitfield_property'
         ']'),
        '_descriptor = Tuple[str, _property]',
    ]
    shed.def_(
        old=rst_name,
        new='def __init__(self, addr: int, descriptor: _descriptor, layout_type: int = NATIVE, /)',
    )
    shed.vars(old='.. data:: LITTLE_ENDIAN', class_var=None)
    shed.vars(old='.. data:: BIG_ENDIAN', class_var=None)
    shed.vars(old='.. data:: NATIVE', class_var=None)
    shed.def_(
        pre_str='# noinspection PyShadowingNames',
        old=r'.. function:: sizeof(struct, layout_type=NATIVE, /)',
        new='def sizeof(struct: Union["struct", _descriptor], layout_type: int = NATIVE, /) -> int',
        indent=0,
    )
    shed.def_(
        old=r'.. function:: addressof(obj)',
        new='def addressof(obj: _AnyReadableBuf, /) -> int',
        indent=0,
    )
    shed.def_(
        old=r'.. function:: bytes_at(addr, size)',
        new='def bytes_at(addr: int, size: int, /) -> bytes',
        indent=0,
    )
    shed.def_(
        old=r'.. function:: bytearray_at(addr, size)',
        new='def bytearray_at(addr: int, size: int, /) -> bytearray',
        indent=0,
    )
    shed.vars(
        old=[
            '.. data:: UINT8',
            'INT8',
            'UINT16',
            'INT16',
            'UINT32',
            'INT32',
            'UINT64',
            'INT64',
        ],
        class_var=None
    )
    shed.vars(
        old=[
            '.. data:: FLOAT32',
            'FLOAT64',
        ],
        class_var=None
    )
    shed.vars(old='.. data:: VOID', class_var=None)
    shed.vars(
        old=[
            '.. data:: PTR',
            'ARRAY',
        ],
        class_var=None,
        end='Structure descriptors and instantiating structure objects'
    )
    shed.extra_docs(end=None)
    shed.write()
Beispiel #3
0
def _rtc(_: str, shed: RST2PyI) -> str:
    shed.pyi.append('RTC: Type[pyb.RTC] = pyb.RTC\n')
    nxt = 'machine.Timer.rst'
    shed.consume(end=nxt)
    return nxt
def ucryptolib(shed: RST2PyI) -> None:
    shed.module(name='ucryptolib',
                old='cryptographic ciphers',
                post_doc=f'''
from typing import overload, TypeVar

from uarray import array

{repdefs.AnyReadableBuf}
{repdefs.AnyWritableBuf}
''',
                end=R'Classes')
    shed.consume(end='.. class:: aes')
    rst_name = R'.. classmethod:: __init__(key, mode, [IV])'
    shed.class_(
        pre_str='# noinspection PyPep8Naming',
        name='aes',
        end=rst_name,
    )
    shed.def_(
        old=rst_name,
        new=[
            'def __init__(self, key: _AnyReadableBuf, mode: int, /)',
            'def __init__(self, key: _AnyReadableBuf, mode: int, IV: _AnyReadableBuf, /)'
        ],
    )
    shed.def_(
        old='.. method:: encrypt(in_buf, [out_buf])',
        new=[
            'def encrypt(self, in_buf: _AnyReadableBuf, /) -> bytes',
            'def encrypt(self, in_buf: _AnyReadableBuf, out_buf: _AnyWritableBuf, /) -> None'
        ],
    )
    shed.def_(
        old='.. method:: decrypt(in_buf, [out_buf])',
        new=[
            'def decrypt(self, in_buf: _AnyReadableBuf, /) -> bytes',
            'def decrypt(self, in_buf: _AnyReadableBuf, out_buf: _AnyWritableBuf, /) -> None'
        ],
    )
    shed.write()