* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class Python extends CodeGeneratorAbstract { /** * @inheritDoc */ public function getFileName(string $file): string { return $file . '.py'; } /** * @inheritDoc */ protected function newTypeGenerator(array $mapping): GeneratorInterface { return new Type\Python($mapping); } /** * @inheritDoc */ protected function writeStruct( string $name, array $properties, ?string $extends, ?array $generics, StructType $origin, ): string { $code = 'class ' . $name; if (!empty($extends)) { $code.= '(' . $extends . ')'; } $code.= ':' . "\n"; $items = []; $args = []; foreach ($properties as $name => $property) { /** @var Code\Property $property */ $args[] = $name . ': ' . $property->getType(); $items[] = $name; } $code.= $this->indent . 'def __init__(self, ' . implode(', ', $args) . '):' . "\n"; foreach ($items as $item) { $code.= $this->indent . $this->indent . 'self.' . $item . ' = ' . $item . "\n"; } return $code; } protected function writeMap(string $name, string $type, MapType $origin): string { $subType = $this->generator->getType($origin->getAdditionalProperties()); return 'class ' . $name . '(Dict[str, ' . $subType . ']):' . "\n"; } protected function writeReference(string $name, string $type, ReferenceType $origin): string { return 'class ' . $name . '(' . $type . '):' . "\n"; } protected function writeHeader(TypeAbstract $origin): string { $code = ''; if (!empty($this->namespace)) { // TODO can we namespace? } $imports = $this->getImports($origin); if (!empty($imports)) { $code.= "\n"; $code.= implode("\n", $imports); $code.= "\n"; } $code.= "\n"; $comment = $origin->getDescription(); if (!empty($comment)) { $code.= '# ' . $comment . "\n"; } return $code; } private function getImports(TypeAbstract $origin): array { $imports = []; $imports[] = 'from typing import Any'; if (TypeUtil::contains($origin, ArrayType::class)) { $imports[] = 'from typing import List'; } if (TypeUtil::contains($origin, MapType::class)) { $imports[] = 'from typing import Dict'; } if (TypeUtil::contains($origin, UnionType::class)) { $imports[] = 'from typing import Union'; } return $imports; } } __halt_compiler();----SIGNATURE:----ujLPA0o8sq8RSqHu4prmACjQmXpGq99sCG1HrTZcGhv+vacns1dLiOds9vCAu2GO4RKbTt8x3C/YvMrm99v7/xhe6U/DCaXBZFZr0PPuoWmBB7t0ODX0KcGwOpq1gP6Sv7XdOQti9qvSfHMEYEVd1ReS2oGetqsOMWpQjXHVfvwuhVhDU2d2SMNoadB0jJk+wsuBiebH11zvq1r/+gqf6EkhNZYtH2tPYw1LdO0PIQ0f664aTKkfCc+BxUTtEFuY6Tbm+oxa2Mv30kogQgEzS0vidatiyaEAgT4Yl/n++77KT7y84hI5d7Tff9Fk/St2debN2UBRM5WehV/AhTmH04HCxmGkETqlWKyD8yH7pWIvvh/xLpMJJOAO+Osg5F5dYzG6zjmMEYEXX2PRl+tpAn2K725SDH5lzHxI5e11EPM5LCpmL92yZmvB5Rt/eWwvSUQUoTMxAbzYwWjRhpVouzPlRMGRgkIwf5uNErJxQu3aFx6lH/rkJeUa7Q9GT2Yoq1PJh5UJrJ9o3Mf63Yr+tOMwIFA6miZwzFqWPbCzwTHiJKhnIu2ExKBhRR2OMob7X9+fQmCWu0tPA+wnrHLPcYslYmrNPqnmZp1+aXQIuZGk7uMGb53+lnfzBaJ7BPKNwdXTm4HqBqnZiFjQil1f9TsWc5iPFES2llttAL5AxNU=----ATTACHMENT:----NTg1MzU2MTUwMTQ2NzQxOSA1MjU4MDQ1MjczODU1MTYxIDc0NTI4MDcxNjY4MDc2NDU=