개발/python

[Python] python-gconf 간단한 example

Dsp 2009. 2. 25. 23:39
우분투 상의 python 2.5.2에서 python-gconf 간단한 예입니다.
배경화면 이미지를 set하며, notify가 제대로 오는지 확인하는 예제입니다.

소스 코드는 다음과 같습니다.

#! /usr/bin/python
import gconf
import sys, time
import gtk

BGPATH = '/desktop/gnome/background/picture_filename'
client = gconf.client_get_default()
client.add_dir('/desktop/gnome/background', gconf.CLIENT_PRELOAD_NONE)

def bg_changed( *args ):
    print '[Notify]', client.get_string( BGPATH )

def set_value( value ):
    client.set_string(BGPATH , value)

if __name__ == '__main__':
    if len( sys.argv ) > 1:
        param = sys.argv[1]
        if param.lower() == "server":
            print "[Server] Waiting........."
            client.notify_add(BGPATH, bg_changed)
            gtk.main()
        else :
            print "[Param]", param
            set_value( param )

즉, 아래와 같이 첫번째 인자로 'server'를 주면 notify를 기다리기만 하고,
다른 값을 주면 해당 값은 배경화면 파일명을 변경합니다.

dsp@dsplinux:/view/tmp$ ./gconf_test.py server
[Server] Waiting.........
[Notify] /home/dsp/Pictures/ss22-hires.jpg
[Notify] /home/dsp/Pictures/01641_sandilandslookoutsunset_1680x1050.jpg

dsp@dsplinux:/view/tmp$ python ./gconf_test.py /home/dsp/Pictures/ss22-hires.jpg
[Param] /home/dsp/Pictures/ss22-hires.jpg
dsp@dsplinux:/view/tmp$ python ./gconf_test.py /home/dsp/Pictures/01641_sandilandslookoutsunset_1680x1050.jpg
[Param] /home/dsp/Pictures/01641_sandilandslookoutsunset_1680x1050.jpg

cf. gconf 모듈이 gtk thread를 사용하므로 import gtk를 하지 않으면 동작을 안합니다.