Looking for advice on how to develop applets for Gnome / Ubuntu - python

I am a linux (mostly ubuntu) user with a reasonable understanding of how the system works (although I am certainly not a linux guru!). In the past I have developed small cross-platform desktop applications in python/GTK and I delivered them to clients as self-contained filetrees, so that the only dependencies were Python itself and GTK.
Now I would like to develop a small applet for ubuntu, that I would like to release under GPL 2 or 3.
In particular these are the new steps I know I must learn in order to achieve my goal (it is very possible there are a few more that I am unaware of, though!):
Integrating with gnome: I want my application to be available as an applet in the taskbar.
Using D-bus: In particular I want my applet to use the new osd-notification framework for ubuntu, but communication with other applets is also a possible feature for a second iteration.
Packaging: I would like to setup a public PPA as soon as the application will reach alpha stage, but I also would like to use dependencies from existing packages in the official repos, rather than include the libraries again in my own package.
Of course official documentation will be my first source of knowledge, but - basing my judgment on the very useful answers that I received on another topic here on SO - I decided to turn to the SO community to collect additional advice like for example:
Are there additional steps to those I outlined before, that I have to learn in order to be able to implement my project?
Based on your own experience, would you advise me to learn those steps in advance (as the knowledge of those will influence my way of coding the core functionality) or would you consider integration with gnome / d-bus and packaging as "higher encapsulating levels" that can be added on top of core functionality afterwards (note: D-bus will be used at first just for pushing data. Input data will be retrieved with a webservice)?
Would you advise me to separate my application in two packages (back-end and front-end) or to keep it together in a single package?,
Do you know of any useful resource that you would advise me to look at, for learning any of the things that I have to?
Are you aware of any common "beginner's mistakes" that I should be aware of?
These questions are not meant to be exhaustive, though: if you feel that I am missing something from the general picture, you are more than welcomed to point me in the right direction!
PS: Should I have failed in explaining my final goal, take a look at project hamster: what I want to achieve is similar in terms of user interface (meaning: the applet should display the status and clicking on it should open the application itself, from which you could both configure the applet and perform various operations).

Well, you list python, so you'll want to have pynotify in your arsenal. It wraps DBus, and gives you a direct api for manipulating the osd-notification system.
>>> import pynotify
>>> pynotify.init("Lil' Applet")
True
>>> note = pynotify.Notification(
... pynotify.get_app_name(),
... "Lil' Applet wants you to know something's up.",
... "/usr/share/icons/Human/48x48/status/dialog-information.png")
>>> note.show()
True
This displays a notification that looks like this:
[ ] **Lil' Applet**
[ICON]
[ ] Lil' Applet wants you to know something's up.

As you already know, your first and best friend will be the code written by others - copy, paste, dissect, understand.
Luckily there are a few projects that do what you intend to achieve.
I can recommend conduit's code as a prime reference how to do things in a clean fashion. I think they also have stuff on dbus. Others to keep an eye on, would be deskbar-applet, hamster (heh), and any other app you remember having feature X. Sometimes it might require some C code deciphering though (like the applet button bit - i suggest you better take it from hamster as i was having some major time getting the thing straight)
Then the "devhelp" app will be of great assistance - it allows you to read and search in man pages fast and easy. Make sure that you also have the -doc packages for all the modules you intend to use.
For user interface i strongly suggest using glade, as that will allow you to change interface later much easier. Where you can't use glade - add and alignment box and add the widget in the box in the code.
There certainly will be quirks and things that you will learn the hard way. Should not be too hard though!
The packaging, especially the autotools will be bit of a struggle, but you will get it right. For how to do debians (and from there to PPA), you can dig in the hamster's repository history. There was once a "debian" folder.
I would suggest to start small - see if you can get a window. Then put a button on it.
You don't have to do it "right" the first time. For first time it will be ok, if something works at all.
As for the separation - i would not bother about it until you get there. Splitting up into two parts and have a core, should not be too hard later. But that all depends on your priorities.
Last thing - getting friends who know the field helps too. And one way to get new friends, is by taking part in other projects, heh.

There are some very good recommendations here already, but let me suggest that you develop your applet not so much "for Ubuntu" as "for Gnome". It doesn't take much extra effort to also make RPM packages for distributions such as Fedora, and Arch Linux packages, to name two examples. There is one major disadvantage though -- to stay compatible with Debian stable you have to stick to ancient versions of GTK and GLib, or at least make any functionality depending on newer versions optional. It's painful, but apparently Debian stable users appreciate it.
I'd also suggest setting up a source code management system somewhere as early as possible. You may not be worried about your disk crashing, but sometimes it saves you a lot of trouble just to be able to revert everything you did since the last commit.
Here's the link to the documentation on the official Gnome Panel Applet library. I don't know if it has Python bindings or not.

