1 # python program to automate the process of
2 # checking in and checking out of my timesheet
3 # Future features may include reports
4 # or even replacing this script with a
8 from xml
.etree
import ElementTree
10 from elementtree
import ElementTree
14 import gdata
.spreadsheet
15 import gdata
.spreadsheet
.service
20 import googledocs_timesheet_conf
as c
22 class GoogleDocsTimesheet():
25 self
.gd_client
= self
._login
();
26 self
.wbid
= self
._get
_workbook
()
27 self
.wsid
= self
._get
_worksheet
()
28 self
.cur_date
= self
._get
_current
_date
()
29 self
.cur_time
= self
._get
_current
_time
()
31 def _spreadsheet_query(self
, sq
):
33 not currently returning as I expect
34 so I'm currently not using it
36 q
= gdata
.spreadsheet
.service
.ListQuery()
39 feed
= self
.gd_client
.GetListFeed(self
.wbid
, self
.wsid
, query
=q
)
42 def _PrintFeed(self
, feed
):
43 for i
, entry
in enumerate(feed
.entry
):
44 if isinstance(feed
, gdata
.spreadsheet
.SpreadsheetsCellsFeed
):
45 print '%s %s\n' % (entry
.title
.text
, entry
.content
.text
)
46 elif isinstance(feed
, gdata
.spreadsheet
.SpreadsheetsListFeed
):
47 print '%s %s %s\n' % (i
, entry
.title
.text
, entry
.content
.text
)
49 print '%s %s\n' % (i
, entry
.title
.text
)
51 def _get_workbook(self
):
53 This can be enhanced to query directly for the
54 spreadsheet we need by passing in the query parameter
55 to the GetSpreadsheetsFeed call, but it is
56 currently not necessary as the number of spreadsheets
59 feed
= self
.gd_client
.GetSpreadsheetsFeed()
61 if wb
.title
.text
== c
.spreadsheet_name
:
62 timesheetid
= wb
.id.text
.rsplit('/', 1)[1]
66 def _get_worksheet(self
):
67 feed
= self
.gd_client
.GetWorksheetsFeed(self
.wbid
)
68 worksheetid
= feed
.entry
[string
.atoi('0')].id.text
.rsplit('/', 1)[1]
72 gd_client
= gdata
.spreadsheet
.service
.SpreadsheetsService()
73 gd_client
.email
= c
.login
74 gd_client
.password
= c
.password
75 gd_client
.source
= c
.source
77 gd_client
.ProgrammaticLogin()
78 except socket
.sslerror
:
79 pass # this is a known error, but not a problem
80 # see http://mail.python.org/pipermail/python-list/2005-August/338280.html
83 def _make_google_date(self
, datestr
):
85 mm/dd/yyyy to m/d/yyyy
87 month
, day
, year
= datestr
.split("/")
92 return "/".join([month
, day
, year
])
94 def _get_current_date(self
):
95 d
= time
.strftime("%m/%d/%Y")
96 gdate
= self
._make
_google
_date
(d
)
99 def _get_current_time(self
):
100 return time
.strftime("%H:%M:%S")
102 def _timestr_to_timedelta(self
, timestr
):
103 hours
, minutes
, seconds
= [int(item
) for item
in timestr
.split(":")]
104 rdt
= datetime
.timedelta(hours
=hours
, minutes
=minutes
, seconds
=seconds
)
107 def _calc_hours_worked_today(self
):
108 hwt
= datetime
.timedelta()
109 for row
in self
.feed
.entry
:
110 clockin
= row
.custom
['clockin'].text
111 clockout
= row
.custom
['clockout'].text
113 clockout
= self
._get
_current
_time
()
114 clockin
= self
._timestr
_to
_timedelta
(clockin
)
115 clockout
= self
._timestr
_to
_timedelta
(clockout
)
116 hwt
+= clockout
- clockin
119 def _print_hours_worked_today(self
):
120 hwt
= self
._calc
_hours
_worked
_today
()
121 seconds
= hwt
.seconds
124 minutes_left_over
= minutes
%60
125 print "%s hours and %s minutes worked today." % (hours
, minutes_left_over
)
127 def clock_in(self
, punch_time
):
131 print "Clocking in..."
134 row
['date'] = self
.cur_date
135 row
['clockin'] = punch_time
136 result
= self
.gd_client
.InsertRow(row
, self
.wbid
, self
.wsid
)
137 if isinstance(result
, gdata
.spreadsheet
.SpreadsheetsList
):
138 print "Clocked in at %s %s" % (self
.cur_date
, punch_time
)
139 self
._print
_hours
_worked
_today
()
141 print "Problem clocking in"
143 def clock_out(self
, punch_time
):
147 print "Clocking out..."
149 row
['date'] = self
.feed
.entry
[0].custom
['date'].text
150 row
['clockin'] = self
.feed
.entry
[0].custom
['clockin'].text
151 row
['clockout'] = punch_time
152 result
= self
.gd_client
.UpdateRow(self
.feed
.entry
[0], row
)
153 if isinstance(result
, gdata
.spreadsheet
.SpreadsheetsList
):
154 print "Clocked out at %s %s" % (self
.cur_date
, punch_time
)
155 self
._print
_hours
_worked
_today
()
157 print "Problem clocking out"
159 def _is_clocked_in(self
):
160 "returns true if clocked in"
161 query
= 'date=%s' % self
.cur_date
162 self
.feed
= self
._spreadsheet
_query
(query
)
164 for row
in self
.feed
.entry
:
165 if row
.custom
['clockout'].text
is None:
170 def toggle_login(self
, punch_time
=False):
172 punch_time
= self
.cur_time
173 if (self
._is
_clocked
_in
()):
174 self
.clock_out(punch_time
)
176 self
.clock_in(punch_time
)
178 def main(self
, args
):
179 if len(args
) > 1 and args
[1] == "hours":
180 if self
._is
_clocked
_in
():
184 self
._print
_hours
_worked
_today
()
185 elif len(args
) > 1 and args
[1].find(":") != -1: # a time
186 self
.toggle_login(args
[1])
189 # stop the windows command prompt from going away immediately
192 if __name__
== "__main__":
193 a
= GoogleDocsTimesheet()