Example #1
0
def test_scanf():
    r"""Test scanf."""
    num_codes = 'dieEfgGouxX'
    fmt_list = [("%g%+gj", complex(1)), ("%s", "hello"), ("%5s", "the"),
                ("%5s\t%ld\t%lf\t%lf%+lfj\n", ("one", 1, 1.0, complex(1, 1)))]
    fmt_list += [("%" + s, 1) for s in num_codes]
    fmt_list += [("%5.2" + s, 1) for s in num_codes]
    fmt_list += [("%+5.2" + s, 1) for s in num_codes]
    fmt_list += [("%-5.2" + s, 1) for s in num_codes]
    fmt_list += [("% 5.2" + s, 1) for s in num_codes]
    fmt_list += [("%05.2" + s, 1) for s in num_codes]
    for fmt, val in fmt_list:
        if isinstance(val, tuple):
            val_tup = val
        else:
            val_tup = (val, )
        new_tup = []
        for v in val_tup:
            if isinstance(v, complex):
                new_tup += [v.real, v.imag]
            else:
                new_tup.append(v)
        val_str = fmt % tuple(new_tup)
        res = scanf.scanf(fmt, val_str)
        assert_equal(res, val_tup)
        # Test bytes version
        res = backwards.scanf_bytes(fmt, backwards.as_bytes(val_str))
        assert_equal(
            res, backwards.as_bytes(val_tup, recurse=True, allow_pass=True))
        # Test unicode version
        res = backwards.scanf_bytes(fmt, backwards.as_unicode(val_str))
        assert_equal(
            res, backwards.as_unicode(val_tup, recurse=True, allow_pass=True))
    def normalize(cls, obj):
        r"""Normalize an object, if possible, to conform to this type.

        Args:
            obj (object): Object to normalize.

        Returns:
            object: Normalized object.

        """
        if cls.is_fixed and ('subtype' in cls.fixed_properties):
            if (cls.fixed_properties['subtype'] == 'bytes'):
                if isinstance(obj, backwards.string_types):
                    obj = backwards.as_bytes(obj)
                else:
                    obj = backwards.as_bytes(str(obj))
            elif (cls.fixed_properties['subtype'] == 'unicode'):
                if isinstance(obj, backwards.string_types):
                    obj = backwards.as_unicode(obj)
                else:
                    obj = backwards.as_unicode(str(obj))
            else:
                dtype = ScalarMetaschemaProperties._python_scalars[
                    cls.fixed_properties['subtype']][0]
                try:
                    obj = dtype(obj)
                except (TypeError, ValueError):
                    pass
        return obj
    def run_cmake(self, target=None):
        r"""Run the cmake command on the source.

        Args:
            target (str, optional): Target to build.

        Raises:
            RuntimeError: If there is an error in running cmake.
        
        """
        curdir = os.getcwd()
        os.chdir(self.sourcedir)
        if not os.path.isfile('CMakeLists.txt'):
            os.chdir(curdir)
            self.cleanup()
            raise IOError('No CMakeLists.txt file found in %s.' %
                          self.sourcedir)
        # Configuration
        if target != 'clean':
            config_cmd = ['cmake'] + self.cmakeargs
            config_cmd += ['-H.', self.sourcedir, '-B%s' % self.builddir]
            self.debug(' '.join(config_cmd))
            comp_process = tools.popen_nobuffer(config_cmd)
            output, err = comp_process.communicate()
            exit_code = comp_process.returncode
            if exit_code != 0:
                os.chdir(curdir)
                self.cleanup()
                self.error(backwards.as_unicode(output))
                raise RuntimeError("CMake config failed with code %d." %
                                   exit_code)
            self.debug('Config output: \n%s' % output)
        # Build
        build_cmd = ['cmake', '--build', self.builddir, '--clean-first']
        if self.target is not None:
            build_cmd += ['--target', self.target]
        self.info(' '.join(build_cmd))
        comp_process = tools.popen_nobuffer(build_cmd)
        output, err = comp_process.communicate()
        exit_code = comp_process.returncode
        if exit_code != 0:  # pragma: debug
            os.chdir(curdir)
            self.error(backwards.as_unicode(output))
            self.cleanup()
            raise RuntimeError("CMake build failed with code %d." % exit_code)
        self.debug('Build output: \n%s' % output)
        self.debug('Make complete')
        os.chdir(curdir)
