Changeset 8750 for main/waeup.stress


Ignore:
Timestamp:
18 Jun 2012, 10:22:32 (12 years ago)
Author:
uli
Message:

Add support for importing CSV files automatically into an existing instance.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.stress/trunk/src/waeup/stress/scripts.py

    r8654 r8750  
     1"""Helpers when inside an instance context.
     2"""
    13import os
    24import sys
     
    911#: will be put into the root folder
    1012APP_NAME = 'stress_app'
     13
     14IMPORTERS = {
     15    'faculties.csv': 'facultyprocessor',
     16    'departments.csv': 'departmentprocessor',
     17    'courses.csv': 'courseprocessor',
     18    'certificates.csv': 'certificateprocessor',
     19    'certificatecourses.csv': 'certificatecourseprocessor',
     20    'applicantcontainers.csv': 'applicants container processor',
     21    'applicants.csv': 'applicantprocessor',
     22    }
     23
     24def configure(instance_path=''):
     25    config_file = os.path.join(
     26        instance_path, 'parts', 'etc', 'site.zcml')
     27    print "WAEUP.STRESS: configuring ZODB from %s" % (
     28        config_file,)
     29    context = config(config_file)
     30    return context
    1131
    1232def open_zodb(instance_path=''):
     
    2343    to this DB and the root folder.
    2444    """
    25     config_file = os.path.join(
    26         instance_path, 'parts', 'etc', 'site.zcml')
     45    print "WAEUP.STRESS: opening ZODB"
    2746    zodb_file = os.path.join(
    2847        instance_path, 'var', 'filestorage', 'Data.fs')
    29     print "WAEUP.STRESS: configuring ZODB from %s / %s" % (
    30         config_file, zodb_file)
    31     context = config(config_file)
    3248    db = database(zodb_file)
    3349    conn = db.open()
     
    6278    the root folder of an opened, writable ZODB.
    6379    """
    64     print [x for x in root_folder]
    65     root_folder['stress_app'] = University()
    66     print "WAEUP.STRESS.SCRIPTS: created 'stress_app' in root folder."
     80    root_folder[APP_NAME] = University()
     81    print "WAEUP.STRESS.SCRIPTS: created '%s' in root folder." % APP_NAME
    6782    return
    6883
    69 create_app()
     84@zodb
     85def import_files(root_folder, *args):
     86    from zope.component.hooks import setSite
     87    from zope.component import getUtility
     88    from waeup.kofa.interfaces import IBatchProcessor
     89    setSite(root_folder[APP_NAME])
     90    for path in args:
     91        if not os.path.isfile(path):
     92            print "WAEUP.STRESS.SCRIPTS: no such file: %s" % path
     93            continue
     94        basename = os.path.basename(path)
     95        util_name = IMPORTERS.get(basename, None)
     96        if util_name is None:
     97            print "WAEUP.STRESS.SCRIPTS: not a valid import filename: %s" % (
     98                basename)
     99            print "  valid names: %s" % IMPORTERS.keys()
     100            continue
     101        util = getUtility(IBatchProcessor, name=util_name)
     102        for line in open(path, 'rb'):
     103            break
     104        headerfields = line.strip().split(',')
     105        print "WAEUP.STRESS.SCRIPTS: importing %s" % os.path.basename(path)
     106        results = util.doImport(path, headerfields)
     107        print "WAEUP.STRESS.SCRIPTS: import done."
     108    return
     109
     110def main(cmd, *args, **kw):
     111    if cmd == 'install_app':
     112        configure()
     113        create_app()
     114        return
     115    elif cmd == 'import_csv':
     116        configure()
     117        create_app()
     118        import_files(*args)
     119        return
     120
     121if __name__ == '__main__':
     122    cmd = sys.argv[1]
     123    args = []
     124    if len(sys.argv) > 2:
     125        args = sys.argv[2:]
     126    main(cmd, *args)
Note: See TracChangeset for help on using the changeset viewer.