Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Updating the desktop icon in python

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
leke
LXF regular


Joined: Mon Oct 22, 2007 6:45 pm
Posts: 500
Location: Oulu, Finland

PostPosted: Fri Dec 17, 2010 9:03 am    Post subject: Updating the desktop icon in python Reply with quote

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:
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:
#!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.

_________________
I codes here: http://notsure.cz.cc/
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8364
Location: Warrington, UK

PostPosted: Fri Dec 17, 2010 9:42 am    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
leke
LXF regular


Joined: Mon Oct 22, 2007 6:45 pm
Posts: 500
Location: Oulu, Finland

PostPosted: Fri Dec 17, 2010 12:22 pm    Post subject: Reply with quote

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.
_________________
I codes here: http://notsure.cz.cc/
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast