Permalink
Browse files

Setup script now adds multiple accounts

  • Loading branch information...
simoncadman committed Aug 15, 2012
1 parent 7e1cf5b commit 8e3a013567c21f2dd4b2e893f2f22d697af4ed9e
Showing with 73 additions and 45 deletions.
  1. +40 −17 auth.py
  2. +1 −1 listcloudprinters.py
  3. +32 −27 setupcloudprint.py
View
57 auth.py
@@ -14,14 +14,18 @@
# 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 urllib, urllib2, mimetools, time
+import urllib, urllib2, mimetools, time, json, os
from oauth2client import client
from oauth2client.file import Storage
from oauth2client import multistore_file
from cloudprintrequestor import cloudprintrequestor
class Auth():
+ clientid = "843805314553.apps.googleusercontent.com"
+ clientsecret = 'MzTBsY4xlrD_lxkmwFbBrvBv'
+ config = '/etc/cloudprint.conf'
+
@staticmethod
def GetUrl(url, data=None, cookies=False, anonymous=False, boundary=None):
"""Get URL, with GET or POST depending data, adds Authorization header.
@@ -64,37 +68,56 @@ def GetUrl(url, data=None, cookies=False, anonymous=False, boundary=None):
retry_count += 1
if retry_count == 5:
return err_msg
+
+ @staticmethod
+ def AddAccount(storage, userid=None):
+ if userid == None:
+ userid = raw_input("Name for this user account? ")
+
+ flow = client.OAuth2WebServerFlow(client_id=Auth.clientid,
+ client_secret=Auth.clientsecret,
+ scope='https://www.googleapis.com/auth/cloudprint',
+ user_agent=userid)
+ auth_uri = flow.step1_get_authorize_url()
+ print(userid)
+ print("Open this URL and provide the code: " + auth_uri)
+ code = raw_input('Code from Google: ')
+ credentials = flow.step2_exchange(code)
+ storage.put(credentials)
+ return credentials
@staticmethod
def SetupAuth(interactive=False):
- clientid = '843805314553.apps.googleusercontent.com'
- clientsecret = 'MzTBsY4xlrD_lxkmwFbBrvBv'
- userids = [ "cloudprint" ]
+
+ # parse config file and extract useragents, which we use for account names
+ userids = []
+ if os.path.exists( Auth.config ):
+ with open(Auth.config, 'r') as content_file:
+ content = content_file.read()
+ data = json.loads(content)
+ for user in data['data']:
+ userids.append(str(user['credential']['user_agent']))
+
+
+ if len(userids) == 0:
+ userids = [ None ]
+
requestors = []
for userid in userids:
storage = multistore_file.get_credential_storage(
- '/etc/cloudprint.conf',
- clientid,
+ Auth.config,
+ Auth.clientid,
userid,
['https://www.googleapis.com/auth/cloudprint'])
credentials = storage.get()
if not credentials and interactive:
- flow = client.OAuth2WebServerFlow(client_id=clientid,
- client_secret=clientsecret,
- scope='https://www.googleapis.com/auth/cloudprint',
- user_agent=userid)
- auth_uri = flow.step1_get_authorize_url()
- print(userid)
- print("Open this URL and provide the code: " + auth_uri)
- code = raw_input('Code from Google: ')
- credentials = flow.step2_exchange(code)
- storage.put(credentials)
+ credentials = Auth.AddAccount(storage, userid)
elif not interactive and not credentials:
return False
requestor = cloudprintrequestor()
requestor = credentials.authorize(requestor)
requestor.setAccount(userid)
requestors.append(requestor)
- return requestors
+ return requestors, storage
View
@@ -19,7 +19,7 @@
from auth import Auth
from printer import Printer
-requestors = Auth.SetupAuth(True)
+requestors, storage = Auth.SetupAuth(True)
printer = Printer(requestors)
printers = printer.getPrinters(requestors)
if printers == None:
View
@@ -7,24 +7,25 @@
useConfigDetails = True
tokens = None
-addedCount = 0
-requestors = Auth.SetupAuth(True)
while True:
+ requestors, storage = Auth.SetupAuth(True)
print "You currently have these accounts configured: "
for requestor in requestors:
print requestor.getAccount()
answer = raw_input("Add more accounts? ")
if not ( answer.startswith("Y") or answer.startswith("y") ):
break
else:
- print "Adding account"
+ Auth.AddAccount(storage)
+
for requestor in requestors:
+ addedCount = 0
answer = raw_input("Add all Google Cloud Print printers to local CUPS install from " + requestor.getAccount() + " ? ")
if not ( answer.startswith("Y") or answer.startswith("y") ):
print("Not adding printers automatically")
- sys.exit(0)
+ continue
prefix = raw_input("Use a prefix for names of created printers ( e.g. GCP- )? ")
if prefix == "":
@@ -37,14 +38,11 @@
printers = printer.getPrinters()
if printers == None:
print("No Printers Found")
- sys.exit(1)
+ continue
- printeruris = []
-
for ccpprinter in printers:
uri = printer.printerNameToUri(ccpprinter['account'], ccpprinter['name'].encode('ascii', 'replace'))
found = False
- printeruris.append(uri)
for cupsprinter in cupsprinters:
if cupsprinters[cupsprinter]['device-uri'] == uri:
found = True
@@ -53,27 +51,34 @@
addedCount+=1
if addedCount > 0:
- print("Added",addedCount,"new printers to CUPS")
+ print("Added " + str(addedCount) + " new printers to CUPS")
else:
print("No new printers to add")
-
- # check for printers to prune
- prunePrinters = []
- cupsprinters = connection.getPrinters()
- for cupsprinter in cupsprinters:
- if cupsprinters[cupsprinter]['device-uri'].startswith( Printer.PROTOCOL ):
- if cupsprinters[cupsprinter]['device-uri'] not in printeruris:
- prunePrinters.append(cupsprinter)
+printeruris = []
+printer = Printer(requestors)
+printers = printer.getPrinters()
+for foundprinter in printers:
+ printeruris.append(printer.printerNameToUri(foundprinter['account'], foundprinter['name'].encode('ascii', 'replace')))
- if len( prunePrinters ) > 0 :
- print("Found",len( prunePrinters ),"printers with no longer exist on cloud print:")
+# check for printers to prune
+prunePrinters = []
+connection = cups.Connection()
+cupsprinters = connection.getPrinters()
+
+for cupsprinter in cupsprinters:
+ if cupsprinters[cupsprinter]['device-uri'].startswith( Printer.PROTOCOL ):
+ if cupsprinters[cupsprinter]['device-uri'] not in printeruris:
+ prunePrinters.append(cupsprinter)
+
+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? ")
+ if answer.startswith("Y") or answer.startswith("y"):
for printer in prunePrinters:
- print(printer)
- answer = raw_input("Remove? ")
- if answer.startswith("Y") or answer.startswith("y"):
- for printer in prunePrinters:
- connection.deletePrinter(printer)
- print("Deleted",printer)
- else:
- print("Not removing old printers")
+ connection.deletePrinter(printer)
+ print("Deleted",printer)
+ else:
+ print("Not removing old printers")

0 comments on commit 8e3a013

Please sign in to comment.