예제 #1
0
class PasswordStore:
    def __init__(self, chain="login", account=None, service=None):
        if account is None:
            account = keyinfo.account
        if service is None:
            service = keyinfo.realm
        self.chain = chain
        self.account = account
        self.service = service
        self.keychain = Keychain()

    def unlock(self):
        self.keychain.unlockkeychain(self.chain)

    def lock(self):
        self.keychain.lockkeychain(chain)

    def password(self, account=None):
        if account is None:
            account = self.account
        _, password = self.keychain.getgenericpassword(self.chain, account, self.service)
        return password

    def save_password(self, password, account=None):
        if account is None:
            account = keyinfo.account
        self.keychain.setgenericpassword(self.chain, account, password, self.service)
예제 #2
0
class Preferences(NSWindowController):
	# IBOutlets
	email = IBOutlet()
	password = IBOutlet()
	
	def init(self):
		NSLog('Preferences: init')
		self = super(Preferences, self).init()
		self.initWithWindowNibName_('Preferences')
		return self
	
	def awakeFromNib(self):
		NSLog('Preferences: awakeFromNib')
		
		self.keychain = Keychain()
		
		# We need to get the account email so we can do the keychain lookup
		try:
			f = open('account')
		except IOError:
			pass
		else:
			account = f.readline()
				
			# If there is no value then the details haven't been set yet
			if account != '':			
				account = account.replace('\n', '')
				
				key = self.keychain.getgenericpassword('login', account)
				
				# If the keychain exists then we can set the default value for the fields
				if key != False:
					NSLog('Preferences: awakeFromNib - keychain exists, setting fields')
					self.email.setStringValue_(key['account'])
					self.password.setStringValue_(key['password'])
		
	@IBAction
	def problem_(self, sender):
		NSLog('Preferences: problem_')
		ws = NSWorkspace.sharedWorkspace()
		url = NSURL.URLWithString_('http://www.google.com/support/accounts/bin/answer.py?answer=48598')
		ws.openURL_(url)
		
	@IBAction
	def showWindow_(self, sender):
		NSLog('Preferences: showWindow')
		super(Preferences, self).showWindow_(sender)
		
	@IBAction
	def save_(self, sender):
		NSLog('Preferences: save_')
		
		self.keychain.setgenericpassword('login', self.email.stringValue(), self.password.stringValue(), 'GFGain')
		
		# Save the account email so we can use it later
		f = open('account', 'w')
		f.write(self.email.stringValue())
		f.close()
		
		self.close()
예제 #3
0
class AppDelegate(NSObject):
	gf = False
	now = NSDate.date()
	preferences = False
	keychain = False

	def applicationDidFinishLaunching_(self, sender):
		NSLog('AppDelegate: applicationDidFinishLaunching')
		
		# Set this up as a class attribute so we can modify it later
		NSLog('AppDelegate: applicationDidFinishLaunching - setting up statusItem')
		self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength_(NSVariableStatusItemLength)
		self.statusItem.setHighlightMode_(TRUE)
		self.statusItem.setEnabled_(TRUE)
		self.statusItem.retain() # http://stackoverflow.com/questions/141432/how-can-i-create-a-status-bar-item-with-cocoa-and-python-pyobjc
		
		self.GetGain()
		
		# Build a simple menu for the statusItem
		NSLog('AppDelegate: applicationDidFinishLaunching - setting up menu')
		menu = NSMenu.alloc().init()
		
		menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Preferences', 'launchPreferences:', '')
		menu.addItem_(menuitem)
		menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Quit', 'terminate:', '')
		menu.addItem_(menuitem)
		
		self.statusItem.setMenu_(menu)
		
		# Set up the timer to periodically update the details
		# Timer help from http://the.taoofmac.com/space/blog/2007/04/22/1745
		NSLog('AppDelegate: applicationDidFinishLaunching - setting up timer')
		self.timer = NSTimer.alloc().initWithFireDate_interval_target_selector_userInfo_repeats_(self.now, 60.0, self, 'tick:', None, True)
		NSRunLoop.currentRunLoop().addTimer_forMode_(self.timer, NSDefaultRunLoopMode)
		self.timer.fire()
		
	def GetGain(self):
		NSLog('AppDelegate: GetGain')
		
		if self.gf == False:
			NSLog('AppDelegate: GetGain - gf == False')
			
			# We need to get the account email so we can do the keychain lookup
			try:
				f = open('account')
			except IOError:
				pass
			else:
				account = f.readline()
				
				# If there is no value then the details haven't been set yet
				if account != '':
					account = account.replace('\n', '') # Don't need the trailing newline character
				
					if self.keychain == False:
						NSLog('AppDelegate: GetGain - keychain == false')
						self.keychain = Keychain()
					
					key = self.keychain.getgenericpassword('login', account)
					print key
					
					# Can we create the GoogleFinance object from the keychain?
					if key != False:	
						NSLog('AppDelegate: GetGain - Keychain details exist')
						self.gf = GoogleFinance(key['account'], key['password'])	
					else:
						NSLog('AppDelegate: GetGain - Keychain details don\'t exist')
		
		if self.gf != False:
			# GoogleFinance exists so we can reget the gain data
			NSLog('AppDelegate: GetGain - Getting gain')		
			self.statusItem.setTitle_('Updating...')
			gain = '%.2f' % self.gf.GetGain()
			self.statusItem.setTitle_(gain)
		else:
			# User needs to set their prefs
			NSLog('AppDelegate: GetGain - gf == false (again)')
			self.statusItem.setTitle_('Click to set prefs')
	
	def launchPreferences_(self, notification):
		NSLog('AppDelegate: launchPreferences')
		self.preferences = Preferences.alloc().init()
		self.preferences.showWindow_(self)
		
	def tick_(self, notification):
		NSLog('AppDelegate: tick')
		
		# We only want to get the gain again if trading is ongoing (i.e. Mon - Fri 9:30 a.m. to 4:00 p.m EST)
		utc = time.gmtime()
		
		# Determine whether it is DST or not because if it is then the UTC offset will be different
		if utc[8] == 0:
			# EST = UTC - 5 hours (18000 seconds)
			NSLog('AppDelegate: tick - Timezone = EST')
			est = time.mktime(utc) - 18000
			marketTime = time.gmtime(est)
		else:
			# If DST then EDT
			# EDT = UTC - 4 hours (14400 seconds)
			NSLog('AppDelegate: tick - Timezone = EDT')
			edt = time.mktime(utc) - 14400
			marketTime = time.gmtime(edt)
		
		# If it is Fri or Sat, don't bother getting data
		if marketTime[6] == 5 or marketTime[6] == 6:
			NSLog('AppDelegate: tick - It\'s the weekend! Yay')
			open = False
		else:		
			NSLog('AppDelegate: tick - Mon - Fri')
			# If it's after 9.30am then open, otherwise closed!
			if marketTime[3] == 9:
				if marketTime[4] >= 30:
					NSLog('AppDelegate: tick - 9...open')
					open = True
				else:
					NSLog('AppDelegate: tick - 9...closed')
					open = False
			
			# Define which hours are open to make it easier than devising crazy hour based logic
			hoursOpen = [10, 11, 12, 13, 14, 15]
			
			# So if it is within 10am, 11am, 12pm, 1pm, 2pm or 3pm then it's open. Before and after those closed
			if marketTime[3] not in hoursOpen:
				NSLog('AppDelegate: tick - closed')
				open = False
			else:
				NSLog('AppDelegate: tick - open')
				open = True;
		
		if open == True:				
			self.GetGain()