Permalink
Browse files

Only allow letters, numbers, underscores and hyphens in cups printer …

…names, fixed issue #13
  • Loading branch information...
simoncadman committed Aug 26, 2012
1 parent 8c83d6d commit 0a1deb3cd8181095a2e7b059c708d428c40d7bf4
Showing with 18 additions and 6 deletions.
  1. +14 −2 printer.py
  2. +4 −4 setupcloudprint.py
View
@@ -13,7 +13,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-import json, urllib, os, mimetypes, base64, mimetools
+import json, urllib, os, mimetypes, base64, mimetools, re
from auth import Auth
from urlparse import urlparse
@@ -61,6 +61,18 @@ def printerNameToUri( self, account, printer ) :
"""
return self.PROTOCOL + urllib.quote(printer) + "/" + urllib.quote(account)
+
+ def sanitizePrinterName ( self, name ) :
+ """Sanitizes printer name for CUPS
+
+ Args:
+ name: string, name of printer from Google Cloud Print
+
+ Returns:
+ string: CUPS-friendly name for the printer
+ """
+ return re.sub('[^a-zA-Z0-9\-_]', '', name.encode('ascii', 'replace').replace( ' ', '_' ) )
+
def addPrinter( self, printername, uri, connection ) :
"""Adds a printer to CUPS
@@ -73,7 +85,7 @@ def addPrinter( self, printername, uri, connection ) :
None
"""
# fix printer name
- printername = printername.replace(' ','_')
+ printername = self.sanitizePrinterName(printername)
result = None
try:
result = connection.addPrinter(name=printername,ppdname='CloudPrint.ppd',info=printername,location='Google Cloud Print',device=uri)
View
@@ -58,19 +58,19 @@
if prefix == "":
print("Not using prefix")
- printername = prefix + ccpprinter['name'].encode('ascii', 'replace')
+ printername = prefix + ccpprinter['name']
# check if printer name already exists
foundbyname = False
for ccpprinter2 in cupsprinters:
- if cupsprinters[ccpprinter2]['printer-info'].replace(' ', '_') == printername.replace(' ', '_'):
+ if printer.sanitizePrinterName(cupsprinters[ccpprinter2]['printer-info']) == printer.sanitizePrinterName(printername):
foundbyname = True
if ( foundbyname ) :
- answer = raw_input("Printer " + printername + " already exists, supply another name? ")
+ answer = raw_input("Printer " + printer.sanitizePrinterName(printername) + " already exists, supply another name? ")
if ( answer.startswith("Y") or answer.startswith("y") ):
printername = raw_input("New printer name? ")
else:
- answer = raw_input("Overwrite " + printername + " with new printer? ")
+ answer = raw_input("Overwrite " + printer.sanitizePrinterName(printername) + " with new printer? ")
if ( answer.startswith("N") or answer.startswith("n") ):
printername = ""

0 comments on commit 0a1deb3

Please sign in to comment.