def printMethods(self, out: TextIOWrapper, file: PGFile, pyClass: PyClass, indent: int): for item in file.declarations: if util.inferParentClass(item) == pyClass.fqname: v_type, req = file.declarations[item] short = util.inferShortName(item) # Get methods if v_type in ACCEPTED_TYPES: out.write( f'\n{tab*indent}def get_{short}(self) -> {PYTHON_TYPES[v_type]}:\n' ) else: out.write( f'\n{tab*indent}def get_{short}(self) -> {v_type}:\n') out.write( f'{tab*(indent+1)}return self.data[\'{short}\'][0]\n') # Set methods if v_type in PYTHON_TYPES: out.write( f'\n{tab*indent}def set_{short}(self, {short}: {PYTHON_TYPES[v_type]}) -> \'{pyClass.name}\':\n' f'{tab*(indent+1)}self._assertType("{short}", {short}, {PYTHON_TYPES[v_type]}, "{v_type}")\n' ) else: out.write( f'\n{tab*indent}def set_{short}(self, {short}: {v_type}) -> \'{pyClass.name}\':\n' ) out.write( f'{tab*(indent+1)}self.data[\'{short}\'][0] = {short}\n' f'{tab*(indent+1)}return self\n')
def printMethods(self, out: TextIOWrapper, file: PGFile, pyClass: PyClass, indent: int): for item in file.declarations: if util.inferParentClass(item) == pyClass.fqname: v_type, req = file.declarations[item] short = util.inferShortName(item) # Get methods self.printArgs(out, file, pyClass, indent, item, 'get') out.write(f'{tab*indent}get_{short}() {{\n') out.write( f'{tab*(indent+1)}return this.data.{short}[0] // {v_type}\n') out.write(f'{tab*indent}}}\n\n') # Set methods self.printArgs(out, file, pyClass, indent, item, 'set') if v_type in JS_TYPES: out.write( f'{tab*indent}set_{short}({short}) {{ // {JS_TYPES[v_type]}\n') out.write( f'{tab*(indent+1)}this._assertType("{short}", {short}, "{JS_TYPES[v_type]}", "{v_type}")\n') # out.write('{}if (typeof {} != "{}") throw new TypeError("{} should be of type {}")\n'.format( # (tab*(indent+1)), short, JS_TYPES[v_type], short, JS_TYPES[v_type])) else: out.write( f'{tab*indent}set_{short}({short}) {{ // {v_type}\n') out.write(f'{tab*(indent+1)}this.data.{short}[0] = {short}\n') out.write(f'{tab*(indent+1)}return this\n{tab*indent}}}\n')
def printAttributes(self, out: TextIOWrapper, file: PGFile, pyClass: PyClass, indent: int): out.write(f'{tab*(indent+1)}self.data = {{\n') for item in file.declarations: if util.inferParentClass(item) == pyClass.fqname: v_type, required = file.declarations[item] short = util.inferShortName(item) # primitive data type if v_type == 'list': out.write( f'{tab*(indent+2)}\'{short}\': [[], {required}, False],\n' ) elif v_type == 'map': out.write( f'{tab*(indent+2)}\'{short}\': [{{}}, {required}, False],\n' ) elif v_type in ACCEPTED_TYPES: out.write( f'{tab*(indent+2)}\'{short}\': [None, {required}, False],\n' ) # local, nested class (needs 'self') elif v_type in pyClass.gatherSubclasses('name'): out.write( f'{tab*(indent+2)}\'{short}\': [self.{v_type}(), {required}, True],\n' ) # local, non-nested class (doesn't need 'self') else: out.write( f'{tab*(indent+2)}\'{short}\': [{v_type}(), {required}, True],\n' ) out.write(f'{tab*(indent+1)}}}\n')
def printArgs(self, out: TextIOWrapper, file: PGFile, pyClass: PyClass, indent: int, file_item, set_get: str): if util.inferParentClass(file_item) == pyClass.fqname: thing = file.declarations[file_item] short = util.inferShortName(file_item) v_type = JS_TYPES.get(thing[0], thing[0]) out.write(f'{tab*indent}/**\n') if set_get == 'set': short = util.inferShortName(file_item) out.write(f'{tab*indent} * @param {{ {v_type} }} {short}\n') elif set_get == 'get': out.write( f'{tab*indent} * @returns {{ {v_type} }}\n') out.write(f'{tab*indent} */\n')
def printData(self, out: TextIOWrapper, file: PGFile, pyClass: PyClass, indent: int): out.write(f"{tab*indent}if (data != null) {{\n") out.write(f"{tab*(indent+1)}this.data = {{\n") for item in file.declarations: if util.inferParentClass(item) == pyClass.fqname: # variable type, required/optional v_type, req = file.declarations[item] short = util.inferShortName(item) if v_type in ACCEPTED_TYPES: out.write( f'{tab*(indent+2)}{short}: [data[\'{short}\'], {str(req).lower()}, false], // {v_type} \n') else: out.write( f'{tab*(indent+2)}{short}: [new {v_type}(data[\'{short}\']), {str(req).lower()}, true], // {v_type}\n') out.write(f"{tab*(indent+1)}}}\n") out.write(f"{tab*indent}}} else {{\n") out.write(f"{tab*(indent+1)}this.data = {{\n") for item in file.declarations: if util.inferParentClass(item) == pyClass.fqname: v_type, req = file.declarations[item] short = util.inferShortName(item) # primitive data type if v_type == 'list': out.write( f'{tab*(indent+2)}{ short}: [[], {str(req).lower()}, false], // {v_type} \n') elif v_type == 'map': out.write( f'{tab*(indent+2)}{short}: [{{}}, {str(req).lower()}, false], // {v_type} \n') elif v_type in ACCEPTED_TYPES: out.write( f'{tab*(indent+2)}{short}: [null, {str(req).lower()}, false], // {v_type} \n') else: out.write( f'{tab*(indent+2)}{short}: [new {v_type}(), {str(req).lower()}, true], // {v_type}\n') out.write(f"{tab*(indent+1)}}}\n{tab*indent}}}\n")
def printAttributes(self, out: TextIOWrapper, file: PGFile, pyClass: PyClass): for item in file.declarations: if util.inferParentClass(item) == pyClass.fqname: v_type, req = file.declarations[item] req = '**required**' if req else '*optional*' short = util.inferShortName(item) if v_type in ACCEPTED_TYPES: out.write( f"* {short} → [`{v_type}`](./standard_types#{v_type}) {req}\n" ) else: out.write( f"* {short} → [`{v_type}`](#{v_type}) {req}\n" ) out.write('\n')
def printClass(self, out: TextIOWrapper, file: PGFile, pyClass: PyClass, indent: int, root: bool): if root: out.write(f"\nclass {pyClass.name}(Serializable, Printable):\n") else: out.write( f"\n{tab*indent}class {pyClass.name}(Serializable, Printable):\n" ) out.write( f"\n{tab*(indent+1)}def __init__(self, data: dict = None):\n") self.printAttributes(out, file, pyClass, indent + 1) out.write(f"\n{tab*(indent+2)}if data is not None:\n") for item in file.declarations: if util.inferParentClass(item) == pyClass.fqname: short = util.inferShortName(item) v_type, required = file.declarations[item] if v_type in ACCEPTED_TYPES: out.write( f"{tab*(indent+3)}self.data['{short}'][0] = data['{short}']\n" ) # local, nested class (needs 'self') elif v_type in pyClass.gatherSubclasses('name'): out.write( f"{tab*(indent+3)}self.data['{short}'][0] = self.{v_type}(data['{short}'])\n" ) # local, non-nested class (doesn't need 'self') else: out.write( f"{tab*(indent+3)}self.data['{short}'][0] = {v_type}(data['{short}'])\n" ) for item in pyClass.subclasses: self.printClass(out, file, item, indent + 1, False) self.printMethods(out, file, pyClass, indent + 1) out.write(f"{tab*indent}# End Class {pyClass.name}\n")