#!/usr/bin/python
#
ver=0.031
# This version not ready for prime time
# scrapes craigslist for work
# pflint Sat 19 Apr 2014 09:42:23 AM EDT 
# Sun 20 Apr 2014 10:04:47 AM EDT begin add write file capability
# probably need to functionalize this code...
# trying to include functional doctesting see http://pythontesting.net/start-here/
#
# Internationalization Strings
FirstWelcome="Welcome to the Beautiful Soup Test Package. For operating information type '%s -h'"
TitleLine="Beautiful Soup Test Software Support Menu"
VersionLine="This code is version %s, and the code, %s is copyright 2013 by fits and licensed under GPLI"
#
import os
import re
import sys
import stat
import optparse
import subprocess
import doctest
import signal
import time
import serial
import io
import csv
import string
from subprocess import call
from serial import Serial
from string import digits
from BeautifulSoup import BeautifulSoup
import urllib2
#
def intro():
	''' Prints the main story about this program and tries to warn you.'''
	print '%s documentation: A tale of searching....' %  os.path.basename(__file__),
	print main.__doc__
	print "for operating information type '%s -h'" % __file__
#
def dathing():
	'''This is the unstructured un-functional stuff.  Now we get the parser going...'''
	cityw = {
		'Washington DC':'http://washingtondc.craigslist.org/search/',
		'Montreal CA':'http://montreal.fr.craigslist.ca/search/',
		'Eastern CT':'http://newlondon.craigslist.org/search/',
		'Hartford CT':'http://hartford.craigslist.org/search/',
		'New Haven CT':'http://newhaven.craigslist.org/search/',
		'Northwest CT':'http://nwct.craigslist.org/search/',
		'Albany NY':'http://albany.craigslist.org/search/'}
	#	'':'',
	specs =['eng','sad','sof','tsh']
	for key, value in dict.items(cityw):
		# print key,value
		# f = open(fname,'a')
		city=key
		base=value
		# print city,base
		for spec in specs:
			url=base+spec+"?addOne=telecommuting"
			# print url
			#Create the soup
			page=urllib2.urlopen(url)
			soup = BeautifulSoup(page.read())
			#Search the soup
			sline = soup.findAll('p',{'class':'row'})
			#output the result
			print('<p> '+city+' '+spec+' </p>')	
			print('<base href="'+base+'">')
			for word in sline:
				print word
				# f.writelines (word)
		# f.close()


def main():
	''' 
	Sun 20 Apr 2014 10:51:30 AM EDT pflint, a new start into the baroque
	This is the main subroutine, any detail about this program belongs here.
	This program has the following basic parts:
	1. A fixed dictionary of locations and urls that this program will 
	   interrogate for work.  Currently limited to craigslist
	2. A series of job descriptions.  This program always looks for Telecomuting.
        '''

start_time = time.time()
# frfile = Beautiful Soup Test() # instantiate the Beautiful Soup Test class to frfile
# print "starting %s at %s " % (__file__, \
go_time=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time))
print "starting %s at %s " % (__file__, go_time)
if len(sys.argv) == 1:
	print FirstWelcome % __file__
	sys.exit()
#  Above handles no arguments
#
else:
# parse options and act
    	parser = optparse.OptionParser(description=TitleLine,
                 prog=os.path.basename(__file__),
                 version=VersionLine % ( ver,os.path.basename(__file__) ),
                 usage= '%prog [option]')
   	#
	parser.add_option('-?', '--query',   dest='bool',default=False, action='store_true',help='print document string with extensive result',)
	parser.add_option('-s', '--serial',  dest='serialp',default=False, action='store_true',help='gather data from serial port',)
	parser.add_option("-f", "--file",    dest="filename",  default="USB",help="read test data from a file", metavar="FILE")
	parser.add_option("-p", "--port",    dest="serial_port",  default="/dev/ttyUSB0",help="set serial port; \tdefault=\"/dev/ttyUSB0\"", metavar="STRING")
	parser.add_option("-b", "--baud",    dest="port_baud",  default="9600",help="set port speed;  \tdefault=\"9600\"", metavar="STRING")
	parser.add_option("-t", "--test",    dest="ftest",  default="function test",help="Test a Function;  \tdefault=\"ftest\"", metavar="STRING")
#
# Evaluates inbound arguments
	options, arguments = parser.parse_args()	# evaluate options
	if options.bool: 
		# print len(sys.argv) # useful if you really want it...
		intro()
		sys.exit()	
	elif options.serialp:
		print "You are at the serial routine"
		# serial = Beautiful Soup Test() # instantiate the Beautiful Soup Test class to serial
		# out=serial #enable the instance
		# mser=otrs()
		# print mser.__doc__rsdl(mser)
		# print dir(mser)
		#d print rsdl(mser) 
		doserial()
		sys.exit()	
	elif options.filename != "USB":
		print "You are at file routines"
		# dline=otrf() # open file
		print "this is the end of opening"
		sys.exit()	
	elif options.serial_port != "/dev/ttyUSB0":
		print "You are at port selection"
		sys.exit()
	elif options.port_baud != "9600":
		print "You are at port selection"
		sys.exit()	
	elif options.ftest != "function test":
		print "You are at the function test selection"
		sys.exit()	


if __name__ == '__main__':
	main()
	print "that's it"

