Skip to content

Commit

Permalink
PEP8 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vintitres committed Feb 18, 2012
1 parent 35ad867 commit 2a37e1e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,4 @@
*.pyc *.pyc
*~ *~
excludes excludes
config.json
4 changes: 3 additions & 1 deletion config.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
''' '''
import json import json



def set(parameter, value): def set(parameter, value):
config = get(None) config = get(None)
config[parameter] = value config[parameter] = value
with open('config.json', 'w') as fp: with open('config.json', 'w') as fp:
json.dump(config, fp) json.dump(config, fp)



def get(parameter): def get(parameter):
try: try:
with open('config.json', 'r') as fp: with open('config.json', 'r') as fp:
Expand All @@ -20,7 +22,7 @@ def get(parameter):


if parameter is None: if parameter is None:
return config return config
if config.has_key(parameter): if parameter in config:
return config.get(parameter) return config.get(parameter)
else: else:
config[parameter] = None config[parameter] = None
15 changes: 7 additions & 8 deletions gnome-shell-google-calendar.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ def get_key(self):


@write_traceback @write_traceback
def as_gnome_event(self): def as_gnome_event(self):
return ('', # uid return ('', # uid
self.title if self.title else '', # summary self.title if self.title else '', # summary
'', # description '', # description
self.allday, # allDay self.allday, # allDay
self.start_time, # date self.start_time, # date
self.end_time, # end self.end_time, # end
{}) # extras {}) # extras


def __repr__(self): def __repr__(self):
return '<Event: %r>' % (self.title) return '<Event: %r>' % (self.title)
Expand Down Expand Up @@ -438,7 +438,6 @@ def GetEvents(self, since, until, force_reload):
if o == '--hide-cal': if o == '--hide-cal':
SHOW_SHORT_CALENDAR_TITLE = False SHOW_SHORT_CALENDAR_TITLE = False



account = config.get('account') account = config.get('account')


# Login # Login
Expand Down
16 changes: 8 additions & 8 deletions keyring.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


_keyring = 'login' _keyring = 'login'
_display_name = 'GNOME Shell Google Calendar Account' _display_name = 'GNOME Shell Google Calendar Account'
_attrs = { 'application': 'gnome-shell-google-calendar' } _attrs = {'application': 'gnome-shell-google-calendar'}
_type = gk.ITEM_GENERIC_SECRET _type = gk.ITEM_GENERIC_SECRET
_item_id = None _item_id = None


Expand All @@ -16,10 +16,10 @@ class KeyringError(Exception):


def get_item_id(): def get_item_id():
global _item_id global _item_id

if _item_id: if _item_id:
return _item_id return _item_id

try: try:
results = gk.find_items_sync(gk.ITEM_GENERIC_SECRET, _attrs) results = gk.find_items_sync(gk.ITEM_GENERIC_SECRET, _attrs)
_item_id = results[0].item_id _item_id = results[0].item_id
Expand All @@ -37,24 +37,24 @@ def get_item_id():
def get_credentials(): def get_credentials():
item_id = get_item_id() item_id = get_item_id()
attrs = gk.item_get_attributes_sync(_keyring, item_id) attrs = gk.item_get_attributes_sync(_keyring, item_id)

if not 'email' in attrs: if not 'email' in attrs:
raise KeyringError('Login credentials not found') raise KeyringError('Login credentials not found')

info = gk.item_get_info_sync(_keyring, item_id) info = gk.item_get_info_sync(_keyring, item_id)

return attrs['email'], info.get_secret() return attrs['email'], info.get_secret()




def set_credentials(email, password): def set_credentials(email, password):
item_id = get_item_id() item_id = get_item_id()

info = gk.ItemInfo() info = gk.ItemInfo()
info.set_display_name(_display_name) info.set_display_name(_display_name)
info.set_type(_type) info.set_type(_type)
info.set_secret(password) info.set_secret(password)
gk.item_set_info_sync(_keyring, item_id, info) gk.item_set_info_sync(_keyring, item_id, info)

attrs = _attrs.copy() attrs = _attrs.copy()
attrs['email'] = email attrs['email'] = email
gk.item_set_attributes_sync(_keyring, item_id, attrs) gk.item_set_attributes_sync(_keyring, item_id, attrs)
Expand Down
49 changes: 29 additions & 20 deletions oauth.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,52 +4,61 @@
@author: flocki @author: flocki
''' '''
import dbus import dbus
#import dbus.mainloop.glib
#import dbus.service
import gdata.calendar.client import gdata.calendar.client




def oauth_prompt(): def oauth_prompt():
bus = dbus.SessionBus() bus = dbus.SessionBus()
#accounts = bus.get_object('org.gnome.OnlineAccounts', '/org/gnome/OnlineAccounts/Accounts') online_accounts = bus.get_object('org.gnome.OnlineAccounts',
online_accounts = bus.get_object('org.gnome.OnlineAccounts', '/org/gnome/OnlineAccounts') '/org/gnome/OnlineAccounts')


l = online_accounts.GetManagedObjects(dbus_interface='org.freedesktop.DBus.ObjectManager') l = online_accounts.GetManagedObjects(
dbus_interface='org.freedesktop.DBus.ObjectManager')


accounts = [] accounts = []
for account_info in l.values(): for account_info in l.values():
#print account_info if 'org.gnome.OnlineAccounts.Account' in account_info:
if account_info.has_key('org.gnome.OnlineAccounts.Account'): email = str(account_info['org.gnome.OnlineAccounts.Account']
email = str(account_info['org.gnome.OnlineAccounts.Account']['PresentationIdentity']) ['PresentationIdentity'])
print "%d. %s" % (len(accounts), email) print "%d. %s" % (len(accounts), email)
accounts.append(email) accounts.append(email)
email = accounts[ int(raw_input('Please choose the Account: ')) ] email = accounts[int(raw_input('Please choose the Account: '))]
print "You choose '{0}'".format(email) print "You choose '{0}'".format(email)
return email return email



