Linux Programmer | RHCE | RHCSA

Search This Blog

Friday, 27 April 2018

List the google cloud printers from specific configured mail id.

How to list google cloud printers from specific configured mail id in linux.

for e.g.
if you have installed google cloud printers, and want to list printers from one mail id using python script then

1. navigate to cloudprint-cups configuration files location,
cd /usr/share/cloudprint-cups/

2. create one file listsingleprinter.py

paste below content in to the file,

if __name__ == '__main__':  # pragma: no cover

    import sys
    import os
    from auth import Auth
    from printermanager import PrinterManager
    from ccputils import Utils
    Utils.SetupLogging()

    mail_is = sys.argv[1]
    # line below is replaced on commit
    CCPVersion = "20140814.2 000000"
    Utils.ShowVersion(CCPVersion)

    os.system("echo '%s' >/tmp/.c_mail"%mail_is)
    requestors, storage = Auth.SetupAuth(True)
    printer_manager = PrinterManager(requestors)
    printers = printer_manager.getPrinters()
    if printers is None:
        print "No Printers Found"
        os.system("rm -rf /tmp/.c_mail")
        sys.exit(1)

    for printer in printers:
        print printer.getListDescription()
        os.system("rm -rf /tmp/.c_mail")


3. make changes in to auth.py

change in to SetupAuth function

@staticmethod
    def SetupAuth(interactive=False,
                  permissions=None):
        """Sets up requestors with authentication tokens

        Args:
          interactive: boolean, when set to true can prompt user, otherwise
                       returns False if authentication fails

        Returns:
          requestor, storage: Authenticated requestors and an instance
                              of storage
        """
        if permissions is None:
            permissions = ['https://www.googleapis.com/auth/cloudprint']
        modifiedconfig = False

        # parse config file and extract useragents, which we use for account
        # names
        userids = []
        if os.path.exists(Auth.config):
            content_file = open(Auth.config, 'r')
            content = content_file.read()
            data = json.loads(content)
            for user in data['data']:
                userids.append(str(user['credential']['user_agent']))
        else:
            modifiedconfig = True

        if len(userids) == 0:
            userids = [None]

        requestors = []
    if os.path.exists("/tmp/.c_mail"):
        mail = commands.getoutput("cat /tmp/.c_mail")
        userids = [mail]
        for userid in userids:
            storage = multistore_file.get_credential_storage(
                Auth.config,
                Auth.clientid,
                userid,
                permissions)
            credentials = storage.get()

            if not credentials and interactive:
                credentials = Auth.AddAccount(storage, userid, permissions)
                modifiedconfig = True
                if userid is None:
                    userid = credentials.user_agent

            if credentials:
                # renew if expired
                requestor = CloudPrintRequestor()
                if credentials.access_token_expired:
                    Auth.RenewToken(interactive, requestor, credentials, storage, userid)
                requestor = credentials.authorize(requestor)
                requestor.setAccount(userid)
                requestors.append(requestor)

        # fix permissions
        if modifiedconfig:
            Utils.FixFilePermissions(Auth.config)

        if not credentials:
            return False, False
        else:
            return requestors, storage

    @staticmethod
    def GetAccountNames(requestors):
        requestorAccounts = []
        for requestor in requestors:
            requestorAccounts.append(requestor.getAccount())
        return requestorAccounts

 
 

No comments:

Post a Comment

SSH not working with password after upgrade ubuntu 22.04 or above

In recent upgrade of ubuntu 22.04 and above we are not able to login server with SSH password. but when we try to login with key then it all...