Exemple #1
0
 def writepy(self, w: FileWriter) -> None:
     left = self._target.getPyExpr()[0]
     if self._declare and self._declaretype:
         left += ': ' + self._target.getPanType().getQuotedPyType()
     if self._expr is None:
         w.line0(left)
     else:
         w.line0(f'{left} = {self._expr.getPyExpr()[0]}')
Exemple #2
0
 def writets(self, w: FileWriter) -> None:
     assert self._ctor is None, "Custom Exception constructor not allowed for TS"
     if self._msg is None:
         assert self._expr is not None
         line = f"throw new Error({self._expr.getPyExpr()[0]})"
     else:
         line = f"throw new Error({self._msg!r})"
     w.line0(line)
Exemple #3
0
 def writepy(self, w: FileWriter) -> None:
     w.line0(
         f'for {self._assign.getPyExpr()[0]} in {self._expr.getPyExpr()[0]}:'
     )
     for stmt in self._statements:
         stmt.writepy(w.with_more_indent())
     # always put a blank line after a for loop
     w.blank()
Exemple #4
0
    def writepy(self, w: FileWriter) -> None:
        w.line0('try:')
        for stmt in self._statements:
            stmt.writepy(w.with_more_indent())

        # catch blocks
        for cb in self._catchblocks:
            # write out catch blocks without increasing indent
            cb.writepy(w)
Exemple #5
0
 def writephp(self, w: FileWriter) -> None:
     ctor = self._ctor or '\\Exception'
     if self._msg is None:
         assert self._expr is not None
         line = f"throw new {ctor}({self._expr.getPHPExpr()[0]});"
     else:
         # TODO: don't import this here
         from paradox.expressions import _phpstr
         line = f"throw new {ctor}({_phpstr(self._msg)});"
     w.line0(line)
Exemple #6
0
 def writepy(self, w: FileWriter) -> None:
     ctor = 'Exception'
     if self._ctor is not None:
         ctor = self._ctor
     if self._msg is None:
         assert self._expr is not None
         line = f"raise {ctor}({self._expr.getPyExpr()[0]})"
     else:
         line = f"raise {ctor}({self._msg!r})"
     w.line0(line)
Exemple #7
0
 def writepy(self, w: FileWriter) -> None:
     intro = 'except'
     if self.catchexpr:
         intro += ' ' + self.catchexpr
         if self.catchvar:
             intro += ' as ' + self.catchvar
     intro += ':'
     w.line0(intro)
     for stmt in self._statements:
         stmt.writepy(w.with_more_indent())
Exemple #8
0
    def writephp(self, w: FileWriter) -> None:
        phptypes = self._target.getPanType().getPHPTypes()
        if self._declare and self._declaretype:
            w.line0(f"/** @var {phptypes[1]} */")

        left = self._target.getPHPExpr()[0]

        # you can't just make a variable declaration in PHP
        assert self._expr is not None
        w.line0(f'{left} = {self._expr.getPHPExpr()[0]};')
Exemple #9
0
    def writets(self, w: FileWriter) -> None:
        left = self._target.getTSExpr()[0]
        if self._declare:
            left = f"let {left}"
            if self._declaretype:
                left += ": " + self._target.getPanType().getTSType()[0]

        if self._expr is None:
            w.line0(f'{left};')
        else:
            w.line0(f'{left} = {self._expr.getTSExpr()[0]};')
Exemple #10
0
 def writepy(self, w: FileWriter) -> None:
     # XXX: remember that for Python you almost certainly don't want a bare "except:" as that
     # would catch process signals and such.
     if self._pyclass:
         intro = "except " + self._pyclass
     else:
         intro = "except Exception"
     if self._var:
         intro += ' as ' + self._var.getPyExpr()[0]
     intro += ":"
     w.line0(intro)
     for stmt in self._statements:
         stmt.writepy(w.with_more_indent())
Exemple #11
0
    def writephp(self, w: FileWriter) -> None:
        w.line0(f'if ({self._expr.getPHPExpr()[0]}) {{')
        for stmt in self._statements:
            stmt.writephp(w.with_more_indent())
        w.line0('}')

        # always put a blank line after a conditional
        w.blank()
Exemple #12
0
 def writets(self, w: FileWriter) -> None:
     w.line0(
         f'for (let {self._assign.getTSExpr()[0]} of {self._expr.getTSExpr()[0]}) {{'
     )
     for stmt in self._statements:
         stmt.writets(w.with_more_indent())
     w.line0(f'}}')
     # always put a blank line after a for loop
     w.blank()
Exemple #13
0
def test_generate_interface_spec() -> None:
    from paradox.generate.files import FileWriter
    from paradox.generate.statements import InterfaceSpec
    from paradox.typing import CrossBool

    spec = InterfaceSpec('Window')
    spec.addProperty('closed', CrossBool())

    # Note that InterfaceSpec doesn't yet support methods

    # FIXME: we generate the interface but don't actually check its contents
    spec.writets(FileWriter(io.StringIO(), '  '))

    # this will probably never be implemented
    with pytest.raises(NotImplementedError):
        spec.writepy(FileWriter(io.StringIO(), '  '))

    # this is not implemented yet
    with pytest.raises(NotImplementedError):
        spec.writephp(FileWriter(io.StringIO(), '  '))
Exemple #14
0
    def writephp(self, w: FileWriter) -> None:
        # TODO: don't import this here
        from paradox.expressions import _phpstr

        phptype = self._type.getPHPTypes()[0]
        if phptype:
            w.line0(f'/** @var {phptype} */')

        inner = ', '.join([
            _phpstr(k) + ' => $' + k for k, allowomit in self._keys
            if not allowomit
        ])

        varstr = self._var.getPHPExpr()[0]

        w.line0(f'{varstr} = [{inner}];')

        # now do the omittable args
        for k, allowomit in self._keys:
            raise Exception("omittable args aren't supported by PHP")
Exemple #15
0
    def writets(self, w: FileWriter) -> None:
        inner = ', '.join(
            [f'{k!r}: {k}' for k, allowomit in self._keys if not allowomit])

        varstr = self._var.getTSExpr()[0]

        w.line0(f'let {varstr}: {self._type.getTSType()[0]} = {{{inner}}};')

        # now do the omittable args
        for k, allowomit in self._keys:
            if allowomit:
                w.line0(f'if (typeof {k} !== "undefined") {{')
                w.line1(f'{varstr}[{k!r}] = {k};')
                w.line0(f'}}')
Exemple #16
0
    def writepy(self, w: FileWriter) -> None:
        inner = ', '.join(
            [f'{k!r}: {k}' for k, allowomit in self._keys if not allowomit])

        varstr = self._var.getPyExpr()[0]

        w.line0(f'{varstr}: {self._type.getQuotedPyType()} = {{{inner}}}')

        # now do the omittable args
        for k, allowomit in self._keys:
            if allowomit:
                # FIXME: this isn't how we want to do omitted args - we should be doing ellipsis
                expr = pannotomit(PanVar(k, None))
                w.line0(f'if {expr.getPyExpr()[0]}:')
                w.line1(f'{varstr}[{k!r}] = {k}')
Exemple #17
0
 def writets(self, w: FileWriter) -> None:
     w.line0(self._expr.getTSExpr()[0] + ";")
Exemple #18
0
 def writephp(self, w: FileWriter) -> None:
     w.line0(self._expr.getPHPExpr()[0] + ';')
Exemple #19
0
 def writepy(self, w: FileWriter) -> None:
     w.line0(self._expr.getPyExpr()[0])
Exemple #20
0
 def writephp(self, w: FileWriter) -> None:
     if isinstance(self._expr, PanOmit):
         w.line0('return;')
     else:
         w.line0('return ' + self._expr.getPHPExpr()[0] + ';')
Exemple #21
0
 def writets(self, w: FileWriter) -> None:
     for stmt in self._lines:
         w.line0(stmt)
Exemple #22
0
 def writepy(self, w: FileWriter) -> None:
     if self._python is None:
         raise Exception("Not implemented in Python")
     w.line0(self._python)
Exemple #23
0
 def writets(self, w: FileWriter) -> None:
     if self._typescript is None:
         raise Exception("Not implemented in TS")
     w.line0(self._typescript)
Exemple #24
0
    def writets(self, w: FileWriter) -> None:
        w.line0(f"try {{")
        for stmt in self._statements:
            stmt.writets(w.with_more_indent())

        assert len(self._catchblocks
                   ), "TryCatchBlock must have at least one Catch block"

        # all catch blocks must be a CatchBlock2 and have the same var name
        catchvar = None
        catchspecific: List[CatchBlock2] = []
        catchall: Optional[CatchBlock2] = None
        for cb in self._catchblocks:
            assert isinstance(cb, CatchBlock2)
            assert cb._var is not None

            # TODO: get rid of this dirty hack
            if catchvar is None:
                catchvar = cb._var._name
            else:
                assert cb._var._name == catchvar

            if cb._tsclass:
                catchspecific.append(cb)
            else:
                catchall = cb
        assert catchvar is not None

        w.line0(f"}} catch ({catchvar}) {{")
        if catchspecific:
            for cb in catchspecific:
                assert isinstance(cb, CatchBlock2)
                w.line1(f"if ({catchvar} instanceof {cb._tsclass}) {{")
                for stmt in cb._statements:
                    stmt.writets(w.with_more_indent().with_more_indent())
            if catchall:
                w.line1(f"}} else {{")
                for stmt in catchall._statements:
                    stmt.writets(w.with_more_indent().with_more_indent())
            # close off the last catch block
            w.line1(f"}}")

            if not catchall:
                # if there was no catch block added to handle any exception types, we need to
                # rethrow the exception
                w.line1(f"throw {catchvar};")
        else:
            assert catchall is not None
            for stmt in catchall._statements:
                stmt.writets(w.with_more_indent())

        w.line0(f"}}")
Exemple #25
0
 def writephp(self, w: FileWriter) -> None:
     if self._php is None:
         raise Exception("Not implemented in PHP")
     w.line0(self._php)
Exemple #26
0
 def writephp(self, w: FileWriter) -> None:
     w.blank()
Exemple #27
0
    def writepy(self, w: FileWriter) -> None:
        assert not self._isasync, "async FunctionSpec not yet supported in Python"

        # first write out overloads
        for overload in self._overloads:
            overload.writepy(w)

        # blank line
        w.blank()

        # decorators
        if self._isstaticmethod:
            w.line0('@staticmethod')

        for dec in self._decorators_py:
            w.line0(dec)
        if self._isabstract:
            w.line0('@abc.abstractmethod')

        # header
        w.line0(f'def {self._name}(')

        if self._ismethod and not self._isstaticmethod:
            w.line1('self,')

        for argname, crosstype, argdefault in self._pargs:
            argstr = argname + ': ' + crosstype.getQuotedPyType()
            if argdefault is not None:
                argstr += ' = ' + argdefault.getPyExpr()[0]
            w.line1(argstr + ',')
        if len(self._kwargs):
            # mark start of kwargs
            w.line1('*,')
        for argname, argtype, argdefault in self._kwargs:
            argstr = argname
            argstr += ': ' + argtype.getQuotedPyType()
            if argdefault is not None:
                argstr += ' = ' + argdefault.getPyExpr()[0]
            w.line1(argstr + ',')

        if self._rettype is None:
            w.line0(f') -> None:')
        else:
            w.line0(f') -> {self._rettype.getQuotedPyType()}:')

        if self._docstring:
            w.line1('"""')
            for docline in self._docstring:
                w.line1(docline.strip())
            w.line1('"""')
            havebody = True

        havebody = False

        for stmt in self._statements:
            stmt.writepy(w.with_more_indent())
            havebody = True

        if not havebody:
            w.line1('pass')
Exemple #28
0
 def writephp(self, w: FileWriter) -> None:
     list_, prec = self._list.getPHPExpr()
     if prec.value >= PHPPrecedence.Arrow.value:
         list_ = '(' + list_ + ')'
     w.line0(list_ + '[] = ' + self._value.getPHPExpr()[0] + ';')
Exemple #29
0
 def writephp(self, w: FileWriter) -> None:
     w.line0('// ' + self._text)
Exemple #30
0
 def writepy(self, w: FileWriter) -> None:
     w.line0(f'if {self._expr.getPyExpr()[0]}:')
     for stmt in self._statements:
         stmt.writepy(w.with_more_indent())
     # always put a blank line after a conditional
     w.blank()