def find_addons(*dirs): """ Search given directory for addons. A folder is considered to be an addon, if it contains an 'addon.xml' file. :param dirs: A list of directorys to scan. """ filename = safe_path("addon.xml") for path in dirs: path = safe_path(path) for item in os.listdir(path): plugin_file = os.path.join(path, item, filename) if os.path.exists(plugin_file): yield ensure_unicode(plugin_file)
def __init__(self, plugin_path, plugin_profile): super(Settings, self).__init__() # Populate settings from the addon source settings file settings_path = safe_path( os.path.join(plugin_path, "resources", "settings.xml")) if os.path.exists(settings_path): xmldata = ETree.parse(settings_path).getroot() self._extractor(xmldata) # Populate settings from the addon saved profile settings file self._settings_path = settings_path = safe_path( os.path.join(plugin_profile, "settings.xml")) if os.path.exists(settings_path): xmldata = ETree.parse(settings_path).getroot() self._extractor(xmldata)
def copy(source, destination): """ Copy file to destination, returns true/false. :param str source: string - file to copy. :param str destination: string - destination file :returns: True if successed :rtype: bool Example:: success = xbmcvfs.copy(source, destination) """ try: shutil.copyfile(safe_path(source), safe_path(destination)) except shutil.Error: return False else: return True
def __init__(self): self.repo_url = "http://mirrors.kodi.tv/addons/{}/{}".format( self.repo, "{}") self._package_dir = kodi_paths["packages"] self._addon_dir = kodi_paths["addons"] self.db = {} # Check if an update is scheduled self.update_file = safe_path( os.path.join(kodi_paths["temp"], u"update_check")) if self.update_required(): self.update()
def rename(file, newFile): """ Rename a file :param str file: string - File to rename :param str newFile: string - New filename, including the full path :returns: True if successed :rtype: bool .. note:: Moving files between different filesystem (eg. local to nfs://) is not possible on all platforms. You may have to do it manually by using the copy and deleteFile functions. Example:: success = xbmcvfs.rename(file,newFileName) """ try: os.rename(safe_path(file), safe_path(newFile)) except EnvironmentError: return False else: return True
def changelog(self): data = self._metadata.findall("news") if data is not None: return data.text else: changelog_file = safe_path( os.path.join(self.path, u"changelog-{}.txt".format(self.version))) if os.path.exists(changelog_file): with _open(changelog_file, "r", "utf8") as stream: return stream.read() else: return ""
def exists(path): """ Check for a file or folder existence :param str path: string - file or folder :returns: True if successed :rtype: bool Example:: success = xbmcvfs.exists(path) """ return os.path.exists(safe_path(path))
def _search_strings(self, resources_path): # Possible locations for english strings.po string_loc = [ os.path.join(resources_path, "strings.po"), os.path.join(resources_path, "language", "English", "strings.po"), os.path.join(resources_path, "language", "resource.language.en_gb", "strings.po"), os.path.join(resources_path, "language", "resource.language.en_us", "strings.po") ] # Return the first strings.po file that is found for path in string_loc: path = safe_path(path) if os.path.exists(path): return self._extractor(path) # Unable to find a strings.po file # Search for any strings.po file strtext = safe_path("strings.po") for root, _, files in os.walk( safe_path(os.path.join(resources_path, "language"))): if strtext in files: return self._extractor(os.path.join(root, strtext))
def deleteFile(file): """ Delete the file :param str file: string - file to delete :returns: True if successed :rtype: bool Example:: success = xbmcvfs.deleteFile(file) """ try: os.remove(safe_path(file)) except EnvironmentError: return False else: return True
def rmdir(path): """ Remove a folder. :param str path: Folder to remove :returns: True if successed :rtype: bool Example:: success = xbmcvfs.rmdir(path) """ try: os.rmdir(safe_path(path)) except EnvironmentError: return False else: return True
def mkdirs(path): """ Make all directories along the path Create folder(s) - it will create all folders in the path. :param str path: olders to create. :returns: True if successed :rtype: bool example:: success = xbmcvfs.mkdirs(path) """ try: os.makedirs(safe_path(path)) except EnvironmentError: return False else: return True
def setup_paths(): # Location of support files system_dir = os.path.join( ensure_unicode(os.path.dirname(__file__), sys.getfilesystemencoding()), u"data") kodi_paths["support"] = system_dir # Kodi path structure kodi_paths["home"] = home = appdirs.user_cache_dir(u"kodi_mock") kodi_paths["addons"] = addon_dir = os.path.join(home, u"addons") kodi_paths["packages"] = os.path.join(addon_dir, u"packages") kodi_paths["temp"] = temp_dir = os.path.join(home, u"temp") kodi_paths["system"] = os.path.join(home, u"system") kodi_paths["profile"] = userdata = os.path.join(home, u"userdata") kodi_paths["data"] = os.path.join(userdata, u"addon_data") kodi_paths["database"] = os.path.join(userdata, u"Database") kodi_paths["thumbnails"] = os.path.join(userdata, u"Thumbnails") kodi_paths["playlists"] = playlists = os.path.join(userdata, u"playlists") kodi_paths["musicplaylists"] = os.path.join(playlists, u"music") kodi_paths["videoplaylists"] = os.path.join(playlists, u"video") # Ensure that all directories exists for path in kodi_paths.values(): path = safe_path(path) if not os.path.exists(path): os.makedirs(path) # Rest of kodi's special paths kodi_paths["logpath"] = os.path.join(temp_dir, u"kodi.log") kodi_paths["masterprofile"] = userdata kodi_paths["masterprofile"] = userdata kodi_paths["userdata"] = userdata kodi_paths["subtitles"] = temp_dir kodi_paths["recordings"] = temp_dir kodi_paths["screenshots"] = temp_dir kodi_paths["cdrips"] = temp_dir kodi_paths["skin"] = temp_dir kodi_paths["xbmc"] = home # Return the support system directory and addon directory return system_dir, addon_dir
def download(self, addon): """ Download any requred addon :param Addon addon: The addon to download """ filename = u"{0}-{1}.zip".format(addon.id, addon.version) tmp = os.path.join(self._package_dir, filename) logger.info("Downloading: '{}'".format(filename.encode("utf8"))) # Remove old zipfile before download # This will prevent an error if the addon was manually removed by user if os.path.exists(tmp): os.remove(tmp) # Request the addon zipfile from server url_part = "{0}/{1}".format(addon.id, filename) url = self.repo_url.format(url_part) resp = self._session.get(url) # Read and save contents of zipfile to package directory with _open(tmp, "wb") as stream: for chunk in resp.iter_content(decode_unicode=False): stream.write(chunk) # Remove the old plugin directory if exists # This is needed when updating addons udst = os.path.join(self._addon_dir, addon.id) sdst = safe_path(udst) if os.path.exists(sdst): shutil.rmtree(sdst) resp.close() self.extract_zip(tmp) addon.path = udst addon.preload() avail_addons[addon.id] = addon
def main(): # Parse the cli arguments args = parser.parse_args(sys.argv[1:]) # Enable debug logging if logging flag was given if args.debug: logger.setLevel(logging.DEBUG) # Convert any preselection into a list of selections preselect = list(map( int, args.preselect.split(","))) if args.preselect else None # Set the repo to use for dependency resolving Repo.repo = args.repo # Execute the addon in interactive mode plugin_path = os.path.realpath(decode_arg(args.addonpath)) arguments = [plugin_path, preselect] if args.content_type: arguments.append(args.content_type) # Check if plugin actually exists if os.path.exists(safe_path(plugin_path)): interactive(*arguments, compact_mode=args.compact, no_crop=args.no_crop) # Check if we are already in the requested plugin directory if pluginpath was a plugin id elif args.pluginpath.startswith("plugin.") and os.path.basename( os.getcwd()) == args.pluginpath: arguments[0] = ensure_unicode(os.getcwd(), sys.getfilesystemencoding()) interactive(*arguments, compact_mode=args.compact, no_crop=args.no_crop) else: raise RuntimeError("unable to find requested add-on: {}".format( plugin_path.encode("utf8")))
def listdir(path): """ Lists content of a folder. :param str path: Folder to get list from :returns: Directory content list :rtype: tuple example:: dirs, files = xbmcvfs.listdir(path) """ dirs = [] files = [] path = safe_path(path) for item_name in os.listdir(path): item_path = os.path.join(path, item_name) if os.path.isfile(item_path): files.append(item_name) else: dirs.append(item_name) # Return a tuple of (dir, files) return dirs, files
def from_file(cls, xml_path): xmldata = ETree.parse(safe_path(xml_path)).getroot() obj = cls(xmldata) obj.path = os.path.dirname(xml_path) return obj
def __init__(self, path): self._stat = os.stat(safe_path(path))
def __init__(self, filepath, mode=None): self._filepath = safe_path(filepath) self._file = open(self._filepath, mode if mode else "r")