def evaluated_code(self): if self.eval_as_function: funcname = self.funcname or "_unnamed" code = indent(self.source) code = ("def %s():\n" % funcname + code + "\n_result = %s()" % funcname) else: code = "if True:\n" + indent(self.source) return code
def _dump_package_data_py(items, buf): print >> buf, "# -*- coding: utf-8 -*-\n" for i, (key, value) in enumerate(items): if key in ("description", "changelog") and len(value) > 40: # a block-comment style, triple-quoted string block_str = as_block_string(value) txt = "%s = \\\n%s" % (key, indent(block_str)) elif key == "config": # config is a scope attrs_txt = dict_to_attributes_code(dict(config=value)) txt = "with scope('config') as config:\n%s" % indent(attrs_txt) elif isinstance(value, SourceCode): # source code becomes a python function if key in ("commands", "pre_commands", "post_commands"): value = _commented_old_command_annotations(value) # don't indent code if already indented source = value.source if value.source[0] in (' ', '\t') else indent(value.source) txt = "def %s():\n%s" % (key, source) elif isinstance(value, list) and len(value) > 1: # nice formatting for lists lines = ["%s = [" % key] for j, entry in enumerate(value): entry_txt = pformat(entry) entry_lines = entry_txt.split('\n') for k, line in enumerate(entry_lines): if j < len(value) - 1 and k == len(entry_lines) - 1: line = line + "," lines.append(" " + line) lines.append("]") txt = '\n'.join(lines) else: # default serialisation value_txt = pformat(value) if '\n' in value_txt: txt = "%s = \\\n%s" % (key, indent(value_txt)) else: txt = "%s = %s" % (key, value_txt) print >> buf, txt if i < len(items) - 1: print >> buf, ''
def _dump_package_data_py(items, buf): print("# -*- coding: utf-8 -*-\n", file=buf) for i, (key, value) in enumerate(items): if key in ("description", "changelog") and len(value) > 40: # a block-comment style, triple-quoted string block_str = as_block_string(value) txt = "%s = \\\n%s" % (key, indent(block_str)) elif key == "config": # config is a scope attrs_txt = dict_to_attributes_code(dict(config=value)) txt = "with scope('config') as config:\n%s" % indent(attrs_txt) elif isinstance(value, SourceCode): # source code becomes a python function if key in ("commands", "pre_commands", "post_commands"): value = _commented_old_command_annotations(value) txt = value.to_text(funcname=key) elif isinstance(value, list) and len(value) > 1: # nice formatting for lists lines = ["%s = [" % key] for j, entry in enumerate(value): entry_txt = pformat(entry) entry_lines = entry_txt.split('\n') for k, line in enumerate(entry_lines): if j < len(value) - 1 and k == len(entry_lines) - 1: line = line + "," lines.append(" " + line) lines.append("]") txt = '\n'.join(lines) else: # default serialisation value_txt = pformat(value) if '\n' in value_txt: txt = "%s = \\\n%s" % (key, indent(value_txt)) else: txt = "%s = %s" % (key, value_txt) print(txt, file=buf) if i < len(items) - 1: print('', file=buf)
def _dump_package_data_py(items, buf): for i, (key, value) in enumerate(items): if key in ("description", "changelog") and len(value) > 40: # description is a triple-quoted string quoted_str = '"""\n%s\n"""' % value txt = "%s = \\\n%s" % (key, indent(quoted_str)) elif key == "config": # config is a scope attrs_txt = dict_to_attributes_code(dict(config=value)) txt = "with scope('config') as config:\n%s" % indent(attrs_txt) elif isinstance(value, SourceCode): # source code becomes a python function if key in ("commands", "pre_commands", "post_commands"): value = _commented_old_command_annotations(value) # don't indent code if already indented source = value.source if value.source[0] in (' ', '\t') else indent(value.source) txt = "def %s():\n%s" % (key, source) elif isinstance(value, list) and len(value) > 1: # nice formatting for lists lines = ["%s = [" % key] for j, entry in enumerate(value): entry_txt = pformat(entry) entry_lines = entry_txt.split('\n') for k, line in enumerate(entry_lines): if j < len(value) - 1 and k == len(entry_lines) - 1: line = line + "," lines.append(" " + line) lines.append("]") txt = '\n'.join(lines) else: # default serialisation value_txt = pformat(value) if '\n' in value_txt: txt = "%s = \\\n%s" % (key, indent(value_txt)) else: txt = "%s = %s" % (key, value_txt) print >> buf, txt if i < len(items) - 1: print >> buf, ''
def to_text(self, funcname): # don't indent code if already indented if self.source[0] in (' ', '\t'): source = self.source else: source = indent(self.source) txt = "def %s():\n%s" % (funcname, source) for entry in self.decorators: nargs_str = ", ".join(map(repr, entry.get("nargs", []))) name_str = entry.get("name") sig = "@%s(%s)" % (name_str, nargs_str) txt = sig + '\n' + txt return txt