def test_save(self) -> None: with self.subTest(format='rfc'): filepath = env.get_temp_file(suffix='.csv') csv.save( # type: ignore filepath, header=self.rfc_header, values=self.values, comment=self.comment, delimiter=self.rfc_sep) with csv.File(filepath) as file: self.assertEqual(file.delimiter, self.rfc_sep) self.assertEqual(file.header, self.rfc_header) self.assertEqual(file.comment, self.comment) self.assertEqual(file.hformat, csv.CSV_HFORMAT_RFC4180) self.assertEqual(file.namecol, None) self.assertEqual(file.read(), self.values) filepath.unlink() with self.subTest(format='rlang'): filepath = env.get_temp_file(suffix='.tsv') csv.save( # type: ignore filepath, header=self.rlang_header, values=self.values, comment=self.comment, delimiter=self.rlang_sep) with csv.File(filepath) as file: self.assertEqual(file.delimiter, self.rlang_sep) self.assertEqual(list(file.header)[1:], list(self.rlang_header)) self.assertEqual(file.comment, self.comment) self.assertEqual(file.hformat, csv.CSV_HFORMAT_RLANG) self.assertEqual(file.namecol, file.header[0]) self.assertEqual(file.read(), self.values) filepath.unlink()
def test_openx(self) -> None: filepath = env.get_temp_file(suffix='.txt') with self.subTest(file=filepath): with plain.openx(filepath, mode='w') as fh: fh.write(self.text) if filepath.is_file(): with plain.openx(filepath, mode='r') as fh: text = fh.read() filepath.unlink() self.assertTrue(text == self.text) file = filepath.open(mode='w') with self.subTest(file=file): with plain.openx(file, mode='w') as fh: fh.write(self.text) if not file.closed: file.close() file = filepath.open(mode='r') with plain.openx(file, mode='r') as fh: text = fh.read() if not file.closed: file.close() self.assertTrue(text == self.text) if not file.closed: file.close() if filepath.is_file(): filepath.unlink()
def setUp(self) -> None: path = env.get_temp_file() self.rfc_path = path.with_suffix('.csv') self.rfc_header = ('name', 'id', 'value') self.rfc_sep = ',' self.rlang_path = path.with_suffix('.tsv') self.rlang_header = tuple(list(self.rfc_header)[1:]) self.rlang_sep = '\t' self.comment = '-*- coding: utf-8 -*-' self.values = [('r1', 1, 1.), ('r2', 2, 2.), ('r3', 3, 3.)] self.rownames = [col[0] for col in self.values] # Manually Write RFC compliant CSV-File with self.rfc_path.open(mode='w') as file: # Write Comment file.writelines([f"# {self.comment}\n\n"]) # Write Header file.writelines([self.rfc_sep.join(self.rfc_header) + '\n']) # Write Data for row in self.values: strrow = [str(token) for token in row] file.writelines([self.rfc_sep.join(strrow) + '\n']) # Manually Write R Language compliant TSV-File with self.rlang_path.open(mode='w') as file: # Write Comment file.writelines([f"# {self.comment}\n\n"]) # Write Header file.writelines([self.rlang_sep.join(self.rlang_header) + '\n']) # Write Data for row in self.values: strrow = [str(token) for token in row] file.writelines([self.rlang_sep.join(strrow) + '\n'])
def test_get_temp_file(self) -> None: path = env.get_temp_file() self.assertFalse(path.exists()) path.touch() self.assertTrue(path.is_file()) path.unlink() self.assertFalse(path.exists())
def setUp(self) -> None: self.filepath = env.get_temp_file(suffix='.ini') self.comment = '-*- coding: utf-8 -*-' self.obj = { 'n': { 'a': 's', 'b': True, 'c': 1 }, 'l1': { 'a': 1 }, 'l2': { 'a': 2 } } self.scheme = { 'n': { 'a': str, 'b': bool, 'c': int }, 'l[0-9]*': { 'a': int } } self.text = ("# -*- coding: utf-8 -*-\n\n" "[n]\na = s\nb = True\nc = 1\n\n" "[l1]\na = 1\n\n[l2]\na = 2\n\n") ini.save(self.obj, self.filepath, comment=self.comment)
def _locate_logfile( self, filepath: PathLike = _default_file) -> OptPath: # Get valid logfile from filepath if isinstance(filepath, (str, Path)): logfile = env.expand(filepath) if env.touch(logfile): return logfile # Get temporary logfile logfile = env.get_temp_file(suffix='log') if env.touch(logfile): warnings.warn( f"logfile '{filepath}' is not valid: " f"using temporary logfile '{logfile}'") return logfile return None
def __init__(self, file: FileRef, mode: str = 'rw') -> None: """Initialize temporary file.""" super().__init__() self._mode = mode self._children = [] self._connected = False # Create temporary file self._path = env.get_temp_file() self._path.touch() # Connect self.connect(file) # Copy referenced file object to temporary file if 'r' in self._mode: self.pull()
def test_openx(self) -> None: filepath = env.get_temp_file(suffix='.gz') with self.subTest(file=filepath): with raw.openx(filepath, mode='w') as fh: fh.write(self.data) if filepath.is_file(): with raw.openx(filepath, mode='r') as fh: data = fh.read() filepath.unlink() self.assertTrue(data == self.data) file = filepath.open(mode='wb') with self.subTest(file=file): with raw.openx(file, mode='w') as fh: fh.write(self.data) if not file.closed: file.close() file = filepath.open(mode='rb') with raw.openx(file, mode='r') as fh: data = fh.read() if not file.closed: file.close() self.assertTrue(data == self.data)
def setUp(self) -> None: self.filepath = env.get_temp_file(suffix='.gz') self.data = b'eJxrYK4tZDoiGBkGT0ZqotZJzt3/AbFpXoAgyI==' raw.save(self.data, self.filepath)
def setUp(self) -> None: self.filepath = env.get_temp_file(suffix='.txt') self.comment = "comment line" self.content = ['first content line', 'second content line'] self.text = f"# {self.comment}\n\n" + '\n'.join(self.content) plain.save(self.text, self.filepath)