Skip to content

Commit

Permalink
Merge pull request #125 from iferminm/core-returns
Browse files Browse the repository at this point in the history
Core returns
  • Loading branch information
satanas committed Oct 18, 2014
2 parents 1aac98b + b1d9984 commit 3de626f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
22 changes: 11 additions & 11 deletions libturpial/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,6 @@ def upload_media(self, account_id, filepath, message=None):
# Configuration API
###########################################################################

# TODO: Return added option?
def register_new_config_option(self, section, option, default_value):
"""
Register a new configuration *option* in *section* to be handled by
Expand All @@ -654,7 +653,7 @@ def register_new_config_option(self, section, option, default_value):
From this point you can use config methods over this value as usual.
"""
self.config.register_extra_option(section, option, default_value)
return self.config.register_extra_option(section, option, default_value)

def get_shorten_url_service(self):
return self.config.read('Services', 'shorten-url')
Expand Down Expand Up @@ -779,27 +778,27 @@ def list_filters(self):
"""
return self.config.load_filters()

# TODO: Return saved filters or True
def save_filters(self, lst):
"""
Save *lst* a the new filters list
"""
self.config.save_filters(lst)

# TODO: Return True on success
def delete_current_config(self):
"""
Delete current configuration file. This action can not be undone
"""
self.config.delete()
return self.config.delete()

# TODO: Return True on success
def delete_cache(self):
"""
Delete all files in cache
"""
results = list()
for account in self.registered_accounts():
account.delete_cache()
results.append(account.delete_cache())

return not any(results)

def get_cache_size(self):
"""
Expand All @@ -810,21 +809,22 @@ def get_cache_size(self):
total_size += account.get_cache_size()
return total_size

# TODO: Return added friend
def add_friend(self, username):
"""
Save *username* into the friends list
"""
friends = self.config.load_friends()
friends.append(username)
self.config.save_friends(friends)
return self.config.save_friends(friends)

# TODO: Return removed friend
def remove_friend(self, username):
"""
Remove *username* from friends list
"""
friends = self.config.load_friends()
if username in friends:
friends.remove(username)
self.config.save_friends(friends)
result = self.config.save_friends(friends)

return username if username not in result else None

40 changes: 29 additions & 11 deletions libturpial/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
BASEDIR = os.path.join(USERDIR, '.config', 'turpial')


class ConfigBase:
class ConfigBase(object):
"""Base configuration"""
def __init__(self, default=None):
self.__config = {}
Expand All @@ -88,14 +88,21 @@ def __init__(self, default=None):
self.extra_sections = {}

def register_extra_option(self, section, option, default_value):
"""
Registers a new configuration option under a specified section
with a default value. Returns a maping with the new option as a key
and the value
"""
if section in self.__config:
if option in self.__config[section]:
# TODO: raise an exception maybe?
return

if section not in self.extra_sections:
self.extra_sections[section] = {}
self.extra_sections[section][option] = default_value
self.write(section, option, default_value)
return {option: default_value}

def create(self):
for section, v in self.default.iteritems():
Expand Down Expand Up @@ -237,12 +244,12 @@ def load_filters(self):
_fd.close()
return muted

# TODO: Return saved filters?
def save_filters(self, filter_list):
_fd = open(self.filterpath, 'w')
for expression in filter_list:
_fd.write(expression + '\n')
_fd.close()
return filter_list

# TODO: Return added expresion?
def append_filter(self, expression):
Expand Down Expand Up @@ -272,12 +279,13 @@ def load_friends(self):
_fd.close()
return friends

# TODO: Return saved friends?
# TODO: Validate success somehow
def save_friends(self, lst):
_fd = open(self.friendspath, 'w')
for friend in lst:
_fd.write(friend + '\n')
_fd.close()
return lst

def get_stored_accounts(self):
accounts = []
Expand Down Expand Up @@ -313,11 +321,13 @@ def get_proxy(self):
def get_socket_timeout(self):
return int(self.read('Advanced', 'socket-timeout'))

# TODO: Return True when success?
def delete(self):
os.remove(self.configpath)
self.log.debug('Deleted current config. Please restart Turpial')

try:
os.remove(self.configpath)
self.log.debug('Deleted current config. Please restart Turpial')
return True
except AttributeError:
return False

class AccountConfig(ConfigBase):

Expand Down Expand Up @@ -415,13 +425,21 @@ def dismiss(self):
shutil.rmtree(self.basedir)
self.log.debug('Removed base directory')

