cupsami.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/python
  2. #python local cupsatore
  3. #ask to cups server user's jobs infos
  4. #20110311 giorgio_ruffa
  5. # debuggato da blue (28 Gen 2014): il server non restituiva piu' job-name tra gli attributi,
  6. # ho tolto tutto quello che lo riguardava dallo script
  7. from cups import Connection , getUser , setUser
  8. from datetime import datetime
  9. from sys import exit , argv
  10. import getopt
  11. from django.utils.encoding import smart_str #senza non riusciva a printare i nomi dei file in unicode
  12. #===================================================
  13. class Colors :
  14. red = "\033[1;31m"
  15. blue = "\033[1;34m"
  16. pink = "\033[1;35m"
  17. green = "\033[1;32m"
  18. end = "\033[0m"
  19. class Printer_rec:
  20. def __init__(self, u_job_date=datetime.now() , \
  21. u_sheets=0):
  22. self.job_date = u_job_date
  23. self.job_sheets = u_sheets
  24. def __init__(self, job_attr={} ):
  25. if job_attr == None :
  26. self.job_date = datetime.today()
  27. self.job_sheets = 0
  28. else :
  29. if job_attr["time-at-completed"] == None :
  30. print Colors.red + "Warning: " + "the job " ,\
  31. job_attr["job-id"] , " has no time" + Colors.end
  32. print Colors.red + "Warning: will be used today date" +\
  33. Colors.end
  34. self.job_date = datetime.today()
  35. else :
  36. self.job_date = datetime.fromtimestamp(job_attr["time-at-completed"])
  37. self.job_sheets = job_attr["job-media-sheets-completed"]
  38. def __repr__(self):
  39. return "date: " + str(self.job_date) \
  40. + " sheets: " + str(self.job_sheets)
  41. #===================================================
  42. def Usage() :
  43. print "\nCupsami: tells you how many " + \
  44. "sheets you have printed in the last 30 days"
  45. print "\nCupsami: restituisce il numero di pagine" + \
  46. "stampate negli ultimi 30 giorni"
  47. print '\nUsage: '+argv[0]+' [flags]'
  48. print ' -h\tthis help'
  49. exit(1)
  50. #===================================================
  51. debug = False
  52. #debug = True
  53. #getopt //tnxs jacopogh
  54. try: params = getopt.getopt(argv[1:],'h')
  55. except getopt.GetoptError:
  56. print 'Wrong parameter'
  57. Usage()
  58. #help mode (-h)
  59. if "-h" in str(params[0]): Usage()
  60. #get today
  61. t_today = datetime.today()
  62. #our connection
  63. conn = Connection()
  64. jobs_id = conn.getJobs("all",True).keys()
  65. if len(jobs_id) == 0 :
  66. print
  67. print "non hai stampato alcuna pagina questo mese"
  68. print
  69. exit(0)
  70. #get job attributes
  71. jobs_attr = []
  72. for i in jobs_id :
  73. jobs_attr.append( conn.getJobAttributes(i))
  74. #get printers
  75. printers = conn.getPrinters()
  76. #what we are going to print
  77. printers_records = {}
  78. #initialize the dict
  79. # it's not the py way but works
  80. for i in printers :
  81. printers_records[i] = []
  82. #parse our jobs_attr and build records
  83. for i in jobs_attr :
  84. if debug : print i
  85. #assign printer
  86. for j in printers :
  87. if j in i["job-printer-uri"].split("/") :
  88. #the job is of printer j
  89. rec = Printer_rec(i)
  90. if debug : print rec
  91. #date controll
  92. delta = t_today - rec.job_date;
  93. if delta.days > 30 :
  94. continue
  95. printers_records[j].append(rec);
  96. #ok now print "nicely"
  97. total_sheets = 0
  98. print
  99. for i in printers_records.items() :
  100. #do not print if no records
  101. if len(i[1]) == 0 : continue
  102. print Colors.blue + ("="*60) + Colors.end
  103. print
  104. print Colors.green + ( i[0].upper()).center(60) \
  105. + Colors.end
  106. print
  107. print Colors.red + "JOBS:\n" + Colors.end
  108. print Colors.pink + "DATE".ljust(10) +" "*15 \
  109. + "SHEETS" + Colors.end
  110. print
  111. tot_for_printer = 0
  112. for j in i[1]:
  113. print str(smart_str(j.job_date)).rjust(20) +\
  114. (" "*5) +\
  115. str(smart_str(j.job_sheets)).rjust(5)
  116. tot_for_printer += j.job_sheets
  117. total_sheets += j.job_sheets
  118. print Colors.red + "\nTOTAL: " + str(tot_for_printer) + Colors.end
  119. print
  120. print Colors.blue + ("="*60) + "\n" + Colors.end
  121. print Colors.red + ("TOTAL SHEETS: " + str(total_sheets)).center(60) + Colors.end
  122. print
  123. print Colors.green + "se notate un bug siete pregati di segnalarlo a: " \
  124. + "\tadmin(at)lcm.mi.infn.it" + Colors.end
  125. print