def oauth_login(email): def oauth_login(email):
bus = dbus.SessionBus() bus = dbus.SessionBus()
online_accounts = bus.get_object('org.gnome.OnlineAccounts', '/org/gnome/OnlineAccounts') online_accounts = bus.get_object('org.gnome.OnlineAccounts',
'/org/gnome/OnlineAccounts')


l = online_accounts.GetManagedObjects(dbus_interface='org.freedesktop.DBus.ObjectManager') l = online_accounts.GetManagedObjects(
dbus_interface='org.freedesktop.DBus.ObjectManager')


for account_path, account_info in l.items(): for account_path, account_info in l.items():
#print account_path if 'org.gnome.OnlineAccounts.Account' in account_info and \
#print account_info str(account_info['org.gnome.OnlineAccounts.Account']
if account_info.has_key('org.gnome.OnlineAccounts.Account') and str(account_info['org.gnome.OnlineAccounts.Account']['PresentationIdentity']) == email: ['PresentationIdentity']) == email:
consumer_key = str(account_info['org.gnome.OnlineAccounts.OAuthBased']['ConsumerKey']) consumer_key = str(
consumer_secret = str(account_info['org.gnome.OnlineAccounts.OAuthBased']['ConsumerSecret']) account_info['org.gnome.OnlineAccounts.OAuthBased']
['ConsumerKey'])
consumer_secret = str(
account_info['org.gnome.OnlineAccounts.OAuthBased']
['ConsumerSecret'])


o = bus.get_object('org.gnome.OnlineAccounts', account_path) o = bus.get_object('org.gnome.OnlineAccounts', account_path)


oauth_data = o.get_dbus_method('GetAccessToken', 'org.gnome.OnlineAccounts.OAuthBased')() oauth_data = o.get_dbus_method('GetAccessToken',
'org.gnome.OnlineAccounts.OAuthBased')()


access_token = str(oauth_data[0]) access_token = str(oauth_data[0])
access_token_secret = str(oauth_data[1]) access_token_secret = str(oauth_data[1])


client = gdata.calendar.client.CalendarClient(source='gnome-shell-google-calendar') client = gdata.calendar.client.CalendarClient(
client.auth_token = gdata.gauth.OAuthHmacToken(consumer_key, consumer_secret, access_token, access_token_secret, gdata.gauth.ACCESS_TOKEN) source='gnome-shell-google-calendar')
client.auth_token = gdata.gauth.OAuthHmacToken(consumer_key,
consumer_secret, access_token, access_token_secret,
gdata.gauth.ACCESS_TOKEN)
return client return client


raise Exception raise Exception

28 changes: 15 additions & 13 deletions test
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import unittest import unittest
import datetime import datetime



# remove the copied module # remove the copied module
def cleanup(): def cleanup():
import os import os
os.remove('gsgc_test.py') os.remove('gsgc_test.py')
if os.path.exists('gsgc_test.pyc'): if os.path.exists('gsgc_test.pyc'):
os.remove('gsgc_test.pyc') os.remove('gsgc_test.pyc')
import atexit import atexit
atexit.register(cleanup) atexit.register(cleanup)



# import, but have to use a python-compatible name # import, but have to use a python-compatible name
import shutil import shutil
shutil.copy('gnome-shell-google-calendar.py', 'gsgc_test.py') shutil.copy('gnome-shell-google-calendar.py', 'gsgc_test.py')
Expand All @@ -23,24 +25,24 @@ import gsgc_test as gsgc


################################################# #################################################
class TestGnomeGoogleCalendar(unittest.TestCase): class TestGnomeGoogleCalendar(unittest.TestCase):
def test_GetMonthKey(self): def test_GetMonthKey(self):
from time import strftime, localtime from time import strftime, localtime


def tostr(x): def tostr(x):
return [ strftime('%Y%m%d %H%M%S', localtime(x)) for x in x ] return [strftime('%Y%m%d %H%M%S', localtime(x)) for x in x]


self.assertEqual(tostr( self.assertEqual(tostr(
gsgc.get_month_key(datetime.datetime(2011, 12, 12))), gsgc.get_month_key(datetime.datetime(2011, 12, 12))),
['20111127 000000', '20111231 235959']) ['20111127 000000', '20111231 235959'])
self.assertEqual(tostr( self.assertEqual(tostr(
gsgc.get_month_key(datetime.datetime(2011, 11, 1))), gsgc.get_month_key(datetime.datetime(2011, 11, 1))),
['20111030 000000', '20111203 235959']) ['20111030 000000', '20111203 235959'])
self.assertEqual(tostr( self.assertEqual(tostr(
gsgc.get_month_key(datetime.datetime(2011, 1, 31))), gsgc.get_month_key(datetime.datetime(2011, 1, 31))),
['20101226 000000', '20110205 235959']) ['20101226 000000', '20110205 235959'])
self.assertEqual(tostr( self.assertEqual(tostr(
gsgc.get_month_key(datetime.datetime(2012, 1, 31))), gsgc.get_month_key(datetime.datetime(2012, 1, 31))),
['20120101 000000', '20120204 235959']) ['20120101 000000', '20120204 235959'])


suite = unittest.TestLoader().loadTestsFromTestCase(TestGnomeGoogleCalendar) suite = unittest.TestLoader().loadTestsFromTestCase(TestGnomeGoogleCalendar)
unittest.TextTestRunner(verbosity = 2).run(suite) unittest.TextTestRunner(verbosity=2).run(suite)

0 comments on commit 2a37e1e

Please sign in to comment.