import gzip

from io import StringIO

from .models.simple import ARPAModelSimple
from .parsers.quick import ARPAParserQuick


def dump(obj, fp):
    """Serialize obj to fp (a file-like object) in ARPA format."""
    obj.write(fp)


def dumpf(obj, path, mode='wt', encoding=None):
    """Serialize obj to path in ARPA format (.arpa, .gz)."""
    path = str(path)
    if path.endswith('.gz'):
        if (mode is None) or ('t' not in mode):
            raise ValueError('Compressed files must use text mode.')
        with gzip.open(path, mode=mode, encoding=encoding) as f:
            return dump(obj, f)
    else:
        with open(path, mode=mode, encoding=encoding) as f:
            dump(obj, f)


def dumps(obj):
    """Serialize obj to an ARPA formatted str."""
    with StringIO() as f:
        dump(obj, f)
        return f.getvalue()


def load(fp, model=None, parser=None):
    """Deserialize fp (a file-like object) to a Python object."""
    if not model:
        model = 'simple'
    if not parser:
        parser = 'quick'

    if model not in ['simple']:
        raise ValueError
    if parser not in ['quick']:
        raise ValueError

    if model == 'simple' and parser == 'quick':
        return ARPAParserQuick(ARPAModelSimple).parse(fp)
    else:
        raise ValueError


def loadf(path, mode='rt', encoding=None, model=None, parser=None):
    """Deserialize path (.arpa, .gz) to a Python object."""
    path = str(path)
    if path.endswith('.gz'):
        if (mode is None) or ('t' not in mode):
            raise ValueError('Compressed files must use text mode.')
        with gzip.open(path, mode=mode, encoding=encoding) as f:
            return load(f, model=model, parser=parser)
    else:
        with open(path, mode=mode, encoding=encoding) as f:
            return load(f, model=model, parser=parser)


def loads(s, model=None, parser=None):
    """Deserialize s (a str) to a Python object."""
    with StringIO(s) as f:
        return load(f, model=model, parser=parser)

@@ -249,19 +249,8 @@
 ath,
- mode='wt',
  enc
@@ -377,125 +377,8 @@
 '):%0A
-        if (mode is None) or ('t' not in mode):%0A            raise ValueError('Compressed files must use text mode.')%0A
     
@@ -399,36 +399,36 @@
 open(path, mode=
-mode
+'wt'
 , encoding=encod
@@ -501,36 +501,36 @@
 open(path, mode=
-mode
+'wt'
 , encoding=encod
@@ -1180,19 +1180,8 @@
 ath,
- mode='rt',
  enc
@@ -1332,125 +1332,8 @@
 '):%0A
-        if (mode is None) or ('t' not in mode):%0A            raise ValueError('Compressed files must use text mode.')%0A
     
@@ -1354,36 +1354,36 @@
 open(path, mode=
-mode
+'rt'
 , encoding=encod
@@ -1487,20 +1487,20 @@
 h, mode=
-mode
+'rt'
 , encodi

Remove mode parameter