If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Sunday, October 29, 2006

putpath

Since I am (sadly) on Windows platform, I use cygwin a lot.

One of my most used scripts is putpath which puts the windows path of the first argument to the windows clipboard (if called without any arguments it uses the current directory).

It uses the putclip program that comes with cygwin.

#!/usr/bin/env python
'''Put a path in clipboard'''
__author__ = "Miki Tebeka "

from os import system, popen
from os.path import exists
from optparse import OptionParser

# Command line parsing
p = OptionParser("usage: %prog [options] [PATH]")
p.add_option("-u", help="unix path format", dest="unix",
default=0, action="store_true")
p.add_option("-c", help="C string format", dest="cstr",
default=0, action="store_true")

opts, args = p.parse_args()
if len(args) not in (0, 1):
p.error("wrong number of arguments") # Will exit

if len(args) == 0:
path = "."
else:
path = args[0]

if not exists(path):
raise SystemExit("error: %s - no such file or directory" % path)

# Output format (unix/dos)
if opts.unix:
format = "u"
else:
format = "w"

path = popen("cygpath -%sa '%s'" % (format, path)).read().strip()
if opts.cstr and (not opts.unix):
path = path.replace("\\", "\\\\")
popen("putclip", "w").write("%s" % path)

No comments:

Blog Archive