48 | | if __name__ == "__main__" : |
49 | | retcode = -1 |
50 | | try : |
51 | | cmd = PyKotaTool(doc="A tool to display remaining print units") |
52 | | except : |
53 | | crashed("Initialization problem.") |
54 | | else : |
55 | | try : |
56 | | uid = os.geteuid() |
57 | | uname = pwd.getpwuid(uid)[0] |
58 | | user = cmd.storage.getUser(uname) |
| 53 | command line usage : |
| 54 | |
| 55 | pykosd [options] |
| 56 | |
| 57 | options : |
| 58 | |
| 59 | -v | --version Prints pkprinters's version number then exits. |
| 60 | -h | --help Prints this message then exits. |
| 61 | |
| 62 | -d | --duration d Sets the duration of the display in seconds. |
| 63 | Defaults to 3 seconds. |
| 64 | |
| 65 | -l | --loop n Sets the number of times the info will be displayed. |
| 66 | Defaults to 0, which means loop forever. |
| 67 | |
| 68 | -s | --sleep s Sets the sleeping duration between two displays |
| 69 | in seconds. Defaults to 180 seconds (3 minutes). |
| 70 | |
| 71 | |
| 72 | examples : |
| 73 | |
| 74 | $ pykosd -s 60 --loop 5 |
| 75 | |
| 76 | Will launch pykosd. Display will be refreshed every 60 seconds, |
| 77 | and will last for 3 seconds (the default) each time. After five |
| 78 | iterations, the program will exit. |
| 79 | |
| 80 | This program is free software; you can redistribute it and/or modify |
| 81 | it under the terms of the GNU General Public License as published by |
| 82 | the Free Software Foundation; either version 2 of the License, or |
| 83 | (at your option) any later version. |
| 84 | |
| 85 | This program is distributed in the hope that it will be useful, |
| 86 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 87 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 88 | GNU General Public License for more details. |
| 89 | |
| 90 | You should have received a copy of the GNU General Public License |
| 91 | along with this program; if not, write to the Free Software |
| 92 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
| 93 | |
| 94 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
| 95 | |
| 96 | |
| 97 | class PyKOSD(PyKotaTool) : |
| 98 | def main(self, args, options) : |
| 99 | """Main function starts here.""" |
| 100 | try : |
| 101 | duration = int(options["duration"]) |
| 102 | except : |
| 103 | raise PyKotaToolError, "Invalid duration option %s" % str(options["duration"]) |
| 104 | |
| 105 | try : |
| 106 | loop = int(options["loop"]) |
| 107 | except : |
| 108 | raise PyKotaToolError, "Invalid loop option %s" % str(options["loop"]) |
| 109 | |
| 110 | try : |
| 111 | sleep = float(options["sleep"]) |
| 112 | except : |
| 113 | raise PyKotaToolError, "Invalid sleep option %s" % str(options["sleep"]) |
| 114 | |
| 115 | uid = os.geteuid() |
| 116 | uname = pwd.getpwuid(uid)[0] |
| 117 | while 1 : |
| 118 | user = cmd.storage.getUserFromBackend(uname) # don't use cache |
81 | | display = pyosd.osd(colour=color, timeout=DURATION, shadow=2) |
82 | | display.display("PyKota Units left : %.2f" % user.AccountBalance, type=pyosd.TYPE_STRING) |
83 | | time.sleep(DURATION + 1) |
84 | | except : |
85 | | cmd.crashed("Strange problem : please report it ASAP to alet@librelogiciel.com") |
86 | | else : |
87 | | retcode = 0 |
| 141 | display = pyosd.osd(colour=color, timeout=duration, shadow=2) |
| 142 | display.display(_("PyKota Units left : %.2f") % user.AccountBalance, type=pyosd.TYPE_STRING) |
| 143 | time.sleep(duration + 1) |
| 144 | if loop : |
| 145 | loop -= 1 |
| 146 | if not loop : |
| 147 | break |
| 148 | time.sleep(sleep) |
| 149 | |
| 150 | return 0 |
| 151 | |
| 152 | if __name__ == "__main__" : |
| 153 | retcode = -1 |
| 154 | try : |
| 155 | defaults = { \ |
| 156 | "duration" : "3", \ |
| 157 | "loop" : "0", \ |
| 158 | "sleep" : "180", \ |
| 159 | } |
| 160 | short_options = "hvd:l:s:" |
| 161 | long_options = ["help", "version", "duration=", "loop=", "sleep="] |
| 162 | |
| 163 | cmd = PyKOSD(doc=__doc__) |
| 164 | |
| 165 | # parse and checks the command line |
| 166 | (options, args) = cmd.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
| 167 | |
| 168 | # sets long options |
| 169 | options["help"] = options["h"] or options["help"] |
| 170 | options["version"] = options["v"] or options["version"] |
| 171 | options["duration"] = options["d"] or options["duration"] or defaults["duration"] |
| 172 | options["loop"] = options["l"] or options["loop"] or defaults["loop"] |
| 173 | options["sleep"] = options["s"] or options["sleep"] or defaults["sleep"] |
| 174 | |
| 175 | if options["help"] : |
| 176 | cmd.display_usage_and_quit() |
| 177 | elif options["version"] : |
| 178 | cmd.display_version_and_quit() |
| 179 | else : |
| 180 | retcode = cmd.main(args, options) |
| 181 | except KeyboardInterrupt : |
| 182 | retcode = 0 |
| 183 | except SystemExit : |
| 184 | pass |
| 185 | except : |