Пример #1
0
def parseSubscriberCSVFile(subscriberdata, composer, header_row_present=False, delimiter=csv_delimiter):
    """parses file containing subscriber data

    returns list of dictionaries with subscriber data according to composer"""
    properties = component.getUtility(IPropertiesTool)
    charset = properties.site_properties.getProperty("default_charset", "utf-8").upper()
    try:
        data = cStringIO.StringIO(subscriberdata)
        reader = csv.reader(data, delimiter=str(delimiter))
        subscriberslist = []
        errorcandidates = []
        for index, parsedline in enumerate(reader):
            if index == 0:
                if header_row_present:
                    fields = parsedline
                    continue
                else:
                    fields = field.Fields(composer.schema).keys()
            if len(parsedline) < len(fields):
                pass
            else:
                try:
                    subscriber = dict(zip(fields, map(lambda x: x.decode(charset), parsedline)))
                    subscriber["email"] = subscriber["email"].lower()
                    check_email(subscriber["email"])
                except:
                    errorcandidates.append(subscriber["email"])
                else:
                    subscriberslist.append(subscriber)
        return subscriberslist, errorcandidates
    except Exception, e:
        return _(u"Error importing subscriber file. %s" % e), []
Пример #2
0
def parseSubscriberCSVFile(subscriberdata,
                           composer,
                           header_row_present=False,
                           delimiter=csv_delimiter):
    """parses file containing subscriber data

    returns list of dictionaries with subscriber data according to composer"""
    properties = component.getUtility(IPropertiesTool)
    charset = properties.site_properties.getProperty('default_charset',
                                                     'utf-8').upper()
    try:
        data = cStringIO.StringIO(subscriberdata)
        reader = csv.reader(data, delimiter=str(delimiter))
        subscriberslist = []
        errorcandidates = []
        for index, parsedline in enumerate(reader):
            if index == 0:
                if header_row_present:
                    fields = parsedline
                    continue
                else:
                    fields = field.Fields(composer.schema).keys()
            if len(parsedline) < len(fields):
                pass
            else:
                try:
                    subscriber = dict(zip(fields,\
                       map(lambda x:x.decode(charset), parsedline)))
                    subscriber['email'] = subscriber['email'].lower()
                    check_email(subscriber['email'])
                except:
                    errorcandidates.append(subscriber['email'])
                else:
                    subscriberslist.append(subscriber)
        return subscriberslist, errorcandidates
    except Exception, e:
        return _(u"Error importing subscriber file. %s" % e), []
Пример #3
0
def parseSubscriberCSVFile(subscriberdata, composer,
                           header_row_present=False,
                           delimiter=csv_delimiter):
    """parses file containing subscriber data

    returns list of dictionaries with subscriber data according to composer"""
    properties = component.getUtility(IPropertiesTool)
    charset = properties.site_properties.getProperty('default_charset',
                                                     'utf-8').upper()
    try:
        data = cStringIO.StringIO(subscriberdata)
        reader = csv.reader(data, delimiter=str(delimiter))
        subscriberslist = []
        errorcandidates = []
        sections = []
        for index, parsedline in enumerate(reader):
            if index == 0:
                if header_row_present:
                    fields = parsedline
                else:
                    fields = field.Fields(composer.schema).keys()
                    # could be import old csv that does not have sections column
                    if len(parsedline) > len(fields):
                        section_length = len(parsedline) - len(fields)
                        for i in range(section_length):
                            fields.append('section')
                # combine all the sections into one section list
                section_row_fields = []
                section_index = -1
                for i in range(len(fields)):
                    if fields[i] != 'section':
                        section_row_fields.append(fields[i])
                    elif section_index < 0:
                        section_index = i
                        section_row_fields.append(fields[i])
                if header_row_present:
                    continue

            if len(parsedline) < len(fields):
                pass
            else:
                try:
                    # combine all the sections into one section list
                    new_fields = fields
                    old_parsedline = parsedline
                    parsedline = []
                    section_field = []
                    for i in range(len(fields)):
                        if fields[i] != 'section':
                            parsedline.append(old_parsedline[i])
                        else:
                            section_field.append(
                                old_parsedline[i].decode(charset))
                    if section_index > -1:
                        new_fields = section_row_fields
                        parsedline.insert(section_index, section_field)

                    subscriber = dict(
                        zip(new_fields, map(lambda x: x.decode(charset)
                            if isinstance(x, basestring)
                            else x, parsedline)))

                    # splits 'sections' data from subscriber
                    if 'section' in subscriber:
                        section = dict([
                            ('email', subscriber['email']),
                            ('section', subscriber['section'])])
                        sections.append(section)
                        del subscriber['section']

                    subscriber['email'] = subscriber['email'].lower()
                    check_email(subscriber['email'])
                except:
                    errorcandidates.append(subscriber['email'])
                else:
                    subscriberslist.append(subscriber)
        return subscriberslist, errorcandidates, sections
    except Exception, e:
        return _(u"Error importing subscriber file. %s" % e), []
Пример #4
0
def parseSubscriberCSVFile(subscriberdata,
                           composer,
                           header_row_present=False,
                           delimiter=csv_delimiter):
    """parses file containing subscriber data

    returns list of dictionaries with subscriber data according to composer"""
    properties = component.getUtility(IPropertiesTool)
    charset = properties.site_properties.getProperty('default_charset',
                                                     'utf-8').upper()
    try:
        data = cStringIO.StringIO(subscriberdata)
        reader = csv.reader(data, delimiter=str(delimiter))
        subscriberslist = []
        errorcandidates = []
        sections = []
        for index, parsedline in enumerate(reader):
            if index == 0:
                if header_row_present:
                    fields = parsedline
                else:
                    fields = field.Fields(composer.schema).keys()
                    # could be import old csv that does not have sections column
                    if len(parsedline) > len(fields):
                        section_length = len(parsedline) - len(fields)
                        for i in range(section_length):
                            fields.append('section')
                # combine all the sections into one section list
                section_row_fields = []
                section_index = -1
                for i in range(len(fields)):
                    if fields[i] != 'section':
                        section_row_fields.append(fields[i])
                    elif section_index < 0:
                        section_index = i
                        section_row_fields.append(fields[i])
                if header_row_present:
                    continue

            if len(parsedline) < len(fields):
                pass
            else:
                try:
                    # combine all the sections into one section list
                    new_fields = fields
                    old_parsedline = parsedline
                    parsedline = []
                    section_field = []
                    for i in range(len(fields)):
                        if fields[i] != 'section':
                            parsedline.append(old_parsedline[i])
                        else:
                            section_field.append(
                                old_parsedline[i].decode(charset))
                    if section_index > -1:
                        new_fields = section_row_fields
                        parsedline.insert(section_index, section_field)

                    subscriber = dict(
                        zip(
                            new_fields,
                            map(
                                lambda x: x.decode(charset)
                                if isinstance(x, basestring) else x,
                                parsedline)))

                    # splits 'sections' data from subscriber
                    if 'section' in subscriber:
                        section = dict([('email', subscriber['email']),
                                        ('section', subscriber['section'])])
                        sections.append(section)
                        del subscriber['section']

                    subscriber['email'] = subscriber['email'].lower()
                    check_email(subscriber['email'])
                except:
                    errorcandidates.append(subscriber['email'])
                else:
                    subscriberslist.append(subscriber)
        return subscriberslist, errorcandidates, sections
    except Exception, e:
        return _(u"Error importing subscriber file. %s" % e), []