* @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:----KSaBqAHIs16f2fpxCzS2HPIWks5PYX/tcz4/5TgGX3R4DluKebwUT5ZFoi6hUXRtARNsmEl9TGOsIwKTwpqssigvy/KJMBwwsAdnem0JhpiOXjsK5gunoLKjMKmnYJefDKsnOwQr5JvxRhhpbfl1tZnOOxML2u8Oz8UfITExDOkq95HZEao6To0nehonmx0wqubqDQ06tT/ZR49shYn/tDDbzAizqVmCLGnoHe7uURIlxjvB9+Np6eI3eI6BHRTG8+BArPx/3AhuteMcFuBO76j5qbFd1SPYs++if3D4JGJj/j0Mes5webDqkI4D6v+q0xXVFWWPjil6YSu3EfPHph0aTng4V8Q4CuJKUk45Vi83eO5jTZ0Jgp93hsQzxstc1geQdS5ECGp7yLQSX1jfb8RPxl6wwIoBVK1vzA+Bs8p5L/ryCp206OELP4mnWPX/P9N7A3mr3ULM1WkoPA0Zd8lls9cbOhzgPYDb2T5KoSpMnaYA/SWSMv30QxgsPJXjdQ3ZuOIKdj2VBe1c4neAKOm+DijVEe+FAE8DxV+ZQn4nPonwcdq+Y8OWbw9S8hnqXf6LggaZfVqbXNPPduP6FCAO07aVJ9eJi6ARrihJsunPtMsJtm8n3iV256S/kg/Dd11iT/49ngK05+/peMqhQbcTYr37LVS5LfIJEeUjN4c=----ATTACHMENT:----MzMzNjM1MTcyMzA2MTg3NSA5MTkyMzk4MjE5MzkyNCA1NzA4NTI0MDY2NTY0OTc=