U Ce76@sddlZddlZddlZddlmZmZddlmZddddd gZ d d Z dd dZ dddZ edddZ eddddZddZedddZdS)N)pluginplugin_function)generalz&Official MySQL Shell Plugin RepositoryzRThe official MySQL Shell Plugin Repository maintained by the MySQL Team at Oracle.z6https://cdn.mysql.com/mysql_shell/plugins_manifest.zipz*manifest/mysql-shell-plugins-manifest.json)name descriptionurlZ manifestPathc CsXd}d}|D]F}||dd|ddd|dd d|d dd 7}|d7}q |S) zReturns a formatted list of repositories. Args: repositories (list): A list of repositories. Returns: The formated list as string rz>4 rz??z r-rz get) repositoriesoutirrG/usr/lib/mysqlsh/python-packages/mysqlsh/plugin_manager/repositories.pyformat_repository_listing,s : rFc Cstjtd}tj|rz,t|d}t| d}W5QRX|WStj k r}z:t d|dt |j d|jd|j|rWYdSd}~XYqtk r}z"t d |dt ||r҂W5d}~XYqXngSdS) zFetches the registered user repositories Args: raise_exceptions (bool): Whether exceptions are raised Returns: The registered repositories as list plugin-repositories.jsonrrz"ERROR: Could not parse JSON file ''. , line: , col: NzERROR: Error reading file ')ospathjoinrget_shell_user_dirisfileopenjsonloadr JSONDecodeErrorprintstrmsglinenocolnoOSError)raise_exceptionsuser_repo_file_pathuser_repo_file user_reposerrrget_user_repositories@s(   $r-c Csdd|d}tjtd}z,t|d}tj||ddW5QRXWdStk r}z(t d |d t ||rzWYd Sd }~XYnXd S) zStores the registered user repositories Args: user_repos (list): The list of raise_exceptions (bool): Whether exceptions are raised Returns: True on success zMySQL Shell Plugin Repositoriesz0.0.1)ZfileTypeversionrrw)indentTzERROR: Error writing to file 'rFN) rrrrrrrdumpr'r"r#)r+r(Zuser_repo_file_contentr)r*r,rrrset_user_repositoriesas   r3zplugins.repositories.listcKsz|dd}|dd}t}t|d}|r8|||s@|Stdt|ddt|d t|d krjd nd d dS)aLists all registered plugin repositories This function will list all registered MySQL Shell plugin repositories. To add a repository use the plugins.repositories.add() function. Args: **kwargs: Optional parameters Keyword Args: return_formatted (bool): If set to true, a list object is returned. raise_exceptions (bool): Whether exceptions are raised Returns: None or a list of dicts representing the plugins return_formattedTr(Fr(z-Registered MySQL Shell Plugin Repositories. )rz Total of z repositorryZiesz. N)r DEFAULT_PLUGIN_REPOSITORIEScopyr-extendr"rlen)kwargsr4r(Zreposr+rrrget_plugin_repositoriess    r<zplugins.repositories.addc Ks|dd}|dd}|s@|r@tdtjjdddi}|sPtdd S|d r| d }t |d krtd d S|d }t |dkrd}n|d}d|d |d}nH|ds|dsd|}| ds| d r|d}n |d}zt |}|sWd St|}|d} | d} | sJtdWd S| d} | shtdWd S|d} | rt | dkrtdWd Stdtd d!| d"d#d!| d$|d% | D]} td&| d'd"qtjjd(| d)dd*i}|d+kr td,Wd Std-t|d.}|d krBWd S|D]6}|d/|krFtd0| d1Wd SqFtd2| d3|| | |d4t|rtd5| d6WnXtjk r}z6td7t|jd8|jd9|j|rWYd Sd }~XYnXd S):aeAdds a new plugin repository By calling this function a new plugin repository can be added. The url parameter allows for shortcuts. 'domain.com' expands to https://domain.com/mysql-shell-plugins-manifest.json 'domain.com/plugins' expands to https://domain.com/plugins/mysql-shell-plugins-manifest.json 'github/username' expands to https://raw.githubusercontent.com/username/mysql-shell-plugins/master/mysql-shell-plugins-manifest.json 'github/username/repo' expands to https://raw.githubusercontent.com/username/repo/master/mysql-shell-plugins-manifest.json Args: url (str): The url of a MySQL Shell plugins repository. **kwargs: Optional parameters Keyword Args: interactive (bool): Whether user input is accepted raise_exceptions (bool): Whether exceptions are raised Returns: None interactiveTr(zTo add a new MySQL Shell plugin repository the URL of the repository manifest file needs to be specified. Examples: domain.com domain.com/plugins github/username github/username/repository z/Please enter the URL of the plugin repository: Z defaultValuer z"No URL given. Cancelling OperationNzgithub//rz#A github user needs to be specifiedzmysql-shell-pluginsz"https://raw.githubusercontent.com/z)/master/mysql-shell-plugins-manifest.jsonzhttps://zhttp://z.jsonz!mysql-shell-plugins-manifest.jsonz"/mysql-shell-plugins-manifest.json repositoryrz0The given repository misses the 'name' property.rz7The given repository misses the 'description' property.pluginsrzAThe repository does not contain any plugins. Operation cancelled.a WARNING: You are about to add an external MySQL Shell plugin repository. External plugin repositories and their plugins complement the functionality of MySQL Shell and can contain system level software that could be potentially harmful to your system. Please review the description below and only proceed if you have obtained the external plugin repository URL from a trusted source. Oracle and its affiliates cannot be held responsible for any potential harm caused by using plugins from external sources. z Repository :r  z Description:z URL: z1 The repository contains the following plugins: z - Zcaptionz-Are you sure you want to add the repository 'z ' [yes/NO]: noyeszOperation cancelled.z%Fetching current user repositories...r5rThe repository 'z' has already been added. zAdding repository ''...)rrrz Repository 'z' successfully added. z"ERROR: Could not parse JSON file. rr)r r"mysqlshglobalsshellpromptstriplower startswithsplitr:endswithr download_filerloadsr-appendr3r!r#r$r%r&)rr;r=r(Z github_strZ github_userZ github_repoZmanifestZmanifest_contentr@rrrArrJr+rr,rrradd_plugin_repositorys                 &       $rScCs@d}d}|D]"}|d|kr&d}q0|d7}q |r8|SdSdS)NrFrTrr )rr+nrfoundrrrrsearch_user_repoBs rVzplugins.repositories.removec Ks|d}|dd}|dd}t|d}|ss(   !  %