Created
March 2, 2014 23:07
-
-
Save Diastro/9315344 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re, os | |
| def formatMethods(methods): | |
| formattedMethods = '' | |
| for m in methods: | |
| m['params'] = filter(None, m['params']) | |
| if m['params']: | |
| methodStruct = """ | |
| {access} {type} {name}({params}){{ | |
| return parent::makeRequest(__FUNCTION__, func_get_args()); | |
| }} | |
| """ | |
| else: | |
| methodStruct = """ | |
| {access} {type} {name}(){{ | |
| return parent::makeRequest(__FUNCTION__); | |
| }} | |
| """ | |
| context = { | |
| 'access' : m['access'], | |
| 'type' : m['type'], | |
| 'name' : m['name'], | |
| 'params' : ', '.join(m['params']) | |
| } | |
| formattedMethods = formattedMethods + methodStruct.format(**context) | |
| return formattedMethods | |
| def formatClass(methods, context): | |
| classStruct = """<?php | |
| class {proxyName} extends Proxy implements {interName} {{ | |
| public $app; | |
| public $obj = '{objName}'; | |
| public function __construct() {{ | |
| global $app; | |
| $this->app = $app; | |
| }} | |
| {methods} | |
| }} | |
| ?> | |
| """ | |
| return classStruct.format(**context) | |
| def main(): | |
| for file in os.listdir("C:\Users\David\Desktop\Frank&Oak\Remote-Kepler-Repo\common\interfaces"): | |
| fileloc = r"C:\Users\David\Desktop\Frank&Oak\Remote-Kepler-Repo\common\interfaces\\" + file | |
| if file.startswith('I') and file.endswith('.php'): | |
| print 'Creating proxy for : ' + file.split('.')[0] | |
| f = open(fileloc, 'r') | |
| interName = '' | |
| proxyName = '' | |
| objName = '' | |
| methods = [] | |
| for line in f: | |
| line = line.strip() | |
| splLine = line.split(' ') | |
| if splLine[0] == 'interface': | |
| interName = splLine[1] | |
| objName = splLine[1][1:].lower() | |
| proxyName = objName.title() + 'Proxy' | |
| elif splLine[0] == 'private' or splLine[0] == 'public': | |
| access = splLine[0] | |
| function = splLine[1] | |
| name = splLine[2].split('(')[0] | |
| params = re.search('\((.*?)\)', line).group(1).replace(' ', '').split(',') | |
| methods.append({'access' : access, | |
| 'type' : function, | |
| 'name' : name, | |
| 'params' : params}) | |
| out = open(proxyName+'.php', 'w') | |
| formattedMethods = formatMethods(methods) | |
| context = { | |
| 'proxyName' : proxyName, | |
| 'objName' : objName, | |
| 'interName' : interName, | |
| 'methods' : formattedMethods | |
| } | |
| print 'Writing to ' + proxyName+'.php' | |
| out.write(formatClass(formattedMethods, context)) | |
| print 'Operation complete.' | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment