News for hackers. Stuff that matters.
Re:GNU Shepherd by tmalsburg2 on Saturday June 03, 2017 @06:40AM (#14475768)
I'm curious. Why are you not a huge fan of it?
Re:GNU Shepherd by _wmd on Saturday June 03, 2017 @09:37AM (#14476315)
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.

Re:GNU Shepherd by badsectoracula on Saturday June 03, 2017 @11:16AM (#14476755)
So, 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.

Re:GNU Shepherd by cyphar on Saturday June 03, 2017 @10:01AM (#14476423)
It'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).

Re:GNU Shepherd by eriknstr on Saturday June 03, 2017 @11:49AM (#14476921)
> both of the most widely-used editors (emacs and vim)

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.

Re:GNU Shepherd by cyphar on Saturday June 03, 2017 @12:03PM (#14476984)
The 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.
Re:GNU Shepherd by kazinator on Saturday June 03, 2017 @10:56AM (#14476662)
> 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!".

Re:GNU Shepherd by mcguire on Saturday June 03, 2017 @10:52AM (#14476639)
As 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.

Re:GNU Shepherd by openasocket on Saturday June 03, 2017 @11:54AM (#14476945)
So 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.
Re:GNU Shepherd by avar on Saturday June 03, 2017 @01:02PM (#14477258)
This isn't applicable to scheme as a config format, but some config formats are line based and have programmatic tools to manipulate them, e.g. the .ini-like format Git uses, try adding some comments to ~/.gitconfig and manipulating it with `git config --global <...>`, it'll all be preserved.

You will lose any comments like this though:

    [user]
        name = My Name ; that's my name!
Programming is hard in the face of arbitrary user data.
Re:GNU Shepherd by _wmd on Saturday June 03, 2017 @12:08PM (#14477004)

    $ 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>
Re:GNU Shepherd by openasocket on Saturday June 03, 2017 @02:18PM (#14477645)
... 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.
Re:GNU Shepherd by _wmd on Saturday June 03, 2017 @02:34PM (#14477723)
The suggestion was to use XSLT to transform Scheme syntax
Re:GNU Shepherd by openasocket on Saturday June 03, 2017 @02:44PM (#14477787)
My 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.
Re:GNU Shepherd by mcguire on Sunday June 04, 2017 @09:06PM (#14484657)
XPath and a tree transformation? Works well.
Re:GNU Shepherd by hashkb on Saturday June 03, 2017 @09:54AM (#14476383)
You 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.

Re:GNU Shepherd by phkahler on Saturday June 03, 2017 @10:25AM (#14476503)
>> 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.

Re:GNU Shepherd by hashkb on Saturday June 03, 2017 @10:32AM (#14476534)
Maybe 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.

Re:GNU Shepherd by wtetzner on Sunday June 04, 2017 @07:28AM (#14480837)
Are you suggesting that Lisp programs aren't readable by humans?
Re:GNU Shepherd by _wmd on Saturday June 03, 2017 @10:32AM (#14476537)
So 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.

Re:GNU Shepherd by orangeshark on Saturday June 03, 2017 @12:38PM (#14477140)
So 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.
Re:GNU Shepherd by _wmd on Saturday June 03, 2017 @01:47PM (#14477481)
> 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.

Re:GNU Shepherd by openasocket on Saturday June 03, 2017 @02:52PM (#14477836)
You're complaining that there isn't good tooling around the config file format for a project that is described as "in its early development stages" and provides "alpha releases". If you're talking about using this "while building business solutions on somebody else's dime" your getting way ahead of yourself. I'm sure they will expose a library for parsing and editing the config format before a general release.
[dead]
Re:GNU Shepherd by openasocket on Saturday June 03, 2017 @05:42PM (#14478652)
You're right, I can't find a python library with the exact functionality you want. But a solution does exist, you would just have to write it yourself. It takes about 200 lines of python, less if you use a parser generator. But if you don't want to do that (nothing wrong with that! Why do work when you don't have to, after all), well then you'll have to either ask someone else to do it for you or just not use this project, assuming this functionality is critical for you. Or you could use sed or a templating system, up to you.
Re:GNU Shepherd by _wmd on Saturday June 03, 2017 @06:07PM (#14478739)
I have little doubt it could be written, but at risk of going blue in the face, this was irrelevant to the original question ("what do you dislike about this syntax?"). Never mind, 'truth' has won out in the end regardless, and I note for future that the presence of Lisp on a CV is fair reason to pause and reconsider.
Re:GNU Shepherd by openasocket on Saturday June 03, 2017 @06:23PM (#14478804)
"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?"

Sorry for trying to answer your question then...

Re:GNU Shepherd by michaelmrose on Saturday June 03, 2017 @03:12PM (#14477931)
Regarding 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.
Re:GNU Shepherd by Slackwise on Saturday June 03, 2017 @05:14PM (#14478537)
And 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.

Re:GNU Shepherd by michaelmrose on Saturday June 03, 2017 @03:09PM (#14477920)
My 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.

Re:GNU Shepherd by blowski on Saturday June 03, 2017 @11:02AM (#14476683)
I have no dog in this fight, but I downvoted because your subsequent edits are not at all constructive.
Re:GNU Shepherd by brians on Saturday June 03, 2017 @09:39AM (#14476321)
It's Guile s-expressions, so at least there's a good C reader and writer?