Three Useful Python Bindings - ClamAV, Apt and Evolution

1 December 2007

Python is not just cool because it easy to code with, it also has loads of bindings to almost every major open source project, (as well as to some famous proprietary software that we don't care about here).

In this article, I will quickly look at three sets of bindings that you may not have noticed before. Hopefully, one of them may be useful in your own programs.

To join in, get the dependencies when I say and then paste the code samples into the Python interpreter line by line (type python at a command prompt to start the command prompt). Another way is to save the snippets to files and run them at the command line (e.g. python filename.py) but that is somewhat less fun.

1. Virus checking with ClamAV

ClamAV is the leading Open Source Virus Checker, it is often used in mail servers and firewalls and so on to check for viruses that might infect Windows PCs.

pyClamAV are Python bindings for ClamAV. Get it from the pyClamAV homepage, or from your friendly neighbourhood package manager, for example:

On Gentoo, you can go:

emerge pyclamav

On Ubuntu and Debian, you can just go:

apt-get install python-clamav

You may also want to get the test virus from Eicar

So now we can do:

testfilename = 'eicar_com.zip'
    import pyclamav
    if pyclamav.scanfile(testfilename)[0]:
        print "Rejecting File"

This is immediately useful. For example, say we have made a website form that allows the user to upload a file, we can pass this file to pyclamav.scanfile and then reject the file if it contains a virus.

2. Package Management in Ubuntu

In Gentoo, the whole packager manager (portage) is in Python, making it easy to get information from it (just type import portage and go from there).

A lesser known fact is that Ubuntu and Debian have a package called python- apt, which allows for similar capabilities. To get it, you need to:

apt-get install python-apt

Now lets have some fun:

import apt
    t = apt.cache.Cache()

    # Lets see all the packages available:
    for i in t:
        print i.name

    # Lets choose the package called 'pacman' and look at some
    # information about it:

    print t['pacman'].summary
    print t['pacman'].description
    print t['pacman'].packageSize
    print t['pacman'].section

    # Lets see all the dependencies for pacman:

    for i in t['pacman'].candidateDependencies:
        print 'Depends:', i.or_dependencies[0].name,
        i.or_dependencies[0].version

3. Groupware in Evolution

Evolution is the default email client for GNOME. However, it is also a powerful workgroup tool, the bottom-left corner of Evolution shows the main features:

Evolution Screenshot

The Evolution Python bindings allow you to access your workgroup information from within Python. Lets go through these features, one at a time.

It is not in Gentoo at the moment so go get it yourself, and then run the included script:

./autogen.sh

If you do not have the correct dependencies then it will pause and ask you to install them, then you can run it again to carry on.

On Ubuntu and Debian, you can use:

sudo apt-get install python-evolution

Evolution Contacts

Being able to access the user's address book could be a useful feature in many programs:

# Access the address book
import evolution
addresses = evolution.ebook.open_addressbook('default')

# Print out the names and email addresses
for i in addresses.get_all_contacts():
    print i.get_name(), i.get_property('email-1')

# View all the properties that we have available:
print addresses.get_all_contacts()[0].__doc__

# Handy search method
r = addresses.search('Zeth') # Returns List of results
l = [x.get_name() for x in r] # list of results
u = r[0].get_name() # name of the first result
print r[0].get_vcard_string() # vcard for first result

Evolution Calendar

Now lets look at events in the Evolution Calendar:

# Access the Calendar events
import evolution
events = evolution.ecal.open_calendar_source('default',
evolution.ecal.CAL_SOURCE_TYPE_EVENT)

# Print out all of the Events
for i in events.get_all_objects():
    print i.get_summary()

Evolution Memos

A similar process for memos:

# Access the memos
import evolution
memos = evolution.ecal.open_calendar_source('default', \
evolution.ecal.CAL_SOURCE_TYPE_JOURNAL)

# Print out memo summaries and descriptions
for i in memos.get_all_objects():
    print i.get_summary(), i.get_description()

Evolution Tasks

And again, the same process for tasks - by now it is easy:

# Access your tasks
import evolution
tasks = evolution.ecal.open_calendar_source('default', \
evolution.ecal.CAL_SOURCE_TYPE_TODO)

# Print out all tasks and their due dates
for i in tasks.get_all_objects():
    print i.get_summary(),
    datetime.datetime.fromtimestamp(i.get_due()).ctime()

All of my examples have been reading data, but the python bindings for evolution allow your programs to create new contacts, events, tasks and memos.

Digg this

Discuss this post - Leave a comment

1 Paul says...

I get:

"/usr/lib/python2.5/site-packages/apt/*_init_*.py:18: FutureWarning: apt API
not stable yet
warnings.warn("apt API not stable yet", FutureWarning)"

when importing apt. Is that supposed to happen?

Posted at 12:08 p.m. on December 2, 2007


2 Zeth says...

Hi Paul,

Thanks for visiting and getting your hands dirty! Yeah the future warning is there, the writer of the apt API is giving you a tip that he might change his mind, it is just warning not an error so you can carry on anyway. Many APIs authors just change them as needs arise, so this author is just being particularly polite.

Best Wishes, Zeth

Posted at 12:18 p.m. on December 2, 2007


3 Brock Noland says...

That ClamAV binding is awesome! Thanks!

Posted at 2:21 a.m. on February 26, 2008


4 Tun says...

Hi, Do You know, haw can i get start date for tasks evolution? If exists the similar way to your example:

System Message: ERROR/3 (<string>, line 4)

Unexpected indentation.
i.get_due() ?

System Message: WARNING/2 (<string>, line 5)

Block quote ends without a blank line; unexpected unindent.

I would like to have sth like i.get_start_date().

Best Regards, Tun.

Posted at 8:10 p.m. on January 5, 2009


5 Frederik says...

Thanks for this summary! There is so little documentation for python-evolution that this bit really helped me getting started.

Posted at 6:11 p.m. on September 5, 2009


6 vetetix says...

Sorry to bother you nearly two years after you wrote this blog article, but I can't manage to find how to modify an existing field.

I am trying to change the name (or the full-name) of my contacts but it doesn't work.

How do I use the set_property(), or set_data() method?

Posted at 1:36 p.m. on November 15, 2009


What do you have to say?

Show Editing Help

About

Hello, my name is Zeth, I'll be your host here.

Command Line Warriors is about taking control of your own technology, it looks at our experiences of computing; especially using GNU/Linux, the Python programming language, the command-line and issues such as techno-ethics, best practices and whatever is cool now. If you take control of your technology then you are a Warrior too!

This site is your site too which means that you can contribute and get involved. You can leave comments using the facility provided. For me, the comments and discussions are by far the best part of the site. So please do have your say!

Latest Discussions

Essex Web Design

September 3, 2010
A lot of contract providers give you free internet usage now, but if you have Pay As You Go, then you are going to be paying heavy prices.
Calling time on mobile internet nonsense?

Krasochka

September 2, 2010
Hack again?!
Adding more terminals to your function keys

GenryFlorist

September 2, 2010
<b>Cheap flowers delivery around the world!</b> Celebrate summer with our gorgeous flowers. They?re the perfect gift for any summer occasion. From birthdays to anniversaries, we offer beautiful flowers, lush plants, ...
Burning an iso to CD on Windows

auto-financing.co.cc

September 2, 2010
auto-financing
ReStructuredText tables and doctests

rubaxa

September 1, 2010
FTP = NOT RANDOM software Dominated hands postflop suckout often on all-ins. EX. AK vs. A9 or KQ vs. K6. Both players hit top pair. Bad player goes all in ...
Burning an iso to CD on Windows

empodayaddelm

September 1, 2010
Sorry admin - my post is test
This Week: Heroes and Monsters

increase synthroid dosage

September 1, 2010
Latest world news: 1 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/viagra-cialis-buy-no>buy cheap cialis generic levitra viagra</a> Viagra 2 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/buy-viagra-online-at-lowest>rainbowpush discussion board buy viagra</a> Viagra 3 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/how-to-get-generic-brand>search viagra ...
SFTP in Python: Paramiko

Lacilslaw

September 1, 2010
HYUN JAIMIE enniless and homele JAMILA
This Week: Heroes and Monsters

domaserisk

August 31, 2010
who was shaking his head back and forth knowingly Grissom shifted his eyes over at Brass,
How I Removed Windows from my Laptop

get ready loan

August 30, 2010
Though, by the you kill the legitimate PC user from visiting the site. Also, think about the dynamic IP's issue.
Only the penitent man will pass - on captchas and cotton wool

Packers and movers in pune

August 30, 2010
The topic you disscussed here is very amazing, informative and useful in future...
On Comment Spam

serhanters1

August 30, 2010
?? ???????? ??... ??????...... ??. ????????? ??? ??? ???????D ???????? ??. ????? ????? ???? ??? ???=) ?? ?????- http://letitbit.net/download/8746.894a84bc20f38f1661895aeee0/stereokartinki.html ???http://f-zona.ru ? ? ?? ????????????
Burning an iso to CD on Windows

