Permalink
Browse files

Setup prints many printer names in multiple columns, instead of one c…

…olumn.
  • Loading branch information...
Jacob Marble
Jacob Marble committed Mar 19, 2014
1 parent 9978e90 commit c5c14ed62eaefa83ea6c1291d2c4b010bba3e697
Showing with 68 additions and 20 deletions.
  1. +68 −20 setupcloudprint.py
View
@@ -15,17 +15,68 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+def getWindowSize():
+ """Gets window height and width.
+
+ Gets window (aka terminal, console) height and width using IOCtl Get WINdow SiZe
+ method.
+
+ Returns:
+ The tuple (height, width) of the window as integers, or None if the
+ windows size isn't available.
+ """
+ try:
+ bytes = struct.pack('HHHH', 0, 0, 0, 0)
+ winsize = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, bytes)
+ height, width = struct.unpack('HHHH', winsize)[:2]
+ except:
+ return None
+
+ if height > 0 and width > 0:
+ return height, width
+ return None
+
+def printPrinters(printers):
+ """Prints display name of printers.
+
+ Formats as multiple columns if possible. Enumerates each printer name
+ with integers starting with 1 (not zero).
+
+ Args:
+ printers: List of printers.
+ """
+
+ printer_names = ["%d) %s" % (i + 1, printer['displayName'])
+ for i, printer in enumerate(printers)]
+
+ window_size = getWindowSize()
+ if window_size is None or window_size[0] > len(printer_names):
+ for printer_name in printer_names:
+ print printer_name
+ else:
+ window_width = window_size[1]
+ max_name_length = max((len(printer_name) for printer_name in printer_names))
+ # How many columns fit in the window, with one space between columns?
+ column_quantity = max(1, (window_width + 1) / (max_name_length + 1))
+ row_quantity = int(math.ceil(len(printer_names) / float(column_quantity)))
+
+ for row_i in xrange(row_quantity):
+ row_printers = []
+ for printer_name in printer_names[row_i::row_quantity]:
+ row_printers.append(printer_name.ljust(max_name_length))
+ print ' '.join(row_printers)
+
if __name__ == '__main__': # pragma: no cover
- import cups, os, json, sys, logging
+ import cups, os, json, sys, fcntl, termios, struct, math
from auth import Auth
from printer import Printer
from ccputils import Utils
Utils.SetupLogging()
-
+
# line below is replaced on commit
CCPVersion = "20140316 143822"
Utils.ShowVersion(CCPVersion)
-
+
unattended = False
answer = ""
@@ -77,22 +128,19 @@
print "Not adding printers automatically"
while int(answer) != 0:
- i=0
- for printeritem in printers:
- i+=1
- print str(i) + ") " + printeritem['displayName']
- maxprinterid = i
- answer = raw_input("Add printer (1-" + str(maxprinterid) + ", 0 to cancel)? ")
+ printPrinters(printers)
+ maxprinterid = len(printers)
+ answer = raw_input("Add printer (1-%d, 0 to cancel)? " % maxprinterid)
try:
answer = int(answer)
except ValueError:
answer = 0
if answer != 0:
if answer >= 1 and answer <= maxprinterid:
- ccpprinter = printers[answer-1]
- print "Adding " + printers[answer-1]['displayName']
+ ccpprinter = printers[answer - 1]
+ print "Adding " + printers[answer - 1]['displayName']
prefixanswer = raw_input("Use a prefix for name of printer (Y/N)? ")
- if ( prefixanswer.startswith("Y") or prefixanswer.startswith("y") ):
+ if prefixanswer.startswith("Y") or prefixanswer.startswith("y"):
prefix = raw_input("Prefix ( e.g. GCP- )? ")
if prefix == "":
print "Not using prefix"
@@ -104,7 +152,7 @@
if cupsprinters[cupsprinter]['device-uri'] == uri:
found = True
if found == True:
- print "\nPrinter with " + printername +" already exists\n"
+ print "\nPrinter with " + printername + " already exists\n"
else:
printer.addPrinter(printername, uri, connection)
else:
@@ -117,7 +165,7 @@
else:
prefixanswer = raw_input("Use a prefix for names of created printers (Y/N)? ")
- if ( prefixanswer.startswith("Y") or prefixanswer.startswith("y") ):
+ if prefixanswer.startswith("Y") or prefixanswer.startswith("y"):
prefix = ""
if unattended:
prefix = "GCP-"
@@ -142,13 +190,13 @@
for ccpprinter2 in cupsprinters:
if printer.sanitizePrinterName(cupsprinters[ccpprinter2]['printer-info']) == printer.sanitizePrinterName(printername):
foundbyname = True
- if ( foundbyname and not unattended ) :
+ if foundbyname and not unattended:
answer = raw_input("Printer " + printer.sanitizePrinterName(printername) + " already exists, supply another name (Y/N)? ")
- if ( answer.startswith("Y") or answer.startswith("y") ):
+ if answer.startswith("Y") or answer.startswith("y"):
printername = raw_input("New printer name? ")
else:
answer = raw_input("Overwrite " + printer.sanitizePrinterName(printername) + " with new printer (Y/N)? ")
- if ( answer.startswith("N") or answer.startswith("n") ):
+ if answer.startswith("N") or answer.startswith("n"):
printername = ""
elif foundbyname and unattended:
print "Not adding printer " + printername + ", as already exists"
@@ -157,7 +205,7 @@
if printername != "":
printer.addPrinter(printername, uri, connection)
cupsprinters = connection.getPrinters()
- addedCount+=1
+ addedCount += 1
if addedCount > 0:
print "Added " + str(addedCount) + " new printers to CUPS"
@@ -180,14 +228,14 @@
if cupsprinters[cupsprinter]['device-uri'] not in printeruris:
prunePrinters.append(cupsprinter)
- if len( prunePrinters ) > 0 :
+ if len(prunePrinters) > 0 :
print "Found " + str(len( prunePrinters )) + " printers which no longer exist on cloud print:"
for printer in prunePrinters:
print printer
answer = raw_input("Remove (Y/N)? ")
if answer.startswith("Y") or answer.startswith("y"):
for printer in prunePrinters:
connection.deletePrinter(printer)
- print "Deleted",printer
+ print "Deleted", printer
else:
print "Not removing old printers"

0 comments on commit c5c14ed

Please sign in to comment.