Mandag 26 marts 2007 15:21 kvad Philippe Andersson:
> Hi Johnny,
>
> Johnny Ernst Nielsen wrote:
> > Mandag 26 marts 2007 15:05 kvad Philippe Andersson:
> >> Johnny Ernst Nielsen wrote:
> >>> My printer's ink level program is unable to display ink level
> >>> as well as print when either one of the four cartridges are
> >>> empty. The cartridges are opaque and the printer itself only
> >>> lights up one single light to signal that one of the cartridges
> >>> are empty.
> >>
> >> Would this be some sort of Epson all-in-one device, by any
> >> chance ?
> >
> > No. Printer only. Epson Stylus C66.
>
> Well, maybe the printer part of the all-in-one uses the same
> driver. My kid sister has one of those, and faces the same troubles
> managing the ink. I'd be interested in your scripts, if you agree
> to share them.

Sure.
It's basically just a loop that calls the printer driver's command 
line ink level function, appends the answer to a file, and goes to 
sleep for 60 seconds.

Please note that this script is hardcoded for my printer and the 
specific proprietary printer driver I use.

Please also note that the posted code is translated from danish, so 
typing errors may have been introduced.

Here is the Python code:

---o---
#!/usr/bin/python
# -*- coding: utf8 -*-

#IMPORT PRACTICAL MODULES FOR DATA HANDLING.

#Handling of sub processes/calls of programs to have their output 
directly readable in the script.
import subprocess
#Handling of files.
import os
#For suspending the script for a given time.
import time
#Date and time.
import datetime
#String manipulation:
import string

#PRACTICAL VARIABLES.

#The place and name for the ink level log.
inklevellogfile="/root/Inklevellog.txt"
#The place and name of the ink level log error file.
inklevellogerrorfile="/root/Inklevellogerror.txt"

#HERE STARTS THE SCRIPT.

#Make a list to keep the previous result.
previousresult=[]
#Get the latest log entry.
if os.path.isfile(inklevellogfile):
        file=open(inklevellogfile, 'rU')
        #Read all the file's lines into a result list.
        previousresult=file.readlines()
        #Close the file.
        file.close()
        #If the file was not brand new.
        if len(previousresult) > 1:
                #Remove all lines, except the last four, which contains the 
latest 
ink level. I.e. keep from the 5Th last line to 2Nd last line.
                previousresult=previousresult[len(previousresult)-5:-1]
                #Make the list into a string.
                previousresult=repr(previousresult)
                #Remove alle encoded line breaks.
                previousresult=string.replace(previousresult,"\\n","")
                #Remove all double spacing.
                while "  " in previousresult:
                        previousresult=string.replace(previousresult,"  "," ")
#There was no ink level log.
else:
        #Make an empty log file.
        file=open(inklevellogfile, "w")
        file.close()
        #Assume all cartridges are 100% filled.
        previousresult="['Black : 100%', 'Cyan : 100%', 'Magenta : 
100%', 'Yellow : 100%']"

#Never ending loop...
while True:
        #List USB-devices.
        process=subprocess.Popen("/usr/sbin/lsusb", shell=True, 
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        result=process.stdout.read()
        #See if the printer is among the USB devices.
        if "Seiko Epson Corp. Stylus Printer" in result:
                #See if the printer is idle/not engaged in printing.
                process=subprocess.Popen("lpstat -p", shell=True, 
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                result=process.stdout.read()
                #If the printer is idle.
                if " is idle." in result:
                        #Get date and time.
                        time=datetime.datetime.now()
                        #Check ink level.
                        process=subprocess.Popen("/usr/bin/tpconfig --ink", 
shell=True, 
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                        result=process.stdout.readlines()
                        errorresult=process.stderr.read()
                        #Make the result into a string.
                        result=repr(result)
                        #Remove all encoded line breaks.
                        result=string.replace(result,"\\n","")
                        #Remove all double spaces.
                        while "  " in result:
                                result=string.replace(result,"  "," ")
                        #If an ink level is reported...
                        if "Black :" in result:
                                #...and the ink level is different from the 
previous ink level; we 
must add a log entry.
                                if not result==previousresult:
                                        #Make the string into a list, so we can 
write the four lines of 
ink levels in the log.
                                        result=eval(result)
                                        #Append date, time and ink level to the 
log file.
                                        file=open(inklevellogfile, "aw")
                                        file.write(time.strftime("Day: %d, 
month: %m, year: %Y, time: %H.
%M\n"))
                                        for line in result:
                                                file.write(line+'\n')
                                                #Dividing line.
                                        file.write("---o---\n")
                                        file.close()
                                        #Remember the new result for the next 
comparison.
                                        previousresult=repr(result)
                        #If no ink level is reported, we must log an error.
                        else:
                                #Open the error log.
                                file=open(inklevellogerrorfile, "aw")
                                #Write date and time.
                                file.write(time.strftime("Day: %d, month: %m, 
year: %Y, time: %H.
%M\n"))
                                #Write the error.
                                file.write(errorresult)
                                #Dividing line.
                                file.write("---o---\n")
                                file.close()
        #Wait 60 seconds before next ink level check.
        time.sleep(60)
#[END OF SCRIPT]

---o---

Best regards :o)

Johnny :o)
-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to