Пример #1
0
 def __call__(self, *args):
     if self.body is None:
         self.body = args[0]  # dangerous!!!!!!
         return self
     else:
         return LazyFormat("{}({})", self.name,
                           ", ".join([tostring(e) for e in args]))
Пример #2
0
    def write_field(self, name, sinfo, parent=None):
        typ = sinfo.get("type")

        if typ == "struct":
            self.cw.write(name, sinfo, parent=parent)
            signature = make_signature(sinfo)
            self.cont.append((name, sinfo, signature))
            typename = self.typename_map[signature]
            self.m.stmt(LazyFormat('{} {}', name, typename))
        else:
            if "." in typ:
                self.im.import_(typ.rsplit(".", 1)[0])
            typ = to_type_struct_info(sinfo)
            if "/" in typ:
                typ = typ.rsplit("/", 1)[-1]
            self.m.stmt('{} {}'.format(name, typ))

        # append tag
        if "example" in sinfo:
            example = ' example:"{}"'.format(sinfo["example"])
        else:
            example = ''
        if is_omitempty_struct_info(sinfo, parent):
            omitempty = '.omitempty'
        else:
            omitempty = ''
        self.m.insert_after(' `json:"{name}{omitempty}"{example}`'.format(
            name=sinfo["jsonname"], omitempty=omitempty, example=example))
Пример #3
0
 def _emit_code(sinfo, name, m, parent=None):
     typ = sinfo.get("type")
     if typ == "struct":
         signature = make_signature(sinfo)
         cont.append((name, sinfo, signature))
         m.stmt(LazyFormat('{} {}', name, typename_map[signature]))
     else:
         m.stmt('{} {}'.format(name, to_type_struct_info(sinfo)))
     # append tag
     if is_omitempty_struct_info(sinfo, parent):
         m.insert_after('  `json:"{},omitempty"`'.format(sinfo["jsonname"]))
     else:
         m.insert_after('  `json:"{}"`'.format(sinfo["jsonname"]))
Пример #4
0
    def _emit_code(sinfo, name, m, parent=None):
        if "." in sinfo.get("type"):
            im.import_(sinfo.get("type").rsplit(".", 1)[0])

        if sinfo.get("type") == "struct":
            signature = make_signature(sinfo)
            cont.append((name, sinfo, signature))
            typ = typename_map[signature]
            typ.body.append(name)
        else:
            typ = to_type_struct_info(sinfo)
            if "/" in typ:
                typ = typ.rsplit("/", 1)[-1]
        m.stmt(LazyFormat('{} {}', name, typ))

        # append tag
        omitempty = ",omitempty" if is_omitempty_struct_info(sinfo, parent) else ""
        example = ' example:"{}"'.format(sinfo["example"]) if "example" in sinfo else ""
        m.insert_after('  `json:"{}{omitempty}"{example}`'.format(sinfo["jsonname"], omitempty=omitempty, example=example))
Пример #5
0
    def _emit_code(sinfo, name, m, parent=None):
        typ = sinfo.get("type")
        if parent is not None and typ == "struct":
            cm = m.cm_map[parent["jsonname"]]
            with cm.scope():
                cm.stmt(name)
                m.cm_map[sinfo["jsonname"]] = cm.submodule(newline=False)

        if typ == "struct":
            signature = make_signature(sinfo)
            cont.append((name, sinfo, signature))
            m.stmt(LazyFormat('{} {}', name, typename_map[signature]))
        else:
            m.stmt('{} {}'.format(name, to_type_struct_info(sinfo)))

        # append tag
        if is_omitempty_struct_info(sinfo, parent):
            m.insert_after('  `json:"{},omitempty"`'.format(sinfo["jsonname"]))
        else:
            m.insert_after('  `json:"{}"`'.format(sinfo["jsonname"]))
Пример #6
0
    def write_field(self, name, sinfo, parent=None):
        typ = sinfo.get("type")

        if typ == "struct":
            self.cw.write(name, sinfo, parent=parent)
            signature = make_signature(sinfo)
            self.cont.append((name, sinfo, signature))
            typename = self.typename_map[signature]
            self.m.stmt(LazyFormat('{} {}', name, typename))
        else:
            if "." in typ:
                self.im.import_(typ.rsplit(".", 1)[0])
            self.m.stmt('{} {}'.format(name, to_type_struct_info(sinfo)))

        # append tag
        if is_omitempty_struct_info(sinfo, parent):
            self.m.insert_after(' `json:"{},omitempty"`'.format(
                sinfo["jsonname"]))
        else:
            self.m.insert_after(' `json:"{}"`'.format(sinfo["jsonname"]))
Пример #7
0
 def __call__(self, *args, prefix=None):
     if prefix:
         LazyFormat("{prefix}.{fn}({args})", args=LazyArguments(args), fn=self.name, prefix=prefix)
     else:
         return LazyFormat("{fn}({args})", args=LazyArguments(args), fn=self.name)
Пример #8
0
 def _emit_struct(sinfo, name, parent=None):
     m.comment(
         LazyFormat("{name} : auto generated JSON container", name=name))
     with m.type_(name, to_type_struct_info(sinfo)):
         for name, subinfo in sorted(sinfo["children"].items()):
             _emit_code(subinfo, name, m, parent=sinfo)
Пример #9
0
 def __call__(self, *args):
     return LazyFormat("{}({})", self.tostring(),
                       ", ".join([tostring(e) for e in args]))
Пример #10
0
 def test_it_empty_args(self):
     from prestring import LazyFormat, LazyArgumentsAndKeywords
     args = LazyArgumentsAndKeywords(kwargs={"x": 1})
     target = LazyFormat("{fnname}({args})", fnname="foo", args=args)
     self.assertEqual(str(target), "foo(x=1)")