~ktenney/+junk/docubi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet ekr_test?>
<leo_file>
<leo_header file_format="2" tnodes="0" max_tnode_index="0" clone_windows="0"/>
<globals body_outline_ratio="0.5">
	<global_window_position top="48" left="10" height="600" width="800"/>
	<global_log_window_position top="0" left="0" height="0" width="0"/>
</globals>
<preferences/>
<find_panel_settings/>
<vnodes>
<v t="ktenney.20090429100000.369" str_leo_pos="1,0"><vh>@chapters</vh></v>
<v t="ktenney.20090429100000.371" a="E"><vh>Explanans</vh>
<v t="ktenney.20090430085348.2" a="E"><vh>@rst ~/work/explanans/buildout/README.rst</vh>
<v t="ktenney.20090429100000.372" a="E"><vh>:explanans: Buildout</vh>
<v t="ktenney.20090429100000.373"><vh>Create a virtualenv</vh></v>
<v t="ktenney.20090430085348.3"><vh>NewHeadline</vh></v>
</v>
</v>
<v t="ktenney.20090429100000.374"><vh>@auto explanans/__init__.py</vh></v>
<v t="ktenney.20090429100000.375"><vh>@auto explanans/explanans.py</vh></v>
<v t="ktenney.20090429100000.376"><vh>rescuetime</vh>
<v t="ktenney.20090429100000.377"><vh>@auto /home/ktenney/src/rescuetime-linux-uploader/RescueTimeUploader/extensions/Firefox/extended_info</vh></v>
<v t="ktenney.20090429100000.378"><vh>@auto /home/ktenney/src/rescuetime-linux-uploader/RescueTimeUploader/uploader.py</vh>
<v t="ktenney.20090429100000.379"><vh>uploader declarations</vh></v>
<v t="ktenney.20090429100000.380"><vh>class XScreenSaverInfo</vh></v>
<v t="ktenney.20090429100000.381"><vh>escape_xml</vh></v>
<v t="ktenney.20090429100000.382"><vh>escape_less</vh></v>
<v t="ktenney.20090429100000.383"><vh>class time_obj</vh>
<v t="ktenney.20090429100000.384"><vh>__init__</vh></v>
<v t="ktenney.20090429100000.385"><vh>__str__</vh></v>
<v t="ktenney.20090429100000.386"><vh>__repr__</vh></v>
<v t="ktenney.20090429100000.387"><vh>get_duration</vh></v>
<v t="ktenney.20090429100000.388"><vh>get_extended_info</vh></v>
<v t="ktenney.20090429100000.389"><vh>_get_idle_time</vh></v>
<v t="ktenney.20090429100000.390"><vh>_get_raw_active_application</vh></v>
<v t="ktenney.20090429100000.391"><vh>get_active_application</vh></v>
<v t="ktenney.20090429100000.392"><vh>still_active</vh></v>
<v t="ktenney.20090429100000.393"><vh>close</vh></v>
<v t="ktenney.20090429100000.394"><vh>_get_raw_window_title</vh></v>
<v t="ktenney.20090429100000.395"><vh>get_active_window_title</vh></v>
<v t="ktenney.20090429100000.396"><vh>_decode_window_title</vh></v>
<v t="ktenney.20090429100000.397"><vh>print_out</vh></v>
</v>
<v t="ktenney.20090429100000.398"><vh>handle_signal</vh></v>
<v t="ktenney.20090429100000.399"><vh>class Uploader</vh>
<v t="ktenney.20090429100000.400"><vh>__init__</vh></v>
<v t="ktenney.20090429100000.401"><vh>get_latest_time_obj</vh></v>
<v t="ktenney.20090429100000.402"><vh>retrieve_credentials</vh></v>
<v t="ktenney.20090429100000.403"><vh>shutdown</vh></v>
<v t="ktenney.20090429100000.404"><vh>main</vh></v>
<v t="ktenney.20090429100000.405"><vh>test_connection</vh></v>
<v t="ktenney.20090429100000.406"><vh>test_login</vh></v>
<v t="ktenney.20090429100000.407"><vh>upload_data</vh></v>
<v t="ktenney.20090429100000.408"><vh>prepare_data</vh></v>
</v>
</v>
</v>
<v t="ktenney.20090429100000.409"><vh>import analysis - waveform signatures</vh></v>
<v t="ktenney.20090429100000.410"><vh>@auto explanans/README.rst</vh></v>
<v t="ktenney.20090429100000.411"><vh>pydoctor</vh></v>
</v>
</vnodes>
<tnodes>
<t tx="ktenney.20090429100000.369"></t>
<t tx="ktenney.20090429100000.371">
    Topics specifically related to generating explanations.</t>
