1
1
"""
2
- Copyright (c) 2020, Oracle Corporation and/or its affiliates.
2
+ Copyright (c) 2020, 2021 Oracle Corporation and/or its affiliates.
3
3
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
4
4
5
5
Methods for template substitution.
@@ -41,7 +41,7 @@ def create_file_from_resource(resource_path, template_hash, output_file, excepti
41
41
__logger .throwing (ex , class_name = __class_name , method_name = _method_name )
42
42
raise ex
43
43
44
- _create_file_from_stream (template_stream , template_hash , output_file , exception_type )
44
+ _create_file_from_stream (template_stream , template_hash , output_file )
45
45
46
46
47
47
def create_file_from_file (file_path , template_hash , output_file , exception_type ):
@@ -58,49 +58,87 @@ def create_file_from_file(file_path, template_hash, output_file, exception_type)
58
58
try :
59
59
template_stream = FileUtils .getFileAsStream (file_path )
60
60
if template_stream is not None :
61
- _create_file_from_stream (template_stream , template_hash , output_file , exception_type )
61
+ _create_file_from_stream (template_stream , template_hash , output_file )
62
62
except (IOException , IllegalArgumentException ), ie :
63
63
ex = exception_helper .create_exception (exception_type , 'WLSDPLY-01666' , file_path , ie )
64
64
__logger .throwing (ex , class_name = __class_name , method_name = _method_name )
65
65
raise ex
66
66
67
67
68
- def _create_file_from_stream (template_stream , template_hash , output_file , exception_type ):
68
+ def _create_file_from_stream (template_stream , template_hash , output_file ):
69
69
template_reader = BufferedReader (InputStreamReader (template_stream ))
70
70
file_writer = open (output_file .getPath (), "w" )
71
71
72
- current_block_key = None
72
+ block_key = None
73
73
block_lines = []
74
74
75
- more = True
76
- while more :
75
+ line = ''
76
+ while line is not None :
77
77
line = template_reader .readLine ()
78
78
if line is not None :
79
79
block_start_key = _get_block_start_key (line )
80
- block_end_key = _get_block_end_key (line )
81
80
82
- # if this is a nested block start, continue and add without substitution
83
- if (block_start_key is not None ) and (current_block_key is None ):
84
- current_block_key = block_start_key
85
- block_lines = []
81
+ # if inside a block, collect lines until end key is found, then process the block.
82
+ if block_key is not None :
83
+ block_end_key = _get_block_end_key (line )
84
+ if block_end_key == block_key :
85
+ _process_block (block_key , block_lines , template_hash , file_writer )
86
+ block_key = None
87
+ else :
88
+ block_lines .append (line )
86
89
87
- # if this is a nested block end, continue and add without substitution
88
- elif ( block_end_key == current_block_key ) and ( current_block_key is not None ) :
89
- _write_block ( current_block_key , block_lines , template_hash , file_writer )
90
- current_block_key = None
90
+ # if this is a block start, begin collecting block lines
91
+ elif block_start_key is not None :
92
+ block_key = block_start_key
93
+ block_lines = []
91
94
95
+ # otherwise, substitute and write the line
92
96
else :
93
97
line = _substitute_line (line , template_hash )
98
+ file_writer .write (line + "\n " )
94
99
95
- if current_block_key is not None :
96
- block_lines .append (line )
100
+ file_writer .close ()
101
+
102
+
103
+ def _process_block (block_key , template_lines , template_hash , file_writer ):
104
+ value = dictionary_utils .get_element (template_hash , block_key )
105
+
106
+ if value is None :
107
+ return
108
+
109
+ if not isinstance (value , list ):
110
+ value = [value ]
111
+
112
+ for list_element in value :
113
+ nested_block_key = None
114
+ nested_block_lines = []
115
+
116
+ for line in template_lines :
117
+ block_start_key = _get_block_start_key (line )
118
+
119
+ # if inside a block, collect lines until end key is found,
120
+ # then process the block with an updated hash.
121
+ if nested_block_key is not None :
122
+ block_end_key = _get_block_end_key (line )
123
+ if block_end_key == nested_block_key :
124
+ nested_hash = dict (template_hash )
125
+ if isinstance (list_element , dict ):
126
+ nested_hash .update (list_element )
127
+ _process_block (nested_block_key , nested_block_lines , nested_hash , file_writer )
128
+ nested_block_key = None
97
129
else :
98
- file_writer . write (line + " \n " )
130
+ nested_block_lines . append (line )
99
131
100
- else :
101
- more = False
132
+ # if this is a block start, begin collecting block lines
133
+ elif block_start_key is not None :
134
+ nested_block_key = block_start_key
135
+ nested_block_lines = []
102
136
103
- file_writer .close ()
137
+ # otherwise, substitute and write the line
138
+ else :
139
+ if isinstance (list_element , dict ):
140
+ line = _substitute_line (line , list_element )
141
+ file_writer .write (line + "\n " )
104
142
105
143
106
144
def _get_block_start_key (line ):
@@ -142,31 +180,3 @@ def _substitute_line(line, template_hash):
142
180
if replacement is not None :
143
181
line = line .replace (token , replacement )
144
182
return line
145
-
146
-
147
- def _write_block (key , lines , template_hash , file_writer ):
148
- """
149
- Write a block of lines to the file writer, making substitutions as necessary.
150
- This method does not currently handle nested blocks.
151
- :param key: the key of this block
152
- :param lines: the lines to be output
153
- :param template_hash: the parent hash
154
- :param file_writer: to write the output
155
- """
156
- value = dictionary_utils .get_element (template_hash , key )
157
-
158
- # skip block for value of False, None, or empty collection
159
- if not value :
160
- return
161
-
162
- # if value is not a list, make it a list with one item
163
- if not isinstance (value , list ):
164
- value = [value ]
165
-
166
- for list_element in value :
167
- for line in lines :
168
- # this does not account for nested blocks
169
-
170
- if isinstance (list_element , dict ):
171
- line = _substitute_line (line , list_element )
172
- file_writer .write (line + "\n " )
0 commit comments