Python import code beautify
This commit is contained in:
parent
5562deec62
commit
44dd28950c
1 changed files with 60 additions and 77 deletions
|
|
@ -1034,7 +1034,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------
|
/* ------------------------------------------------------------
|
||||||
* tail = subpkg_tail(base, other)
|
* subpkg_tail()
|
||||||
*
|
*
|
||||||
* Return the name of 'other' package relative to 'base'.
|
* Return the name of 'other' package relative to 'base'.
|
||||||
*
|
*
|
||||||
|
|
@ -1049,8 +1049,8 @@ public:
|
||||||
*
|
*
|
||||||
* Examples:
|
* Examples:
|
||||||
*
|
*
|
||||||
* # base other tail
|
* # base other tail
|
||||||
* -- ---- ----- ----
|
* -- ---- ----- ----
|
||||||
* 1 "Foo" "Foo.Bar" -> "Bar"
|
* 1 "Foo" "Foo.Bar" -> "Bar"
|
||||||
* 2 "Foo" "Foo." -> ""
|
* 2 "Foo" "Foo." -> ""
|
||||||
* 3 "Foo" "FooB.ar" -> NULL
|
* 3 "Foo" "FooB.ar" -> NULL
|
||||||
|
|
@ -1061,8 +1061,8 @@ public:
|
||||||
* NOTE: the example #2 is actually a syntax error (at input). I believe
|
* NOTE: the example #2 is actually a syntax error (at input). I believe
|
||||||
* swig parser prevents us from this case happening here.
|
* swig parser prevents us from this case happening here.
|
||||||
* ------------------------------------------------------------ */
|
* ------------------------------------------------------------ */
|
||||||
static String* subpkg_tail(const String* base, const String* other)
|
|
||||||
{
|
static String *subpkg_tail(const String *base, const String *other) {
|
||||||
int baselen = Len(base);
|
int baselen = Len(base);
|
||||||
int otherlen = Len(other);
|
int otherlen = Len(other);
|
||||||
|
|
||||||
|
|
@ -1080,9 +1080,9 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------
|
/* ------------------------------------------------------------
|
||||||
* out = abs_import_directive_string(pkg, mod, pfx)
|
* abs_import_directive_string()
|
||||||
*
|
*
|
||||||
* Return a String* containing python code to import module.
|
* Return a string containing python code to import module.
|
||||||
*
|
*
|
||||||
* pkg package name or the module being imported
|
* pkg package name or the module being imported
|
||||||
* mod module name of the module being imported
|
* mod module name of the module being imported
|
||||||
|
|
@ -1090,11 +1090,9 @@ public:
|
||||||
*
|
*
|
||||||
* NOTE: keep this function consistent with abs_import_name_string().
|
* NOTE: keep this function consistent with abs_import_name_string().
|
||||||
* ------------------------------------------------------------ */
|
* ------------------------------------------------------------ */
|
||||||
static String* abs_import_directive_string(const String* pkg,
|
|
||||||
const String* mod,
|
static String *abs_import_directive_string(const String *pkg, const String *mod, const char *pfx = "") {
|
||||||
const char* pfx = "")
|
String *out = NewString("");
|
||||||
{
|
|
||||||
String* out = NewString("");
|
|
||||||
|
|
||||||
if (pkg && *Char(pkg)) {
|
if (pkg && *Char(pkg)) {
|
||||||
Printf(out, "import %s.%s%s\n", pkg, pfx, mod);
|
Printf(out, "import %s.%s%s\n", pkg, pfx, mod);
|
||||||
|
|
@ -1105,9 +1103,9 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------
|
/* ------------------------------------------------------------
|
||||||
* out = rel_import_directive_string(mainpkg, pkg, mod, pfx)
|
* rel_import_directive_string()
|
||||||
*
|
*
|
||||||
* Return a String* containing python code to import module that
|
* Return a string containing python code to import module that
|
||||||
* is potentially within a package.
|
* is potentially within a package.
|
||||||
*
|
*
|
||||||
* mainpkg package name of the module which imports the other module
|
* mainpkg package name of the module which imports the other module
|
||||||
|
|
@ -1117,11 +1115,9 @@ public:
|
||||||
*
|
*
|
||||||
* NOTE: keep this function consistent with rel_import_name_string().
|
* NOTE: keep this function consistent with rel_import_name_string().
|
||||||
* ------------------------------------------------------------ */
|
* ------------------------------------------------------------ */
|
||||||
static String* rel_import_directive_string(const String* mainpkg,
|
|
||||||
const String* pkg,
|
static String *rel_import_directive_string(const String *mainpkg, const String *pkg, const String *mod, const char *pfx = "") {
|
||||||
const String* mod,
|
|
||||||
const char* pfx = "")
|
|
||||||
{
|
|
||||||
/* NOTE: things are not so trivial. This is what we do here (by examples):
|
/* NOTE: things are not so trivial. This is what we do here (by examples):
|
||||||
*
|
*
|
||||||
* 0. To import module 'foo', which is not in any package, we do absolute
|
* 0. To import module 'foo', which is not in any package, we do absolute
|
||||||
|
|
@ -1158,29 +1154,31 @@ public:
|
||||||
*
|
*
|
||||||
* NOTE: [1] is necessary for pkg2.foo to be present in the importing module
|
* NOTE: [1] is necessary for pkg2.foo to be present in the importing module
|
||||||
*/
|
*/
|
||||||
String* apkg = 0; // absolute (FQDN) package name of pkg
|
|
||||||
String* rpkg = 0; // relative package name
|
String *apkg = 0; // absolute (FQDN) package name of pkg
|
||||||
|
String *rpkg = 0; // relative package name
|
||||||
int py3_rlen1 = 0; // length of 1st level sub-package name, used by py3
|
int py3_rlen1 = 0; // length of 1st level sub-package name, used by py3
|
||||||
String* out = NewString("");
|
String *out = NewString("");
|
||||||
|
|
||||||
if (pkg && *Char(pkg)) {
|
if (pkg && *Char(pkg)) {
|
||||||
if(mainpkg) {
|
if (mainpkg) {
|
||||||
String* tail = subpkg_tail(mainpkg, pkg);
|
String *tail = subpkg_tail(mainpkg, pkg);
|
||||||
if (tail) {
|
if (tail) {
|
||||||
if(*Char(tail)) {
|
if (*Char(tail)) {
|
||||||
rpkg = NewString(tail);
|
rpkg = NewString(tail);
|
||||||
const char* py3_end1 = Strchr(rpkg, '.');
|
const char *py3_end1 = Strchr(rpkg, '.');
|
||||||
if(!py3_end1) py3_end1 = (Char(rpkg)) + Len(rpkg);
|
if (!py3_end1)
|
||||||
py3_rlen1 = py3_end1 - (Char(rpkg));
|
py3_end1 = (Char(rpkg)) + Len(rpkg);
|
||||||
} else {
|
py3_rlen1 = py3_end1 - (Char(rpkg));
|
||||||
rpkg = NewString("");
|
} else {
|
||||||
}
|
rpkg = NewString("");
|
||||||
Delete(tail);
|
}
|
||||||
} else {
|
Delete(tail);
|
||||||
apkg = NewString(pkg);
|
} else {
|
||||||
}
|
apkg = NewString(pkg);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
apkg = NewString(pkg);
|
apkg = NewString(pkg);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
apkg = NewString("");
|
apkg = NewString("");
|
||||||
|
|
@ -1192,7 +1190,7 @@ public:
|
||||||
} else {
|
} else {
|
||||||
if (py3) {
|
if (py3) {
|
||||||
if (py3_rlen1)
|
if (py3_rlen1)
|
||||||
Printf(out, "from . import %.*s\n", py3_rlen1, rpkg);
|
Printf(out, "from . import %.*s\n", py3_rlen1, rpkg);
|
||||||
Printf(out, "from .%s import %s%s\n", rpkg, pfx, mod);
|
Printf(out, "from .%s import %s%s\n", rpkg, pfx, mod);
|
||||||
} else {
|
} else {
|
||||||
Printf(out, "import %s%s%s%s\n", rpkg, *Char(rpkg) ? "." : "", pfx, mod);
|
Printf(out, "import %s%s%s%s\n", rpkg, *Char(rpkg) ? "." : "", pfx, mod);
|
||||||
|
|
@ -1203,13 +1201,10 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------
|
/* ------------------------------------------------------------
|
||||||
* out = import_directive_string(mainpkg, pkg, mod, pfx)
|
* import_directive_string()
|
||||||
* ------------------------------------------------------------ */
|
* ------------------------------------------------------------ */
|
||||||
static String* import_directive_string(const String* mainpkg,
|
|
||||||
const String* pkg,
|
static String *import_directive_string(const String *mainpkg, const String *pkg, const String *mod, const char *pfx = "") {
|
||||||
const String* mod,
|
|
||||||
const char* pfx = "")
|
|
||||||
{
|
|
||||||
if (!relativeimport) {
|
if (!relativeimport) {
|
||||||
return abs_import_directive_string(pkg, mod, pfx);
|
return abs_import_directive_string(pkg, mod, pfx);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1218,9 +1213,9 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------
|
/* ------------------------------------------------------------
|
||||||
* out = abs_import_name_string(mainpkg, mainmod, pkg, mod, sym)
|
* abs_import_name_string()
|
||||||
*
|
*
|
||||||
* Return a String* with the name of a symbol (perhaps imported
|
* Return a string with the name of a symbol (perhaps imported
|
||||||
* from external module by absolute import directive).
|
* from external module by absolute import directive).
|
||||||
*
|
*
|
||||||
* mainpkg package name of current module
|
* mainpkg package name of current module
|
||||||
|
|
@ -1232,13 +1227,9 @@ public:
|
||||||
* NOTE: mainmod, mod, and sym can't be NULL.
|
* NOTE: mainmod, mod, and sym can't be NULL.
|
||||||
* NOTE: keep this function consistent with abs_import_directive_string()
|
* NOTE: keep this function consistent with abs_import_directive_string()
|
||||||
* ------------------------------------------------------------ */
|
* ------------------------------------------------------------ */
|
||||||
static String* abs_import_name_string(const String* mainpkg,
|
|
||||||
const String* mainmod,
|
static String *abs_import_name_string(const String *mainpkg, const String *mainmod, const String *pkg, const String *mod, const String *sym) {
|
||||||
const String* pkg,
|
String *out = NewString("");
|
||||||
const String* mod,
|
|
||||||
const String* sym)
|
|
||||||
{
|
|
||||||
String* out = NewString("");
|
|
||||||
if (pkg && *Char(pkg)) {
|
if (pkg && *Char(pkg)) {
|
||||||
if (mainpkg && *Char(mainpkg)) {
|
if (mainpkg && *Char(mainpkg)) {
|
||||||
if (Strcmp(mainpkg,pkg) != 0 || Strcmp(mainmod, mod) != 0) {
|
if (Strcmp(mainpkg,pkg) != 0 || Strcmp(mainmod, mod) != 0) {
|
||||||
|
|
@ -1255,9 +1246,9 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------
|
/* ------------------------------------------------------------
|
||||||
* out = rel_import_name_string(mainpkg, mainmod, pkg, mod, sym)
|
* rel_import_name_string()
|
||||||
*
|
*
|
||||||
* Return a String* with the name of a symbol (perhaps imported
|
* Return a string with the name of a symbol (perhaps imported
|
||||||
* from external module by relative import directive).
|
* from external module by relative import directive).
|
||||||
*
|
*
|
||||||
* mainpkg package name of current module
|
* mainpkg package name of current module
|
||||||
|
|
@ -1269,20 +1260,16 @@ public:
|
||||||
* NOTE: mainmod, mod, and sym can't be NULL.
|
* NOTE: mainmod, mod, and sym can't be NULL.
|
||||||
* NOTE: keep this function consistent with rel_import_directive_string()
|
* NOTE: keep this function consistent with rel_import_directive_string()
|
||||||
* ------------------------------------------------------------ */
|
* ------------------------------------------------------------ */
|
||||||
static String* rel_import_name_string(const String* mainpkg,
|
|
||||||
const String* mainmod,
|
static String *rel_import_name_string(const String *mainpkg, const String *mainmod, const String *pkg, const String *mod, const String *sym) {
|
||||||
const String* pkg,
|
String *out = NewString("");
|
||||||
const String* mod,
|
|
||||||
const String* sym)
|
|
||||||
{
|
|
||||||
String* out = NewString("");
|
|
||||||
if (pkg && *Char(pkg)) {
|
if (pkg && *Char(pkg)) {
|
||||||
String* tail = 0;
|
String *tail = 0;
|
||||||
if (mainpkg)
|
if (mainpkg)
|
||||||
tail = subpkg_tail(mainpkg, pkg);
|
tail = subpkg_tail(mainpkg, pkg);
|
||||||
if (!tail)
|
if (!tail)
|
||||||
tail = NewString(pkg);
|
tail = NewString(pkg);
|
||||||
if(*Char(tail)) {
|
if (*Char(tail)) {
|
||||||
Printf(out, "%s.%s.", tail, mod);
|
Printf(out, "%s.%s.", tail, mod);
|
||||||
} else if (Strcmp(mainmod, mod) != 0) {
|
} else if (Strcmp(mainmod, mod) != 0) {
|
||||||
Printf(out, "%s.", mod);
|
Printf(out, "%s.", mod);
|
||||||
|
|
@ -1296,14 +1283,10 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------
|
/* ------------------------------------------------------------
|
||||||
* out = import_name_string(mainpkg, mainmod, pkg, mod, sym)
|
* import_name_string()
|
||||||
* ------------------------------------------------------------ */
|
* ------------------------------------------------------------ */
|
||||||
static String* import_name_string(const String* mainpkg,
|
|
||||||
const String* mainmod,
|
static String *import_name_string(const String *mainpkg, const String *mainmod, const String *pkg, const String *mod, const String *sym) {
|
||||||
const String* pkg,
|
|
||||||
const String* mod,
|
|
||||||
const String* sym)
|
|
||||||
{
|
|
||||||
if (!relativeimport) {
|
if (!relativeimport) {
|
||||||
return abs_import_name_string(mainpkg,mainmod,pkg,mod,sym);
|
return abs_import_name_string(mainpkg,mainmod,pkg,mod,sym);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1330,9 +1313,9 @@ public:
|
||||||
String *pkg = options ? Getattr(options, "package") : 0;
|
String *pkg = options ? Getattr(options, "package") : 0;
|
||||||
if (shadowimport) {
|
if (shadowimport) {
|
||||||
if (!options || (!Getattr(options, "noshadow") && !Getattr(options, "noproxy"))) {
|
if (!options || (!Getattr(options, "noshadow") && !Getattr(options, "noproxy"))) {
|
||||||
String* _import = import_directive_string(package, pkg, modname, "_");
|
String *_import = import_directive_string(package, pkg, modname, "_");
|
||||||
if (!GetFlagAttr(f_shadow_imports, _import)) {
|
if (!GetFlagAttr(f_shadow_imports, _import)) {
|
||||||
String* import = import_directive_string(package, pkg, modname);
|
String *import = import_directive_string(package, pkg, modname);
|
||||||
Printf(builtin ? f_shadow_builtin_imports : f_shadow, "%s", import);
|
Printf(builtin ? f_shadow_builtin_imports : f_shadow, "%s", import);
|
||||||
Delete(import);
|
Delete(import);
|
||||||
SetFlag(f_shadow_imports, _import);
|
SetFlag(f_shadow_imports, _import);
|
||||||
|
|
@ -3437,9 +3420,9 @@ public:
|
||||||
if (mod) {
|
if (mod) {
|
||||||
String *modname = Getattr(mod, "name");
|
String *modname = Getattr(mod, "name");
|
||||||
Node *options = Getattr(mod, "options");
|
Node *options = Getattr(mod, "options");
|
||||||
String* pkg = options ? Getattr(options, "package") : 0;
|
String *pkg = options ? Getattr(options, "package") : 0;
|
||||||
String* sym = Getattr(n, "sym:name");
|
String *sym = Getattr(n, "sym:name");
|
||||||
String* importname = import_name_string(package, mainmodule, pkg, modname, sym);
|
String *importname = import_name_string(package, mainmodule, pkg, modname, sym);
|
||||||
Setattr(n, "python:proxy", importname);
|
Setattr(n, "python:proxy", importname);
|
||||||
Delete(importname);
|
Delete(importname);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue