Last active
August 29, 2015 14:01
-
-
Save blaxter/f8fad8fcb06ad513171a to your computer and use it in GitHub Desktop.
puncher for internal Zentyal timetracker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import keyring | |
import re | |
import os | |
import sys | |
import requests | |
import configobj | |
import getpass | |
class bcolors: | |
HEADER = '\033[1m\033[95m' | |
OKBLUE = '\033[94m' | |
OKGREEN = '\033[1m\033[92m' | |
FAIL = '\033[1m\033[91m' | |
ENDC = '\033[0m' | |
class Puncher(object): | |
APP_NAME = 'ttzentyal puncher' | |
BASE_URL = 'https://tt.zentyal.com' | |
ROUTES = {'punch-in': '/timetracking/punch/punch_in', | |
'punch-out': '/timetracking/punch/punch_out', | |
'hq': '/timetracking/hq'} | |
def __init__(self): | |
self._get_username() | |
self._get_password() | |
def _get_username(self): | |
config_file = os.path.join(os.environ['HOME'], '.ttpuncher') | |
try: | |
config_file = configobj.ConfigObj(config_file) | |
self.username = config_file["username"] | |
except: | |
print ("You need to create a %s file and define username key, something like:\n" | |
"username=jconnor" % config_file) | |
sys.exit(1) | |
def _get_password(self): | |
self.password = keyring.get_password(self.APP_NAME, self.username) | |
if not self.password: | |
self.password = getpass.getpass(prompt=("Password for %s: " % self.username)) | |
keyring.set_password(self.APP_NAME, self.username, self.password) | |
def punch_in(self): | |
body = self._get('punch-in') | |
return self.ROUTES['punch-out'] in body | |
def punch_out(self): | |
body = self._get('punch-out') | |
return self.ROUTES['punch-in'] in body | |
def people_working(self): | |
body = self._get('hq') | |
name_re = re.compile(r"<td>([^0-9]*)</td>") | |
ret = [] | |
for line in body.split("\n"): | |
name = name_re.search(line) | |
if name: | |
ret.append(name.group(1)) | |
return ret | |
def _get(self, route): | |
return requests.get(self.BASE_URL + self.ROUTES[route], | |
auth=(self.username, self.password)).text | |
def usage(): | |
print "Use %s in|out" % sys.argv[0] | |
if __name__ == '__main__': | |
p = Puncher() | |
if len(sys.argv) != 2: | |
usage() | |
elif sys.argv[1] == 'in': | |
if not p.punch_in(): | |
print bcolors.FAIL + "I couldn't punch in :-/" + bcolors.ENDC | |
else: | |
print bcolors.HEADER + "People working" | |
print "==============" + bcolors.ENDC | |
for person in p.people_working(): | |
print bcolors.OKBLUE + person | |
print bcolors.ENDC | |
print bcolors.OKGREEN + "Punch in done!" + bcolors.ENDC | |
elif sys.argv[1] == 'out': | |
if not p.punch_out(): | |
print bcolors.FAIL + "I couldn't punch out :-/" + bcolors.ENDC | |
else: | |
print bcolors.OKGREEN + "Punch out done!" + bcolors.ENDC | |
else: | |
usage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
moved to repo https://github.com/blaxter/puncher