<t tx="ktenney.20090429100000.372"></t>
<t tx="ktenney.20090429100000.373">:command:`cd ~`
:command:`virtualenv --no-site-packages venv`
:command:`cd venv`
:command:`source bin/activate`</t>
<t tx="ktenney.20090429100000.376"></t>
<t tx="ktenney.20090429100000.378">@language python
@tabwidth -4
@others
#print "finished initing module!"
#out.flush()
@ignore</t>
<t tx="ktenney.20090429100000.379">"""
Created by Angus Helm

This is a moniter/uploader for rescuetime.com on Linux.  Once started, it periodically checks the focused window to see what program you are using.  It then uploads that data to rescuetime.com every half-hour, so that you can view how you spend your time.

Coded with Python 2.5.1, on Ubuntu 7.10 (2.6.22-14-generic kernel)
"""

import os
import sys
import subprocess
import datetime
import signal
import time
import pwd
import getpass
import ctypes
from getopt import gnu_getopt
import re
import traceback
from pkg_resources import resource_filename
import gnomekeyring as gkey
import socket
socket.setdefaulttimeout(15)
import urllib
import urllib2

import RescueTimeUploader.options
import RescueTimeUploader.dirstructure

RESCUETIME_API_URL = "https://api.rescuetime.com/api/"

if os.getenv('http_proxy') != None:
    proxy_url = os.getenv('http_proxy') 
    proxy_support = urllib2.ProxyHandler({'http': proxy_url})
    opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
    urllib2.install_opener(opener)

USER_AGENT = 'RescueTimeLinuxUploader/0.82 +https://launchpad.net/rescuetime-linux-uploader'

xlib = None
try:
    xlib = ctypes.cdll.LoadLibrary( 'libX11.so.6')
except OSError:
    print "Could not load libX11.so.6"
    print "Idle time tracking will not be functional"
    ##out.flush()

xss = None
try:
    if xlib != None:
        xss = ctypes.cdll.LoadLibrary( 'libXss.so.1')
except OSError:
    print "Could not load libXss.so.1"
    print "Idle time tracking will not be functional"
    ##out.flush()

if xlib != None and xss != None:
    xlib_dpy = xlib.XOpenDisplay( os.environ['DISPLAY'])
    xlib_root = xlib.XDefaultRootWindow( xlib_dpy)


# This is messing with rather private data, so don't let anyone read it
os.umask(0066)

os_name = os.uname()[1] + "\\" + pwd.getpwuid(os.geteuid())[0]

</t>
<t tx="ktenney.20090429100000.380"># Used for getting idle time
class XScreenSaverInfo( ctypes.Structure):
  """ typedef struct { ... } XScreenSaverInfo; """
  _fields_ = [('window',      ctypes.c_ulong), # screen saver window
              ('state',       ctypes.c_int),   # off,on,disabled
              ('kind',        ctypes.c_int),   # blanked,internal,external
              ('since',       ctypes.c_ulong), # milliseconds
              ('idle',        ctypes.c_ulong), # milliseconds
              ('event_mask',  ctypes.c_ulong)] # events

