Skip to content

Commit

Permalink
Fix or suppress pylint errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Meredith committed Aug 24, 2018
1 parent a073802 commit 960cb90
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 185 deletions.
8 changes: 6 additions & 2 deletions ews_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
""" Process CLI args and invoke main program """

#!/usr/bin/python3

import sys
import argparse
from . import ews

def main():
""" Parse cli args and invoke main program """
parser = argparse.ArgumentParser(description=('Outlook Web Access CLI Client'))
parser.add_argument('-d', '--daemon', required=False, action='store_true', help='Run non-interactive')
parser.add_argument('-d', '--daemon',
required=False, action='store_true', help='Run non-interactive')

args = parser.parse_args()

ews.exchange_web_access(args)
ews.ExchangeWebAccess(args)

if __name__ == "__main__":
sys.exit(main())
155 changes: 83 additions & 72 deletions ews_cli/ews.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
""" Main Program """

import os
import sys, traceback
import argparse
import getpass
import sys
import traceback
from configparser import SafeConfigParser
from collections import defaultdict
import keyring
from datetime import datetime, timedelta
import time
import pytz
from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, ServiceAccount, \
EWSDateTime, EWSTimeZone, Configuration, NTLM, GSSAPI, CalendarItem, Message, \
Mailbox, Attendee, Q, ExtendedProperty, FileAttachment, ItemAttachment, \
HTMLBody, Build, Version, FolderCollection, Folder
import keyring
from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, ServiceAccount, EWSDateTime # pylint: disable=W0611
from exchangelib import EWSTimeZone, Configuration, NTLM, GSSAPI, CalendarItem, Message, Mailbox # pylint: disable=W0611
from exchangelib import Attendee, Q, ExtendedProperty, FileAttachment, ItemAttachment, HTMLBody # pylint: disable=W0611
from exchangelib import Build, Version, FolderCollection, Folder # pylint: disable=W0611

from . import gui, util, msg, rules

# exchangelib usage and info: GitHub repo.
# https://github.com/ecederstrand/exchangelib

keyring_service_name = 'outlook_web_access'
KEYRING_SERVICE_NAME = 'outlook_web_access'

class exchange_web_access:
class ExchangeWebAccess:
""" Main Application Class """

def __init__(self, program_args):
self.unread = 0
Expand Down Expand Up @@ -51,7 +51,7 @@ def __init__(self, program_args):
print("Sleeping for 5 minutes")
for i in range(1, 300, 1):
sec = timedelta(seconds=int(i))
d = datetime(1, 1, 1) + sec
d = datetime(1, 1, 1) + sec # pylint: disable=C0103
print("%d:%02d" % (d.minute, d.second), end='\r', flush=True)
time.sleep(1)
print('', end='\n')
Expand All @@ -61,8 +61,8 @@ def __init__(self, program_args):
try:
self.connect()
gui.menu_main(self)
except Exception as ex:
if self.account == None:
except Exception as ex: # pylint: disable=W0703
if self.account is None:
print(ex)
util.pause()
gui.menu_account(self)
Expand All @@ -72,110 +72,120 @@ def __init__(self, program_args):
sys.exit(1)

def connect(self):
""" Establish connection to EWS Server """

print("Connecting to EWS...")

# Specify your credentials. Username is usually in WINDOMAIN\username format, where WINDOMAIN is
# the name of the Windows Domain your username is connected to, but some servers also
# accept usernames in PrimarySMTPAddress ('myusername@example.com') format (Office365 requires it).
# UPN format is also supported, if your server expects that.
self.credentials = Credentials(username='{0}\\{1}'.format(self.domain,self.user), password=self.password)

# If the server doesn't support autodiscover, or you want to avoid the overhead of autodiscover,
# use a Configuration object to set the server location instead:
self.config = Configuration(server=self.server, credentials=self.credentials, auth_type=NTLM)
# Specify your credentials. Username is usually in WINDOMAIN\username format,
# where WINDOMAIN is the name of the Windows Domain your username is connected to, but
# some servers also accept usernames in PrimarySMTPAddress ('myusername@example.com')
# format (Office365 requires it). UPN format is also supported, if your server expects that.
self.credentials = Credentials(username='{0}\\{1}'.format(self.domain, self.user),
password=self.password)

# If the server doesn't support autodiscover, or you want to avoid the overhead of
# autodiscover, use a Configuration object to set the server location instead:
self.config = Configuration(server=self.server,
credentials=self.credentials, auth_type=NTLM)
self.account = Account(primary_smtp_address=self.email, config=self.config,
autodiscover=False, access_type=DELEGATE)
autodiscover=False, access_type=DELEGATE)

# If you're connecting to the same account very often, you can cache the autodiscover result for
# later so you can skip the autodiscover lookup:
# If you're connecting to the same account very often, you can cache the autodiscover
# result for later so you can skip the autodiscover lookup:
self.ews_url = self.account.protocol.service_endpoint
self.ews_auth_type = self.account.protocol.auth_type
self.primary_smtp_address = self.account.primary_smtp_address

# You can also get the local timezone defined in your operating system
self.tz = EWSTimeZone.localzone()
self.tz = EWSTimeZone.localzone() # pylint: disable=C0103

def get_unread_count(self):
""" Get unread email count. """

if self.account == None:
if self.account is None:
self.connect()

