pyinfra
pyinfra copied to clipboard
Text file manipulation that is simpler than files.Block
I think that the files.Block operation is taking on too much, and also too little. It handles insert, append, replace and delete, along with block markers, backup and file creation. But it doesn't handle some of the other things that the Ansible lineinfile module handles such as validation, mode, existence check and lines w/o markers.
What I'd like to see is a set of small, single purpose operators for simple text file operations that can be composed as callbacks within an outer files.modify() call. The outer call could handle backup, validation, etc. The inner operators would be for actual text operations such as insert, replace, etc. Markers could even be an optional function that wraps edit operator(s). By using Python as your scripting language you get the ability to compose operators in this way. That's a huge advantage over Ansible's miserable attempt to build a DSL in YAML.
This approach would be much more in line with the time-honored Unix philosophy of simple composable utilities. It would allow more flexibility, such as creating a single backup file and performing many text operations in one "transaction". The individual text operators could be based on the ed editor, which is rather simpler than sed, and by being single purpose they could be simple both to use and to code.