Commit 339038c6 authored by Gaute Hope's avatar Gaute Hope

list labels

ign secret
parent 45047cd9
......@@ -6,25 +6,48 @@
import os, sys
import argparse
from oauth2client import tools
from .remote import *
from .local import *
class Gmailieer:
def __init__ (self):
def main (self):
parser = argparse.ArgumentParser ('Gmailieer')
parser = argparse.ArgumentParser ('Gmailieer', parents = [tools.argparser])
self.parser = parser
parser.add_argument ('action', choices = ['fetch', 'push-tags'],
help = 'fetch: get new e-mail and remote tag-changes, push-tags: push local tag-changes')
parser.add_argument ('-d', '--dry-run', action='store_true', default = False,
parser.add_argument ('-c', '--credentials', type = str, default = 'client_secret.json',
help = 'credentials file for google api (default: client_secret.json)')
parser.add_argument ('-d', '--dry-run', action='store_true', default = False,
help = 'do not make any changes')
parser.add_argument ('-l', '--list-labels', action='store_true', default = False,
help = 'list all labels')
args = parser.parse_args (sys.argv[1:])
self.args = args
self.action = args.action
self.dry_run = args.dry_run
self.credentials_file = args.credentials
self.list_labels = args.list_labels
print ("action: ", self.action)
print ("dry-run: ", self.dry_run)
if self.dry_run:
print ("dry-run: ", self.dry_run)
if self.action == 'fetch':
r = Remote (self)
if self.list_labels:
print ("labels:")
print ("-------")
for l in r.get_labels ():
print (l)
import os
import httplib2
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
class Remote:
authorized = False
def __init__ (self, g):
self.gmailieer = g
self.CLIENT_SECRET_FILE = g.credentials_file
def require_auth (func):
def func_wrap (self, *args, **kwargs):
if not self.authorized:
self.authorize ()
return func (self, *args, **kwargs)
return func_wrap
def get_labels (self):
results = self.service.users ().labels ().list (userId = 'me').execute ()
labels = results.get ('labels', [])
return [l['name'] for l in labels]
def authorize (self):
self.credentials = self.get_credentials ()
http = self.credentials.authorize (httplib2.Http())
self.service = ('gmail', 'v1', http = http)
authorized = True
def get_credentials (self):
Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Credentials, the obtained credential.
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
credential_path = os.path.join(credential_dir,
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(self.CLIENT_SECRET_FILE, self.SCOPES)
flow.user_agent = self.APPLICATION_NAME
credentials = tools.run_flow(flow, store, flags = self.gmailieer.args)
print('Storing credentials to ' + credential_path)
return credentials
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment