Example #1
0
 def testValidGermanDecimals(self):
     germanDataFormat = data.createDataFormat(data.FORMAT_CSV)
     germanDataFormat.set(data.KEY_DECIMAL_SEPARATOR, ",")
     germanDataFormat.set(data.KEY_THOUSANDS_SEPARATOR, ".")
     germanDecimalFieldformat = _createGermanDecimalFormat()
     self.assertEqual(decimal.Decimal("17.23"), germanDecimalFieldformat.validated("17,23"))
     self.assertEqual(decimal.Decimal("12345678"), germanDecimalFieldformat.validated("12.345.678"))
     self.assertEqual(decimal.Decimal("171234567.89"), germanDecimalFieldformat.validated("171.234.567,89"))
Example #2
0
 def testValidGermanDecimals(self):
     germanDataFormat = data.createDataFormat(data.FORMAT_CSV)
     germanDataFormat.set(data.KEY_DECIMAL_SEPARATOR, ",")
     germanDataFormat.set(data.KEY_THOUSANDS_SEPARATOR, ".")
     germanDecimalFieldformat = _createGermanDecimalFormat()
     self.assertEqual(decimal.Decimal("17.23"),
                      germanDecimalFieldformat.validated("17,23"))
     self.assertEqual(decimal.Decimal("12345678"),
                      germanDecimalFieldformat.validated("12.345.678"))
     self.assertEqual(decimal.Decimal("171234567.89"),
                      germanDecimalFieldformat.validated("171.234.567,89"))
Example #3
0
def createDataFormat(readable, **keywords):
    """
    Data format describing the contents of ``readable``, which should be a
    a raw binary input stream as returned by ``open(..., 'rb')``. Do not use
    ``codecs.open(...)`` because it returns Unicode strings instead of raw
    strings.

    Supported formats are delimited data (such as CSV), ODS and Excel.
    """
    assert readable is not None
    encoding = keywords.get("encoding", DEFAULT_ENCODING)
    assert encoding is not None

    icdHeader = readable.read(4)
    _log.debug(u"header=%r", icdHeader)
    if _tools.isEqualBytes(icdHeader, _ODS_HEADER):
        # Consider ICD to be ODS.
        dataFormatName = data.FORMAT_ODS
    else:
        icdHeader += readable.read(4)
        assert isinstance(icdHeader, str), u"icdHeader=%r but must be a string; use open(..., 'rb') instead of codecs.open()" % icdHeader
        assert isinstance(_EXCEL_HEADER, str), u"_EXCEL_HEADER=%r" % _EXCEL_HEADER
        if _tools.isEqualBytes(icdHeader, _EXCEL_HEADER):
            # Consider ICD to be Excel.
            dataFormatName = data.FORMAT_EXCEL
        else:
            # Consider ICD to be CSV.
            dataFormatName = data.FORMAT_DELIMITED
    result = data.createDataFormat(dataFormatName)
    if result.name == data.FORMAT_DELIMITED:
        readable.seek(0)
        options = delimitedOptions(readable, **keywords)
        for key, value in options.items():
            propertyName = _tools.decamelized(key)
            if key == _LINE_DELIMITER:
                value = _LINE_DELIMITER_TO_NAME_MAP[value]
            result.set(propertyName, value)
    readable.seek(0)
    return result
Example #4
0
def _createGermanDecimalFormat():
    germanFormat = data.createDataFormat(data.FORMAT_CSV)
    germanFormat.set(data.KEY_DECIMAL_SEPARATOR, ",")
    germanFormat.set(data.KEY_THOUSANDS_SEPARATOR, ".")
    result = fields.DecimalFieldFormat("x", False, None, "", germanFormat)
    return result
Example #5
0
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import decimal
import logging
import unittest

import data
import fields

_anyFormat = data.createDataFormat(data.FORMAT_CSV)
_fixedFormat = data.createDataFormat(data.FORMAT_FIXED)


def _createGermanDecimalFormat():
    germanFormat = data.createDataFormat(data.FORMAT_CSV)
    germanFormat.set(data.KEY_DECIMAL_SEPARATOR, ",")
    germanFormat.set(data.KEY_THOUSANDS_SEPARATOR, ".")
    result = fields.DecimalFieldFormat("x", False, None, "", germanFormat)
    return result


class AbstractFieldFormatTest(unittest.TestCase):
    """
    Test for base validation in `AbstractFieldFormatTest`.
    """
Example #6
0
def _createGermanDecimalFormat():
    germanFormat = data.createDataFormat(data.FORMAT_CSV)
    germanFormat.set(data.KEY_DECIMAL_SEPARATOR, ",")
    germanFormat.set(data.KEY_THOUSANDS_SEPARATOR, ".")
    result = fields.DecimalFieldFormat("x", False, None, "", germanFormat)
    return result
Example #7
0
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import decimal
import logging
import unittest

import data
import fields

_anyFormat = data.createDataFormat(data.FORMAT_CSV)
_fixedFormat = data.createDataFormat(data.FORMAT_FIXED)


def _createGermanDecimalFormat():
    germanFormat = data.createDataFormat(data.FORMAT_CSV)
    germanFormat.set(data.KEY_DECIMAL_SEPARATOR, ",")
    germanFormat.set(data.KEY_THOUSANDS_SEPARATOR, ".")
    result = fields.DecimalFieldFormat("x", False, None, "", germanFormat)
    return result


class AbstractFieldFormatTest(unittest.TestCase):
    """
    Test for base validation in `AbstractFieldFormatTest`.
    """
Example #8
0
 def testCreateDataFormat(self):
     for formatName in [data.FORMAT_CSV, data.FORMAT_DELIMITED, data.FORMAT_FIXED, data.FORMAT_ODS]:
         dataFormat = data.createDataFormat(formatName)
         self.assertTrue(dataFormat)
         self.assertTrue(dataFormat.__str__())
     self.assertRaises(data.DataFormatSyntaxError, data.createDataFormat, "no-such-data-format")