# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8

__all__ = ('IntegerF',)

import re

from ..base import isnum
from ..uio import _s, _u

from .strings import UnicodeF

class IntegerF(UnicodeF):
    RE_INTEGER = re.compile(r'(?:-)?\d+$')

    def parse(self, s):
        if isnum(s): return s

        s = UnicodeF.parse(self, s)
        if s is None: return None

        if self.RE_INTEGER.match(s) is None:
            raise ValueError("Invalid number: %s" % _s(s))

        return int(s)

    def format(self, i):
        if i is None: return u""
        else: return _u(i)
