Skip to content

Commit 3e3f475

Browse files
committed
added get_format method to FormatWidgetMixin
1 parent 67a8889 commit 3e3f475

2 files changed

Lines changed: 14 additions & 15 deletions

File tree

progressbar/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ def __cmp__(self, other): # pragma: no cover
1515

1616
class UnknownLength(six.with_metaclass(FalseMeta, object)):
1717
pass
18+
19+
20+
class Undefined(six.with_metaclass(FalseMeta, object)):
21+
pass

progressbar/widgets.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,19 @@ def __init__(self, format, new_style=False, **kwargs):
114114
self.new_style = new_style
115115
self.format = format
116116

117+
def get_format(self, progress, data, format=None):
118+
return format or self.format
119+
117120
def __call__(self, progress, data, format=None):
118121
'''Formats the widget into a string'''
122+
format = self.get_format(progress, data, format)
119123
try:
120124
if self.new_style:
121-
return (format or self.format).format(**data)
125+
return format.format(**data)
122126
else:
123-
return (format or self.format) % data
127+
return format % data
124128
except (TypeError, KeyError):
125-
print('Error while formatting %r' % self.format, file=sys.stderr)
129+
print('Error while formatting %r' % format, file=sys.stderr)
126130
pprint.pprint(data, stream=sys.stderr)
127131
raise
128132

@@ -633,17 +637,13 @@ def __init__(self, format='%(percentage)3d%%', na='N/A%%', **kwargs):
633637
FormatWidgetMixin.__init__(self, format=format, **kwargs)
634638
WidgetBase.__init__(self, format=format, **kwargs)
635639

636-
def __call__(self, progress, data, format=None):
637-
return FormatWidgetMixin.__call__(self, progress, data,
638-
self.get_format(progress, data))
639-
640640
def get_format(self, progress, data, format=None):
641641
# If percentage is not available, display N/A%
642-
if ('percentage' in data and not data['percentage'] and
643-
data['percentage'] != 0):
642+
percentage = data.get('percentage', base.Undefined)
643+
if not percentage and percentage != 0:
644644
return self.na
645645

646-
return format or self.format
646+
return FormatWidgetMixin.get_format(self, progress, data, format)
647647

648648

649649
class SimpleProgress(FormatWidgetMixin, WidgetBase):
@@ -934,11 +934,6 @@ def __init__(self, format='%(percentage)2d%%', na='N/A%%', **kwargs):
934934
Percentage.__init__(self, format, na=na, **kwargs)
935935
FormatLabelBar.__init__(self, format, **kwargs)
936936

937-
def __call__(self, progress, data, width, format=None):
938-
return FormatLabelBar.__call__(
939-
self, progress, data, width,
940-
format=Percentage.get_format(self, progress, data, format=None))
941-
942937

943938
class Variable(FormatWidgetMixin, VariableMixin, WidgetBase):
944939
'''Displays a custom variable.'''

0 commit comments

Comments
 (0)