def test_non_ascii(): from clldutils.path import Path, path_component, as_unicode assert path_component(b'abc') == 'abc' p = Path(path_component('äöü')).joinpath(path_component('äöü')) assert isinstance(as_unicode(p), text_type) assert isinstance(as_unicode(p.name), text_type)
def __call__(self, args): opargs, opkw = _args_kw(args.name) readargs, readkw = _args_kw(args.object) oname, if_, input_ = readargs input_ = text_type(input_) if Path(path_component(input_)).exists(): # We heuristically interpret the input as filename, if a file with that name # exists. input_ = Path(path_component(input_)) res = run( opargs[0], read(oname, getattr(interfaces, if_), input_, **readkw), **opkw) p = jsonlib.dump(res, outdir=Path(args.output)) print('Result written to <%s>' % as_unicode(p)) return p
def _str_path(path, mkdir=False): """Get a file-system path as text_type, suitable for passing into io.open. Parameters ---------- path : {text_type, Path} A fs path either as Path instance or as text_type. mkdir : bool (default=False) If True, create the directories within the path. Returns ------- path : text_type The path as text_type. """ res = Path(path_component(path)) if mkdir and res.parent and not res.parent.exists(): res.parent.mkdir(parents=True) return res.as_posix()
def _str_path(path, mkdir=False): """Get a file-system path as text_type, suitable for passing into io.open. Parameters ---------- path : {text_type, Path} A fs path either as Path instance or as text_type. mkdir : bool (default=False) If True, create the directories within the path. Returns ------- path : text_type The path as text_type. """ res = Path(path_component(path)) if mkdir and res.parent and not res.parent.exists(): res.parent.mkdir(parents=True) return res.as_posix()
def write(name, obj, _outdir=None, _stem=None, **kw): adapter = get_adapter(obj, interfaces.IWriter, name=name) outfile = _outdir.joinpath(path_component('{0}.{1}'.format(_stem, name))) adapter.write(outfile, **kw) file_written(outfile) return outfile
def test_non_ascii(self): from clldutils.path import Path, path_component, as_unicode p = Path(path_component('äöü')).joinpath(path_component('äöü')) self.assertIsInstance(as_unicode(p), text_type) self.assertIsInstance(as_unicode(p.name), text_type)
def path_from_checksum(checksum, outdir=None): return (outdir or Path('.')).joinpath(path_component('{0}.json'.format(checksum)))
def _path(self, key): return self._dir.joinpath(path_component(key))