OESF Portables Forum

Everything Else => Zaurus - Everything Development => Distros, Development, and Model Specific Forums => Archived Forums => Python => Topic started by: bam on August 09, 2005, 03:35:37 pm

Title: Python Question
Post by: bam on August 09, 2005, 03:35:37 pm
I need to search a line of text out in a file and replace it, so far the code examples on the net have produced empty files.

Basically the file is a *.conf file, now the structure of the file looks alot like a win *.ini file and in visual basic there is a function to access the sections (the headers in brackets) and each entry (the name with '='). Now the question,  does python support or rather have a built in function for handleing that? If yes what is it? If no then how do I do a search and replace? I only have a partial string and want to replace the whole line until the eof(its the last line in the file)

and second question how is the Launcher.conf 'reloaded' after a change, does it get checked every so often?

or even better, what command line will change my wallpaper? Obviously you have the direction I am looking in so far.
Title: Python Question
Post by: bam on August 10, 2005, 03:06:38 pm
ok got the generator to work properly, I can randonly generate any pic from a specific directory and change the launcher.conf file.

Problem: How to tell the launcher to "refresh" the wallpaper. Or is there a commandline to set the wallpaper already, so I dont have to edit the launcher.config file?

BTW: in that file containes a list of fastload apps too, along with some other interesting stuff.
Title: Python Question
Post by: nilch on August 10, 2005, 03:15:02 pm
So you got the string replace command in Python ?
If you didnt ....

You can search each line till eof after reading it into a list (or tuple, I keep getting confused which is which)

readFile = open(dataPath + "launcher.conf")
readLines = readFile.readlines()
for line in readLines :
    header = line.split("=")[0]
    entry = line.split("=")[1]

Once you find a match (on header or enrty), use replace function to replace a string with new string

For reference - string functions of Python
http://docs.python.org/lib/string-methods.html (http://docs.python.org/lib/string-methods.html)
Title: Python Question
Post by: bam on August 10, 2005, 03:59:28 pm
yea, go that, actually I only know the entry name "wallFName =" the rest is unknown, so I used the "for line in sFile" then did a if/then/else construct when it finds the line re-write the entire line(this is what *.replace does not do"

hek I will post the code, I just have to find a way to tell the launcher o refresh the wallpaper, google provided nothing
Code: [Select]
#!/usr/bin/env python

import os

import sys

import fileinput

import glob

import random

import re



piclist=glob.glob('/hdd3/Documents/Image_Files/wallpapers/*.*')
wallpic=random.choice(piclist)

wppath="/hdd3/Documents/Image_Files/wallpapers/" + wallpic

#/home/root/settings/launcher.conf


textToSearchFor= "wallFName"

newWallP= "wallFName =" + wppath

fileToSearch= "/hdd3/Documents/temp/launcher.conf"

fileToOutput= "/hdd3/Documents/temp/tempfile.conf"
        
fileToOutput = open( fileToOutput, 'w' )

#print "outfile = ", fileToOutput


for line in fileinput.input( fileToSearch ):

    #fileToOutput.write( line.replace( textToSearchFor, textToReplaceWith ) )

    slinee=""

    sline=line[0:9]

    
    if sline == "wallFName":

  fileToOutput.write(newWallP)

  
    else:

  fileToOutput.write(line)  

  
  
fileinput.close()

fileToOutput.close()
Title: Python Question
Post by: kopsis on August 10, 2005, 10:56:12 pm
Regular expressions are your friend
Code: [Select]
for line in fileinput.input( fileToSearch ):
    fileToOutput.write(re.sub(r'wallFName =.*', "wallFName = " + wallpic, line))
And yes, a compiled re would be faster but I'm too lazy to type the extra line of code
Title: Python Question
Post by: bam on August 11, 2005, 04:08:00 am
cool, gotta try that one, you recommend any books? I am a vb programmer as well, with some fortran.
Title: Python Question
Post by: zmiq2 on August 11, 2005, 04:27:29 am
For having the launcher reloaded, serach the forums for qcop; sending a qcop message will tell qtopia to reload its settings.

HTH
Title: Python Question
Post by: kopsis on August 11, 2005, 07:44:41 am
Quote
cool, gotta try that one, you recommend any books? I am a vb programmer as well, with some fortran.
[div align=\"right\"][{POST_SNAPBACK}][/a][/div] (http://index.php?act=findpost&pid=91453\")
Truth be told, I learned Python just by reading the [a href=\"http://docs.python.org/tut/tut.html]Tutorial[/url] and the Library Reference (http://docs.python.org/lib/lib.html) but I came in with a lot of experience in other languages.

If you're an experienced programmer, you may find Dive Into Python (http://diveintopython.org/toc/index.html) helpful. It's probably a good place to start since you can read it online and then if it looks too advanced save your $$$ for something a little more beginner oriented. If you're looking for something a little more introductory I'm told that the O'Reilly Learning Python (http://www.oreilly.com/catalog/lpython2/) book is pretty good.
Title: Python Question
Post by: bam on August 12, 2005, 12:08:26 am
Quote
For having the launcher reloaded, serach the forums for qcop; sending a qcop message will tell qtopia to reload its settings.

HTH
[div align=\"right\"][a href=\"index.php?act=findpost&pid=91455\"][{POST_SNAPBACK}][/a][/div]

havent found anything to tell it to reload its settings, maybe you saw something I did not?
Title: Python Question
Post by: zmiq2 on August 16, 2005, 06:33:41 am
You can take a look at

https://www.oesf.org/index.php?title=Sharp_...a_QCop_Messages (https://www.oesf.org/index.php?title=Sharp_Qtopia_QCop_Messages)

Maybe the QPE/System restart() would do the trick for you.
Title: Python Question
Post by: slapout on August 16, 2005, 01:58:23 pm
Quote
Quote
cool, gotta try that one, you recommend any books? I am a vb programmer as well, with some fortran.
[div align=\"right\"][{POST_SNAPBACK}][/a][/div] (http://index.php?act=findpost&pid=91453\")
Truth be told, I learned Python just by reading the [a href=\"http://docs.python.org/tut/tut.html]Tutorial[/url] and the Library Reference (http://docs.python.org/lib/lib.html) but I came in with a lot of experience in other languages.

If you're an experienced programmer, you may find Dive Into Python (http://diveintopython.org/toc/index.html) helpful. It's probably a good place to start since you can read it online and then if it looks too advanced save your $$$ for something a little more beginner oriented. If you're looking for something a little more introductory I'm told that the O'Reilly Learning Python (http://www.oreilly.com/catalog/lpython2/) book is pretty good.
[div align=\"right\"][a href=\"index.php?act=findpost&pid=91470\"][{POST_SNAPBACK}][/a][/div]

Other resouces include: A Byte of Python (http://www.byteofpython.info) and the The Python Grimoire (http://the.taoofmac.com/space/Python/Grimoire)