Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #!/usr/local/bin/python
  2.  
  3. import sys
  4. import ast
  5. import inspect
  6. import keyring # for storing password in OS X Keychain
  7. from datetime import datetime
  8. from subprocess import check_output
  9. from astunparse import unparse
  10. from grab import Grab
  11. from getpass import getpass
  12. CLOCK_IN = True
  13. AST_INDEX = (inspect.currentframe().f_lineno - 4) # index of tree node holding CLOCK_IN Assign
  14. USERNAME = '1450398'
  15. SERVICE_NAME = 'ct' # as it will appear on OS X Keychain
  16. PT_IN = 'timeIn'
  17. PT_OUT = 'timeOut'
  18.  
  19. def pt_to_str(pt):
  20. # "timeIn" -> "In"
  21. return pt.lstrip('time')
  22.  
  23. def get_and_set_password():
  24. password = getpass('password: ')
  25. keyring.set_password(SERVICE_NAME, USERNAME, password)
  26. return password
  27.  
  28. if "-p" in sys.argv: # when password reset time comes around
  29. password = get_and_set_password()
  30. else:
  31. password = keyring.get_password(SERVICE_NAME, USERNAME) or get_and_set_password()
  32. # manual clock in/out or automatic (based on CLOCK_IN value)
  33. if "in" in sys.argv:
  34. punch_type = PT_IN
  35. elif "out" in sys.argv:
  36. punch_type = PT_OUT
  37. else:
  38. punch_type = PT_IN if CLOCK_IN else PT_OUT
  39. g = Grab()
  40. g.setup(follow_location=True) # this might not be necessary
  41. try:
  42. # login
  43. g.go("CITY TIME URL")
  44. g.doc.set_input('username', USERNAME)
  45. g.doc.set_input('password', password)
  46. g.doc.set_input('startPage', 'webClock')
  47. g.doc.submit() # does not take you to webClock, ahhh city time...
  48. # clock in/out
  49. g.go("WEB CLOCK URL")
  50. g.doc.set_input('punchType', punch_type)
  51. g.doc.submit()
  52. except Exception as e:
  53. print("Failed to clock {}: {}".format(pt_to_str(punch_type), e))
  54. else:
  55. print("Clocked {} at {}".format(pt_to_str(punch_type), datetime.now()))
  56. # parse this file into an abstract syntax tree
  57. with open(__file__) as fp:
  58. tree = ast.parse(fp.read())
  59. # set CLOCK_IN value based on punch type
  60. tree.body[AST_INDEX].value.id = str(punch_type != PT_IN)
  61. # 'update' this file with the new value
  62. with open(__file__, 'w') as fp:
  63. # comments (including shebang) are not part of the syntax tree and will not be preserved
  64. fp.write(('#!' + str(check_output(['which', 'python']))))
  65. fp.write(unparse(tree))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement