Use shlex for rc.conf parsing.

This makes the line parsing a lot more robust (and easier to read).
Code supplied by @dhozac, thanks!

Remove re import because this is not used anywhere.
This commit is contained in:
Patrik Lundin 2012-11-25 03:24:49 +01:00
parent 672b83b54b
commit 543c5d0de2

11
service
View file

@ -72,8 +72,8 @@ examples:
import platform import platform
import os import os
import re
import tempfile import tempfile
import shlex
class Service(object): class Service(object):
""" """
@ -209,11 +209,10 @@ class Service(object):
# Build a list containing the possibly modified file. # Build a list containing the possibly modified file.
for rcline in RCFILE: for rcline in RCFILE:
# Only parse non-comment and non-empty lines. # Parse line removing whitespaces, quotes, etc.
if not re.search('^(#.*)?$', rcline): rcarray = shlex.split(rcline, comments=True)
key = rcline.split('=')[0] if len(rcarray) >= 1 and '=' in rcarray[0]:
# We need to strip any newline and " signs from the value. (key, value) = rcarray[0].split("=", 1)
value = rcline.split('=')[1].strip('\n"')
if key == self.rcconf_key: if key == self.rcconf_key:
if value == self.rcconf_value: if value == self.rcconf_value:
# Since the proper entry already exists we can stop iterating. # Since the proper entry already exists we can stop iterating.