-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AUTOGENERATED] Add worker hrl with definitions of fields for parser
- Loading branch information
Showing
5 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
% This is an auto generated .hrl file | ||
-define(NERLPLANNER_VERSION,"1.0.0"). | ||
|
||
-define(KEY_MODEL_TYPE,modelType). | ||
-define(KEY_LAYER_SIZES_LIST,layersSizes). | ||
-define(KEY_LAYER_TYPES_LIST,layerTypesList). | ||
-define(KEY_LAYERS_FUNCTIONS,layers_functions). | ||
-define(KEY_LOSS_METHOD,lossMethod). | ||
-define(KEY_LEARNING_RATE,lr). | ||
-define(KEY_EPOCHS,epochs). | ||
-define(KEY_OPTIMIZER_TYPE,optimizer). |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import argparse | ||
import os | ||
from ErlHeadersExporterDefs import * | ||
from JsonElementWorkerDefinitions import * | ||
from Definitions import VERSION as NERLPLANNER_VERSION | ||
|
||
EMPTY_LINE = '\n' | ||
DEBUG = False | ||
|
||
def gen_erlang_exporter_logger(message : str): | ||
if DEBUG: | ||
print(f'[NERLPLANNER][AUTO_HEADER_GENERATOR][DEBUG] {message}') | ||
|
||
def gen_worker_fields_hrl(header_path : str, debug : bool = False): | ||
global DEBUG | ||
DEBUG = debug | ||
|
||
auto_generated_header = AutoGeneratedHeader() | ||
gen_erlang_exporter_logger(auto_generated_header.generate_code()) | ||
|
||
nerlplanner_version = Definition('NERLPLANNER_VERSION', f'"{NERLPLANNER_VERSION}"') | ||
gen_erlang_exporter_logger(nerlplanner_version.generate_code()) | ||
|
||
fields_list_vals = [KEY_MODEL_TYPE, KEY_LAYER_SIZES_LIST, | ||
KEY_LAYER_TYPES_LIST, KEY_LAYERS_FUNCTIONS, | ||
KEY_LOSS_METHOD, KEY_LEARNING_RATE, | ||
KEY_EPOCHS, KEY_OPTIMIZER_TYPE] | ||
fields_list_strs = ['KEY_MODEL_TYPE', 'KEY_LAYER_SIZES_LIST', | ||
'KEY_LAYER_TYPES_LIST', 'KEY_LAYERS_FUNCTIONS', | ||
'KEY_LOSS_METHOD', 'KEY_LEARNING_RATE', | ||
'KEY_EPOCHS', 'KEY_OPTIMIZER_TYPE'] | ||
|
||
fields_list_defs = [ Definition(fields_list_strs[idx], f'{Definition.assert_not_atom(fields_list_vals[idx])}') for idx in range(len(fields_list_vals))] | ||
[gen_erlang_exporter_logger(x.generate_code()) for x in fields_list_defs] | ||
|
||
|
||
if os.path.dirname(header_path): | ||
os.makedirs(os.path.dirname(header_path), exist_ok=True) | ||
|
||
with open(header_path, 'w') as f: | ||
f.write(auto_generated_header.generate_code()) | ||
f.write(nerlplanner_version.generate_code()) | ||
f.write(EMPTY_LINE) | ||
[f.write(x.generate_code()) for x in fields_list_defs] | ||
|
||
|
||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Generate C++ header file for nerlPlanner') | ||
parser.add_argument('-o', '--output', help='output header file path', required=True) | ||
parser.add_argument('-d', '--debug', help='debug mode', action='store_true') | ||
args = parser.parse_args() | ||
gen_worker_fields_hrl(args.output, args.debug) | ||
|
||
if __name__=="__main__": | ||
main() | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from collections import OrderedDict | ||
|
||
# Generates automated Erlang code! | ||
|
||
class AutoGeneratedHeader: | ||
def __init__(self) -> None: | ||
pass | ||
|
||
def generate_code(self): | ||
return f'% This is an auto generated .hrl file\n' | ||
|
||
class ModuleName: | ||
def __init__(self, module_name: str = '') -> None: | ||
self.module_name = module_name | ||
|
||
def generate_code(self): | ||
return f'-module({self.module_name}).\n ' | ||
|
||
class Record: | ||
def __init__(self, record_name : str, in_ordered_dict : OrderedDict, all_caps : bool = False, prefix = '') -> None: | ||
self.record_name = record_name.lower() | ||
self.ordered_dict = in_ordered_dict | ||
self.prefix = prefix | ||
self.all_caps = all_caps | ||
|
||
def generate_code(self): | ||
code = f'-record({self.record_name}'+'{' | ||
for key, value in self.ordered_dict.items(): | ||
key = key.upper() if self.all_caps else key | ||
last_key = list(self.ordered_dict.keys())[-1].upper() if self.all_caps else list(self.ordered_dict.keys())[-1] | ||
code += f'{self.prefix}_{key} = {value}' | ||
code += ',' if last_key != key else '' | ||
return code.replace('-','_') + '}).\n' | ||
|
||
class Definition: | ||
def __init__(self, macro, value : str ) -> None: | ||
self.macro = macro | ||
self.value = value | ||
|
||
def force_atom_value(value : str): | ||
# validates that first letter is lower case which is required by atoms in Erlang | ||
return value[0].lower() + value[1:] if value else value | ||
|
||
def assert_not_atom(value : str): | ||
if value: | ||
assert value[0].islower(), f'Value {value} is not an Erlang atom' | ||
else: | ||
raise 'Erlang atom cannot be None or empty string!' | ||
return value | ||
|
||
def generate_code(self): | ||
return f'-define({self.macro},{self.value}).\n' |
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