o
    Mf{                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZ d	d
lmZ d	dlmZ d	dlmZmZ G dd deZdS )    N)get_user_model)ImproperlyConfigured)BaseCommand)import_string)api_settings)APIRequestFactoryforce_authenticate)APIView   )openapi)swagger_settings)OpenAPICodecJsonOpenAPICodecYamlc                   @   sB   e Zd ZdZdd Zdd ZdddZd	d
 Zdd Zdd Z	dS )Commandz8Write the Swagger schema to disk in JSON or YAML format.c              	   C   s   |j ddddtdd |j ddd	d
dd |j ddddddgtdd |j ddddtdd |j dddd	d
dd |j dd td!d" |j d#d$d%d& |j d'd(d	d
d)d |j d*d+d,dd-d. d S )/Noutput_filezoutput-file?-z>Output path for generated swagger document, or "-" for stdout.)metavarnargsdefaulttypehelpz-oz--overwriteF
store_truezfOverwrite the output file if it already exists. Default behavior is to stop if the output file exists.)r   actionr   z-fz--formatformat jsonyamlz_Output format. If not given, it is guessed from the output file extension and defaults to json.)destr   choicesr   r   z-uz--urlapi_urlzMBase API URL - sets the host and scheme attributes of the generated document.)r   r   r   r   z-mz--mock-requestmockzUse a mock request when generating the swagger schema. This is useful if your views or serializers depend on context from a request in order to function.)r   r   r   r   z--api-versionapi_versionzFVersion to use to generate schema. This option implies --mock-request.)r   r   r   z--useruserzbUsername of an existing user to use for mocked authentication. This option implies --mock-request.)r   r   z-pz	--privatea  Hides endpoints not accessible to the target user. If --user is not given, only shows endpoints that are accessible to unauthenticated users.
This has the same effect as passing public=False to get_schema_view() or OpenAPISchemaGenerator.get_schema().
This option implies --mock-request.z-gz--generator-classgenerator_class_namezZImport string pointing to an OpenAPISchemaGenerator subclass to use for schema generation.)r   r   r   )add_argumentstr)selfparser r)   o/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/drf_yasg/management/commands/generate_swagger.pyadd_arguments   s`   	
zCommand.add_argumentsc                 C   sn   |dkrt g dd}||d}|| d S |dkr1tg d}||d}|| d S td| )Nr   T)
validatorsprettyzutf-8r   )r,   zunknown format %s)r   encodedecodewriter   
ValueError)r'   schemastreamr   codecswagger_jsonswagger_yamlr)   r)   r*   write_schemaL   s   
zCommand.write_schemaNc                 C   s<   t  }||d | }|d urt||d t |}|S )Nz	/swagger.)r#   )r   getr   r	   initialize_request)r'   urlr   r#   factoryrequestr)   r)   r*   get_mock_requestY   s   zCommand.get_mock_requestc                 C   s    t j}|r	t|}||||dS )N)infoversionr:   )r   DEFAULT_GENERATOR_CLASSr   )r'   r$   api_infor"   r    generator_classr)   r)   r*   get_schema_generatorb   s   zCommand.get_schema_generatorc                 C   s   |j ||dS )N)r<   public)
get_schema)r'   	generatorr<   rD   r)   r)   r*   rE   m   s   zCommand.get_schemac
                 O   sV  t t j ttdd }t|tjstd|s$t	j
|d dv r$d}|p'd}|p,tj}|r=t jjdi t j|i}|pH|pH|d upH|d u}|rQ|sQtdd }|r\| |||}|p`tj}|rh|rh||_| |	|||}| ||| }|dkr| || j| d S |rd	nd
}t||}| ||| W d    d S 1 sw   Y  d S )NDEFAULT_INFOzgsettings.SWAGGER_SETTINGS["DEFAULT_INFO"] should be an import string pointing to an openapi.Info object   )z.ymlz.yamlr   r   zh--mock-request requires an API url; either provide the --url argument or set the DEFAULT_API_URL settingr   wxr)   )loggingdisableWARNINGgetattrr   
isinstancer   Infor   ospathsplitextDEFAULT_API_URLr   objectsr8   USERNAME_FIELDr=   r   DEFAULT_VERSIONr?   rC   rE   r7   stdoutopen)r'   r   	overwriter   r    r!   r"   r#   privater$   argskwargsr>   r<   rF   r2   flagsr3   r)   r)   r*   handlep   s@   

"zCommand.handle)N)
__name__
__module____qualname__r   r+   r7   r=   rC   rE   r_   r)   r)   r)   r*   r      s    8
	r   )rK   rQ   django.contrib.authr   django.core.exceptionsr   django.core.management.baser   django.utils.module_loadingr   rest_framework.settingsr   rest_framework.testr   r   rest_framework.viewsr	   r   r   app_settingsr   codecsr   r   r   r)   r)   r)   r*   <module>   s    