Best Python code snippet using slash
test__normalize_path.py
Source:test__normalize_path.py
...7# pylint: disable=protected-access8def test__path_not_absolute__throws():9 """Disallowing relative link paths to avoid potential script bugs from ambiguity"""10 with raises(exceptions.PathInvalidError, match='must be absolute'):11 _normalize_path('relative')12 with raises(exceptions.PathInvalidError, match='must be absolute'):13 _normalize_path('../relative')14 with raises(exceptions.PathInvalidError, match='must be absolute'):15 _normalize_path('./relative')16 with raises(exceptions.PathInvalidError, match='must be absolute'):17 _normalize_path('relative/path')18def test__path_with_backslash__throws():19 """Avoid xplat bugs by requiring forward slashes in client code"""20 with raises(exceptions.PathInvalidError, match='forward slashes only'):21 _normalize_path(r'\path')22 with raises(exceptions.PathInvalidError, match='forward slashes only'):23 _normalize_path(r'\path\to\thing')24def test__path_with_redundancy__is_collapsed():25 """Ensure unnecessary `./` and `path/../path` and `//` etc. are collapsed"""26 root = "C:" if platform.WINDOWS else ""27 assert _normalize_path(f'{root}/blah/.') == f'{root}/blah'28 assert _normalize_path(f'{root}/blah/../blah') == f'{root}/blah'29 assert _normalize_path(f'{root}/blah/./.././blah') == f'{root}/blah'30 assert _normalize_path(f'{root}/blah//foo') == f'{root}/blah/foo'31def test__path_with_tilde__is_expanded_to_home():32 """Ensure only leading ~ is expanded to user home, and with forward slashes"""33 home = os.path.expanduser('~').replace('\\', '/')34 assert _normalize_path('~') == home35 assert _normalize_path('~/path/to/thing.txt') == f'{home}/path/to/thing.txt'36 assert _normalize_path('~/path/to/~/thing.txt') == f'{home}/path/to/~/thing.txt'37def test__path_with_user_specific_tilde__throws():38 """Ensure that non-xplat-compatible ~foo style paths are not permitted"""39 with raises(exceptions.PathInvalidError, match='Tilde-based paths that select a user'):40 _normalize_path('~foo/path')41def test__path_with_macros__expands(monkeypatch):42 """Ensure basic macro expansion works"""43 monkeypatch.setenv('ABSOLUTE', f'{HOME}/path/to/something')44 monkeypatch.setenv('INNER', 'something-else')45 monkeypatch.setenv('RELATIVE', 'another/path')46 #|47 result1 = _normalize_path('$ABSOLUTE/and/$INNER/but/$RELATIVE/file.txt')48 result2 = _normalize_path('~/and/$RELATIVE/file.txt')49 result3 = _normalize_path('~/partial$INNER/$RELATIVE.txt~')50 with raises(exceptions.PathInvalidError, match='must be absolute'):51 result2 = _normalize_path("$INNER/file.txt")52 with raises(exceptions.PathInvalidError, match='must be absolute'):53 result2 = _normalize_path("$RELATIVE/~INNER.txt")54 #|55 assert result1 == f'{HOME}/path/to/something/and/something-else/but/another/path/file.txt'56 assert result2 == f'{HOME}/and/another/path/file.txt'57 assert result3 == f'{HOME}/partialsomething-else/another/path.txt~'58def test__path_with_abs_macro_in_middle__throws(monkeypatch):59 """Paths using an absolute path macro in the middle should throw"""60 monkeypatch.setenv('ABSOLUTE', f'{HOME}/path/to/something')61 #|62 with raises(exceptions.MacroExpansionError, match='absolute path.*not used from the start'):63 _normalize_path('/$ABSOLUTE/path')64 with raises(exceptions.MacroExpansionError, match='absolute path.*not used from the start'):65 _normalize_path('f{HOME}/$ABSOLUTE')66def test__path_with_invalid_macros__throws(monkeypatch):67 """Paths using invalid or empty macros should throw"""68 monkeypatch.delenv('MISSING', False)69 monkeypatch.setenv('EMPTY', '')70 #|71 with raises(exceptions.MacroExpansionError, match='Macro.*not found'):72 _normalize_path('$MISSING')73 with raises(exceptions.MacroExpansionError, match='Macro.*not found'):74 _normalize_path(f'{HOME}/path/to/$MISSING/file')75 with raises(exceptions.MacroExpansionError, match='Macro.*not found'):76 _normalize_path('$EMPTY')77 with raises(exceptions.MacroExpansionError, match='Macro.*not found'):78 _normalize_path(f'{HOME}/path/to/$EMPTY/file')79def test__path_with_escaped_dollar_sign__returns_unescaped(monkeypatch):80 """Can use a backslash to escape `$`"""81 monkeypatch.setenv('FOO', 'foo')82 monkeypatch.delenv('MISSING', False) # ensure no match by accident83 #|84 result = _normalize_path(r'~/$FOO/\$FOO/$/path/test$FOO/and\$FOO')85 #|86 assert result == f'{HOME}/foo/$FOO/$/path/testfoo/and$FOO'87if platform.WINDOWS: # this test makes no sense on posix (it will throw that the env var isn't found)88 def test__macro_expansion_with_case_mismatched_env__throws(monkeypatch):89 monkeypatch.setenv('FOO', 'foo')90 #|91 with raises(exceptions.MacroExpansionError, match='does not match case of actual env'):92 _normalize_path('~/$Foo/bar.txt')93if platform.POSIX: # this test is useless on windows (the second `setenv` will replace the first)94 def test__macro_expansion_with_multiple_icase_env_entries__throws(monkeypatch):95 monkeypatch.setenv('Foo', 'baz')96 monkeypatch.setenv('FOO', 'bar')97 #|98 with raises(exceptions.MacroExpansionError, match='has multiple case-insensitive env matches'):99 _normalize_path('~/$Foo/$FOO')100if __name__ == "__main__":...
abfs_util.py
Source:abfs_util.py
...31 stdout, stderr = process.communicate()32 status = process.returncode33 return (status, stdout, stderr)34 def create_file(self, path, file_data, overwrite=True):35 fixed_path = self._normalize_path(path)36 if not overwrite and self.exists(fixed_path): return False37 f = tempfile.NamedTemporaryFile(delete=False)38 tmp_path = f.name39 f.write(file_data)40 f.close()41 (status, stdout, stderr) = \42 self._hadoop_fs_shell(['-put', tmp_path, fixed_path])43 return status == 044 def make_dir(self, path, permission=None):45 fixed_path = self._normalize_path(path)46 self._hadoop_fs_shell(['-mkdir', '-p', fixed_path])47 return True48 def copy(self, src, dst):49 fixed_src = self._normalize_path(src)50 fixed_dst = self._normalize_path(dst)51 (status, stdout, stderr) = \52 self._hadoop_fs_shell(['-cp', fixed_src, fixed_dst])53 assert status == 0, \54 'ABFS copy failed: ' + stderr + "; " + stdout55 assert self.exists(dst), \56 'ABFS copy failed: Destination file {dst} does not exist'\57 .format(dst=dst)58 def _inner_ls(self, path):59 fixed_path = self._normalize_path(path)60 (status, stdout, stderr) = self._hadoop_fs_shell(['-ls', fixed_path])61 # Trim the "Found X items" line and trailing new-line62 entries = stdout.split("\n")[1:-1]63 files = []64 for entry in entries:65 fields = re.split(" +", entry)66 files.append({67 'name': fields[7],68 'length': int(fields[4]),69 'mode': fields[0]70 })71 return files72 def ls(self, path):73 fixed_path = self._normalize_path(path)74 files = []75 for f in self._inner_ls(fixed_path):76 fname = f['name'].split("/")[-1]77 if not fname == '':78 files += [fname]79 return files80 def exists(self, path):81 fixed_path = self._normalize_path(path)82 (status, stdout, stderr) = self._hadoop_fs_shell(['-test', '-e', fixed_path])83 return status == 084 def delete_file_dir(self, path, recursive=False):85 fixed_path = self._normalize_path(path)86 rm_command = ['-rm', fixed_path]87 if recursive:88 rm_command = ['-rm', '-r', fixed_path]89 (status, stdout, stderr) = self._hadoop_fs_shell(rm_command)90 return status == 091 def get_all_file_sizes(self, path):92 """Returns a list of integers which are all the file sizes of files found93 under 'path'."""94 fixed_path = self._normalize_path(path)95 return [f['length'] for f in96 self._inner_ls(fixed_path) if f['mode'][0] == "-"]97 def _normalize_path(self, path):98 # Paths passed in may lack a leading slash...
local.py
Source:local.py
...16 storage_options: key-value17 May be credentials, or other configuration specific to the backend.18 """19 self.cwd = os.getcwd()20 def _normalize_path(self, path):21 """Ensure paths are absolute and normalized"""22 if not os.path.isabs(path):23 return os.path.join(self.cwd, path)24 return os.path.normpath(path)25 def glob(self, path):26 """For a template path, return matching files"""27 try:28 return sorted(glob(self._normalize_path(path), recursive=True))29 except TypeError: # recursive kwarg is new in Python 3.530 return sorted(glob(self._normalize_path(path)))31 def mkdirs(self, path):32 """Make any intermediate directories to make path writable"""33 path = self._normalize_path(path)34 try:35 os.makedirs(path)36 except OSError:37 assert os.path.isdir(path)38 def open(self, path, mode="rb", **kwargs):39 """Make a file-like object40 Parameters41 ----------42 mode: string43 normally "rb", "wb" or "ab" or other.44 kwargs: key-value45 Any other parameters, such as buffer size. May be better to set46 these on the filesystem instance, to apply to all files created by47 it. Not used for local.48 """49 return open(self._normalize_path(path), mode=mode)50 def ukey(self, path):51 """Unique identifier, so we can tell if a file changed"""52 path = self._normalize_path(path)53 return tokenize(path, os.stat(path).st_mtime)54 def size(self, path):55 """Size in bytes of the file at path"""56 return os.stat(self._normalize_path(path)).st_size57 def _get_pyarrow_filesystem(self):58 """Get an equivalent pyarrow filesystem"""59 import pyarrow as pa60 return pa.filesystem.LocalFileSystem.get_instance()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!