# TODO: Return True on success?
def delete_cache(self):
"""
Returns a list of unsusccessful deletions
"""
unsuccessful = list()
for root, dirs, files in os.walk(self.imgdir):
for f in files:
path = os.path.join(root, f)
self.log.debug("Deleting %s" % path)
os.remove(path)
try:
path = os.path.join(root, f)
self.log.debug("Deleting %s" % path)
os.remove(path)
except AttributeError:
unsuccessful.append(path)
self.log.debug('There were unsuccessful deletions %s' % unsuccessful)
return unsuccessful

def calculate_cache_size(self):
size = 0
Expand Down
8 changes: 4 additions & 4 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def test_load_filters(self, monkeypatch):

# TODO: How to test that this works? Return 0 maybe?
def test_save_filters(self):
assert self.app_config.save_filters(['foo', 'bar']) == None
assert self.app_config.save_filters(['foo', 'bar']) == ['foo', 'bar']

# TODO: How to test that this works? Return 0 maybe?
def test_append_filter(self, monkeypatch):
Expand All @@ -246,7 +246,7 @@ def test_load_friends(self, monkeypatch):

# TODO: How to test that this works? Return 0 maybe?
def test_save_friends(self):
assert self.app_config.save_friends(['foo', 'bar']) == None
assert self.app_config.save_friends(['foo', 'bar']) == ['foo', 'bar']

def test_get_stored_accounts(self, monkeypatch):
monkeypatch.setattr(os, 'walk', lambda x: DummyGenerator([('foopath', ['dirpath1', 'dirpath2'], ['filename1'])]))
Expand Down Expand Up @@ -287,7 +287,7 @@ def test_get_socket_timeout(self, monkeypatch):
# TODO: How to test that this works? Return 0 maybe?
def test_delete(self, monkeypatch):
monkeypatch.setattr(os, 'remove', lambda x: None)
assert self.app_config.delete() == None
assert self.app_config.delete() == True

class TestAccountConfig:
@classmethod
Expand Down Expand Up @@ -342,7 +342,7 @@ def test_delete_cache(self, monkeypatch):
monkeypatch.setattr(os, 'walk', lambda x: [('/tmp', ['my_dir'], ['file1', 'file2'])])

# TODO: How to test this?
assert self.account_config.delete_cache() == None
assert self.account_config.delete_cache() == list()

def test_calculate_cache_size(self, monkeypatch):
monkeypatch.setattr(os, 'walk', lambda x: [('/tmp', ['my_dir'], ['file1', 'file2'])])
Expand Down
12 changes: 6 additions & 6 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def test_unfollow(self, monkeypatch):
profile = Profile()
profile.username = "foo"
monkeypatch.setattr(self.account, "unfollow", lambda x: profile)
monkeypatch.setattr(self.core, "remove_friend", lambda x: None)
monkeypatch.setattr(self.core, "remove_friend", lambda x: [])

response = self.core.unfollow(self.acc_id, "foo")
assert response == profile
Expand All @@ -433,7 +433,7 @@ def test_block(self, monkeypatch):
profile = Profile()
profile.username = "foo"
monkeypatch.setattr(self.account, "block", lambda x: profile)
monkeypatch.setattr(self.core, "remove_friend", lambda x: None)
monkeypatch.setattr(self.core, "remove_friend", lambda x: [])

response = self.core.block(self.acc_id, "foo")
assert response == profile
Expand All @@ -450,7 +450,7 @@ def test_report_as_spam(self, monkeypatch):
profile = Profile()
profile.username = "foo"
monkeypatch.setattr(self.account, "report_as_spam", lambda x: profile)
monkeypatch.setattr(self.core, "remove_friend", lambda x: None)
monkeypatch.setattr(self.core, "remove_friend", lambda x: [])

response = self.core.report_as_spam(self.acc_id, "foo")
assert response == profile
Expand Down Expand Up @@ -807,7 +807,7 @@ def test_delete_cache(self, monkeypatch):
monkeypatch.setattr(self.core, "registered_accounts", lambda: [self.account])

response = self.core.delete_cache()
assert response is None
assert response is True

def test_get_cache_size(self, monkeypatch):
monkeypatch.setattr(self.account, "get_cache_size", lambda: 10)
Expand All @@ -823,8 +823,8 @@ def test_add_friend(self, monkeypatch):
assert response is None

def test_remove_friend(self, monkeypatch):
monkeypatch.setattr(self.core.config, "save_friends", lambda x: None)
monkeypatch.setattr(self.core.config, "save_friends", lambda x: [])
monkeypatch.setattr(self.core.config, "load_friends", lambda: ['foo'])

response = self.core.remove_friend("foo")
assert response is None
assert response is 'foo'

0 comments on commit 3de626f

Please sign in to comment.