Created
December 7, 2023 23:19
-
-
Save bukowa/a4064f834d4974863fa5403411f0cfb0 to your computer and use it in GitHub Desktop.
rithmic connection params generator for c sharp
This file contains 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
# loop over all files in the current folder | |
# and process the file if the name ends with _connection_params.txt | |
import os | |
import re | |
import sys | |
def get_files(): | |
files = [] | |
for file in os.listdir(): | |
if file.endswith("_connection_params.txt"): | |
files.append(file) | |
return files | |
patterns = { | |
'AdmCnnctPt': r'\s*REngineParams\.AdmCnnctPt\s*:\s*(\S+)', | |
'DmnSrvrAddr': r'\s*REngineParams\.DmnSrvrAddr\s*:\s*(\S+)', | |
'DomainName': r'\s*REngineParams\.DomainName\s*:\s*(\S+)', | |
'LicSrvrAddr': r'\s*REngineParams\.LicSrvrAddr\s*:\s*(\S+)', | |
'LocBrokAddr': r'\s*REngineParams\.LocBrokAddr\s*:\s*(\S+)', | |
'LoggerAddr': r'\s*REngineParams\.LoggerAddr\s*:\s*(\S+)', | |
'sCnnctPt': r'\s*sCnnctPt\s*:\s*(\S+)', | |
'sMdCnnctPt': r'\s*sMdCnnctPt\s*:\s*(\S+)', | |
'sIhCnnctPt': r'\s*sIhCnnctPt\s*:\s*(\S+)', | |
'sTsCnnctPt': r'\s*sTsCnnctPt\s*:\s*(\S+)', | |
'sPnLCnnctPt': r'\s*sPnLCnnctPt\s*:\s*(\S+)', | |
} | |
def get_params(file): | |
lookup_text = 'For .NET Rithmic APIs' | |
with open(file, "r") as f: | |
text = f.read() | |
assert lookup_text in text | |
# find everything below lookup_text | |
text = text[text.find(lookup_text):] | |
params = {} | |
for k, v in patterns.items(): | |
match = re.search(v, text) | |
if match: | |
params[k] = match.group(1) | |
else: | |
raise ValueError(f"Could not find {k} in {file}") | |
print(params) | |
return params | |
def generate_csharp_code(all_params): | |
code = """using System.Collections.Generic; | |
namespace Rithmic | |
{ | |
public class RithmicParams | |
{ | |
// Properties for REngineParams | |
public string AdmCnnctPt { get; set; } | |
public string DmnSrvrAddr { get; set; } | |
public string DomainName { get; set; } | |
public string LicSrvrAddr { get; set; } | |
public string LocBrokAddr { get; set; } | |
public string LoggerAddr { get; set; } | |
// Properties for loginRepository() Params | |
public string sCnnctPt { get; set; } | |
// Properties for login() Params | |
public string sMdCnnctPt { get; set; } | |
public string sIhCnnctPt { get; set; } | |
public string sTsCnnctPt { get; set; } | |
public string sPnLCnnctPt { get; set; } | |
// Additional property | |
public string Server { get; set; } | |
} | |
public class RithmicParamsList : List<RithmicParams> | |
{ | |
private RithmicParamsList() | |
{ | |
} | |
public static RithmicParamsList ParamsList { get; } = new RithmicParamsList | |
{ | |
""" | |
for app_name, params_dict in all_params.items(): | |
code += f""" | |
new RithmicParams | |
{{ | |
// {app_name} | |
""" | |
for param_name, param_value in params_dict.items(): | |
code += f""" | |
{param_name} = "{param_value}", | |
""" | |
code += "}," | |
code += """ | |
}; | |
} | |
} | |
""" | |
with open("RithmicConnection.cs", "w") as f: | |
f.write(code) | |
# ... (rest of the script remains unchanged) | |
if __name__ == '__main__': | |
files = get_files() | |
all_params = {} | |
for file in files: | |
# the key is everything until _connection_params.txt | |
file_key = file.split('_connection_params.txt')[0].replace('_', ' ') | |
all_params[file_key] = get_params(file) | |
all_params[file_key]['Server'] = file_key | |
print(all_params) | |
generate_csharp_code(all_params) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment