davesteele / cloudprint-service

Debian packaging for the Python cloudprint proxy utility
GNU General Public License v3.0
71 stars 4 forks source link

printer prefix to handle duplicate printer names. #22

Closed dmonty2 closed 8 years ago

dmonty2 commented 9 years ago

This is a great script! We are using it with multiple schools for our chromebooks.

However we have one issue. Some schools have cups print shares with the same name and it is difficult to sort them out on google cloud print account. Instead of creating different cloud print users for each school is it possible to have this cloudprintd support pre-fixing the printer names with a school name? e.g. /etc/defaults/cloudprintd PREFIX="southill"

When we log into https://www.google.com/cloudprint#printers the PREFIX would sort out duplicates:

southhill-library jenson-library carlson-library

Thanks.

dmonty2 commented 9 years ago

I've also just discovered that adding a 2nd school couldprintd deletes all the printers from cloudprint schools added before. So this PREFIX="southill" should also ensure that it only adds/removes printers for that school.

dmonty2 commented 9 years ago

I've updated the code to support SITE="schoolname" in /etc/default/cloudprintd /usr/share/cloudprint/cloudprint/cloudprint.py

--- /home/d/dmonty/cloudprint.py    2015-04-30 11:51:49.340267649 -0700
+++ /usr/share/cloudprint/cloudprint/cloudprint.py  2015-04-30 14:01:14.416379718 -0700
@@ -71,7 +71,9 @@
         self.verbose = verbose
         self.auth = None
         self.cups= cups.Connection()
-        self.proxy =  platform.node() + '-Armooo-PrintProxy'
+        self.site = 'Armooo-PrintProxy'
+        self.prefix = False
+        self.proxy =  platform.node() + '-' + self.site
         self.auth_path = os.path.expanduser('~/.cloudprintauth')
         self.xmpp_auth_path = os.path.expanduser('~/.cloudprintauth.sasl')
         self.username = None
@@ -188,7 +190,7 @@
             'application/x-www-form-urlencoded',
             { 'X-CloudPrint-Proxy' : 'ArmoooIsAnOEM'},
         )
-        return [ PrinterProxy(self, p['id'], p['name']) for p in printers['printers'] ]
+        return [ PrinterProxy(self, p['id'], re.sub('^' + self.site + '-','', p['name'])) for p in printers['printers'] ]

     def delete_printer(self, printer_id):
         r = self.get_rest()
@@ -205,6 +207,8 @@
             LOGGER.info('Deleted printer '+ printer_id)

     def add_printer(self, name, description, ppd):
+        if self.prefix:
+            name = self.site + '-' + name
         r = self.get_rest()
         r.post(
             PRINT_CLOUD_URL + 'register',
@@ -225,6 +229,8 @@
             LOGGER.info('Added Printer ' + name)

     def update_printer(self, printer_id, name, description, ppd):
+        if self.prefix:
+            name = self.site + '-' + name
         r = self.get_rest()
         r.post(
             PRINT_CLOUD_URL + 'update',
@@ -461,6 +467,8 @@
             help='include local printers matching %(metavar)s')
     parser.add_argument('-x', metavar='regexp', dest='exclude', default=[], action='append',
             help='exclude local printers matching %(metavar)s')
+    parser.add_argument('-s', dest='site', default='Armooo-PrintProxy',
+            help='unique site name that will be prefix printers')
     parser.add_argument('-v', dest='verbose', action='store_true',
             help='verbose logging')
     args = parser.parse_args()
@@ -503,6 +511,10 @@

     cpp.include = args.include
     cpp.exclude = args.exclude
+    if cpp.site != args.site:
+        cpp.prefix = True
+    cpp.site = args.site
+    cpp.proxy = platform.node() + '-' + args.site

     #try to login
     while True:

/etc/default/cloudprintd

--- /etc/default/cloudprintd.orig   2015-04-30 14:11:07.000000000 -0700
+++ /etc/default/cloudprintd    2015-04-30 14:13:19.000000000 -0700
@@ -22,3 +22,7 @@
 # EXCLUDE_LIST="GCF test"
 # INCLUDE_LIST=""

+# Site is a string that is prefixed onto all printers.
+# This allows for multiple cloudprintd servers to share a single cloudprint account.
+# If two different sites have same "library" printer then each site will get SITE-library
+#SITE=""

/etc/init.d/cloudprintd

--- /etc/init.d/cloudprintd.orig    2015-04-30 14:14:53.000000000 -0700
+++ /etc/init.d/cloudprintd 2015-04-30 12:43:15.000000000 -0700
@@ -37,6 +37,11 @@
      INCLUDE_OPT="$INCLUDE_OPT -i $str";\
 done

+for str in $SITE ; do \
+     INCLUDE_OPT="$INCLUDE_OPT -s $str";\
+done
+
+
 AUTHFILE=/var/lib/cloudprintd/authfile
 PIDFILE=/var/run/$NAME.pid
 DAEMON_ARGS="-d -u -a $AUTHFILE -p $PIDFILE $FAST_POLL $INCLUDE_OPT"

I haven't updated the man page but the here document contains brief note. -s unique site name that will be prefix printers

davesteele commented 9 years ago

A couple of comments:

I'd also prefer a pull request, if you are comfortable with that.

dmonty2 commented 9 years ago

My boss needed this working feature working right away for our schools. If you could merge something similar into the code. In response to your feedback:

Please forgive my python skills. Thanks again for your great script - much better than running a full blown chrome browser. Your INCLUDE_LIST was great as For 50 sites I was able to filter down necessary 106 printers for our chromebooks. If I get time today I can look into creating a pull request.

dmonty2 commented 9 years ago

Some feature requests:

davesteele commented 9 years ago

To be clear, I am not the author of the cloudprint script - there is an upstream project. You can submit to me for a (probably) quicker response, but the patches should also go to armooo.

dmonty2 commented 9 years ago

I've re-posted the request upstream: https://github.com/armooo/cloudprint/issues/96

davesteele commented 8 years ago

It is incorporated upstream, post 0.14 - https://github.com/armooo/cloudprint/commit/277a5b052af6c9244fd51f8615436479caedf1af

davesteele commented 8 years ago

... and cherry-picked, post 0.;14-4 - https://github.com/davesteele/cloudprint-service/commit/af737b8742995dd37107bf151871933852f263f5