self.account.inbox.total_count
self.account.inbox.child_folder_count
self.account.inbox.unread_count
self.account.inbox.total_count # pylint: disable=E1101,W0104
self.account.inbox.child_folder_count # pylint: disable=E1101,W0104
self.account.inbox.unread_count # pylint: disable=E1101,W0104
# Update the counters
self.account.inbox.refresh()
# The folder structure is cached after first access.
self.account.inbox.refresh() # pylint: disable=E1101
# The folder structure is cached after first access.
# To clear the cache, refresh the root folder
# account.root.refresh()

self.unread = self.account.inbox.unread_count
self.unread = self.account.inbox.unread_count # pylint: disable=E1101

def show_unread_count(self):

""" Display Unread email count. """
self.get_unread_count()
print(util.get_entry("Unread Messages: {0}",self.unread))
print(util.get_entry("Unread Messages: {0}", self.unread))
util.pause()

def show_tree(self):
if self.account == None:
""" Show Email Folders """
if self.account is None:
self.connect()

print(self.account.inbox.tree())
print(self.account.inbox.tree()) # pylint: disable=E1101
util.pause()

def filter_mail(self):
if self.account == None:
""" Filter email """
if self.account is None:
self.connect()

self.account.inbox.refresh()
total = self.account.inbox.total_count
self.account.inbox.refresh() # pylint: disable=E1101
total = self.account.inbox.total_count # pylint: disable=E1101
idx = 1
if total > 0:
print("Getting mail in INBOX...", flush=True)
inbox_items = self.account.inbox.all()
inbox_items = self.account.inbox.all() # pylint: disable=E1101
status = "Process msg {0} of {1}"
for item in inbox_items:
print(status.format(idx, total), end='\r', flush=True)
folder = self.filters.process_msg(item)
if folder != '':
f = util.get_folder(self.account.inbox, folder)
if f != None:
item.move(to_folder=f)
f_inst = util.get_folder(self.account.inbox, folder)
if f_inst is not None:
item.move(to_folder=f_inst)
else:
print('\n')
print(util.get_entry('Error moving msg into folder: {0}', folder), flush=True)
print(util.get_entry('Error moving msg into folder: {0}', folder),
flush=True)
idx = idx + 1
print('\n')

def show_mail(self):
if self.account == None:
""" Display Message Headers for each email """
if self.account is None:
self.connect()

total = self.account.inbox.total_count
total = self.account.inbox.total_count # pylint: disable=E1101
idx = 1
if total > 0:
print("Getting mail in INBOX...", flush=True)
status = "Displaying msg {0} of {1}"
for item in self.account.inbox.all():
print(status.format(idx,total), flush=True)
m = msg.header(item)
self.print_header(m)
for item in self.account.inbox.all(): # pylint: disable=E1101
print(status.format(idx, total), flush=True)
hdr = msg.Header(item)
self.print_header(hdr)
util.pause()
idx = idx + 1
os.system("clear")

def print_header(self, msg):
def print_header(self, headers):
""" Actually print email headers """

print(util.get_entry("sender: {0}", msg.sender))
print(util.get_entry("from: {0}", msg.author))
print(util.get_entry("to: {0}", msg.to_email))
print(util.get_entry("cc: {0}", msg.cc_email))
print(util.get_entry("reply-to: {0}", msg.reply_to))
print(util.get_entry("subject: {0}", msg.subject))
print(util.get_entry("sender: {0}", headers.sender))
print(util.get_entry("from: {0}", headers.author))
print(util.get_entry("to: {0}", headers.to_email))
print(util.get_entry("cc: {0}", headers.cc_email))
print(util.get_entry("reply-to: {0}", headers.reply_to))
print(util.get_entry("subject: {0}", headers.subject))

def config_load(self):
""" load config from ini file """

config = SafeConfigParser()

Expand All @@ -187,13 +197,13 @@ def config_load(self):
if not config.has_section('setup'):
config.add_section('setup')

self.server = config.get('setup','server')
self.domain = config.get('setup','domain')
self.email = config.get('setup','email')
self.user = config.get('setup','user')
self.server = config.get('setup', 'server')
self.domain = config.get('setup', 'domain')
self.email = config.get('setup', 'email')
self.user = config.get('setup', 'user')

keyring.get_keyring()
self.password = keyring.get_password(keyring_service_name, 'password')
self.password = keyring.get_password(KEYRING_SERVICE_NAME, 'password')

if self.password != '':
self.has_password = True
Expand All @@ -203,6 +213,7 @@ def config_load(self):
return 1

def config_save(self):
""" Save config to ini file """

config = SafeConfigParser()

Expand All @@ -213,15 +224,15 @@ def config_save(self):
if not config.has_section('setup'):
config.add_section('setup')

config.set('setup','server',self.server)
config.set('setup','domain',self.domain)
config.set('setup','email',self.email)
config.set('setup','user',self.user)
config.set('setup', 'server', self.server)
config.set('setup', 'domain', self.domain)
config.set('setup', 'email', self.email)
config.set('setup', 'user', self.user)

# Obtain keyring from OS
keyring.get_keyring()

keyring.set_password(keyring_service_name, 'password', self.password)
keyring.set_password(KEYRING_SERVICE_NAME, 'password', self.password)

with open(cfg, 'w') as f:
config.write(f)
with open(cfg, 'w') as cfg_file:
config.write(cfg_file)
Loading

0 comments on commit 960cb90

Please sign in to comment.