Created
February 28, 2014 06:55
-
-
Save Diastro/9266557 to your computer and use it in GitHub Desktop.
Proxy creator
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: | |
| methodStruct = """ | |
| {access} {type} {name}({params}){{ | |
| 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 = """\ | |
| 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("./"): | |
| if file.startswith('I') and file.endswith('.php'): | |
| print 'Creating proxy for : ' + file.split('.')[0] | |
| f = open('interfaceTest.php', '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 | |
| } | |
| 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