ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
need python program to handle XML contacts under Console (CLI, no X11, DISPLAY="")
The format looks like this, and please find a sample:
cat .osmo/contacts_records.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<osmo_contacts>
<contacts_groups/>
<contacts_records>
<record>
<group>None</group>
<first_name>first name linux</first_name>
<last_name>last name linux</last_name>
<nickname>Nick linux</nickname>
<birthday_date>0</birthday_date>
<name_day_date>0</name_day_date>
<home_address>address </home_address>
<home_postcode>postcode linux</home_postcode>
<home_city>city in the world</home_city>
<home_phone_1>home phone nr 4334554</home_phone_1>
<work_phone_1>work phone nr 435454</work_phone_1>
<cell_phone_1>43545344243</cell_phone_1>
<email_1>linux@gmail.com</email_1>
<www_1>www.linux.com</www_1>
<im_skype>my skype pseudo</im_skype>
<additional_info>additional info
this field is important</additional_info>
</record>
<record>
<group>None</group>
<first_name>second contact first name </first_name>
<last_name>last name</last_name>
<nickname>nickname</nickname>
<birthday_date>0</birthday_date>
<name_day_date>0</name_day_date>
<home_address> my address</home_address>
<home_postcode>postcode </home_postcode>
<home_city>city</home_city>
<home_phone_1>45345435</home_phone_1>
</record>
</contacts_records>
</osmo_contacts>
Would you have a simple python code that could :
- add
- edit
those categories mentioned above in this sample?
thank you in advance a lot lot if it might be possible for an experienced coder.
Firstly, don't put "urgent" in your thread titles. What're urgent to me are my own problems, not yours
What sort of thing are you looking for? Do you mean an interactive programme which will allow you to browse through and amend contact details, or just a script that will do something predefined to all the contacts? If you're a bit more specific, then we should be able to help you
Xeratul, if you want an interactive program (as opposed to a batch processing tool), do you want a command-line application, or a GUI one? Which toolkit to use? PyQt4?
There are basically two approaches to editing:
Parse the entire dataset into an abstract data structure (associative arrays in Python) in memory, edit, then emit the entire structure as XML
Read the entire dataset as text, editing only the fields, keeping everything else in both that record and all other records intact
(When adding a new record, use the first record as a template)
The first is easier to implement, but often results in loss of fidelity if extraneous data (especially attributes) are added to the XML file by some other utility. The second one is extremely useful in that it edits only field contents and keeps any extraneous data intact, but it is also a lot more difficult to implement, as you basically need to implement an XML parser yourself.
Parse the entire dataset into an abstract data structure (associative arrays in Python) in memory, edit, then emit the entire structure as XML
For this xml.etree.ElementTree from Python's standard library or lxml are great choices. They parse XML into an object tree and provide various ways to traverse and manipulate it. Both have tutorials and should be easy to start using.
Agreed. (By the associative arrays I meant the structures you'd use internally in the application, extracted from the ElementTree in this case. Accessing the data dynamically via the ElementTree would be a bit cumbersome for e.g. search functionality.)
The Python 2 vs. Python 3 differences are a bit annoying in this regard, however; so do consider whether you wish to use Python 2 or Python 3, before selecting the library, and starting the implementation.
In my opinion, Python 2.7 + PyQt4 + etree would be my choice for a cross-platform utility -- only requirements being Python 2.7 + PyQt4, and both are available in easy packages for practically all platforms --, but I'd be aware of the need to transition to Python 3 in a few years. In a corporate-type environment where the Python 3 packages are guaranteed to be installed, I'd go with Python 3 instead.
Last edited by Nominal Animal; 05-10-2012 at 11:09 AM.
Firstly, don't put "urgent" in your thread titles. What're urgent to me are my own problems, not yours
What sort of thing are you looking for? Do you mean an interactive programme which will allow you to browse through and amend contact details, or just a script that will do something predefined to all the contacts? If you're a bit more specific, then we should be able to help you
Thank you for your improvements. I have changed the title, and content. It is indeed better.
The screenshot you posted looks like it uses the curses module for python, a tutorial for which is here. If you have any problems, post back here and we'll be glad to help you
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.