|
11 | 11 | from java.io import FileInputStream
|
12 | 12 | from java.io import FileOutputStream
|
13 | 13 | from java.io import FileReader
|
| 14 | +from java.io import PrintWriter |
14 | 15 | from java.io import IOException
|
15 | 16 | from java.util import Properties
|
16 | 17 |
|
@@ -70,23 +71,56 @@ def write_variables(program_name, variable_map, file_path, append=False):
|
70 | 71 | """
|
71 | 72 | _method_name = 'write_variables'
|
72 | 73 | _logger.entering(program_name, file_path, append, class_name=_class_name, method_name=_method_name)
|
73 |
| - props = Properties() |
74 |
| - for key, value in variable_map.items(): |
75 |
| - props.setProperty(key, value) |
| 74 | + if isinstance(variable_map, OrderedDict): |
| 75 | + write_ordered_variables(program_name, variable_map, file_path, append) |
| 76 | + else: |
| 77 | + props = Properties() |
| 78 | + for key, value in variable_map.items(): |
| 79 | + props.setProperty(key, value) |
| 80 | + |
| 81 | + comment = exception_helper.get_message('WLSDPLY-01731', program_name) |
| 82 | + output_stream = None |
| 83 | + try: |
| 84 | + output_stream = FileOutputStream(File(file_path), Boolean(append)) |
| 85 | + props.store(output_stream, comment) |
| 86 | + output_stream.close() |
| 87 | + except IOException, ioe: |
| 88 | + _logger.fine('WLSDPLY-20007', file_path, ioe.getLocalizedMessage()) |
| 89 | + ex = exception_helper.create_variable_exception('WLSDPLY-20007', file_path, |
| 90 | + ioe.getLocalizedMessage(), error=ioe) |
| 91 | + _logger.throwing(ex, class_name=_class_name, method_name=_method_name) |
| 92 | + if output_stream is not None: |
| 93 | + output_stream.close() |
| 94 | + raise ex |
| 95 | + _logger.exiting(class_name=_class_name, method_name=_method_name) |
| 96 | + return |
| 97 | + |
76 | 98 |
|
77 |
| - comment = exception_helper.get_message('WLSDPLY-01731', program_name) |
78 |
| - output_stream = None |
| 99 | +def write_ordered_variables(program_name, variable_map, file_path, append=False): |
| 100 | + """ |
| 101 | + Write variables to file while preserving order of the variables. |
| 102 | + :param program_name: name of the calling program |
| 103 | + :param variable_map: map or variable properties to write to file |
| 104 | + :param file_path: the file to which to write the properties |
| 105 | + :param append: defaults to False. Append properties to the end of file |
| 106 | + :raises VariableException if an error occurs while storing the variables in the file |
| 107 | + """ |
| 108 | + _method_name = 'write_ordered_variables' |
| 109 | + _logger.entering(program_name, file_path, append, class_name=_class_name, method_name=_method_name) |
| 110 | + pw = None |
79 | 111 | try:
|
80 |
| - output_stream = FileOutputStream(File(file_path), Boolean(append)) |
81 |
| - props.store(output_stream, comment) |
82 |
| - output_stream.close() |
| 112 | + pw = PrintWriter(FileOutputStream(File(file_path), Boolean(append)), Boolean('true')) |
| 113 | + for key, value in variable_map.iteritems(): |
| 114 | + formatted = '%s=%s' % (key, value) |
| 115 | + pw.println(formatted) |
| 116 | + pw.close() |
83 | 117 | except IOException, ioe:
|
84 | 118 | _logger.fine('WLSDPLY-20007', file_path, ioe.getLocalizedMessage())
|
85 | 119 | ex = exception_helper.create_variable_exception('WLSDPLY-20007', file_path,
|
86 | 120 | ioe.getLocalizedMessage(), error=ioe)
|
87 | 121 | _logger.throwing(ex, class_name=_class_name, method_name=_method_name)
|
88 |
| - if output_stream is not None: |
89 |
| - output_stream.close() |
| 122 | + if pw is not None: |
| 123 | + pw.close() |
90 | 124 | raise ex
|
91 | 125 | _logger.exiting(class_name=_class_name, method_name=_method_name)
|
92 | 126 | return
|
|
0 commit comments