Skip to content

Commit

Permalink
implemented basic logging in issue #59
Browse files Browse the repository at this point in the history
  • Loading branch information
jovanbulck committed Feb 13, 2015
1 parent 980af26 commit 1994507
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
9 changes: 6 additions & 3 deletions communicator/coloramac.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#from ..tools import cursor ## Om cursor te verbergen/tonen
from tools import cursor ## Om cursor te verbergen/tonen

import logging
logger = logging.getLogger(__name__)

## Gijs:
## We hoeven geen relatieve import te gebruiken omdat de map waarin
## kotnetcli.py zich bevindt de rootmap ($PYTHONPATH) is. De map 'tools'
Expand All @@ -36,15 +39,15 @@


try:
print "Probeert Colorama te importeren...",
logging.debug("Probeert Colorama te importeren..."),
from colorama import ( ## Voor gekleurde tekst.
Fore,
Style,
init as colorama_init
)
print "OK"
logging.debug("OK")
except ImportError:
print "Couldn't import the colorama library."
logging.error("Couldn't import the colorama library.")
pass

class SuperColoramaCommunicator(SuperPlaintextCommunicator):
Expand Down
2 changes: 1 addition & 1 deletion communicator/fabriek.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def createPlaintextCommunicator(self):
return LoginPlaintextCommunicator()

def createColoramaCommunicator(self):
from coloramac import LoginColoramaCommunicator
from coloramac import LoginColoramaCommunicator
return LoginColoramaCommunicator()

def createSummaryCommunicator(self):
Expand Down
26 changes: 16 additions & 10 deletions kotnetcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
from credentials import KeyRingCredentials, ForgetCredsException ## Opvragen van nummer en wachtwoord
from worker import LoginWorker, LogoutWorker #, ForceerLoginWorker

import logging
logger = logging.getLogger(__name__)

version = "1.3.0-dev"

## An argument parse action that prints license information
Expand Down Expand Up @@ -76,6 +79,8 @@ def voegArgumentenToe(self):
## general flags
self.parser.add_argument("-v", "--version", action="version", version=version)
self.parser.add_argument("-l", "--license", action=PrintLicenceAction, nargs=0)
self.parser.add_argument("-d", "--debug", help="show debug messages", \
action="store_const", const="debug")