Example #4
0
    def _send(self, msg):
        r"""Write message to a file.

        Args:
            msg (bytes, str): Data to write to the file.

        Returns:
            bool: Success or failure of writing to the file.

        """
        try:
            if msg != self.eof_msg:
                if not self.open_as_binary:
                    msg = backwards.as_unicode(msg)
                self.fd.write(msg)
                if self.append == 'ow':
                    self.fd.truncate()
            self.fd.flush()
        except (AttributeError, ValueError):  # pragma: debug
            if self.is_open:
                raise
            return False
        if msg != self.eof_msg and self.is_series:
            self.advance_in_series()
            self.debug("Advanced to %d", self._series_index)
        return True
def test_format_bytes():
    r"""Test formating of bytes string."""
    s0 = "%s, %s"
    ans = "one, one"
    arg0 = "one"
    args = (backwards.as_bytes(arg0), backwards.as_unicode(arg0))
    for cvt in [backwards.as_bytes, backwards.as_unicode]:
        res = backwards.format_bytes(cvt(s0), args)
        assert_equal(res, cvt(ans))
def test_as_unicode():
    r"""Test as_unicode."""
    # Just strings
    res = u'hello'
    vals = ['hello', b'hello', u'hello', bytearray('hello', 'utf-8')]
    for v in vals:
        backwards.assert_unicode(res)
        assert_equal(backwards.as_unicode(v), res)
    assert_raises(TypeError, backwards.as_unicode, 1)
    # Recursive
    valpha = 'abcdef'
    ralpha = [backwards.as_unicode(a) for a in valpha]
    vals_rec = [vals, tuple(vals), {k: v for k, v in zip(valpha, vals)}]
    resl = [res for v in vals]
    resl_rec = [resl, tuple(resl), {k: v for k, v in zip(ralpha, resl)}]
    for v, r in zip(vals_rec, resl_rec):
        assert_equal(backwards.as_unicode(v, recurse=True), r)
    # Integer
    assert_equal(backwards.as_unicode(int(1), convert_types=(int, )), u'1')
    assert_equal(backwards.as_unicode(int(1), allow_pass=True), int(1))
Example #7
0
def create_metaschema(overwrite=False):
    r"""Create the meta schema for validating ygg schema.

    Args:
        overwrite (bool, optional): If True, the existing meta schema will be
            overwritten. If False and the metaschema exists, an error will be
            raised. Defaults to False.

    Returns:
        dict: Meta schema specifying rules for ygg type schema. This includes
            all original JSON schema rules with the addition of types and
            property definitions.

    Raises:
        RuntimeError: If the file already exists and overwrite is False.

    """
    if (not overwrite) and os.path.isfile(_metaschema_fname):
        raise RuntimeError("Metaschema file already exists.")
    out = copy.deepcopy(_base_validator.META_SCHEMA)
    out['title'] = "Ygg meta-schema for data type schemas"
    # Lower versions have a differing draft4
    if _jsonschema_ver_maj < 3:
        for x in ['minItems', 'uniqueItems']:
            if x in out['properties']['enum']:
                del out['properties']['enum'][x]
    # TODO: Replace schema with a link to the metaschema in the documentation
    # del out['$schema']
    # Add properties
    for k, v in get_registered_properties().items():
        if v.schema is not None:
            assert (k not in out['properties'])
            out['properties'][k] = v.schema
    # Add types
    for k, v in sorted(get_registered_types().items()):
        if k not in out['definitions']['simpleTypes']['enum']:
            out['definitions']['simpleTypes']['enum'].append(k)
        for p in v.properties:
            assert (p in out['properties'])
    # Convert to unicode if python 2
    if backwards.PY2:  # pragma: Python 2
        out = backwards.as_unicode(out,
                                   recurse=True,
                                   convert_types=(str, ),
                                   allow_pass=True)
    # Print
    print('Created metaschema')
    pprint.pprint(out)
    # Save it to a file
    with open(_metaschema_fname, 'w') as fd:
        encode_json(out, fd)
    return out
Example #8
0
def print_encoded(msg, *args, **kwargs):
    r"""Print bytes to stdout, encoding if possible.

    Args:
        msg (str, bytes): Message to print.
        *args: Additional arguments are passed to print.
        **kwargs: Additional keyword arguments are passed to print.


    """
    try:
        print(backwards.as_unicode(msg), *args, **kwargs)
    except (UnicodeEncodeError, UnicodeDecodeError):  # pragma: debug
        logging.debug("sys.stdout.encoding = %s, cannot print unicode",
                      sys.stdout.encoding)
        kwargs.pop('end', None)
        try:
            print(msg, *args, **kwargs)
        except UnicodeEncodeError:  # pragma: debug
            print(backwards.as_bytes(msg), *args, **kwargs)