- 浏览: 296756 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (98)
- philosophy (0)
- python (21)
- mac (6)
- linux (12)
- vfx (8)
- web2.0 (2)
- win (2)
- java (2)
- it (1)
- ruby (1)
- gtd (6)
- digest (1)
- maya (1)
- sns (1)
- dip (2)
- ldap (1)
- eclipse (1)
- mba (1)
- lisp (2)
- haskell (3)
- life (4)
- c# (1)
- c++ (3)
- sci-fi (1)
- news (2)
- poem (2)
- reading (2)
- mysql (1)
- coffee (0)
- houdini (1)
- economics (1)
- emacs (1)
- render (1)
- expect (0)
- shake (1)
最新评论
-
aib628:
真是好东东,正在学习中!
Jython 简单入门 -
jiguanghover:
不错的例子,好好看看
Jython 简单入门 -
rmn190:
不错, 现在正在从Java转到Python这边来, 以前用Ja ...
Jython 简单入门 -
kandari:
有没有openSUSE的
用NTP网络时间协议同步你的IT系统 -
CharlesCui:
winmail.dat是个垃圾!气死我了.
如何提取 winmail.dat ?
VObject
VObject simplifies the process of parsing and creating iCalendar and vCard objects.
Installation
To install vobject, run:
python setup.py install
vobject requires the dateutil package, which can be installed via easy_install or downloaded from http://labix.org/python-dateutil
Running tests
Unit tests live in doctests throughout the source code, to run all tests, use:
python tests/tests.py
Usage
Creating iCalendar objects
vobject has a basic datastructure for working with iCalendar-like syntaxes. Additionally, it defines specialized behaviors for many of the commonly used iCalendar objects.
To create an object that already has a behavior defined, run:
>>> import vobject >>> cal = vobject.newFromBehavior('vcalendar') >>> cal.behavior <class 'vobject.icalendar.VCalendar2_0'>
Convenience functions exist to create iCalendar and vCard objects:
>>> cal = vobject.iCalendar() >>> cal.behavior <class 'vobject.icalendar.VCalendar2_0'> >>> card = vobject.vCard() >>> card.behavior <class 'vobject.vcard.VCard3_0'>
Once you have an object, you can use the add method to create children:
>>> cal.add('vevent') <VEVENT| []> >>> cal.vevent.add('summary').value = "This is a note" >>> cal.prettyPrint() VCALENDAR VEVENT SUMMARY: This is a note
Note that summary is a little different from vevent, it's a ContentLine, not a Component. It can't have children, and it has a special value attribute.
ContentLines can also have parameters. They can be accessed with regular attribute names with _param appended:
>>> cal.vevent.summary.x_random_param = 'Random parameter' >>> cal.prettyPrint() VCALENDAR VEVENT SUMMARY: This is a note params for SUMMARY: X-RANDOM ['Random parameter']
There are a few things to note about this example
- The underscore in x_random is converted to a dash (dashes are legal in iCalendar, underscores legal in Python)
- X-RANDOM's value is a list.
If you want to access the full list of parameters, not just the first, use <paramname>_paramlist:
>>> cal.vevent.summary.x_random_paramlist ['Random parameter'] >>> cal.vevent.summary.x_random_paramlist.append('Other param') >>> cal.vevent.summary <SUMMARY{'X-RANDOM': ['Random parameter', 'Other param']}This is a note>
Similar to parameters, If you want to access more than just the first child of a Component, you can access the full list of children of a given name by appending _list to the attribute name:
>>> cal.add('vevent').add('summary').value = "Second VEVENT" >>> for ev in cal.vevent_list: ... print ev.summary.value This is a note Second VEVENT
The interaction between the del operator and the hiding of the underlying list is a little tricky, del cal.vevent and del cal.vevent_list both delete all vevent children:
>>> first_ev = cal.vevent >>> del cal.vevent >>> cal <VCALENDAR| []> >>> cal.vevent = first_ev
vobject understands Python's datetime module and tzinfo classes.
>>> import datetime >>> utc = vobject.icalendar.utc >>> start = cal.vevent.add('dtstart') >>> start.value = datetime.datetime(2006, 2, 16, tzinfo = utc) >>> first_ev.prettyPrint() VEVENT DTSTART: 2006-02-16 00:00:00+00:00 SUMMARY: This is a note params for SUMMARY: X-RANDOM ['Random parameter', 'Other param']
Components and ContentLines have serialize methods:
>>> cal.vevent.add('uid').value = 'Sample UID' >>> icalstream = cal.serialize() >>> print icalstream BEGIN:VCALENDAR VERSION:2.0 PRODID:-//PYVOBJECT//NONSGML Version 1//EN BEGIN:VEVENT UID:Sample UID DTSTART:20060216T000000Z SUMMARY;X-RANDOM=Random parameter,Other param:This is a note END:VEVENT END:VCALENDAR
Observe that serializing adds missing required lines like version and prodid. A random UID would be generated, too, if one didn't exist.
If dtstart's tzinfo had been something other than UTC, an appropriate vtimezone would be created for it.
Parsing iCalendar objects
To parse one top level component from an existing iCalendar stream or string, use the readOne function:
>>> parsedCal = vobject.readOne(icalstream) >>> parsedCal.vevent.dtstart.value datetime.datetime(2006, 2, 16, 0, 0, tzinfo=tzutc())
Similarly, readComponents is a generator yielding one top level component at a time from a stream or string.
>>> vobject.readComponents(icalstream).next().vevent.dtstart.value datetime.datetime(2006, 2, 16, 0, 0, tzinfo=tzutc())
More examples can be found in source code doctests.
vCards
Making vCards proceeds in much the same way. Note that the 'N' and 'FN' attributes are required.
>>> j = vobject.vCard() >>> j.add('n') <N{} > >>> j.n.value = vobject.vcard.Name( family='Harris', given='Jeffrey' ) >>> j.add('fn') <FN{}> >>> j.fn.value ='Jeffrey Harris' >>> j.add('email') <EMAIL{}> >>> j.email.value = 'jeffrey@osafoundation.org' >>> j.email.type_param = 'INTERNET' >>> j.prettyPrint() VCARD EMAIL: jeffrey@osafoundation.org params for EMAIL: TYPE ['INTERNET'] FN: Jeffrey Harris N: Jeffrey Harris
serializing will add any required computable attributes (like 'VERSION')
>>> j.serialize() u'BEGIN:VCARD\r\nVERSION:3.0\r\nEMAIL;TYPE=INTERNET:jeffrey@osafoundation.org\r\nFN:Jeffrey Harris\r\nN:Harris;Jeffrey;;;\r\nEND:VCARD\r\n' >>> j.prettyPrint() VCARD VERSION: 3.0 EMAIL: jeffrey@osafoundation.org params for EMAIL: TYPE ['INTERNET'] FN: Jeffrey Harris N: Jeffrey Harris
Parsing vCards
>>> s = """ ... BEGIN:VCARD ... VERSION:3.0 ... EMAIL;TYPE=INTERNET:jeffrey@osafoundation.org ... FN:Jeffrey Harris ... N:Harris;Jeffrey;;; ... END:VCARD ... """ >>> v = vobject.readOne( s ) >>> v.prettyPrint() VCARD VERSION: 3.0 EMAIL: jeffrey@osafoundation.org params for EMAIL: TYPE [u'INTERNET'] FN: Jeffrey Harris N: Jeffrey Harris >>> v.n.value.family u'Harris'
发表评论
-
用python ctypes调用动态链接库
2009-10-12 15:10 6966ctypes is very cool! Great piec ... -
MoinMoin 1.5.8 上传附件的XMLRPC API实现
2009-09-22 16:44 2515一、服务端 1. 修改/usr/lib/python2.5/ ... -
用python实现SSH的免密码输入访问客户端
2009-07-10 09:52 83831. pexpect - http://pexpect.sou ... -
为心爱的MoinMoin写一个小小的Done宏
2009-04-15 16:04 1481############################# ... -
根据CSV文件自动形成表格的MoinMoin插件——InsertCSV
2009-02-28 00:34 3065MoinMoin本身的制表语法很简单,但是如果其他软件制作好的 ... -
使用appscript+python来控制Mac下的GUI应用程序
2009-02-15 13:28 8569在Mac下,appscript是一个与应用程序通信交互的强大工 ... -
PyFileMaker介绍
2009-02-15 11:57 1372PyFileMaker是一个用于访问和修改FileMaker ... -
如何将.py编译成.pyc/.pyo文件
2009-02-10 15:14 3744使用方式如下: python -O -m py_com ... -
Darwin Calendar Server,一个开放源代码的日历服务器
2009-02-06 19:18 3841Darwin Calendar Server是一个 ... -
python library extra
2009-02-06 00:18 9451. dateutil - The dateutil modu ... -
Epydoc
2009-02-05 12:15 1004http://epydoc.sourceforge.net/ ... -
一则魅族M8下歌词乱码的程序小故事
2009-01-20 00:41 2221最近,一朋友买了个魅族的M8手机,整天拨弄,爱不释手。一次找我 ... -
Jython 简单入门
2008-12-30 19:38 370501. 用Jython调用Java类库 第一步、创建Java类 ... -
readline
2008-12-02 23:33 1124There are two ways to configure ... -
feedparser学习摘要
2008-10-13 01:22 2932号称Universal Feed Parser,通吃所有合法不 ... -
py2exe 把python脚本转成windows下可执行文件
2008-10-12 23:42 3520py2exe是一个可以把python脚本转成windows下的 ... -
13.4 shelve -- Python object persistence
2008-10-05 02:22 1185import shelve d = shelve.o ... -
python相关拾零
2008-09-09 16:51 666* python-psycopg2 - Python modu ... -
getopt -- Parser for command line options
2008-08-27 12:56 1164getopt -- Parser for command li ... -
TurboGears 和 Django 的比较
2008-08-21 22:50 7493TurboGears 和 Django 的比较 ...
相关推荐
资源来自pypi官网。 资源全名:types-vobject-0.9.0.tar.gz
VObject 旨在成为一个功能齐全的 Python 包,用于解析和生成 vCard 和 vCalendar 文件。 它最初是由 Jeffrey Harris 与开源应用程序基金会的 Chandler 项目共同开发的。 非常感谢的奉献和支持。 和 目前正在维护该...
军刀/ vobject 通过VObject库,您可以使用PHP轻松解析和处理和对象。 VObject库的目标是使用一个易于使用的API创建一个非常完整的库。 安装 确保已安装 ,然后运行: composer require sabre/vobject "^4.0" 该...
webbrowser1.Document.QueryInterface(IViewObject, VObject); if VObject <> nil then try SRect := Rect(0, 0, Image1.Width, Image1.Height); VObject.Draw(DVASPECT_CONTENT, 1, nil, nil, Self....
python库。 资源全名:types-vobject-0.9.7.tar.gz
python库。 资源全名:types-vobject-0.9.2.tar.gz
#vCardOrganizer 应该: 1. display partial implementation vcard 2. allow to add and remove vcard... 使用 vobject, ://vobject.skyhouseconsulting.com/(使用 Apache 2.0 许可)但是 vobject 目前不支持 vCard 4
python-vobject 配置 编辑主 python 文件,并更改变量以满足您的需要 欢迎提供反馈,我不太容易用 python 编码,所以请随意对当前这段代码提出一些改进建议。 致谢 一位法国博主,他提供了一段代码,我用作使用 ...
vdirsyncer::card_index:同步日历和联系人