strona startowa

August 29, 2010
Thanks For This Post, was added to my bookmarks.
Python CGI contact forms

lerexottori

August 29, 2010
?????????????? ??????????
Adding more terminals to your function keys

KelpAugmeme

August 29, 2010
aofaapsymp, http://forums.quark.com/members/jennaq.aspx online stock trading broker, rdgofzary
PuTTY Series: Adding PuTTY to your system path

Cheeday

August 28, 2010
What flowers do you like?
This Week: Heroes and Monsters

magfcvb

August 28, 2010
??????? ?????????????? ?????? - ????? ?????? ?????????????? ??????, ?????????????? ?????? ???????, ?????????????? ?????? crosman, ???? ??????????????? ??????, ?????????????? ?????? ?????? ????????. ???? magazin-oruzhie.ru
Include ODF support in the Linux Standard Base?

noni

August 28, 2010
I find myself coming to your blog more and more often to the point where my visits are almost daily now!
On Comment Spam

Latenadsfes

August 28, 2010
http://mynewblog.for-breastcancer.com/ http://mynewblog.photoblogcentral.com/ http://ilovezebras.thechicks.org/ http://mynewblog.cyberbardsymposium.com/ http://wewphost.com/ilovezebras/
Burning an iso to CD on Windows

LeupoldEst

August 28, 2010
pretty cool stuff here thank you!!!!!!!
OOXML Vote Coverage