</t>
<t tx="ktenney.20090429100000.381">def escape_xml(data):
    """ I think some of the upload errors I'm getting are from pages with &gt;
    in their titles.  So this is a hack to try and fix this.
    """
    #return data.replace("&amp;", "&amp;amp;").replace("&gt;", "&amp;gt;").replace("&lt;", "&amp;lt;").replace("\000", "")
    return urllib.quote_plus(data.replace("\000", ""), "()")

</t>
<t tx="ktenney.20090429100000.382">def escape_less(data):
    return data.replace("\000", "").replace("'", "\\'")

</t>
<t tx="ktenney.20090429100000.383">class time_obj(object):
    """
    A structure that holds information about a slice of time, and the 
    window/app that was focused at that time.
    """
    @others
</t>
<t tx="ktenney.20090429100000.384">
def __init__(self, options, dir_structure):
    """
    Creates a new object, initializing its start_time, app_name, 
    and window_title properties based on the current time and 
    focused application.
    """
    self.options = options
    self.dir_structure = dir_structure
    self.start_time = datetime.datetime.today()
    self.app_name = self.get_active_application()
    self.window_title = self.get_active_window_title()

    self.end_time = None
    extended_info = self.get_extended_info()
    print self.app_name, "(" + str(self.window_title) + ")", "now focused", "(Extended info: " + str(extended_info) + ")"
    ##out.flush()
    if extended_info != None:
        self.extended_info = extended_info

</t>
<t tx="ktenney.20090429100000.385">def __str__(self):
    if hasattr(self, "extended_info"):
        return "(%s: %s %is)" % (self.app_name, self.extended_info, self.get_duration())
    return "(%s: %is)" % (self.app_name, self.get_duration())

</t>
<t tx="ktenney.20090429100000.386">def __repr__(self):
    return self.__str__()

</t>
<t tx="ktenney.20090429100000.387">def get_duration(self):
    if self.end_time == None:
        return (datetime.datetime.today() - self.start_time).seconds
    else:
        return (self.end_time - self.start_time).seconds

</t>
<t tx="ktenney.20090429100000.388">def get_extended_info(self):
    """
    Tries to get any extended info available for the application
    stored in self.app_name.  Returns None if there is none 
    available for that application.
    """
    if self.app_name == None:
        return None

    try:
        extension_name = resource_filename(__name__,  
             "extensions/%s/extended_info" % self.app_name)
    except KeyError:
        return None

    try:
        os.stat(extension_name)
    except OSError:
        return None

    send_to = '%s\n%s\n' % (self.app_name, 
        self.get_active_window_title())

    if self.options.debug:
        print "Getting extended info from", extension_name
        print "send-to:", send_to
        ##out.flush()
    try:
        runner = subprocess.Popen(extension_name, shell=True, 
             stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        get_back = runner.communicate(send_to)[0][:-1]
        if self.options.debug:
            print "get_back:", get_back
            ##out.flush()
        return get_back
    except:
        return None

</t>
<t tx="ktenney.20090429100000.389">def _get_idle_time(self):
    if xlib == None or xss == None:
        return 0
    xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo)
    xss_info = xss.XScreenSaverAllocInfo()
    xss.XScreenSaverQueryInfo( xlib_dpy, xlib_root, xss_info)
    return xss_info.contents.idle / 1000

