So here's my shellconfig.py, presented for comment / inspiration.
A couple notes:
* This is an interesting construct that I wasn't aware of:
def quote(s):
for c in s:
if c not in _safechars:
break
else:
return s
...
Apparently, if you have an 'else:' block after 'for:', it gets executed
if you 'break' out of the loop. Neat!
But _parseline is really the interesting part:
def _parseline(self, line):
s = line.strip()
if '#' in s:
s = s[:s.find('#')] # remove from comment to EOL
s = s.strip() # and any unnecessary whitespace
key, eq, val = s.partition('=')
if self.read_unquote:
val = unquote(val)
if key != ' and eq == '=':
return (key, val)
else:
return (None, None)
* You want to remove the comment before the partition(), otherwise this
line would end up with a weird value:
GRAPHICS="vnc" # other choices: "spice", "none"
* quote() and unquote() use pipes._safechars and shlex.split(),
respectively.
* pipes.quote() doesn't work because it puts single-quotes around '$',
assuming that the given string is a literal filename
* There's no write() method but it's pretty obvious - you'd just open a
file and do f.write(str(config)) or similar.
Hope that's useful to somebody,
-w
_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
07-02-2012, 01:52 PM
John Reiser
Parsing shell-style config files
On 06/29/2012, Will Woods wrote:
> So here's my shellconfig.py, presented for comment / inspiration.
[snip]
> Apparently, if you have an 'else:' block after 'for:', it gets executed
> if you 'break' out of the loop. Neat!
Yes, that part is useful and cute.
It would be helpful to give a URL which documents the syntax.
Also, please insert a comment with a table of special characters. Such as:
'
' (newline) statement terminator
# (hash, sharp, pound) introduces comment (to end of line)
(backslash) suppress special interpretation of next character
" (double quote) forces interpretation as character string (not as number)
Also, please mention shell syntax which appears to be omitted. Such as:
; (semicolon) statement separator [thus only one statement per line]
$ (dollar) lookup and expand previous key
' (single quote) start character string which suppresses interpretation of $
It seems to me that there may be some rough edges:
No error detection, no error or warning messages:
Unterminated double quote [except by EndOfLine]
No newline at EndOfFile [often because of emacs editing]
Continuation lines (backslash newline: yes or no?; line length restrictions?).
Cannot have '#' in a string (comment takes precedence).
These also deserve comment (for maintenance, to set expectations.)
Python's lack of an equivalent to LISP "(read)" [namely: runtime operator
which converts from character string into language] surely is a handicap.
--
_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list