eachImport()

in lib/generator.js [589:690]


  eachImport(imports, usedModels, innerModule, filepath, level) {
    this.moduleTypedef = {};
    if (imports.length > 0) {
      const lockPath = path.join(this.config.pkgDir, '.libraries.json');
      const lock = fs.existsSync(lockPath) ? JSON.parse(fs.readFileSync(lockPath, 'utf8')) : {};
      for (let i = 0; i < imports.length; i++) {
        const item = imports[i];
        let comments = DSL.comment.getFrontComments(this.comments, item.tokenRange[0]);
        this.visitComments(comments, level);
        const aliasId = item.lexeme;
        const main = item.mainModule;
        const inner = item.module;
        const moduleDir = main ? this.config.libraries[main] : this.config.libraries[aliasId];
        const innerPath = item.innerPath;
        if (!moduleDir && innerPath) {
          let pyPath = innerPath.replace(/(\.tea)$|(\.spec)$|(\.dara)$/gi, '') + '.py';
          if (pyPath.startsWith('./') || pyPath.startsWith('../')) {
            pyPath = pyPath.split('/').map(dir => _snakeCase(dir)).join(path.sep);
            pyPath = path.join(path.dirname(filepath), `${pyPath}`);
          } else if (pyPath.startsWith('/')) {
            pyPath = pyPath.split('/').map(dir => _snakeCase(dir)).join(path.sep);
            pyPath = `${pyPath}`;
          }
          
          const classNamespace = this.getClassNamespace(pyPath);
          const className = this.getInnerClient(aliasId, pyPath);
          const aliasName = this.getAliasName(className, aliasId);
          const filename = _snakeCase(path.basename(pyPath, '.py'));
          this.packageInfo[aliasId] = {
            aliasName: aliasName || className,
            clientName: className,
            namemespace: classNamespace,
            fileName: filename,
            models: `${filename}_models`,
            exceptions: `${filename}_exceptions`,
          };

          innerModule.set(aliasId, pyPath);
          continue;
        }
        let targetPath = '';
        if (moduleDir.startsWith('./') || moduleDir.startsWith('../')) {
          targetPath = path.join( this.config.pkgDir, moduleDir);
        } else if (moduleDir.startsWith('/')) {
          targetPath = moduleDir;
        } else {
          targetPath = path.join(this.config.pkgDir, lock[moduleDir]);
        }
        const pkgPath = fs.existsSync(path.join(targetPath, 'Teafile')) ? path.join(targetPath, 'Teafile') : path.join(targetPath, 'Darafile');
        const pkg = JSON.parse(fs.readFileSync(pkgPath));
        // 这部分是import
        const pyPkg = pkg.python;
        if (!pyPkg) {
          throw new Error(`The '${aliasId}' has no Python supported.`);
        }
        let className = _camelCase(pyPkg.clientName || 'client');
        let filename = _snakeCase(className);
        let classNamespace = pyPkg.package;
        let models = 'models';
        let exceptions = 'exceptions';
        if(inner && pkg.exports[inner]) {
          let pyPath = pkg.exports[inner];
          filename = pyPath.split(path.sep).slice(-1)[0].replace(/(\.tea)$|(\.spec)$|(\.dara)$/gi, '');
          models = filename + '_models';
          exceptions = filename + '_exceptions';
          const arr = pyPath.split(path.sep).slice(1, -1);
          arr.map(key => {
            classNamespace += '.' + _snakeCase(key);
          });
          const exportClientName = pyPkg.exports && pyPkg.exports[inner];
          className = _camelCase(exportClientName || 'Client');
        }
        const aliasName = this.getAliasName(className, aliasId);
        this.packageInfo[aliasId] = {
          aliasName: aliasName || className,
          fileName: filename,
          clientName: className,
          namemespace: classNamespace,
          models: models,
          exceptions: exceptions,
        };
        // 这部分处理setup.py
        if (pkg.releases && pkg.releases.python) {
          const REQUIRE = pkg.releases.python ;
          const [pkgName, version] = REQUIRE.split(':');
          this.package.dependencies[pkgName] = `${version}`;
        }
        const typedef = pkg.python && pkg.python.typedef || {};
        this.moduleTypedef[aliasId] = typedef;
        Object.keys(typedef).forEach(type => {
          if (typedef[type].package) {
            let [pack, ver] = typedef[type].package.split(':');
            if (!this.package.dependencies[pack]) {
              this.package.dependencies[pack] = ver;
            }
          }
        });
      }
      this.emit('\n\n');
      this.__externModule = usedModels;
    }
  }