</t>
<t tx="ktenney.20090429100000.390">def _get_raw_active_application(self):
    idle_time = self._get_idle_time()
    if self.options.debug:
        print "idle-time:", idle_time 
        print "max-idle-minutes:", self.options.max_idle_minutes
        ##out.flush()
    if idle_time &gt; (self.options.max_idle_minutes * 60):
        return None
    try:
        exec_cmd = """xprop -id `xprop -root |awk '/_NET_ACTIVE_WINDOW/ {print $5; exit;}'` |awk -F = '/WM_CLASS/ {print $2; exit;}'| awk '{print $2}' | sed 's/^"//g' | sed 's/"$//g'"""
        runner = subprocess.Popen(exec_cmd, shell=True, 
             stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        runner_res = runner.communicate()[0][:-1]
        if not runner_res.isalnum():
            exec_cmd = """xprop -id `xprop -root |awk '/_NET_ACTIVE_WINDOW/ {print $5; exit;}'` |awk -F = '/^WM_NAME/ {print $2; exit;}' | sed -e's/^ *"//g' | sed -e's/\"$//g'"""
            runner = subprocess.Popen(exec_cmd, shell=True,
                                 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
            runner_res = runner.communicate()[0][:-1]
        return runner_res

    except:
        if self.options.debug:
            raise
        return None

</t>
<t tx="ktenney.20090429100000.391">def get_active_application(self):
    """
    Returns the application name of the focused application, such as
    Firefox or Gnome-terminal.  Sometimes returns None.
    """
    if not self.options.tracking:
        return None

    if self.options.force_active_application != None:
        return self.options.force_active_application
    try:
        app_name = self._get_raw_active_application() 
        # Extensions can override the application name
        try:
            extension_name = resource_filename(__name__,  
                 "extensions/%s/app_name" % app_name)
        except KeyError:
            return unicode(app_name).encode('utf8')
        try:
            os.stat(extension_name)
        except OSError:
            return unicode(app_name).encode('utf8')
        send_to = '%s\n%s\n' % (app_name, self.get_active_window_title())
        if self.options.debug:
            print "Getting application name from",
            print extension_name
            print "send_to:", send_to
            ##out.flush()
        runner = subprocess.Popen(extension_name, 
             stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        app_name = runner.communicate(send_to)[0][:-1]
        if self.options.debug:
            print "app_name:", app_name
            ##out.flush()

        return unicode(app_name).encode('utf8')
    except:
        return None

</t>
<t tx="ktenney.20090429100000.392">def still_active(self):
    """
    Returns True if the application that is active is the same as
    the one that was active when this object was first created.
    Checks the application name and the window title.
    """
    if self.app_name != self.get_active_application():
        return False
    if self.window_title != self.get_active_window_title():
        if self.window_title != "":
            return False
    if hasattr(self, 'extended_info'):
        if self.extended_info != self.get_extended_info():
            return False
    return True

</t>
<t tx="ktenney.20090429100000.393">def close(self):
    """
    Assigns this object's end_time parameter to the current time.
    """
    self.end_time = datetime.datetime.today()

</t>
<t tx="ktenney.20090429100000.394">encoded_title_re = re.compile("(0x[0-9abcdefABCDEF]*,* *)+$")
def _get_raw_window_title(self):
    exec_cmd = """xprop -id `xprop -root |awk '/_NET_ACTIVE_WINDOW/ {print $5; exit;}'` |awk -F = '/^WM_NAME/ {print $2; exit;}' | sed -e's/^ *"//g' | sed -e's/\\"$//g'"""
    try:
        runner = subprocess.Popen(exec_cmd, shell=True, 
             stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        app_name = runner.communicate()[0][:-1].strip()
        if time_obj.encoded_title_re.match(app_name) != None:
            app_name = self._decode_window_title(app_name)
        return app_name
    except:
        if self.options.debug:
            raise
        return None


</t>
<t tx="ktenney.20090429100000.395">def get_active_window_title(self):
    """
    Returns the window title of the focused application, or None sometimes.
    """
    return self._get_raw_window_title()

</t>
<t tx="ktenney.20090429100000.396">def _decode_window_title(self, title):
    wordz = title.split(" ")
    words = []
    for word in wordz:
        if word.endswith(","):
            words.append(word[2:-1])
        else:
            words.append(word[2:])
    total_str = ""
    for word in words:
        total_str += chr(int(word, 16))
    return total_str

</t>
<t tx="ktenney.20090429100000.397">def print_out(self,id):
    """
    Prints out a yaml version of this object, suitable for 
    uploading to rescuetime.com  The id should be an integer that
    is different for each object printed out.
    """
    try:
        #if self.window_title == None or self.window_title.isspace() or len(self.window_title) == 0:
        #	return ""
        if self.app_name == None or self.app_name.isspace() or len(self.app_name) == 0 or self.app_name.lower().strip() == "none":
            return ""
        out =  '- os_username: %s\n' % escape_xml(os_name)
        out += '  app_name: %s\n' % escape_xml(str(self.app_name))
        #out =  '&lt;object type="USER_LOGS" id="%i"&gt;\n' % id
        #out += '  &lt;attribute name="start_time" type="string"&gt;' + escape_xml(str(self.start_time).replace("\..*", "")) + '&lt;/attribute&gt;\n'
        #out += '  &lt;attribute name="app_name" type="string"&gt;' + escape_xml(str(self.app_name)) + "&lt;/attribute&gt;\n"
        if self.window_title == None or self.window_title.isspace() or len(self.window_title) == 0:
            out += "  window_title: 'Untitled Window'\n"
            #out += '  &lt;attribute name="window_title" type="string"&gt;Untitled Window&lt;/attribute&gt;\n'
        else:
            out += "  window_title: '%s'\n" % escape_xml(str(self.window_title))
            #out += '  &lt;attribute name="window_title" type="string"&gt;' + escape_xml(str(self.window_title)) + '&lt;/attribute&gt;\n'
        #out += '  &lt;attribute name="end_time" type="string"&gt;' + escape_xml(str(self.end_time)) + '&lt;/attribute&gt;\n'
        #out += '  &lt;attribute name="os_username" type="string"&gt;' + escape_xml(os_name) + '&lt;/attribute&gt;\n'
        if hasattr(self, "extended_info"):
            out += "  extended_info: '%s'\n" % escape_less(str(self.extended_info))
            #out += '&lt;attribute name="extended_info" type="string"&gt;' + escape_xml(self.extended_info) + '&lt;/attribute&gt;'
        else:
            out += "  extended_info: ''\n"
        #out += '&lt;/object&gt;\n'
        out += '  start_time: %s\n' % re.sub("\..*","",str(self.start_time))
        out += '  end_time: %s\n' % re.sub("\..*","",str(self.end_time))
        return out
    except:
        traceback.print_exc()



</t>
<t tx="ktenney.20090429100000.398">def handle_signal(a , b):
    """
    A function suitable for acting as a signal handler.

    Prepares the latest data for uploading, then quits.  Does NOT upload
    anything.
    """
    sys.exit(0)

</t>
<t tx="ktenney.20090429100000.399">class Uploader(object):
    @others
</t>
<t tx="ktenney.20090429100000.400">def __init__(self, headless=False):
    #print "starting init!"
    ##out.flush()
    self.dir_structure = RescueTimeUploader.dirstructure.DirStructure()
    self.options = RescueTimeUploader.options.Options(self.dir_structure)

    signal.signal(signal.SIGTERM, handle_signal)
    signal.signal(signal.SIGHUP, handle_signal)

    self.active = True

    email_address, password = self.retrieve_credentials()

    if (not email_address or not password) and not headless:
        while True:
            email_address = raw_input("Your rescuetime username: ")
            password = getpass.getpass("Your rescuetime password: ")
            if not self.test_login(email_address, password):
                print "Login failed."
                ##out.flush()
            else:
                break
        self.options.save_preferences(email_address, password)

    self.time_objs = []
    self.archived_time_objs = []

    f = file(self.dir_structure.next_upload_file)
    self.next_upload = int(f.readline())
    f.close()
    #print "finished init!"
    ##out.flush()


</t>
<t tx="ktenney.20090429100000.401">def get_latest_time_obj(self):
    f = file(os.path.join(self.dir_structure.tmp_dir, "notifier.debuglog"), "a")
    f.write("---- STARTING CHECK ----\n")
    f.write("archived_time_objs: %s\n" % self.archived_time_objs)
    min_delay = self.options.applet_notifier_minimum_seconds
    f.write("min_delay: %i\n" % min_delay)
    if len(self.archived_time_objs) == 0:
        f.write("no self.time_objs; returning none\n")
        f.close()
        return None

    latest_time_obj = self.archived_time_objs[-1]
    for timeobj in reversed(self.archived_time_objs[:-1]):
        f.write("timeobj: %s\n" % timeobj)
        if timeobj.app_name != latest_time_obj.app_name:
            f.write("app_name wrong\n")
            continue
        if hasattr(latest_time_obj, 'extended_info') and not hasattr(timeobj, 'extended_info'):
            f.write("No extended info on timeobj")
            continue
        if not hasattr(latest_time_obj, 'extended_info') and hasattr(timeobj, 'extended_info'):
            f.write("No extended info on latest_time_obj")
            continue
        if hasattr(latest_time_obj,'extended_info') and hasattr(timeobj,'extended_info')\
            and timeobj.extended_info != latest_time_obj.extended_info:
            f.write("extended_info wrong\n")
                continue
        duration_delta = latest_time_obj.start_time - timeobj.end_time
        if duration_delta.seconds &gt; min_delay:
            f.write("duration: %i too great\n" %duration_delta.seconds)
            break
        latest_time_obj = timeobj
    f.write("Returning %s\n" % latest_time_obj)
    f.close()
    return latest_time_obj

</t>
<t tx="ktenney.20090429100000.402">def retrieve_credentials(self):
    f = file(self.dir_structure.preference_file)
    pref_data = f.readlines()
    f.close()

    password = False
    email_address = False

    for line in pref_data:
        LL = line.split(":")
        if LL[0].strip() == "email":
            email_address = LL[1].strip()
    try:
        gkey.get_default_keyring_sync()
        attrs = { "server": "http://www.rescuetime.com",
              "user": email_address}
        items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs)
        password =  items[0].secret
    except:
        pass
    return (email_address, password)

</t>
<t tx="ktenney.20090429100000.403">def shutdown(self):
    print "shutting down..."
    ##out.flush()
    if not self.active:
        return
    try:
        email_address, password = self.retrieve_credentials()
        self.options.save_preferences(email_address, password)
    except:
        print "Failed to save preferences on shutdown."
        ##out.flush()
    try:
        self.time_objs[-1].close()
        self.prepare_data(self.time_objs)
    except:
        print "Failed to prepare data on shutdown."
        ##out.flush()
    try:
        for filename in os.listdir(self.dir_structure.tmp_dir):
            os.unlink(os.path.join(
                self.dir_structure.tmp_dir,
                filename))
    except OSError:
        print "Failed to clean out temp dir:",
        print self.dir_structure.tmp_dir
        ##out.flush()
    self.active = False
    sys.exit(0)

</t>
<t tx="ktenney.20090429100000.404">def main(self):
    """
    Goes into a loop, monitering the status of the focused window,
    and periodically uploading all the data it gains.
    """
    print "Main() started"
    print "Python version:", sys.version
    ##out.flush()
    try:
        os.stat(self.dir_structure.lock_file)
        f = file(self.dir_structure.lock_file)
        pid = int(f.read())
        f.close()

        exec_str = "ps -A | grep %i" % pid
        runner = subprocess.Popen(exec_str, shell=True, 
                                  stdin=subprocess.PIPE,
                      stdout=subprocess.PIPE)
        anything = runner.communicate()[0]
        if len(anything) != 0 and not anything.isspace():
            print "Uploader already running: pid", pid
            print "Remove", self.dir_structure.lock_file,
            print "to override."
            ##out.flush()
            sys.exit(1)
    except OSError:
        pass
    f = file(self.dir_structure.lock_file, "w")
    f.write(str(os.getpid()))
    f.close()
    try:
        self.archived_time_objs = []
        self.time_objs = []
        self.time_objs.append(time_obj(self.options, 
                                       self.dir_structure))
        self.archived_time_objs.append(self.time_objs[-1])


        next_upload_time = datetime.datetime.today() + self.options.upload_interval

        try:
            while True:
                # If the focused window has changed
                if not self.time_objs[len(self.time_objs) - 1].still_active():
                    self.time_objs[len(self.time_objs) - 1].close()
                    self.time_objs.append(time_obj(self.options, self.dir_structure))
                    self.archived_time_objs.append(self.time_objs[-1])
                # Check if it has been 30 minutes yet
                if datetime.datetime.today() &gt; next_upload_time:
                    self.prepare_data(self.time_objs[:-1])
                    self.upload_data()
                    self.time_objs = [self.time_objs[-1]]
                    next_upload_time = datetime.datetime.today() + self.options.upload_interval
                # Sleep for a few seconds
                time.sleep(self.options.scan_interval_seconds)
        # Aborted by keyboard
        except KeyboardInterrupt:
            self.time_objs[-1].close()
            self.prepare_data(self.time_objs)
            #if "y" == raw_input("Upload the latest data? y/n: ").lower()[0]:
            #	self.upload_data()
    finally:
        self.shutdown()

</t>
<t tx="ktenney.20090429100000.405">def test_connection(self):
    try:
        request = urllib2.Request('https://api.rescuetime.com/')
        request.add_header('User-agent', USER_AGENT)
        urllib2.urlopen(request)
    except urllib2.HTTPError:
        traceback.print_exc(0)
        return False
    except urllib2.URLError:
        traceback.print_exc(0)
        return False
    return True

</t>
<t tx="ktenney.20090429100000.406">def test_login(self, email_address, password):
    print "Logging in..."
    if not self.test_connection():
        print "Connection is down.  Pretending we succeeded in logging in."
        return True
    ##out.flush()
    login_data = urllib.urlencode([("email", email_address),("password", password)])
    login_url = RESCUETIME_API_URL + "handshake"
    try:
        request = urllib2.Request(login_url)
        request.add_header('User-agent', USER_AGENT)
        ret = urllib2.urlopen(request, login_data)
    except urllib2.HTTPError:
        traceback.print_exc(0)
        return False
    except urllib2.URLError:
        traceback.print_exc(0)
        return False
    login_status = "".join(ret.readlines())
    print login_status
    ##out.flush()
    return "login success!" in login_status.lower()


</t>
<t tx="ktenney.20090429100000.407">def upload_data(self):
    """
    Attempts to upload all the data files stored in PREPARED_DIR.  Moves
    any that are successful to FINISHED_DIR, and any that fail to
    FAILED_DIR.
    """
    print "Submitting data..."
    ##out.flush()

    email_address, password = self.retrieve_credentials()

    if not self.test_login(email_address, password):
        print "It looks like the login failed."
        print "Please edit your password and username in", self.dir_structure.preference_file
        ##out.flush()
        return

    print "Uploading data..."
    ##out.flush()
    for prepared_file in os.listdir(self.dir_structure.prepared_dir):
        finished_name = os.path.join(self.dir_structure.finished_dir, prepared_file)
        old_name = os.path.join(self.dir_structure.prepared_dir, prepared_file)
        failed_name = os.path.join(self.dir_structure.failed_dir, prepared_file)
        print "Uploading", prepared_file, "..."
        ##out.flush()
        submit_url = RESCUETIME_API_URL + "userlogs"
        f = file(old_name)
        submit_data = urllib.urlencode([("email", email_address), ("password", password), ("yamldata", "".join(f.readlines()))])
        f.close()
        try:
            request = urllib2.Request(submit_url)
            request.add_header('User-agent', USER_AGENT)
            ret = urllib2.urlopen(request, submit_data)
        except urllib2.HTTPError:
            print "Error uploading."
            ##out.flush()
            continue
        except urllib2.URLError:
            print "Error uploading."
            ##out.flush()
            continue
        returned_data = "".join(ret.readlines())
        print returned_data
        ##out.flush()
        f = file(os.path.join(self.dir_structure.reply_dir, prepared_file), "w")
        f.write(returned_data)
        f.close()
        if not "error" in returned_data:
            os.rename(old_name, finished_name)
        else:
            os.rename(old_name, failed_name)

    print "Done submitting data."
    ##out.flush()

</t>
<t tx="ktenney.20090429100000.408">def prepare_data(self, time_objs):
    """
    Prepares the given data to be uploaded, and stores the prepared
    data in PREPARED_DIR.  Does not upload the data.
    """
    i = 0
    #data = '&lt;NewDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;\n'
    data = '---\n'
    for to in time_objs:
        data += to.print_out(i)
        i += 1
    #data += "&lt;/NewDataSet&gt;"

    # Save the uploaded data
    if self.options.upload:
        f = file(os.path.join(self.dir_structure.prepared_dir, str(self.next_upload) + ".upload.encoded.data"), "w")
        f.write(data)
        f.close()
    
        self.next_upload += 1
        f = file(self.dir_structure.next_upload_file, "w")
        f.write(str(self.next_upload))
        f.close()
</t>
<t tx="ktenney.20090429100000.409">@nocolor

Generalization
==============

- trace or query according to a specification
- normalize (factor out :term:`irrelevent specificities`)
- write file
- diff the files
- interpret the diff
- present the interpretation

This principle reminds me of the technique of saving power usage waveforms
of a machine. Each artifact on the trace reflects an operation 

- motors starting
- magnets releasing
- switches closing
- etc

Access to the trace of a working unit can help troubleshoot a broken one.

Access the the data I describe below could provide the same benefit.

I've borked my laptop, I can't import gtk, seems like a sys.path issue.

I've been laboriously comparing a working install w/ the laptop.

The commands I run can be codified and their responses stored for
comparison between working and broken systems.

Examples
========

Gather a chunkset consisting of

- string sent to import
- response from Python to the import request
- filename of a successful import
- sys.path items containing the string sent to import
- ls -la response to sys.path items
- ls -ls resopnse to imported filename

on laptop::
    
    $ python
    
    &gt;&gt;&gt; import gtk
    Traceback (most ...
    ...
    ImportError: No module named gtk
    
    &gt;&gt;&gt; import sys
    &gt;&gt;&gt; for item in sys.path:
    ...  if 'gtk' in item:
    ...    print item
    /usr/local/lib/python2.5/site-packages/gtk-2.0
    /usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode
    
on nalbert::
    
    $ python
    &gt;&gt;&gt; import gtk
    &gt;&gt;&gt; gtk.__file__
    '/var/lib/python-support/python2.5/gtk-2.0/gtk/__init__.pyc'
    &gt;&gt;&gt; import sys
    &gt;&gt;&gt; for item in sys.path:
    ...  if 'gtk' in item:
    ...    print item
    /usr/lib/python2.5/site-packages/gtk-2.0
    /var/lib/python-support/python2.5/gtk-2.0
    /usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode
    
on lappy::
    
    &gt;&gt;&gt; sys.path.append('/var/lib/python-support/python2.5/gtk-2.0')
    &gt;&gt;&gt; import gtk
    /var/lib/python-support/python2.5/gtk-2.0/gtk/__init__.py:48: 
    RuntimeWarning: tp_compare didn't return -1 or -2 for exception
    from gtk import _gtk
    ImportError: could not import gio
    ImportError: could not import gio
    
on nalbert::
    
    &gt;&gt;&gt; import gtk
    &gt;&gt;&gt; gtk.__file__
    '/var/lib/python-support/python2.5/gtk-2.0/gtk/__init__.pyc'
           
    
            </t>
<t tx="ktenney.20090429100000.411"></t>
<t tx="ktenney.20090430085348.2"></t>
<t tx="ktenney.20090430085348.3">import docutils</t>
</tnodes>
</leo_file>