sunank200 commented on code in PR #48403:
URL: https://github.com/apache/airflow/pull/48403#discussion_r2018624287
##########
airflow-core/src/airflow/configuration.py:
##########
@@ -549,6 +573,88 @@ def _write_value(
if needs_separation:
file.write("\n")
+ def write_custom_config(
+ self,
+ file: IO[str],
+ comment_out_defaults: bool = True,
+ include_descriptions: bool = True,
+ extra_spacing: bool = True,
+ modifications: ConfigModifications | None = None,
+ ) -> None:
+ """
+ Write a configuration file using a ConfigModifications object.
+
+ This method includes only options from the current airflow.cfg. For
each option:
+ - If it's marked for removal, omit it.
+ - If renamed, output it under its new name and add a comment
indicating its original location.
+ - If a default update is specified, apply the new default and output
the option as a commented line.
+ - Otherwise, if the current value equals the default and
comment_out_defaults is True, output it as a comment.
+ Options absent from the current airflow.cfg are omitted.
+
+ :param file: File to write the configuration.
+ :param comment_out_defaults: If True, options whose value equals the
default are written as comments.
+ :param include_descriptions: Whether to include section descriptions.
+ :param extra_spacing: Whether to insert an extra blank line after each
option.
+ :param modifications: ConfigModifications instance with rename,
remove, and default updates.
+ """
+ modifications = modifications or ConfigModifications()
+ output: dict[str, list[tuple[str, str, bool, str]]] = {}
+
+ for section in self._sections: # type: ignore[attr-defined] #
accessing _sections from ConfigParser
+ for option, orig_value in self._sections[section].items(): #
type: ignore[attr-defined]
+ key = (section.lower(), option.lower())
+ if key in modifications.remove:
+ continue
+
+ mod_comment = ""
+ if key in modifications.rename:
+ new_sec, new_opt = modifications.rename[key]
+ effective_section = new_sec
+ effective_option = new_opt
+ mod_comment += f"# Renamed from {section}.{option}\n"
+ else:
+ effective_section = section
+ effective_option = option
+
+ value = orig_value
+ if key in modifications.default_updates:
+ mod_comment += (
+ f"# Default updated from {orig_value} to
{modifications.default_updates[key]}\n"
+ )
+ value = modifications.default_updates[key]
+
+ default_value = self.get_default_value(effective_section,
effective_option, fallback="")
+ is_default = str(value) == str(default_value)
+ output.setdefault(effective_section.lower(), []).append(
+ (effective_option, str(value), is_default, mod_comment)
+ )
+
+ for section, options in output.items():
+ section_buffer = StringIO()
+ section_buffer.write(f"[{section}]\n")
+ if include_descriptions:
+ description = self.configuration_description.get(section,
{}).get("description", "")
+ if description:
+ for line in description.splitlines():
+ section_buffer.write(f"# {line}\n")
+ section_buffer.write("\n")
+ for option, value_str, is_default, mod_comment in options:
+ key = (section.lower(), option.lower())
+ if key in modifications.default_updates and
comment_out_defaults:
+ section_buffer.write(f"# {option} = {value_str}\n")
+ else:
+ if mod_comment:
+ section_buffer.write(mod_comment)
+ if is_default and comment_out_defaults:
+ section_buffer.write(f"# {option} = {value_str}\n")
+ else:
+ section_buffer.write(f"{option} = {value_str}\n")
+ if extra_spacing:
+ section_buffer.write("\n")
+ content = section_buffer.getvalue().strip()
+ if content:
+ file.write(content + "\n\n")
Review Comment:
Changed it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]