## login type flags
self.workergroep.add_argument("-i", "--login",\
Expand Down Expand Up @@ -153,6 +158,9 @@ def voegArgumentenToe(self):
## Parses the arguments corresponding to self.parser
def parseArgumenten(self):
argumenten = self.parser.parse_args()
## 0. general flags
if (argumenten.debug):
logging.basicConfig(level=logging.DEBUG)
## 1. credential-related flags
creds = self.parseCredentialFlags(argumenten)
## 2. login-type flags
Expand All @@ -164,7 +172,7 @@ def parseArgumenten(self):

## returns newly created credentials obj
def parseCredentialFlags(self, argumenten):
print "ik haal de credentials uit de keyring"
logger.info("ik haal de credentials uit de keyring")
return self.parseCredsFlags(argumenten, KeyRingCredentials())

## a helper method with a default credentials object argument
Expand All @@ -176,7 +184,7 @@ def parseCredsFlags(self, argumenten, cr):
return cr

elif argumenten.credentials == "forget":
print "ik wil vergeten"
logger.info("ik wil vergeten")
try:
cr.forgetCreds()
print "You have succesfully removed your kotnetcli credentials."
Expand All @@ -186,7 +194,7 @@ def parseCredsFlags(self, argumenten, cr):
sys.exit(1)

elif argumenten.credentials == "guest_mode":
print "ik wil me anders voordoen dan ik ben"
logger.info("ik wil me anders voordoen dan ik ben")
(gebruikersnaam, wachtwoord) = self.prompt_user_creds()
cr.saveGuestCreds(gebruikersnaam, wachtwoord)
return cr
Expand All @@ -203,12 +211,12 @@ def prompt_user_creds(self):
## returns tuple (worker, fabriek)
def parseActionFlags(self, argumenten):
if argumenten.worker == "login":
print "ik wil inloggen"
logger.info("ik wil inloggen")
worker = LoginWorker()
fabriek = LoginCommunicatorFabriek()

elif argumenten.worker == "logout":
print "ik wil uitloggen"
logger.info("ik wil uitloggen")
worker = LogoutWorker()
fabriek = LogoutCommunicatorFabriek()

Expand All @@ -227,15 +235,15 @@ def parseCommunicatorFlags(self, fabriek, argumenten):
# return fabriek.createQuietCommunicator()

if argumenten.communicator == "plaintext":
print "ik wil terug naar de basis"
logger.info("ik wil terug naar de basis")
return fabriek.createPlaintextCommunicator()

elif argumenten.communicator == "colortext":
print "ik wil vrolijke kleuren"
logger.info("ik wil vrolijke kleuren")
return fabriek.createColoramaCommunicator()

else:
print "ik ga mee met de stroom" # TODO kunnen we default niet specifieren mbv argparse module??
logger.info("ik ga mee met de stroom") # TODO kunnen we default niet specifieren mbv argparse module??
return fabriek.createColoramaCommunicator()

'''
Expand Down Expand Up @@ -301,7 +309,5 @@ def parseCommunicatorFlags(self, fabriek, argumenten):
## Start de zaak asa deze file rechtstreeks aangeroepen is vanuit
## command line (i.e. niet is geimporteerd vanuit een andere file)
if __name__ =='__main__':
print "== kotnetcli started =="
k = KotnetCLI()
k.parseArgumenten()
print "== kotnetcli done =="
13 changes: 8 additions & 5 deletions kotnetcli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
from communicator.fabriek import LoginCommunicatorFabriek, LogoutCommunicatorFabriek ## Voor output op maat
from credentials import DummyCredentials ## Opvragen van nummer en wachtwoord

import logging
logger = logging.getLogger(__name__)

class RunTestsAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
print "not yet implemented"
Expand All @@ -35,6 +38,8 @@ class KotnetCLITester(KotnetCLI):
def __init__(self):
super(KotnetCLITester, self).__init__("[DUMMY] script \
om in- of uit te loggen op KotNet")
## debug output on by default
logging.basicConfig(level=logging.DEBUG)

def voegArgumentenToe(self):
super(KotnetCLITester, self).voegArgumentenToe()
Expand All @@ -45,25 +50,23 @@ def voegArgumentenToe(self):
## override with dummy behavior
def parseActionFlags(self, argumenten):
if argumenten.worker == "login":
print "ik wil inloggen voor spek en bonen"
logger.info("ik wil inloggen voor spek en bonen")
worker = DummyLoginWorker()
fabriek = LoginCommunicatorFabriek()

elif argumenten.worker == "logout":
print "ik wil uitloggen voor spek en bonen"
logger.info("ik wil uitloggen voor spek en bonen")
worker = DummyLogoutWorker()
fabriek = LogoutCommunicatorFabriek()

return (worker, fabriek)

def parseCredentialFlags(self, argumenten):
print "ik wil credentials ophalen voor spek en bonen"
logger.info("ik wil credentials ophalen voor spek en bonen")
return self.parseCredsFlags(argumenten, DummyCredentials())

## Start de zaak asa deze file rechtstreeks aangeroepen is vanuit
## command line (i.e. niet is geimporteerd vanuit een andere file)
if __name__ =='__main__':
print "== kotnetcli_dev started =="
k = KotnetCLITester()
k.parseArgumenten()
print "== kotnetcli_dev done =="
9 changes: 7 additions & 2 deletions worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#from tools import pinger ## Om te checken of we op Kotnet zitten
#from tools import errorcodes as error ## Om magic number errors te voorkomen

import logging
logger = logging.getLogger(__name__)

EXIT_FAILURE = 1 ## Tijdelijke exitcode, moet nog worden geïmplementeerd.
EXIT_SUCCESS = 0

Expand All @@ -40,15 +43,16 @@ def check_kotnet(self, co):
if (self.browser.bevestig_kotnetverbinding()):
co.eventKotnetVerbindingSuccess()
else:
#print "Connection attempt to netlogin.kuleuven.be timed out. Are you on the kotnet network?"
co.eventKotnetVerbindingFailure()
co.beeindig_sessie(EXIT_FAILURE)
logger.error("Connection attempt to netlogin.kuleuven.be timed out. Are you on the kotnet network?")
sys.exit(EXIT_FAILURE)

## A worker class that either succesfully logs you in to kotnet
## or exits with failure, reporting events to the given communicator
class LoginWorker(SuperWorker):
def go(self, co, creds):
logger.info("enter LoginWorker.go()")
self.check_kotnet(co)
self.netlogin(co)
self.kies_kuleuven(co)
Expand Down Expand Up @@ -100,12 +104,13 @@ def login_resultaten(self, co):
tup = self.browser.login_parse_results()
## check whether it worked out
if len(tup) != 2:
#print "resultaten tuple len != 2"
co.beeindig_sessie(EXIT_FAILURE)
sys.exit(EXIT_FAILURE)
logger.error("resultaten tuple len != 2")
else:
co.eventLoginGeslaagd(tup[0], tup[1])
co.beeindig_sessie()
logger.info("LoginWorker: exiting with success")
sys.exit(EXIT_SUCCESS)

class DummyLoginWorker(LoginWorker):
Expand Down

0 comments on commit 1994507

Please sign in to comment.