Updating the desktop icon in python

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Updating the desktop icon in python

Postby leke » Fri Dec 17, 2010 9:03 am

Hi, the following code works fine -- that is it creates the icon and support text in the same location as the python script, but doesn't update the desktop icon correctly. Specifically, this function doesn't seem to work.
Code: Select all
def update_icon_file():
    cmd = "xdg-desktop-icon install --novendor /home/asdf/Desktop/test.desktop"
    os.system(cmd)

If I run xdg-desktop-icon install --novendor /home/asdf/Desktop/test.desktop in the terminal though, it works fine.

Code: Select all
#!usr/bin/env python
# -*- coding: utf-8 -*-

import feedparser, os, re


#### Update the icon on the desktop - Change test.desktop to the name of your icon file.
def update_icon_file():
    cmd = "xdg-desktop-icon install --novendor /home/asdf/Desktop/test.desktop"
    os.system(cmd)

#### End.

#### Store of details for icon config file
def config_file_succsess():
    success = '''#!/usr/bin/env xdg-open
   
[Desktop Entry]
Version=1.0
Name = {0}
GenericName = info icon
Comment = info icon created by http://rebol.vlexo.net
Exec = python /home/asdf/Dev/python/info_icons/icon_weather.py
Terminal = false
Type = Application
Icon = {1}
Categories = Application
MimeType = image/jpeg;text/python;
StartupNotify = false
'''.format(rss_feed_summary + "\\n" + rss_feed_time, icon_image) # {0}, {1}, etc...
    f = open('/home/asdf/Dev/python/info_icons/test.desktop','w') # need to find how to get ls of location of python script.
    f.write(success)   
    f.close()
    update_icon_file()

def config_file_fail():
    failed = '''#!/usr/bin/env xdg-open
   
[Desktop Entry]
Version=1.0
Name = {0}
GenericName = info icon
Comment = info icon created by http://rebol.vlexo.net
Exec = python /home/asdf/Dev/python/info_icons/icon_weather.py
Terminal = false
Type = Application
Icon = /home/asdf/Dev/python/info_icons/icons/icon-mystery.jpg
Categories = Application
MimeType = image/jpeg;text/python;
StartupNotify = false
'''.format(connect_error)
    f = open('/home/asdf/Dev/python/info_icons/test.desktop','w') # need to find how to get ls of location of python script.
    f.write(failed)
    f.close()
    update_icon_file()

#### End.

#### Get some RSS data from server
def write_it():
    global rss_feed_summary, rss_feed_time, connect_error
    try:
        df = feedparser.parse('http://www.rssweather.com/wx/fi/pirkkala/rss.php')
        rss_feed_summary = str(df.entries[0].summary)
        rss_feed_time = str(df.entries[0].updated)
        which_icon()
        config_file_succsess()
    except:
        connect_error = "Could not retrieve data from server."
        config_file_fail()


def which_icon():
    global icon_image, s, temp
   
    # Search for a word or number in string <line1>
    s = rss_feed_summary.lower() # all lowercase
    temp = s.split() # split by space
    temp = temp[0][:-1] # trim the 'c' from temperature
    temp = int(temp) # convert string to integer
   
    if ("fog" or "mist") in s:
        icon_image = "/home/asdf/Dev/python/info_icons/icons/icon-fog.jpg"
    elif "rain" in s:
        if not "grains" in s:
            icon_image = "/home/asdf/Dev/python/info_icons/icons/icon-rain.jpg"
    elif "rain" in s:
        if not "grains" in s:
            icon_image = "/home/asdf/Dev/python/info_icons/icons/icon-rain.jpg"
    elif ("wind" or "gale") in s:
        icon_image = "/home/asdf/Dev/python/info_icons/icons/icon-wind"
    elif temp > 0:
        if temp < 10:
            icon_image = "/home/asdf/Dev/python/info_icons/icons/icon-cold.jpg"
    elif temp < -25:
        icon_image = "/home/asdf/Dev/python/info_icons/icons/icon-hibernate.gif"
    elif temp < 0:
        icon_image = "/home/asdf/Dev/python/info_icons/icons/icon-winter.jpg"
    elif temp > 30:
        icon_image = "/home/asdf/Dev/python/info_icons/icons/icon-fire.jpg"
    elif temp > 20:
        icon_image = "/home/asdf/Dev/python/info_icons/icons/icon-summer.jpg"
    else:
        icon_image = "/home/asdf/Dev/python/info_icons/icons/icon-mystery.jpg"


write_it()
#### End.
User avatar
leke
LXF regular
 
Posts: 506
Joined: Mon Oct 22, 2007 5:45 pm
Location: Oulu, Finland

Postby nelz » Fri Dec 17, 2010 9:42 am

This is probably to do with the execution environment, when you run it from a terminal, you have sourced .bashrc.

The trouble with os.system() is it just sends the command to the system and forgets about it, so you get no return codes or error messages, try using the subprocess module instead.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8502
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby leke » Fri Dec 17, 2010 12:22 pm

nelz wrote:This is probably to do with the execution environment, when you run it from a terminal, you have sourced .bashrc.

The trouble with os.system() is it just sends the command to the system and forgets about it, so you get no return codes or error messages, try using the subprocess module instead.
Thanks, I never knew about that. Just reading up on it now.
User avatar
leke
LXF regular
 
Posts: 506
Joined: Mon Oct 22, 2007 5:45 pm
Location: Oulu, Finland


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests