U C^@sddlmZddlmZddlZzddlmZWnek rLddlZYnXej ddkrbe Z ne Z e ZeaddZGdddeZd d Zd d Zd dZddZddZGdddeZdS))unicode_literals) OrderedDictNcOs.|dd}t|r"td|t||dS)zCreate a new registry. *namespace (str): The namespace, e.g. "spacy" or "spacy", "architectures". RETURNS (Tuple[Callable]): The setter (decorator to register functions) and getter (to retrieve functions). entry_pointsFzNamespace already exists: {})r)get check_exists RegistryErrorformatRegistry) namespacekwargsrr 0/tmp/pip-install-6_kvzl1k/catalogue/catalogue.pycreates rc@sPeZdZdddZddZddZdd Zd d Zd d ZddZ dddZ dS)r FcCs||_d||_||_dS)zInitialize a new registry. namespace (Tuple[str]): The namespace. entry_points (bool): Whether to also check for entry points. RETURNS (Registry): The newly created object. _N)r joinentry_point_namespacer)selfr rr r r__init__&s zRegistry.__init__cCs0tt|j|g}|jo"||}|p.|tkS)zCheck whether a name is in the registry. name (str): The name to check. RETURNS (bool): Whether the name is in the registry. )tuplelistr rget_entry_pointREGISTRY)rnamer Zhas_entry_pointr r r __contains__1szRegistry.__contains__cKs|j|f|S)aRegister a function for a given namespace. Same as Registry.register. name (str): The name to register under the namespace. func (Any): Optional function to register (if not used as decorator). RETURNS (Callable): The decorator. )register)rrr r r r__call__;szRegistry.__call__c s,fdd}|d}|dk r(||S|S)zRegister a function for a given namespace. name (str): The name to register under the namespace. func (Any): Optional function to register (if not used as decorator). RETURNS (Callable): The decorator. csttjg||SN)_setrr )funcrrr rdo_registrationLsz*Registry.register..do_registrationrN)r)rrr r!rr r rrDs  zRegistry.registercCst|jr||}|r|St|j|g}t|sld}d|j}dt|pXd}t | |||t |S)zGet the registered function for a given name. name (str): The name. RETURNS (Any): The registered function. z4Cant't find '{}' in registry {}. Available names: {}z -> z, none) rrrr rrsortedget_allkeysrr _get)rrZfrom_entry_pointr errZcurrent_namespace availabler r rrUs  z Registry.getcstt}jr|tD]L\}tjtdkr"tfddt tjDr"||d<q"|S)zGet a all functions for a given namespace. namespace (Tuple[str]): The namespace to get. RETURNS (Dict[str, Any]): The functions, keyed by name. c3s |]}j||kVqdSrr .0ir%rr r rsz#Registry.get_all..) rrupdateget_entry_pointsritemslenr allrange)rresultvaluer r.rr$gs$ zRegistry.get_allcCs*i}t|jgD]}|||j<q|S)zGet registered entry points from other packages for this namespace. RETURNS (Dict[str, Any]): Entry points, keyed by name. )AVAILABLE_ENTRY_POINTSrrloadr)rr7 entry_pointr r rr2xszRegistry.get_entry_pointsNcCs.t|jgD]}|j|kr|Sq|S)a;Check if registered entry point is available for a given name in the namespace and load it. Otherwise, return the default value. name (str): Name of entry point to load. default (Any): The default value to return. RETURNS (Any): The loaded entry point or the default value. )r9rrrr:)rrdefaultr;r r rrs zRegistry.get_entry_point)F)N) __name__ __module__ __qualname__rrrrrr$r2rr r r rr %s    r cGs|tkS)zyCheck if a namespace exists. *namespace (str): The namespace. RETURNS (bool): Whether the namespace exists. )rr*r r rrsrcCsNtdd|Ds$d}t||t|}|tkrFd|}t|t|S)zGet the value for a given namespace. namespace (Tuple[str]): The namespace. RETURNS: The value for the namespace. css|]}t|tVqdSr) isinstance basestring_)r,rr r rr/sz_get..z9Invalid namespace. Expected tuple of strings, but got: {}(Can't get namespace {} (not in registry))r5 ValueErrorr rrr)r r'r r rr&s r&csTt}tD]@\}ttkrtfddttDr||<q|S)aGet all matches for a given namespace, e.g. ("a", "b", "c") and ("a", "b") for namespace ("a", "b"). namespace (Tuple[str]): The namespace. RETURNS (Dict[Tuple[str], Any]): All entries for the namespace, keyed by their full namespaces. c3s|]}||kVqdSrr r+r%r r rr/sz_get_all..)rrr3r4r5r6)r r7r8r rDr_get_alls   rEcCs|tt|<dS)zzSet a value for a given namespace. namespace (Tuple[str]): The namespace. func (Callable): The value to set. N)rr)r rr r rrsrcCs4t|}|tkr"d|}t|t|}t|=|S)zvRemove a value for a given namespace. namespace (Tuple[str]): The namespace. RETURNS: The removed value. rB)rrr r)r r'removedr r r_removes rGc@s eZdZdS)rN)r=r>r?r r r rrsr) __future__r collectionsrsysimportlib.metadatametadataZimportlib_metadata ImportError version_info basestringrAstrrr9rrobjectr rr&rErrGrCrr r r rs&   k