Use the csv module instead of my own hackish lexer.

The first character in a line is used to determine how the line will be
quoted. If it begins with no quote we use '. If it begins with either
' or " we use that character. So if you have a quoted string and you don't
want it to be considered a comma-separated value put the other quote string
around the whole block.
This commit is contained in:
Rob Crittenden 2009-05-13 17:02:09 -04:00
parent 9147f0da69
commit 7ac2b8ae45

View File

@ -34,11 +34,11 @@ import ldap
import logging import logging
import krbV import krbV
import platform import platform
import shlex
import time import time
import random import random
import os import os
import fnmatch import fnmatch
import csv
class BadSyntax(Exception): class BadSyntax(Exception):
def __init__(self, value): def __init__(self, value):
@ -86,13 +86,30 @@ class LDAPUpdate:
conn = ipaldap.IPAdmin(fqdn) conn = ipaldap.IPAdmin(fqdn)
conn.do_simple_bind(bindpw=self.dm_password) conn.do_simple_bind(bindpw=self.dm_password)
conn.unbind() conn.unbind()
except ldap.CONNECT_ERROR, e: except ldap.CONNECT_ERROR:
raise RuntimeError("Unable to connect to LDAP server %s" % fqdn) raise RuntimeError("Unable to connect to LDAP server %s" % fqdn)
except ldap.SERVER_DOWN, e: except ldap.SERVER_DOWN:
raise RuntimeError("Unable to connect to LDAP server %s" % fqdn) raise RuntimeError("Unable to connect to LDAP server %s" % fqdn)
except ldap.INVALID_CREDENTIALS, e : except ldap.INVALID_CREDENTIALS:
raise RuntimeError("The password provided is incorrect for LDAP server %s" % fqdn) raise RuntimeError("The password provided is incorrect for LDAP server %s" % fqdn)
# The following 2 functions were taken from the Python
# documentation at http://docs.python.org/library/csv.html
def __utf_8_encoder(self, unicode_csv_data):
for line in unicode_csv_data:
yield line.encode('utf-8')
def __unicode_csv_reader(self, unicode_csv_data, quote_char="'", dialect=csv.excel, **kwargs):
# csv.py doesn't do Unicode; encode temporarily as UTF-8:
csv_reader = csv.reader(self.__utf_8_encoder(unicode_csv_data),
dialect=dialect, delimiter=',',
quotechar=quote_char,
skipinitialspace=True,
**kwargs)
for row in csv_reader:
# decode UTF-8 back to Unicode, cell by cell:
yield [unicode(cell, 'utf-8') for cell in row]
def __identify_arch(self): def __identify_arch(self):
"""On multi-arch systems some libraries may be in /lib64, /usr/lib64, """On multi-arch systems some libraries may be in /lib64, /usr/lib64,
etc. Determine if a suffix is needed based on the current etc. Determine if a suffix is needed based on the current
@ -111,40 +128,19 @@ class LDAPUpdate:
except KeyError, e: except KeyError, e:
raise BadSyntax("Unknown template keyword %s" % e) raise BadSyntax("Unknown template keyword %s" % e)
def __remove_quotes(self, line):
"""Remove leading and trailng double or single quotes"""
if line.startswith('"'):
line = line[1:]
if line.endswith('"'):
line = line[:-1]
if line.startswith("'"):
line = line[1:]
if line.endswith("'"):
line = line[:-1]
return line
def __parse_values(self, line): def __parse_values(self, line):
"""Parse a comma-separated string into separate values and convert them """Parse a comma-separated string into separate values and convert them
into a list. This should handle quoted-strings with embedded commas into a list. This should handle quoted-strings with embedded commas
""" """
lexer = shlex.shlex(line) if line[0] == "'":
lexer.wordchars = lexer.wordchars + ".()-" quote_char = "'"
l = [] else:
v = "" quote_char = '"'
for token in lexer: reader = self.__unicode_csv_reader([line], quote_char)
if token != ',': value = []
if v: for row in reader:
v = v + " " + token value = value + row
else: return value
v = token
else:
l.append(self.__remove_quotes(v))
v = ""
l.append(self.__remove_quotes(v))
return l
def read_file(self, filename): def read_file(self, filename):
if filename == '-': if filename == '-':