9 ms·
My first thought is how I'd parse, modify and write it back without losing user's comments, formatting, changes and suchlike. Is this some well understood forma
by _wmd 9y ago
My first thought is how I'd parse, modify and write it back without losing user's comments, formatting, changes and suchlike. Is this some well understood format with libraries for common programming languages? If not, it is just likely to proliferate more crappy config management with chunks of regexes and suchlike
Elsewhere people are saying it's just Scheme. I'm sure I could find a parser for that in Python, and maybe if I'm really lucky that parser will record whitespace and comment nodes, and have a reverse transformation, but do I really want to be writing error prone 20-line AST visitors just to tweak an integer setting? (No, I most certainly do not, and I consider design decisions like this a massive code smell that'd ward me off any further investigation of a system)
edit: downvotes? Is this really such a difficult concept?
edit 2: so apparently the big whoopsie was insulting the religion that is LISP. Yes, using a turing complete language for config syntax causes pain. Nobody does this stuff in the real world for a reason. Deal with it, meanwhile go back to obsessing over novelty code-is-data academic exercises on your obsolete Symbolics machines, you'll cause less damage that way.
- brians 9y agoIt's Guile s-expressions, so at least there's a good C reader and writer?
- hashkb 9y agoYou would just use Guile. That's the magic of the Lisp family. Edit: downvotes are because you're sort of insulting Lisp (which is older than you and all your "best practices") with an ignorant complaint.
- phkahler 9y ago>> downvotes are because you're sort of insulting Lisp (which is older than you and all your "best practices") with an ignorant complaint. I recognized it as lisp right away, but I share the complaint about the format. If you want to write a lisp program then do so. If you want to format something for human readability it's probably a good idea to do it differently. This is just a opinion of course, but I suspect it's not an uncommon one.
- hashkb 9y agoMaybe uncommon over all, but you should definitely expect the GNU project to use Guile for scripting needs. In this context, it's the obvious choice. Furthermore, anything that tricks you into learning Scheme is a good thing. Like eating vegetables.
- wtetzner 9y agoAre you suggesting that Lisp programs aren't readable by humans?
- _wmd 9y agoSo all I asked is whether it was possible to roundtrip an edit to one of these files from Python without writing an AST visitor, and without losing formatting and comments. I have absolutely no idea how this could be considered ignorant. Ignorant of the cave-dwelling depths some would go to ensure the practicality of the arcane religious icon that is LISP as a massively overgeneral solution to a problem, at the cost of all downstream users who must now "just switch to Guile" or link against chunks of a C compiler just to introspect a config? Maybe, but I'm content with that.
- orangeshark 9y agoSo the example you showed with XML uses XPath, what do you think that does? I am sure something similar can be created in python for editing lisp code as easily as this.
- _wmd 9y ago> My first thought is how I'd parse, modify and write it back without losing user's comments, formatting, changes and suchlike. Is this some well understood format with libraries for common programming languages? If not, it is just likely to proliferate more crappy config management with chunks of regexes and suchlike > Elsewhere people are saying it's just Scheme. I'm sure I could find a parser for that in Python, and maybe if I'm really lucky that parser will record whitespace and comment nodes, and have a reverse transformation, but do I really want to be writing error prone 20-line AST visitors just to tweak an integer setting? (No, I most certainly do not, and I consider design decisions like this a massive code smell that'd ward me off any further investigation of a system) XML satisfies all the above: no AST visitors, no custom parsing routines, no attempting to solve the halting problem in order to introspect a simple scalar value. What you're saying is that the same is "possible" with Scheme. At least one followup comment (finally) described an algorithm that would actually allow machine-driven file changes, but we're still left with rather than a working out of the box solution, a description of an algorithm that "might someday work" and that's not the sort of thing I want to be dealing with while building business solutions on somebody else's dime, which drives my conclusion above: this config file syntax is not for me, and my original comment explained precisely why.
- cyphar 9y agoIt's Scheme s-expressions. Parsers for it have existed for it longer than C or Unix have existed (Scheme came out in 1970, First Edition Unix in 1971), let alone Lisp parsers which have existed for even longer. EDIT: Complaining about downvotes and then going on a tirade about how Lisp users are cave-dwellers is not constructive. Personally I think your argument was a valid concern (and I just went to try to find a Scheme parser in Python to help you), but you're not helping your case. Also, like it or not, but both of the most widely-used editors (emacs and vim) both have turing-complete "configuration" languages. By your statements, that would make the majority of programmers academic cave-dwellers (oh wait... :P).
- eriknstr 9y ago> both of the most widely-used editors (emacs and vim) https://insights.stackoverflow.com/survey/2017#technology-most-popular-developer-environments-by-occupation https://insights.stackoverflow.com/survey/2017#technology-mo... If you include full-blown IDEs in your definition of editor (which I think you should), Visual Studio was the most widely used editor among all the 19,772 people that responded to this question in the Stack Overflow Developer survey 2017. Notepad++ was the second-most popular editor. Sublime Text was the third-most popular editor. Vim was the fourth-most popular editor. Visual Studio Code, IntelliJ, Atom, Eclipse, Android Studio, PHPStorm, Xcode, NetBeans and PyCharm followed. Emacs was the 14th-most popular editor.
- cyphar 9y agoThe point still stands if you substitute "both" with "two". I admit that I assumed Emacs and Vim were higher in the "leaderboard" (and I'm not sure StackOverflow is the best place to sample developers) but the point stands regardless.
- mcguire 9y agoAs others have pointed out, it's s-expressions (and possibly simply scheme) so reading and writing shouldn't be difficult. If it uses scheme comments, those will be a problem just as when transforming any programming language. I the other hand, if they syntax supports doc-strings, you're all set. Add far as AST visitors, you're probably better off with XPath/XSLT tree transformations. (Anyone know of a suitable library?) On the other hand, the don't-swim-upstream approach would be to use one of the best programming languages in existence, Scheme, you do your config management tasks directly, rather than an arm's-length parse, modify, write approach.
- kazinator 9y ago> using a turing complete language for config syntax causes pain That may sometimes be so, but only as a general consequence of "programming computers causes pain". You can do something complicated, which can have a bug; then someone comes along with a half-conceived thought like, "that solution shouldn't have been doble in the config language; therefore config language caused pain". A non-computational config language also causes pain. Sometimes to the point that a Turing-complete language is used to generate a configuration. Then you have pains like "# this was generated ... don't touch!".
- blowski 9y agoI have no dog in this fight, but I downvoted because your subsequent edits are not at all constructive.
- badsectoracula 9y agoSo, since nobody mentioned that, i'll ignore your edits and try to answer your original concern. Generally speaking, this is S-Expressions (as others have said) and S-Expressions are trivial to parse and edit - you can write a parser in C that converts this to a tree easily. Note that the comments and such are a problem regardless of the config syntax used but the solution is the same: instead of rewriting the file, edit it in-place (to do that a simple way is store the index range of each node's value in the code buffer when parsing it and when you modify the node's value update the buffer's contents - make sure you also update the other nodes' ranges if the value changes length in characters). All these assuming you want to implement the thing yourself. Which is fine, S-Expressions are one of the easiest tree syntaxes to parse. But there are already libraries that can do this for you (including full Lisp/Scheme interpreters) and some of them can even 'pretty format' the code like the quoted snipped above. So generally speaking this wont be a problem in practice.
- openasocket 9y agoSo you want to programmatically edit a config file, preserving formatting and comments, without using regexes, AST visitors, or XSLT? What's left? I seriously don't know of any other ways to manipulate data while preserving formatting and comments.
- _wmd 9y ago$ cat my-config.xml <root> <!-- i am a comment --> <thing attr="foo"> <!-- i am another comment --> <setting>value</setting> </thing> </root> $ cat c.py import lxml.etree doc = lxml.etree.fromstring(file('my-config.xml').read()) for setting in doc.xpath('.//thing/setting'): setting.text = 'set without screwing up the file' print lxml.etree.tostring(doc) $ python c.py <root> <!-- i am a comment --> <thing attr="foo"> <!-- i am another comment --> <setting>set without screwing up the file</setting> </thing> </root>
- openasocket 9y ago... but you just chewed someone out for suggesting XML. And you are doing tree walking here, you're just reducing some of the boilerplate with XPath. You can do the same thing with Scheme using car, cdr, cadr and friends.
- _wmd 9y agoThe suggestion was to use XSLT to transform Scheme syntax
- openasocket 9y agoMy mistake then. You can still use car, cdr, cadr etc. to accomplish more or less the same thing as XPath. S-expressions are a completely valid serialization format. You can't easily manipulate JSON or YAML data while preserving formatting and comments with any library I've used (though it is possible), that doesn't mean JSON and YAML are invalid.
- michaelmrose 9y agoMy first question would be in what use case do you require the ability to alter via a program the same config file that you expect the user to be able to hand edit and never have a conflict? It was my understanding that issue was best handled by having multiple files and having one take precedence over the other. Like having a global config that is pushed to the machine and allowing the user to locally override values set globally in the local config. Another solution to multiple parties needing to define configuration for the same area without stomping all over each other would be to have a directory wherein all files contained therein are applicable. That way multiple people can maintain their own particular files but some precedence has to be established. Even if you do need to do this I wouldn't think it would be that hard to modify lisp with lisp.
- michaelmrose 9y agoRegarding edit 2: Using a non turing complete language results in hacks to attain the flexibility that would have been provided by the more powerful language.
- Slackwise 9y agoAnd that was kind of the whole point with GuixSD and Shepard. Nix has limited "Nix-expressions" which resulted in a bunch of bash scripts to get around the limitations, and Systemd ends up requiring C services or bash scripts because it has a limited INI-like syntax. On the opposite end, GuixSD is entirely configured, packaged, and maintained with Guile.