From cba2d4d450f58e435d7001af3444578786e10aca Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 10 Apr 2014 12:57:57 +0200 Subject: [PATCH] Basic validation for zoom values --- qutebrowser/config/configdata.py | 7 +++-- qutebrowser/config/conftypes.py | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index be62e3b00..737cbf643 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -126,12 +126,13 @@ def configdata(): "entered."), ('zoomlevels', - SettingValue(types.IntList, "25,33,50,67,75,90,100,110,125,150," - "175,200,250,300,400,500"), + SettingValue(types.PercList, "25%,33%,50%,67%,75%,90%,100%,110%," + "125%,150%,175%,200%,250%,300%,400%," + "500%"), "The available zoom levels, separated by commas."), ('defaultzoom', - SettingValue(types.Int, "100"), + SettingValue(types.ZoomPerc, "100%"), "The default zoom level."), )), diff --git a/qutebrowser/config/conftypes.py b/qutebrowser/config/conftypes.py index 27f60952c..eafd92992 100644 --- a/qutebrowser/config/conftypes.py +++ b/qutebrowser/config/conftypes.py @@ -234,6 +234,53 @@ class IntList(List): raise ValidationError(value, "must be a list of integers!") +class Perc(BaseType): + + """Percentage which may be >100 but needs to be positive.""" + + def transform(self, value): + return int(value.rstrip('%')) + + def validate(self, value): + if not value.endswith('%'): + raise ValidationError(value, "does not end with %") + try: + intval = int(value.rstrip('%')) + except ValueError: + raise ValidationError(value, "invalid percentage!") + else: + if not 0 <= intval: + raise ValidationError(value, "percentage needs to be >= 0!") + + +class PercList(List): + + """Base class for a list of percentages.""" + + typestr = 'perc-list' + + def transform(self, value): + vals = super().transform(value) + return [int(val.rstrip('%')) for val in vals] + + def validate(self, value): + vals = super().transform(value) + try: + for val in vals: + Perc.validate(self, val) + except ValidationError: + raise ValidationError(value, "must be a list of percentages!") + + +class ZoomPerc(Perc): + + """A percentage which needs to be in the current zoom percentages.""" + + def validate(self, value): + super().validate(value) + # FIXME we should validate the percentage is in the list here. + + class PercOrInt(BaseType): """Percentage or integer."""