Show
Ignore:
Timestamp:
02/27/07 10:24:21 (3 years ago)
Author:
akaihola
Message:

Added plugin feature to dbpickle.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/dbpickle/dbpickle.py

    r75 r76  
    3131primary key.  ManyToMany and OneToOne relations are not supported. 
    3232 
     33You can also use you own plugin for pre-processing objects before 
     34saving them into the database.  This is useful e.g. when a model has 
     35changed and you're importing objects from an old version of the 
     36database.  The plugin should be a Python program with a 
     37`pre_save_hook(obj)` function which returns True if it modified the 
     38object, otherwise False. 
     39 
     40 
     41Usage example 
     42============= 
     43 
    3344Let's suppose your project is using PostgreSQL and the database has 
    3445been populated using the app. 
     
    5667import logging 
    5768from django.db import models 
     69from imp import load_source 
    5870 
    5971def dump(filepath): 
     
    7284 
    7385@transaction.commit_on_success 
    74 def load(filepath): 
     86def load(filepath, pluginpath=None): 
    7587    """ 
    7688    Unpickle Django objects from the given file and save them into the 
    7789    database. 
    7890    """ 
     91 
     92    if pluginpath: 
     93        plugin = load_source('plugin', pluginpath) 
     94    else: 
     95        plugin = None 
     96 
     97    pre_save_hook = getattr(plugin, 'pre_save_hook', lambda obj: False) 
    7998     
    8099    # unpickle objects from file 
     
    90109    while data: 
    91110        key, obj = data.popitem() 
    92         load_recursive(data, obj) 
     111        load_recursive(data, obj, pre_save_hook) 
    93112 
    94113 
    95 def load_recursive(data, obj): 
     114def load_recursive(data, obj, pre_save_hook): 
    96115    """ 
    97116    Save the given object into the database.  If the object has 
     
    109128                                        related_model, 
    110129                                        related_pk_val)) 
    111                 load_recursive(data, related_obj) 
     130                load_recursive(data, related_obj, pre_save_hook) 
    112131            except KeyError: 
    113132                logging.debug('probably loaded already: ' 
     
    116135    logging.info('loading %s' % obj) 
    117136    try: 
     137        pre_save_hook(obj) 
    118138        obj.save() 
    119139    except Exception, e: 
     
    127147    p.add_option('-l', '--load', action='store_const', const='load', dest='action', help='Load all Django objects from a file') 
    128148    p.add_option('-f', '--file', default='djangodb.pickle', help='Specify file path [djangodb.pickle]') 
     149    p.add_option('-p', '--plugin', help='Use .py plugin for preprocessing objects to load') 
    129150    p.add_option('-v', '--verbose' , action='store_true', help='Show verbose output for debugging') 
    130151    (opts, args) = p.parse_args() 
     
    134155        dump(opts.file) 
    135156    elif opts.action == 'load': 
    136         load(opts.file) 
     157        load(opts.file, opts.plugin) 
    137158    else: 
    138159        print 'Please specify --dump or --load'