Esempio n. 1
0
    def runyamlfile(self, fn):
        """Read a YAML test case file and process each test case it defines.
        """

        print("Process YAML file: %s" % os.path.basename(fn))

        with _open(fn, encoding="utf-8") as fp:
            testcases = yaml.load(fp)
            for testcase in testcases:
                self.runtestcase(testcase)
def open(file, mode="r"):
    try:
        if os.path.exists(file):
            encoding = get_file_encoding(file)
        else:
            encoding = "utf-8"
        fd = _open(file, mode, encoding=encoding)
        yield fd
    finally:
        fd.close()
Esempio n. 3
0
def open_file(path, mode=None, encoding=None, **kwargs):
    """
    Helper function to open a file for reading/writing.

    :param path: path of file to read.
    :param mode: "b" for bytes or "t" for text (default is "t")
    :param encoding: file encoding for text (default is `utf-8`).
    :returns: stream object for reading/writing
    """
    try:
        from io import open as _open
    except ImportError:
        def _open(path, mode=None, encoding=None, **kwargs):
            return codecs.open(path, mode, encoding=encoding, **kwargs)
    return _open(path, mode=mode, encoding=encoding, **kwargs)
Esempio n. 4
0
def open(name, *, flags=MFD_CLOEXEC, mode='wb+', buffering=0, closefd=True,
         **kwargs):
    """Open a new memfd

    By default the new memfd is CLOEXEC, binary, open for unbuffered reading
    and writing.
    """
    fd = memfd_create(name, flags)
    try:
        f = _open(fd, mode=mode, buffering=buffering, closefd=True, **kwargs)
    except Exception:
        if not closefd:
            _close(fd)
        raise
    f.name = 'memfd:{}'.format(name)
    return f
Esempio n. 5
0
def get_file_encoding(file, default_encoding="utf-8"):
    try:
        import chardet
    except:
        return default_encoding

    try:
        with _open(file, "rb") as fd:
            cur_encoding = chardet.detect(fd.read())["encoding"]
            if cur_encoding == "GB2312":
                cur_encoding = "GBK"
    except:
        print("Problem detecting encoding of " + file + ".  Defaulting to " +
              default_encoding)
        cur_encoding = default_encoding

    return cur_encoding
Esempio n. 6
0
def open_file(path, mode=None, encoding=None, **kwargs):
    """
    Helper function to open a file for reading/writing.

    :param path: path of file to read.
    :param mode: "b" for bytes or "t" for text (default is "t")
    :param encoding: file encoding for text (default is `utf-8`).
    :returns: stream object for reading/writing
    """
    try:
        from io import open as _open
    except ImportError:

        def _open(path, mode=None, encoding=None, **kwargs):
            return codecs.open(path, mode, encoding=encoding, **kwargs)

    return _open(path, mode=mode, encoding=encoding, **kwargs)
Esempio n. 7
0
    def edit_coupling_parameters(cls, ypar, couplings):

        cpl_data = cls._get_couplings_data()
        cpar = Channel.tot_npts

        for cp in couplings:
            new_item = []
            if cp.model == cls.models[1] or cp.model == cls.models[2]:
                units = cp.yunits
                for item in cpl_data[cp.label]:
                    sitem = item.split()
                    if len(sitem) >= 3:
                        new_item.append(
                            f'{float(sitem[0]):10.12f}'
                            f'{float(ypar[cpar]/units):24.14f}'
                            f'{int(sitem[2]):7d}'
                            f'{float(sitem[3]):14.2e}'
                            f'{float(sitem[4]):14.1e}'.lstrip()
                        )
                    else:
                        new_item.append(
                            f'{float(sitem[0]):10.12f}'
                            f'{float(ypar[cpar]/units):24.14f}'
                            f'{int(sitem[2]):7d}'
                        )
                    cpar += 1
            else:
                for item in cpl_data[cp.label]:
                    sitem = item.split()
                    if len(sitem) >= 2:
                        new_item.append(
                            f'{float(ypar[cpar]):18.12f}'
                            f'{int(sitem[1]):3d}'
                            f'{float(sitem[2]):10.1}'.lstrip()
                        )
                    else:
                        new_item.append(
                            f'{float(ypar[cpar]):18.12f}'
                            f'{int(sitem[1]):3d}'.lstrip()
                        )
                    cpar += 1
            cpl_data[cp.label] = new_item

        with _open(cls.cpl_file, 'w', encoding='utf8') as stream:
            yaml.dump(cpl_data, stream=stream)
Esempio n. 8
0
def open(name,
         *,
         flags=MFD_CLOEXEC,
         mode='wb+',
         buffering=0,
         closefd=True,
         **kwargs):
    """Open a new memfd

    By default the new memfd is CLOEXEC, binary, open for unbuffered reading
    and writing.
    """
    fd = memfd_create(name, flags)
    try:
        f = _open(fd, mode=mode, buffering=buffering, closefd=True, **kwargs)
    except Exception:
        if not closefd:
            _close(fd)
        raise
    f.name = 'memfd:{}'.format(name)
    return f
Esempio n. 9
0
def open(grammar_name):
    return _open(os.path.join(os.path.split(__file__)[0], grammar_name))
Esempio n. 10
0
def open(path):
    if path is None: return
    return _open(path, "rb")
Esempio n. 11
0
def open(grammar_name):
    return _open( os.path.join(os.path.dirname(__file__), grammar_name) )
Esempio n. 12
0
 def loadYamlFile(self):
     """Load any created yaml file"""
     self.closeYamlFile()
     with _open(self.testyamlfile, encoding="utf-8") as fp:
         testyaml = yaml.load(fp)
     return testyaml
Esempio n. 13
0
def read(path):
    with _open(path, encoding='utf-8', mode='rt') as fp:
        return loads(fp.read())
Esempio n. 14
0
def open(name, mode='r'):
    return _open(name, mode, encoding='utf-8', newline=linesep)