FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Redhat > Fedora User

 
 
LinkBack Thread Tools
 
Old 06-29-2012, 08:33 PM
Will Woods
 
Default Parsing shell-style config files

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
 
Old 07-02-2012, 01:52 PM
John Reiser
 
Default 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
 

Thread Tools




All times are GMT. The time now is 02:58 AM.

VBulletin, Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2007 - 2008, www.linux-archive.org