def test_missing_config(): """Test that we can deal with a missing file.""" with tempfile.NamedTemporaryFile() as tmp: tmpfn = tmp.name csu.CONFIG_FN = tmpfn cfg = csu.get_config() assert cfg is None csu.save_config({})
def test_config(self): """Make sure we exercise the config logic as things get hairy""" (_, tmpfn) = tempfile.mkstemp() # create bogus config file cfg = dict(a='a', b='b') # Write config to bogus file save_config(cfg, tmpfn) # Attempt to load it back now cfg = get_config(tmpfn) self.assertTrue(cfg is not None) os.unlink(tmpfn)
def test_config(): """Make sure we exercise the config logic as things get hairy""" (_, tmpfn) = tempfile.mkstemp() # create bogus config file cfg = dict(a='a', b='b') # Write config to bogus file save_config(cfg, tmpfn) # Attempt to load it back now cfg = get_config(tmpfn) assert cfg is not None os.unlink(tmpfn)
def test_config(): """Make sure we exercise the config logic as things get hairy""" (_, tmpfn) = tempfile.mkstemp() # create bogus config file cfg = dict(a="a", b="b") # Write config to bogus file csu.save_config(cfg, tmpfn) # Attempt to load it back now cfg = csu.get_config(tmpfn) assert cfg is not None os.unlink(tmpfn)
def drive_changelog(regime, yesterday, html): """ Do something """ drive = util.get_driveclient(CONFIG, regime) folders = util.get_folders(drive) start_change_id = CONFIG[regime]["changestamp"] html += """<p><table border="1" cellpadding="3" cellspacing="0"> <thead> <tr><th>Folder</th><th>Resource</th></tr> </thead> <tbody>""" largestChangeId = -1 hits = 0 page_token = None changestamp = None param = {'includeDeleted': False, 'maxResults': 1000} while True: if start_change_id: param['startChangeId'] = start_change_id if page_token: param['pageToken'] = page_token print(("[%s] start_change_id: %s largestChangeId: %s page_token: %s" ) % (regime, start_change_id, largestChangeId, page_token)) response = drive.changes().list(**param).execute() largestChangeId = response['largestChangeId'] page_token = response.get('nextPageToken') for item in response['items']: if item['file']['mimeType'] in [FMIME, FORM_MTYPE, SITES_MYTPE]: continue changestamp = item['id'] if item['deleted']: continue # don't do more work when this file actually did not change modifiedDate = datetime.datetime.strptime( item['file']['modifiedDate'][:19], '%Y-%m-%dT%H:%M:%S') modifiedDate = modifiedDate.replace(tzinfo=pytz.timezone("UTC")) if modifiedDate < yesterday: continue # Need to see which base folder this file is in! isproject = False for parent in item['file']['parents']: if parent['id'] not in folders: print(('[%s] file: %s has unknown parent: %s' ) % (regime, item['id'], parent['id'])) continue isproject = True if not isproject: print(('[%s] %s (%s) skipped as basefolders are: %s' ) % (regime, repr(item['file']['title']), item['file']['mimeType'], item['file']['parents'])) continue uri = item['file']['alternateLink'] title = item['file']['title'].encode( 'ascii', 'ignore').decode('ascii') localts = modifiedDate.astimezone(LOCALTZ) hits += 1 pfolder = item['file']['parents'][0]['id'] html += """ <tr> <td><a href="https://docs.google.com/folderview?id=%s&usp=drivesdk">%s</a></td> <td><a href="%s">%s</a></td></tr> """ % (pfolder, folders[pfolder]['title'], uri, title) hit = False if 'version' in item['file']: lastmsg = "" try: revisions = drive.revisions().list( fileId=item['file']['id']).execute() except Exception as _exp: print(('[%s] file %s (%s) failed revisions' ) % (regime, title, item['file']['mimeType'])) revisions = {'items': []} for item2 in revisions['items']: # print pprint(item2) md = datetime.datetime.strptime( item2['modifiedDate'][:19], '%Y-%m-%dT%H:%M:%S') md = md.replace(tzinfo=pytz.timezone("UTC")) if md < yesterday: continue localts = md.astimezone(LOCALTZ) # for some reason, some revisions have no user associated # with it. So just skip for now # http://stackoverflow.com/questions/1519072 if 'lastModifyingUser' not in item2: continue luser = item2['lastModifyingUser'] hit = True display_name = luser['displayName'] email_address = luser['emailAddress'] if display_name == CONFIG['service_account']: display_name = "daryl's magic" email_address = "*****@*****.**" thismsg = """ <tr><td colspan="2"><img src="%s" style="height:25px;"/> %s by %s (%s)</td></tr> """ % ((luser['picture']['url'] if 'picture' in luser else ''), localts.strftime("%-d %b %-I:%M %p"), display_name, email_address) if thismsg != lastmsg: html += thismsg lastmsg = thismsg # Now we check revisions if not hit: luser = item['file'].get('lastModifyingUser', dict()) html += """ <tr><td colspan="2"><img src="%s" style="height:25px;"/> %s by %s (%s)</td></tr> """ % (luser['picture']['url'] if 'picture' in luser else '', localts.strftime("%-d %b %-I:%M %p"), luser.get('displayName', 'n/a'), luser.get('emailAddress', 'n/a')) if not page_token: break if changestamp is not None: CONFIG[regime]['changestamp'] = changestamp if hits == 0: html += """<tr><td colspan="5">No Changes Found...</td></tr>\n""" html += """</tbody></table>""" util.save_config(CONFIG) return html
"""Script fetches openauth2 refresh token that my offline scripts can use """ from gdata import gauth import pyiem.cscap_utils as util config = util.get_config() token = gauth.OAuth2Token( client_id=config["appauth"]["client_id"], client_secret=config["appauth"]["app_secret"], scope=config["googleauth"]["scopes"], user_agent="daryl.testing", access_token=config["googleauth"]["access_token"], refresh_token=config["googleauth"]["refresh_token"], ) print("Go to this URL:", token.generate_authorize_url()) code = raw_input("What is the verification code? (auth_token)").strip() token.get_access_token(code) print(f"refresh_token is {token.refresh_token}") config["googleauth"]["refresh_token"] = token.refresh_token util.save_config(config) print("Saved configuration to file...")
def drive_changelog(regime, yesterday, html): """ Do something """ drive = util.get_driveclient(CONFIG, regime) folders = util.get_folders(drive) start_change_id = CONFIG[regime]["changestamp"] html += """<p><table border="1" cellpadding="3" cellspacing="0"> <thead> <tr><th>Folder</th><th>Resource</th></tr> </thead> <tbody>""" largestChangeId = -1 hits = 0 page_token = None changestamp = None param = {"includeDeleted": False, "maxResults": 1000} while True: if start_change_id: param["startChangeId"] = start_change_id if page_token: param["pageToken"] = page_token LOG.debug( "[%s] start_change_id: %s largestChangeId: %s page_token: %s", regime, start_change_id, largestChangeId, page_token, ) response = drive.changes().list(**param).execute() largestChangeId = response["largestChangeId"] page_token = response.get("nextPageToken") for item in response["items"]: if item["file"]["mimeType"] in [FMIME, FORM_MTYPE, SITES_MYTPE]: continue changestamp = item["id"] if item["deleted"]: continue # Files copied in could have a createdDate of interest, but old # modification date created = datetime.datetime.strptime( item["file"]["createdDate"][:19], "%Y-%m-%dT%H:%M:%S" ).replace(tzinfo=datetime.timezone.utc) # don't do more work when this file actually did not change modifiedDate = datetime.datetime.strptime( item["file"]["modifiedDate"][:19], "%Y-%m-%dT%H:%M:%S" ).replace(tzinfo=datetime.timezone.utc) if modifiedDate < yesterday and created < yesterday: continue # Need to see which base folder this file is in! isproject = False for parent in item["file"]["parents"]: if parent["id"] not in folders: LOG.info( "[%s] file: %s has unknown parent: %s", regime, item["id"], parent["id"], ) continue isproject = True if not isproject: LOG.info( "[%s] %s (%s) skipped as basefolders are: %s", regime, repr(item["file"]["title"]), item["file"]["mimeType"], item["file"]["parents"], ) continue uri = item["file"]["alternateLink"] title = ( item["file"]["title"].encode("ascii", "ignore").decode("ascii") ) localts = modifiedDate.astimezone(LOCALTZ) hits += 1 pfolder = item["file"]["parents"][0]["id"] html += """ <tr> <td><a href="https://docs.google.com/folderview?id=%s&usp=drivesdk">%s</a></td> <td><a href="%s">%s</a></td></tr> """ % ( pfolder, folders[pfolder]["title"], uri, title, ) hit = False if "version" in item["file"]: lastmsg = "" try: revisions = ( drive.revisions() .list(fileId=item["file"]["id"]) .execute() ) except Exception: LOG.info( "[%s] file %s (%s) failed revisions", regime, title, item["file"]["mimeType"], ) revisions = {"items": []} for item2 in revisions["items"]: md = datetime.datetime.strptime( item2["modifiedDate"][:19], "%Y-%m-%dT%H:%M:%S" ) md = md.replace(tzinfo=pytz.timezone("UTC")) if md < yesterday: continue localts = md.astimezone(LOCALTZ) # for some reason, some revisions have no user associated # with it. So just skip for now # http://stackoverflow.com/questions/1519072 if "lastModifyingUser" not in item2: continue luser = item2["lastModifyingUser"] hit = True display_name = luser["displayName"] email_address = luser.get("emailAddress", "unknown") if display_name == CONFIG["service_account"]: display_name = "daryl's magic" email_address = "*****@*****.**" thismsg = """ <tr><td colspan="2"><img src="%s" style="height:25px;"/> %s by %s (%s)</td></tr> """ % ( ( luser["picture"]["url"] if "picture" in luser else "" ), localts.strftime("%-d %b %-I:%M %p"), display_name, email_address, ) if thismsg != lastmsg: html += thismsg lastmsg = thismsg # Now we check revisions if not hit: luser = item["file"].get("lastModifyingUser", dict()) html += """ <tr><td colspan="2"><img src="%s" style="height:25px;"/> %s by %s (%s)</td></tr> """ % ( luser["picture"]["url"] if "picture" in luser else "", localts.strftime("%-d %b %-I:%M %p"), luser.get("displayName", "n/a"), luser.get("emailAddress", "n/a"), ) if not page_token: break if changestamp is not None: CONFIG[regime]["changestamp"] = changestamp if hits == 0: html += """<tr><td colspan="5">No Changes Found...</td></tr>\n""" html += """</tbody></table>""" util.save_config(CONFIG) return html
"""Script fetches openauth2 refresh token that my offline scripts can use """ from gdata import gauth import pyiem.cscap_utils as util config = util.get_config() token = gauth.OAuth2Token(client_id=config['appauth']['client_id'], client_secret=config['appauth']['app_secret'], scope=config['googleauth']['scopes'], user_agent='daryl.testing', access_token=config['googleauth']['access_token'], refresh_token=config['googleauth']['refresh_token']) print "Go to this URL:", token.generate_authorize_url() code = raw_input('What is the verification code? (auth_token)').strip() token.get_access_token(code) print "refresh_token is", token.refresh_token config['googleauth']['refresh_token'] = token.refresh_token util.save_config(config) print "Saved configuration to file..."