def test_has_extension(self): self.assertTrue(io.has_extension("/tmp/some-archive.tar.gz", ".tar.gz")) self.assertFalse(io.has_extension("/tmp/some-archive.tar.gz", ".gz")) self.assertTrue(io.has_extension("/tmp/text.txt", ".txt")) # no extension whatsoever self.assertFalse(io.has_extension("/tmp/README", "README"))
def to_dict(arg): if io.has_extension(arg, ".json"): with open(io.normalize_path(arg), mode="rt", encoding="utf-8") as f: return json.load(f) elif arg.startswith("{"): return json.loads(arg) else: return kv_to_map(csv_to_list(arg))
def to_dict(arg, default_parser=kv_to_map): if io.has_extension(arg, ".json"): with open(io.normalize_path(arg), mode="rt", encoding="utf-8") as f: return json.load(f) try: return json.loads(arg) except json.decoder.JSONDecodeError: return default_parser(csv_to_list(arg))
def _configured_plugins(self, variables=None): configured_plugins = [] # each directory is a plugin, each .ini is a config (just go one level deep) for entry in os.listdir(self.plugins_root_path): plugin_path = os.path.join(self.plugins_root_path, entry) if os.path.isdir(plugin_path): for child_entry in os.listdir(plugin_path): if os.path.isfile(os.path.join(plugin_path, child_entry)) and io.has_extension(child_entry, ".ini"): f, _ = io.splitext(child_entry) plugin_name = self._file_to_plugin_name(entry) config = io.basename(f) configured_plugins.append(PluginDescriptor(name=plugin_name, config=config, variables=variables)) return configured_plugins
def _configured_plugins(self): configured_plugins = [] # each directory is a plugin, each .ini is a config (just go one level deep) for entry in os.listdir(self.plugins_root_path): plugin_path = os.path.join(self.plugins_root_path, entry) if os.path.isdir(plugin_path): for child_entry in os.listdir(plugin_path): if os.path.isfile(os.path.join(plugin_path, child_entry)) and io.has_extension(child_entry, ".ini"): f, _ = io.splitext(child_entry) plugin_name = self._file_to_plugin_name(entry) config = io.basename(f) configured_plugins.append(PluginDescriptor(name=plugin_name, config=config)) return configured_plugins
def csv_to_list(csv): if csv is None: return None if io.has_extension(csv, ".json"): with open(io.normalize_path(csv), mode="rt", encoding="utf-8") as f: content = f.read() if not RE_JSON_ARRAY_START.match(content): raise ValueError(f"csv args only support arrays in json but you supplied [{csv}]") return json.loads(content) elif RE_JSON_ARRAY_START.match(csv): return json.loads(csv) elif len(csv.strip()) == 0: return [] else: return [e.strip() for e in csv.split(",")]
def __init__(self, track_path): if not os.path.exists(track_path): raise exceptions.SystemSetupError("Track path %s does not exist" % track_path) if os.path.isdir(track_path): self.track_name = io.basename(track_path) self._track_dir = track_path self._track_file = os.path.join(track_path, "track.json") if not os.path.exists(self._track_file): raise exceptions.SystemSetupError("Could not find track.json in %s" % track_path) elif os.path.isfile(track_path): if io.has_extension(track_path, ".json"): self._track_dir = io.dirname(track_path) self._track_file = track_path self.track_name = io.splitext(io.basename(track_path))[0] else: raise exceptions.SystemSetupError("%s has to be a JSON file" % track_path) else: raise exceptions.SystemSetupError("%s is neither a file nor a directory" % track_path)
def test_has_extension(self): assert io.has_extension("/tmp/some-archive.tar.gz", ".tar.gz") assert not io.has_extension("/tmp/some-archive.tar.gz", ".gz") assert io.has_extension("/tmp/text.txt", ".txt") # no extension whatsoever assert not io.has_extension("/tmp/README", "README")