#!/usr/bin/env python
#ClipManip v.04
#added the comment function. arg is 'c' for comment
#Fri Mar 30 06:05:24 PM PST 2001 Paul Evans <pevans@catholic.org>

#################################
# The comment thing makes these #
# sort of comments              #
# out of selected text          #
#################################

from wxPython.wx import wxTheClipboard, wxTextDataObject
from sys import argv
import string, os

#args are: sort s, add a, or notes n, or comment c
#no dash. only one of these three letters

#read the clipboard
wxTheClipboard.UsePrimarySelection(1) # which clipbuffer to use: in the clipboard or the primary (selected text)
wxTheClipboard.Open()
data = wxTextDataObject();wxTheClipboard.GetData(data)
contents = data.GetText()
wxTheClipboard.Close()


##fixme!! shud be readlines and writelines through out

def sorted(contents):
    #sort the clipboard - had to use xclip, else proper wx app...
    contentslist = string.split(contents,'\n')
    contentslist.sort()
    contents = string.join(contentslist, '\n')

    #f = open("/tmp/clip.txt", 'w') #this works
    #f.write(contents)
    #f.close()
    #cmd = 'xclip /tmp/clip.txt'
    #os.system(cmd)

    f = os.popen('xclip -s', 'w') #this is better
    f.write(contents)
    f.close()

def added(contents):
    #sum the clipboard
    orig = contents
    contentslist = string.split(contents)
    sum = 0
    for i in contentslist:
        if string.find(i,'/') > 0 and string.find(i,'.') < 1:
            i = i + '.0' #force floating if division in i
        try:
            sum = sum + float(eval(i))
        except:
            continue
    contents = '============\n' + str(sum) + '\n'
    print contents
    f = os.popen('xclip -s', 'w')
    f.write(contents)
    f.close()

def noted(contents):
    #add clipboard to notes with date stamp and wininfo
    import getpass
    user = getpass.getuser()
    if user == 'root':
        HOMEDIR = '/root'
    else:
        HOMEDIR = '/home/' + user
    
    try:
        f = open(HOMEDIR + "/notes.txt", 'r')
        orig = f.read()
        f.close()
    except IOError:
        orig = ""
    import time
    stamp = time.ctime(time.time() ) + '\n'
    sep = '\n' +  '='*30 + '\n\n'
    outstr = stamp + contents + sep + orig
    f = open(HOMEDIR + "/notes.txt", 'w')
    f.write(outstr)
    f.close()

def comment(contents):
    #Comment the clipboard with hash marks
    contentslist = string.split(contents,'\n')
    global longest
    longest = 0
    for i in contentslist:
         if len(i) > longest:
             longest = len(i)
    x = '#'*(longest+4)
    x2 = '#' + ' '*(longest+2) + '#'
    newlist = map(padhash, contentslist)
    newlist.insert(0, x2)
    newlist.insert(0, x)
    newlist.append(x2)
    newlist.append(x)
    contents = string.join(newlist, '\n')
    print contents
    f = os.popen('xclip -s', 'w')
    f.write(contents)
    f.close()

def padhash(line):
    global longest
    spaces = ' ' * (longest - len(line))
    padded = "%s%s%s%s" % ('# ', line, spaces, ' #')
    return(padded)

if argv[1] == 's':
    sorted(contents)
elif argv[1] == 'a':
    added(contents)
elif argv[1] == 'n':
    noted(contents)
elif argv[1] == 'c':
    comment(contents)
else:
    pass

