Skip to content

Commit

Permalink
Allow unlimited description and annotation lengths
Browse files Browse the repository at this point in the history
This is alternative proposal to #436.

The advantage to subclassing ConfigParser rather than using
allow_no_value is that we don't have to worry about handling None for
other value types.
  • Loading branch information
ryneeverett committed Jan 29, 2017
1 parent 7ff491e commit 834d568
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
18 changes: 17 additions & 1 deletion bugwarrior/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def get_config_path():


def load_config(main_section, interactive=False):
config = ConfigParser({'log.level': "INFO", 'log.file': None})
config = BugwarriorConfigParser({'log.level': "INFO", 'log.file': None})
path = get_config_path()
config.readfp(codecs.open(path, "r", "utf-8",))
config.interactive = interactive
Expand Down Expand Up @@ -248,5 +248,21 @@ def get_data_path(config, main_section):

return os.path.normpath(os.path.expanduser(data_path))


# ConfigParser is not a new-style class, so inherit from object to fix super().
class BugwarriorConfigParser(ConfigParser, object):
def getint(self, section, option):
""" Accepts both integers and empty values. """
try:
return super(BugwarriorConfigParser, self).getint(section, option)
except ValueError:
if self.get(section, option) is u'':
return None
else:
raise ValueError(
"{section}.{option} must be an integer or empty.".format(
section=section, option=option))


# This needs to be imported here and not above to avoid a circular-import.
from bugwarrior.services import get_service
12 changes: 6 additions & 6 deletions bugwarrior/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,12 @@ def build_annotations(self, annotations, url):
if not message or not author:
continue
message = message.replace('\n', '').replace('\r', '')
final.append(
'@%s - %s%s' % (
author,
message[0:self.anno_len],
if self.anno_len:
message = '%s%s' % (
message[:self.anno_len],
'...' if len(message) > self.anno_len else ''
)
)
final.append('@%s - %s' % (author, message))
return final

@classmethod
Expand Down Expand Up @@ -393,11 +392,12 @@ def build_default_description(
}
url_separator = ' .. '
url = url if self.origin['inline_links'] else ''
desc_len = self.origin['description_length']
return u"%s%s#%s - %s%s%s" % (
MARKUP,
cls_markup[cls],
number,
title[:self.origin['description_length']],
title[:desc_len] if desc_len else title,
url_separator if url else '',
url,
)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,23 @@ class TestOracleEval(TestCase):

def test_echo(self):
self.assertEqual(config.oracle_eval("echo fööbår"), "fööbår")


class TestBugwarriorConfigParser(TestCase):
def setUp(self):
self.config = config.BugwarriorConfigParser()
self.config.add_section('general')
self.config.set('general', 'someint', '4')
self.config.set('general', 'somenone', '')
self.config.set('general', 'somechar', 'somestring')


def test_getint(self):
self.assertEqual(self.config.getint('general', 'someint'), 4)

def test_getint_none(self):
self.assertEqual(self.config.getint('general', 'somenone'), None)

def test_getint_valueerror(self):
with self.assertRaises(ValueError):
self.config.getint('general', 'somechar')

0 comments on commit 834d568

Please sign in to comment.