When I asked this question two years ago Ubuntu and Gnome were much closer to each other than they are today. At present (end of 2011) gnome adopted the gnome-shell, while Canonical decided to develop their very own UI (unity)...
Part of the tension that brought to the split specifically involved libappindicator, which makes the way I formulated this question (and probably part of the answers) obsolete.
Besides, there is now AskUbuntu on stack exchange, that would probably be a much better forum to ask about ubuntu-specific question.

Related

General system architecture for Linux configuration tool

I am going to write configuration tool for my Ubuntu based system. Next I would like to write frontends (text, GUI and web). But it is the most complicated project I wanted to write and I am not sure about general architecture I should use.
At the current I have functions and classes for changing system config. But these functions will probably grow & change. #Abki gave me advice how to write interface for frontends. I am going to make base classes for this interface but I don't know how to connect it with backend and next with frontends. Probably I should use design patterns like fasade, wrapper or something else.
It looks like (without interface_to_backend layer):
I don't care about UI and functions to change system config now. But I don't know how to write middle layer so It would be easy to connect it with the rest and extend functionality i the future.
I need general ideas, design patterns, advices how to implement this in Python.
I'm not sure this is entirely appropriate for SO but I'm intrigued and so I'll bite. As a rubyist I can't help much with the Python but here is some opinion on pattens from my experience.
My initial suggestion is you should review a few of the contenders out there. Specifically I'd be looking at cfengine, chef and bcfg2. They each tell a different story but if I'd summarise I'd say:
Chef has a lovely dsl syntax but is let down by a complicated architecture
bcfg2 is written in python but seems to have an annoying tendency to use XML :(
cfengine has the strongest theoretical underpinnings in promise theory (which is v.interesting BTW) but is C based.
Wikipedia also provides a pretty impressive list of configuration management tools that you will find useful.
In regard to designing your own tool I'd suggest there are three principles you want to pursue:
Simplicity, the simpler you make this the better. Simple in terms of scope, configuration and use are all important.
You'll need a single way to store data, you need to be able to trace the choices as they are made and not trample other people's changes (especially in a team environment).
Security, most configuration management tools need root privileges at some point. So you need to make sure that users can trust the code they're running.
You could use Fabric with Python as described in the article Ubuntu Server Setup with Python Fabric
The Wikipedia article at Comparison of open source configuration management software has several other tools that use Python to do this.
I like the approach taken by SALT.
If you write the GUI, text/CLI, and Web interfaces using Python, they can all use the same Python module. That way a change in one interface transparently affects the others. Plus all of those are in Python's area of strength.

Python newbie - help needed in choosing modules/libraries

i am learning python.. i want to do certain kind of scripting in python.. like,
i want to communicate 'wmic' commands through dos promt.. store the result a file..
access some sqlite database, take the data it has and compare with the result i stored..
now, what i dont get is that, how should i proceed? is there any specific frameworks or modules/libraries? like, win32api / com or what else?
pls guide me what things i should follow/learn to accomplish what i intend to do..
thanks
for your project look here
http://tgolden.sc.sabren.com/python/wmi/index.html
http://docs.python.org/library/sqlite3.html
here is list of generic python modules
http://docs.python.org/modindex.html
http://docs.python.org/ - here you can find all information you need with examples.
One of the particularly attractive features of Python is the "batteries included" philosophy: The standard library is huge, and extremely well thought out in 90% of the modules I've ever used. Conversely, this means that a good approach is to learn it first before branching out and installing third-party libraries that may be much less well supported and, ultimately, have no advantage.
In practice, this means I'd recommend keeping https://docs.python.org/release/2.6.5/library/index.html close to your heart, and delve into the documentation for the sqlite3 and probably the subprocess modules. You may or may not want win32api later (I've never worked with wmic, so I'm not sure what you'd need), or come back here when you have concrete questions and already explored the standard library offering.

python - good places to check out example prog / code online?

there is a year old, similar question - but in case there have been changes afoot:
i'm an intermediate c++ programmer just starting out on python, post some online tuts etc i can do some basic pythoneering, but was wondering if there are good places i can look online for simple(ish) --pref console based-- code that i can learn from, ideally with some sort of commentary.
anything come to mind?
thanks
The standard library is an excellent place to the start. It's maintained by the core python team and is of high quality with a lot of interesting idioms. I'd recommend the newer modules since they don't have much backward compatibility cruft and are more representative of the language as it is now. The older ones were written for earlier versions of Python and have some restrictions when it comes to API changes etc.
The list of modules in the standard library is described at http://docs.python.org/library/. You can go through it and decide which one you want to look at (area of interest etc.).
Their sources are viewable at the mercurial repo here http://hg.python.org/cpython/file/d7e85ddb1336/Lib (as of today). These are for the mainline 2.6 release. You can also checkout the repo and browse it on your local machine.
You can also start up your interpreter, import a module (say os) and do a print os.__file__ to see where the source file is if you want to look at the code in your local editor.
ActiveState Recipes is a good source for all kinds of Python scripts. But if you want to learn the basics of Python, you might just want to look at the standard library that ships with Python ("lib" directory").
i came across This The other day, Probably you can learn some python basics and have a laugh too!
Anyways, look at the libs as they said above, they are very useful
If you enjoy riddles:
www.pythonchallenge.com
If you're an intermediate C++ programmer, you're already equipped to handle to programming concepts. I like it because it gives me a reason to learn each part of the language, without being mundane 'Hello World' tasks.
However, some of the riddles are pretty tough and/or unrelated to programming. Either way, doing the first few will probably be enough to get your confidence up with Python syntax.

Is there a Python library that allows to build user interfaces without writing much code?

I am writing editing front ends in Python since several years now, and I am fed up with micromanaging every UI detail of a window or dialog every single time.
Is there a technology that allows me to, say, specify the relations between a GTK+ Glade-designed interface and the tables and records of an SQLite database to do all the middle man work? It should spare me the work of manually writing event handlers, input evaluators and view updates.
The technologies in question are just examples, but I want to stick with Python as far as possible.
Besides the ones already mentioned I can add:
Kiwi
uxpython
pygtk
treethon
I've never used any of them so have no recommendations but, for what it's worth, I have used at least 2 complex programs built directly on pygtk that worked in both Windows and Linux.
I think Kiwi is the only one of these with baked in support for db (through interface with SQLAlchemy, SQLObject, or Storm) but I would be surprised if you couldn't use one of those ORM's inside any of the other frameworks.
PyQt and its models can automate some of these tasks for you (to some amount off course, e.g. filling widgets with data from a database and handling most of the widgets behaviour, buffering etc.).
If you want a more object-oriented approach to handling SQL you could look into an ORM-oriented solution (for example SQLAlchemy).
Dabo is built on top of wxPython, so you may not prefer it, but it's designed to make it easy to tie a GUI to a database, so I'd recommend you check it out if you haven't already. In particular, it's got good facilities for tying widgets to data, and handling a lot of the common cases of GUI development.
wxGlade may help, although I haven't used it myself so I don't speak from experience.
Boa Constructor apparently has a wxPython GUI builder in it, and there is also PythonCard, though development on these two projects seems to have stalled.
Traits might be a good option for you.
http://code.enthought.com/projects/traits/docs/html/TUIUG/index.html
AS simple as it is to map a UI to an object, it doesn't seem too far fetched to incorporate SQLAlchemy for persistence.
There is a good book on wxPython, "wxPython in Action", which can't be said for some of the other solutions. No knock on the others. I've had success developing with wxPython in the past and it comes with a great set of demo applications with source code from which you can borrow liberally.
The best UI designer I found for wxPython applications is a commercial one, Anthemion DialogBlocks. It's by one of the wxPython programmers and is worth the money. Other solutions for UI design include wxGlade (I found it usable but not featureful) and Boa Constructor (haven't used it). Wing IDE might also have one. Stani's Python Editor bundles wxGlade, I believe. There are a lot of other projects that don't really work or are fairly old.
As far as SQL automation goes, as another answerer says, I'd look at SQL alchemy, but the learning curve for a small application might be too much and you'd be better off just going straight to odbc. The best odbc api is the one used by Django, pyodbc.
It's been a while since I developed with these tools, so there may be something newer for each, but at the time these were definitely the best of breed in my opinion.
I had lots of success with wxPython, but that was some years ago now and there may be better new solutions...
Ok this is an unconventional solution but write yourself a code generator. I have done this several times using Mako. So in my case I auto inspect a table which columns it contains and types and generate classes from that. It's more work upfront but does exactly what you want and is reusable in subsequent projects.

How do I use the wx.lib.docview package?

I'm currently working on a simple wxPython app that's essentially document based. So far I've been manually implementing the usual open/save/undo/redo etc etc stuff.
It occurred to me that wxPython must have something to help me out and after a bit of searching revealed the docview package.
At this point though I'm just not quite sure how to hook everything up and get things started. Anyone got any good links or hints about places to start?
The docs seems to be a little thin about this and Robin Dunn's wxPython book doesn't really cover this package at all.
You might take a look at the docviewdemo.py from the wxPython Docs and Demos:
on my machine they are located:
C:\Program Files\wxPython2.8 Docs and Demos\samples\pydocview\
C:\Program Files\wxPython2.8 Docs and Demos\samples\docview\
In addition to the ones mentioned, there is quite an extensive example docview/pydocview in the samples\ide. If you want it to run you will have to make a few code corrections (I have submitted a ticket that outlines the fixes at trac.wxwidgets.org #11237). It is pretty complex but I found it handy to figure out how to do some more complex things. For example, samples\ide\activegrid\tools\ProjectEditor.py is built from scratch and has undo support etc rather than just relying on a control that does everything for you already. That way you can see how things are supposed to be done at the detailed level. The documentation is rather useless in that regard.
If you have decided against using docview/pydocview I have a spreadsheet application built on wxPython that you may find useful as an example. While it does not implement a document view framework it does have some characteristics of it and I've implemented an undo/redo system. Check it out at http://www.missioncognition.net/pysheet/ I'm currently working on a pydocview based app so I expect that to be up on my site eventually.

Categories

Resources