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'.
|
||||||
*
|
*
|
||||||
|
|
@ -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,10 +1090,8 @@ 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)) {
|
||||||
|
|
@ -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,6 +1154,7 @@ 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 *apkg = 0; // absolute (FQDN) package name of pkg
|
||||||
String *rpkg = 0; // relative package name
|
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
|
||||||
|
|
@ -1170,7 +1167,8 @@ public:
|
||||||
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_end1 = (Char(rpkg)) + Len(rpkg);
|
||||||
py3_rlen1 = py3_end1 - (Char(rpkg));
|
py3_rlen1 = py3_end1 - (Char(rpkg));
|
||||||
} else {
|
} else {
|
||||||
rpkg = NewString("");
|
rpkg = NewString("");
|
||||||
|
|
@ -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,12 +1227,8 @@ 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,
|
|
||||||
const String* mod,
|
|
||||||
const String* sym)
|
|
||||||
{
|
|
||||||
String *out = NewString("");
|
String *out = NewString("");
|
||||||
if (pkg && *Char(pkg)) {
|
if (pkg && *Char(pkg)) {
|
||||||
if (mainpkg && *Char(mainpkg)) {
|
if (mainpkg && *Char(mainpkg)) {
|
||||||
|
|
@ -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,12 +1260,8 @@ 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,
|
|
||||||
const String* mod,
|
|
||||||
const String* sym)
|
|
||||||
{
|
|
||||||
String *out = NewString("");
|
String *out = NewString("");
|
||||||
if (pkg && *Char(pkg)) {
|
if (pkg && *Char(pkg)) {
|
||||||
String *tail = 0;
|
String *tail = 0;
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue