#!/usr/bin/env python2.7

from subprocess import check_output, call
import argparse
from re import findall

#define parsers.
parser = argparse.ArgumentParser(description='Control two Canon dSLR cameras of the same model using the ownername camera feature. Requires the gphoto2 binary in your $PATH. Images download in the current working directory', 
	epilog='This will prompt you for the camera name from those detected. Capture begins after you hit Enter. Your camera may not be able to sustain 5s between shots, so play around with the interval setting') 
parser.add_argument('-f', action='store', dest='frames', default='1440', help='number of frames to capture. default is 1440')
parser.add_argument('-i', action='store', dest='interval', default='5', help='interval to wait between frames in seconds. default is 5s')
parser.add_argument('-c', action='store_const', dest='command', default='--capture-image-and-download', const='--capture-image', help='keep the images on the card. only enable this if you have a large enough memory card in your camera')
args = parser.parse_args()

# returns a list of usb port IDs for use with gphoto2
def findCameraID( lines ):
	return(findall(r"(usb:...,...)", lines))

# use this on elements from a list to return a (port,name) object
def getCameraName( port ):
	ownerText = check_output(['gphoto2', '--port', port, '--get-config', 'ownername'])
	cameraName = findall(r"Current: (.+)\n", ownerText)[0]
	return([port, cameraName])

cameras = check_output(['gphoto2', '--auto-detect']) #get the output from gphoto2
ids = findCameraID(cameras) #strip the usb:ids from raw gphoto2 output
names = [getCameraName(id) for id in ids] #output a list of names

if len(names) == 0:
	exit("No cameras detected. Are they turned on and plugged in?")

print 'detected cameras: ', [name[1] for name in names] #list the cameras

cameraName = raw_input('Please type in the camera name: ') #prompt for camera name

portName = [name[0] for name in names if name[1] == cameraName][0] #extract usb port name

call(['gphoto2', '--port', portName, args.command, '-F', args.frames, '-I', args.interval])
@@ -163,17 +163,19 @@
 ription=
-'
+%22%22%22
 Control 
@@ -197,16 +197,18 @@
 cameras 
+%0A%09
 of the s
@@ -274,16 +274,18 @@
 gphoto2 
+%0A%09
 binary i
@@ -346,17 +346,19 @@
 irectory
-'
+%22%22%22
 , %0A%09epil
@@ -360,17 +360,19 @@
 %09epilog=
-'
+%22%22%22
 This wil
@@ -425,16 +425,18 @@
 tected. 
+%0A%09
 Capture 
@@ -502,16 +502,18 @@
 sustain 
+%0A%09
 5s betwe
@@ -562,17 +562,19 @@
  setting
-'
+%22%22%22
 ) %0Aparse
@@ -641,22 +641,26 @@
 '1440', 
+%0A%09
 help=
-'
+%22%22%22
 number o
@@ -695,17 +695,19 @@
  is 1440
-'
+%22%22%22
 )%0Aparser
@@ -772,22 +772,26 @@
 lt='5', 
+%0A%09
 help=
-'
+%22%22%22
 interval
@@ -839,17 +839,19 @@
 lt is 5s
-'
+%22%22%22
 )%0Aparser
@@ -908,16 +908,18 @@
 mmand', 
+%0A%09
 default=
@@ -979,14 +979,18 @@
 e', 
+%0A%09
 help=
-'
+%22%22%22
 keep
@@ -1051,16 +1051,18 @@
 a large 
+%0A%09
 enough m
@@ -1086,17 +1086,19 @@
 r camera
-'
+%22%22%22
 )%0Aargs =

fix some of the longest lines