Tag Archives: tips

zodb export import from zeo debug prompt – the simple built in way !

I am beginning to enjoy working with zodb. Again this is a quick note, pretty much like my previous post on zodb undo and conflict resolution..

I knew from my early Zope days that one could export and import arbitrary zope objects but never had a reason to code it or inspect its innards. Today though I wanted to export only a sub-section of our new beta site so that we could merge its contents with an already up and running ‘live’ site. So, I decided to search. One of the biggest problems with zope (and it’s related components) is the lack of authoritative and comprehensive documentation.

I found various scripts on the interwebs which showed how to walk a zodb object hierarchy. For instance:

However, there was very less information on how to actually export arbitrary objects using some built-in api (after all there had to be one !) and every less how to import exported .zexp files ! So, I hit the code. ZODB has a nicely named module ExportImport.py which exposes two neat functions exportFile() and importFile(). They do what they say and you’ll see the code is pretty much what you’d expect — walk the oids and serialize/load the objects …but wait there is more ! ZODB Connection class actually derive from ExportImport ! So, these functions may be used quite simply like this:

>>> # ...assuming you are in the debug prompt with context as the
>>> # object you want to export
>>>
>>> oid = context._p_oid
>>> conn = context._p_jar
>>> conn.exportFile(oid, 'context.zexp')
.....
>>> # ...assuming you are in the debug prompt, you have an exported
>>> # zexp and context is the parent where you want to import and
>>> # place your imported 'new_folder'
>>>
>>> conn = context._p_jar
>>> imported = conn.importFile('new_folder.zexp')
>>> context['new_folder'] = imported
>>> import transaction
>>> transaction.commit()    # Do not forget to do this

Simple innit ? Hope someone out there finds this useful.

vim as a syntax highlighting capable pager

Just thought I’d share what I learned sometime back.

Do you ever wish that you could “cat” a file and have it displayed on the terminal with syntax highlighting ? Well, “cat” is a tool for concatenating files. It does not know (or care) where it is cat-ing to (for that matter, it does not know or care, where it is cat-ing from either)

OTOH, Pagers (like ‘more’ or ‘less’) are tools to view files on the terminal. That said, vim also behaves somewhat like a pager when invoked using the command ‘view’. However, this does not do syntax highlighting, nor does it take input from stdin (ie: executing “cat /some/file | view” does not do what you expect). So what do we do ?

Well, we use a little a vim trick:

  • Look for the file /usr/share/vim/vim<version>/macros/less.sh
  • create an alias for that script:

    $ alias vless='/usr/share/vim/vim/macros/less.sh'

    (you may also save the alias later in ~/.bashrc if you like the way it works)

  • now use vless as you would use less

    $ vless foo.c
    $ cat /some/file | some_filter | vless

Nice huh ?