o
    niF                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlmZ d dlmZmZmZ d dlmZmZmZ ejdkr\d dlmZ nd dlmZ d dlmZmZmZmZ d d	lm Z m!Z!m"Z"m#Z#m$Z$m%Z% d d
l&m'Z'm(Z( d dl)Z)d dl)m*Z*m+Z+m,Z, d dl-m.Z. d dl/m0Z0 d dl1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= dddZ>G dd de ddZ?dS )    )annotationsN)datetime)AnyLiteralSequence)quoteunquote	urlencode)      )Self)	BoolValueDoubleValue
Int32ValueStringValue)	BaseModel
ConfigDictFieldcomputed_fieldfield_validatormodel_validator)SchemaValidatorcore_schema)envtermwarnutil)Api
UsageError)wandb_settings_pb2   )apikeycredentials
filesystemipython)GitRepo)	RunMoment)generate_idargsstrreturnc                  G  s   t jt jj|  S )z-Join path and apply os.path.expanduser to it.)ospath
expanduserjoin)r(    r/   V/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/sdk/wandb_settings.py_path_convert/   s   r1   c                   @  s2  e Zd ZU dZeddZdZded< dZded< d	Z	d
ed< d	Z
ded< d	Zded< dZded< d	Zded< d	Zded< edddZded< dZded< edd dZded< dZded< dZded < dZded!< d	Zded"< d	Zded#< d	Zded$< dZded%< d	Zd&ed'< d	Zded(< d)Zded*< d	Zded+< d	Zded,< d-Zd.ed/< d	Z ded0< d	Z!ded1< d	Z"ded2< d	Z#ded3< d4Z$d5ed6< d7Z%d8ed9< dZ&ded:< d	Z'ded;< d	Z(d<ed=< dZ)ded>< dZ*ded?< d	Z+ded@< d	Z,dAedB< edCddZ-dDedE< d	Z.dedF< d	Z/dedG< d	Z0dedH< d	Z1dedI< d	Z2dedJ< dZ3dedK< dZ4dedL< dZ5dedM< d	Z6dNedO< d	Z7d&edP< dZ8dedQ< edRd dZ9dedS< d	Z:dedT< d	Z;dedU< d	Z<dedV< d	Z=dedW< d	Z>dedX< d	Z?dYedZ< dZ@ded[< d	ZAd\ed]< ed^d dZBded_< d	ZCd\ed`< d	ZDd\eda< dZEdedb< dZFdedc< dZGdedd< dZHdede< d	ZIdedf< d	ZJd\edg< dhZKd.edi< djZLd.edk< d	ZMdedl< d	ZNdedm< ednd dZOdedo< d	ZPd\edp< dZQdedq< d	ZRdedr< dZSdeds< dZTdedt< dZUdedu< dZVdedv< dZWdedw< dZXdedx< dZYdedy< dZZdedz< d	Z[ded{< d	Z\ded|< d	Z]d}ed~< d	Z^d}ed< d	Z_dAed< d	Z`d}ed< d	ZadAed< d	ZbdAed< d	ZcdAed< d	Zdd}ed< d	ZedAed< d	ZfdAed< d	ZgdAed< d	Zhded< d	Zid\ed< d	Zjd\ed< d	Zkd}ed< d	ZldAed< d	ZmdAed< d	ZndAed< dZod8ed< d	Zpded< d	Zqded< d	Zrded< d	Zsded< d	Ztd}ed< d	Zud}ed< evjwZxd.ed< d	Zyd}ed< dZzded< d	Z{ded< d	Z|ded< dZ}ded< dZ~ded< d	Zded< dZd8ed< dZded< d	ZdAed< e Zd.ed< eddZd8ed< d	Zded< d	Zded< d	Zded< d	Zded< edd dZded< d	Zded< dZd.ed< dZded< dZded< eddedd ZedddxddZeduddedd Zedddedd ZedddeddÄ Zedddeddń ZedddeddǄ Zed{ddeddɄ Zedddedd˄ Zedddedd̈́ Zed'ddedyddτZed1ddeddф Zed2ddeddӄ Zed6ddeddՄ ZedGddeddׄ ZedHddeddل ZedIddeddۄ ZedJddedd݄ ZedOddedd߄ ZedPddedyddZedSddedd ZedUddedd Zed_ddedd Zedddedd Zedfedd Zedddedd Zedddedd Zedddedd Zedddedd Zedddedd Zedddedd Zedlddedd Zedmddedd ZeedzddZeed{dd Zeed|ddZeed{ddZeed{ddZeed{ddZeed{d	d
Zeed{ddZeed{ddZeed{ddZeed}ddZeed}ddZeed}ddZeed{ddZeed}ddZeed}ddZeed{ddZeed|dd Zeed~d"d#Zeed}d$d%Zeed{d&d'Zeed}d(d)Zeed}d*d+Zeed}d,d-Zeed}d.d/Zeed}d0d1Zeed}d2d3Zeed}d4d5Zeedd7d8Zeed}d9d:Zeed}d;d<Zeed}d=d>Zeed}d?d@Zeed}dAdBZeed}dCdDZeed}dEdFZeed}dGdHZڐdIdJ ZېdKdL ZܐddOdPZݐdQdR ZސddUdVZߐddWdXZddZd[Zd\d] Zd^d_ Zd`da ZeddcddZd|dedfZedddhdiZedddndoZd}dpdqZd}drdsZeddvdwZd	S (  SettingszSettings for the W&B SDK.forbid)extraTboolallow_offline_artifactsFallow_val_changeNz(Literal['allow', 'must', 'never'] | None	anonymous
str | Noneapi_keyzdict[str, str] | Noneazure_account_url_to_access_keyhttps://api.wandb.air)   base_urlcode_dirzSequence[str] | Noneconfig_pathsauto)defaultvalidate_defaultzBLiteral['auto', 'off', 'wrap', 'redirect', 'wrap_raw', 'wrap_emu']consoleconsole_multipartc                   C  s
   t tjS N)r)   r"   DEFAULT_WANDB_CREDENTIALS_FILEr/   r/   r/   r0   <lambda>l   s   
 zSettings.<lambda>)default_factorycredentials_filedisable_codedisable_gitdisable_job_creationdockeremailentityforceRunMoment | None	fork_from
git_commitorigin
git_remotegit_remote_urlgit_root   intheartbeat_secondshost
http_proxyhttps_proxyidentity_token_filer/   ztuple[str, ...]ignore_globsg     V@floatinit_timeoutinsecure_disable_ssljob_namez+Literal['repo', 'artifact', 'image'] | None
job_sourcelabel_disablelaunchlaunch_config_pathzfloat | Nonelogin_timeoutonlinezCLiteral['online', 'offline', 'dryrun', 'disabled', 'run', 'shared']modenotebook_nameprogramprogram_abspathprogram_relpathprojectquietreinitreloginz0Literal['allow', 'must', 'never', 'auto'] | Noneresumeresume_fromresumedc                   C  s   t jt  S rE   )r+   r,   abspathgetcwdr/   r/   r/   r0   rG      s    root_dir	run_grouprun_idrun_job_typerun_name	run_notesztuple[str, ...] | Nonerun_tagssagemaker_disablezbool | None	save_codec                   C  s   t tjddddS )N~z.configwandbsettings)r1   r+   r,   r.   r/   r/   r/   r0   rG      s    settings_systemshow_colors
show_emojishow_errors	show_infoshow_warningssilentstart_methodstrict<   summary_timeout   summary_warningssweep_idsweep_param_pathc                   C     t  dkrdS dS )NWindowsFTplatformsystemr/   r/   r/   r0   rG      s    symlinksync_tensorboard%table_raise_on_max_row_limit_exceededusernamex_cli_only_modex_disable_metax_disable_servicex_disable_setproctitlex_disable_statsx_disable_update_checkx_disable_viewerx_disable_machine_infox_executablex_extra_http_headersz
int | Nonex_file_stream_max_bytesx_file_stream_max_line_bytesx_file_stream_transmit_intervalx_file_stream_retry_max$x_file_stream_retry_wait_min_seconds$x_file_stream_retry_wait_max_secondsx_file_stream_timeout_secondsx_file_transfer_retry_max&x_file_transfer_retry_wait_min_seconds&x_file_transfer_retry_wait_max_secondsx_file_transfer_timeout_secondsx_files_dirx_flow_control_customx_flow_control_disabledx_graphql_retry_max x_graphql_retry_wait_min_seconds x_graphql_retry_wait_max_secondsx_graphql_timeout_secondsg       @x_internal_check_processx_jupyter_namex_jupyter_pathx_jupyter_rootx_labelx_live_policy_rate_limitx_live_policy_wait_timex_log_levelx_network_bufferx_primary_node	x_proxiesx_runqueue_item_idx_require_legacy_servicex_save_requirementsx_service_transportg      >@x_service_waitx_show_operation_statsx_start_timex_stats_pidg      $@)rA   x_stats_sampling_interval"x_stats_neuron_monitor_config_pathx_stats_open_metrics_endpointsz0dict[str, dict[str, str]] | Sequence[str] | Nonex_stats_open_metrics_filters!x_stats_open_metrics_http_headersc                   C  r   )NDarwin)/z/System/Volumes/Data)r   r   r/   r/   r/   r0   rG   K  s   x_stats_disk_pathszSequence[int] | Nonex_stats_gpu_device_idsr   x_stats_buffer_sizex_syncx_update_finish_statebeforerj   c                 C  s:   i }|D ]}| dr|| |d| < q|| ||< q|S )zCheck if a private field is provided and assign to the corrsponding public one.

        This is a compatibility layer to handle previous versions of the settings.
        _x)
startswith)clsvalues
new_valueskeyr/   r/   r0   catch_private_settings]  s   
zSettings.catch_private_settingsafterr*   r   c                 C  s.   t dd | j| j| jfD dkrtd| S )Nc                 s  s    | ]}|d uV  qd S rE   r/   ).0or/   r/   r0   	<genexpr>q  s
    
zGSettings.validate_mutual_exclusion_of_branching_args.<locals>.<genexpr>r    z``fork_from`, `resume`, or `resume_from` are mutually exclusive. Please specify only one of them.)sumrR   rs   rt   
ValueErrorselfr/   r/   r0   +validate_mutual_exclusion_of_branching_argsn  s   
z4Settings.validate_mutual_exclusion_of_branching_argsc                 C  s   |rt ddd |S )NzgDisabling the wandb service is deprecated as of version 0.18.0 and will be removed in future versions. Frepeat)r   r   valuer/   r/   r0   validate_disable_service  s   z!Settings.validate_disable_servicec                 C  s(   |d urt |t | krtd|S )Nz+API key cannot start or end with whitespace)lenstripr   r   r/   r/   r0   validate_api_key  s   zSettings.validate_api_keyc                 C  sT   |  | td|rd|vrt| dtd|r%|ds%td|dS )Nz.*wandb\.ai[^\.]*$zapi.zB is not a valid server address, did you mean https://api.wandb.ai?httpsz3http is not secure, please use https://api.wandb.air   )validate_urlrematchr   r   rstripr   r/   r/   r0   validate_base_url  s   

zSettings.validate_base_urlc                 C     t |tjr
t|S |S rE   
isinstancepathlibPathr)   r   r/   r/   r0   validate_code_dir     zSettings.validate_code_dirc                 C  sL   |dkr|S t  s|jddks|jdrt dkr"d}|S d}|S )Nr@   r   threaddisable_servicer   wrapredirect)r$   
in_jupyterdatagetr   r   r   r   infor/   r/   r0   validate_console  s   
zSettings.validate_consolec                 C  r   rE   r   r   r/   r/   r0   validate_x_executable  r   zSettings.validate_x_executablec                 C  s   |d ur|dk rt d|S )Nr    z1File stream max line bytes must be greater than 0)r   r   r/   r/   r0   #validate_file_stream_max_line_bytes  s   z,Settings.validate_file_stream_max_line_bytesc                 C  r   rE   r   r   r/   r/   r0   validate_x_files_dir  r   zSettings.validate_x_files_dirc                 C  s,   |  |}|r|jd|jkrtd|S )Nrz   zProvided `run_id` is the same as the run to `fork_from`. Please provide a different `run_id` or remove the `run_id` argument. If you want to rewind the current run, please use `resume_from` instead._runmoment_preprocessorr  r  runr   r   r   r  
run_momentr/   r/   r0   validate_fork_from  s   
zSettings.validate_fork_fromc                 C      |d u rd S |  | |dS Nr   r   r   r   r/   r/   r0   validate_http_proxy     

zSettings.validate_http_proxyc                 C  r  r  r  r   r/   r/   r0   validate_https_proxy  r  zSettings.validate_https_proxyc                 C  s   t |ts	t|S |S rE   )r   tupler   r/   r/   r0   validate_ignore_globs     zSettings.validate_ignore_globsc                 C  r   rE   r   r   r/   r/   r0   validate_program  r   zSettings.validate_programc                 C  r   rE   r   r   r/   r/   r0   validate_program_abspath  r   z!Settings.validate_program_abspathc                 C  r   rE   r   r   r/   r/   r0   validate_program_relpath  r   z!Settings.validate_program_relpathc              	     sp    d u rd S t d}t dkrtd d fdd|D }|r6td dd|d	d| S )
Nz/\#?%:   zInvalid project name z: exceeded 128 charactersc                   s   h | ]}| v r|qS r/   r/   )r   charr   r/   r0   	<setcomp>      z,Settings.validate_project.<locals>.<setcomp>z: cannot contain characters ,z, found )listr   r   r.   )r   r   r  invalid_chars_listinvalid_charsr/   r   r0   validate_project  s   zSettings.validate_projectc                 C  s   |du rd S |du rdS |S )NFTr@   r/   r   r/   r/   r0   validate_resume  s
   zSettings.validate_resumec                 C  s,   |  |}|r|jd|jkrtd|S )Nrz   zGBoth `run_id` and `resume_from` have been specified with different ids.r  r  r/   r/   r0   validate_resume_from%  s   
zSettings.validate_resume_fromc                 C  r   rE   r   r   r/   r/   r0   validate_root_dir/  r   zSettings.validate_root_dirc                 C  T   |d u rd S t |dkrtdt |t | krtdt| s(td|S )Nr   zRun ID cannot be emptyz*Run ID cannot start or end with whitespacez%Run ID cannot contain only whitespacer   r   r   r5   r  r/   r/   r0   validate_run_id7  s   zSettings.validate_run_idc                 C  s    t |tjrtt|S t|S rE   )r   r   r   r)   r1   r   r/   r/   r0   validate_settings_systemE  s   z!Settings.validate_settings_systemc                 C     |dk rt d|S )Nr   z$Service wait time cannot be negativer   r   r/   r/   r0   validate_service_waitL     zSettings.validate_service_waitc                 C  sH   |d u r|S dg}t tdr|t 7 }||vr"td|d| |S )Nr   get_all_start_methodszSettings field `start_method`: z not in )hasattrmultiprocessingr2  r   )r   r   available_methodsr/   r/   r0   validate_start_methodS  s   
zSettings.validate_start_methodc                 C     t |tr
t|S |S rE   r   r)   jsonloadsr   r/   r/   r0   validate_x_stats_gpu_device_idsa     

z(Settings.validate_x_stats_gpu_device_idsc                 C  r   rE   r   r   r/   r/   r0   +validate_x_stats_neuron_monitor_config_pathh  r   z4Settings.validate_x_stats_neuron_monitor_config_pathc                 C  r7  rE   r8  r   r/   r/   r0   %validate_stats_open_metrics_endpointsp  r<  z.Settings.validate_stats_open_metrics_endpointsc                 C  r7  rE   r8  r   r/   r/   r0   #validate_stats_open_metrics_filtersw  r<  z,Settings.validate_stats_open_metrics_filtersc                 C  r7  rE   r8  r   r/   r/   r0   (validate_stats_open_metrics_http_headers~  r<  z1Settings.validate_stats_open_metrics_http_headersc                 C  r/  )Ng?z7Stats sampling interval cannot be less than 0.1 secondsr   r   r/   r/   r0    validate_stats_sampling_interval  r1  z)Settings.validate_stats_sampling_intervalc                 C  r+  )Nr   zSweep ID cannot be emptyz,Sweep ID cannot start or end with whitespacez'Sweep ID cannot contain only whitespacer,  r   r/   r/   r0   validate_sweep_id  s   zSettings.validate_sweep_idc                 C  r   rE   r   r   r/   r/   r0   validate_sweep_param_path  r   z"Settings.validate_sweep_param_path	list[str]c                 C  s   | j s
tjdd  S g S )Nr    )_jupytersysargvr   r/   r/   r0   _args  s   zSettings._argsc                 C  s(   ddl m} | }|rt|dsdS dS )z0Check if we are running in a lambda environment.r   )get_lambda_bootstraphandle_event_requestFT)"sentry_sdk.integrations.aws_lambdarI  r3  )r   rI  lambda_bootstrapr/   r/   r0   _aws_lambda  s   zSettings._aws_lambdac                 C  s   | j r	| | j S dS )aT  The relative path from the current working directory to the code path.

        For example, if the code path is /home/user/project/example.py, and the
        current working directory is /home/user/project, then the code path local
        is example.py.

        If couldn't find the relative path, this will be an empty string.
        N)rl   _get_program_relpathr   r/   r/   r0   _code_path_local  s   zSettings._code_path_localc                 C  s
   dt jv S )Nzgoogle.colab)rF  modulesr   r/   r/   r0   _colab     
zSettings._colabc                 C     t  S rE   )r$   
in_ipythonr   r/   r/   r0   _ipython     zSettings._ipythonc                 C  rS  rE   )r$   r  r   r/   r/   r0   rE    rV  zSettings._jupyterc                 C  rS  rE   )r   _is_likely_kaggler   r/   r/   r0   _kaggle  rV  zSettings._kagglec                 C  
   | j dkS )Ndisabledr   r   r/   r/   r0   _noop  rR  zSettings._noopc                 C  s   | j p| jp| jp| jS rE   )rU  rE  rQ  rX  r   r/   r/   r0   	_notebook  s   zSettings._notebookc                 C  s
   | j dv S )N)offlinedryrunr   r   r/   r/   r0   _offline  rR  zSettings._offlinec                 C  s   t j ddS )z7The operating system of the machine running the script.T)aliased)r   r   r/   r/   r0   _os     zSettings._osc                 C  s   t   dt    S N-)r   r   machinelowerr   r/   r/   r0   	_platform  s   zSettings._platformc                 C  s   t   dt   S )N )r   python_implementationpython_versionr   r/   r/   r0   _python  r  zSettings._pythonc                 C  rY  )zWhether we are in shared mode.

        In "shared" mode, multiple processes can write to the same run,
        for example from different machines.
        sharedr   r   r/   r/   r0   _shared  s   
zSettings._sharedc                 C  s$   | j d u rdS t| j }|dS )N z%Y%m%d_%H%M%S)r   r   fromtimestampstrftime)r   datetime_nowr/   r/   r0   _start_datetime  s   

zSettings._start_datetimec                 C  s&   t | j| j d| j d| j ddS )Nrd  tmpcoder1   	wandb_dirrun_modetimespecrz   r   r/   r/   r0   _tmp_code_dir  s   zSettings._tmp_code_dirc                 C  s   t  dkS )Nr   r   r   r/   r/   r0   _windows     zSettings._windowsc                 C  s2   | j sdS | jr| jdrt| j}d| S dS )z3The URL to the Colab notebook, if running in Colab.NfileId=z+https://colab.research.google.com/notebook#)rQ  r   r   r   )r   	unescapedr/   r/   r0   	colab_url  s   
zSettings.colab_urlLiteral['local', 'cloud']c                 C  s   | j rdS dS )Nlocalcloud)is_localr   r/   r/   r0   
deployment%     zSettings.deploymentc                 C  s*   | j pt| j| j d| j d| j dS )zFAbsolute path to the local directory where the run's files are stored.rd  files)r   r1   rv  rw  rx  rz   r   r/   r/   r0   	files_dir*  s
   zSettings.files_dirc                 C  s   t | jdkS )Nr<   )r)   r=   r   r/   r/   r0   r  4  r  zSettings.is_localc                 C  s$   t | j| j d| j d| j dS )z$The directory for storing log files.rd  logsru  r   r/   r/   r0   log_dir9  s   zSettings.log_dirc                 C     t | jdS )z.The path to the file to use for internal logs.debug-internal.logr1   r  r   r/   r/   r0   log_internalA  rb  zSettings.log_internalc                 C  r  )zHThe path to the symlink to the internal log file of the most recent run.r  r1   rv  r   r/   r/   r0   log_symlink_internalG  rb  zSettings.log_symlink_internalc                 C  r  )zLThe path to the symlink to the user-process log file of the most recent run.	debug.logr  r   r/   r/   r0   log_symlink_userM  rb  zSettings.log_symlink_userc                 C  r  )z2The path to the file to use for user-process logs.r  r  r   r/   r/   r0   log_userS  rb  zSettings.log_userc                 C  s$   |   }|sdS |  }| | S )z,The W&B URL where the project can be viewed.rn  )_project_url_base_get_url_query_stringr   project_urlqueryr/   r/   r0   r  Y  s
   zSettings.project_urlc                 C  r  )zThe path to the resume file.zwandb-resume.jsonr  r   r/   r/   r0   resume_fnamee  rb  zSettings.resume_fnameLiteral['run', 'offline-run']c                 C  s   | j sdS dS )Nr  zoffline-run)r_  r   r/   r/   r0   rw  k  r  zSettings.run_modec                 C  >   |   }t|| jgsdS |  }| dt| jpd | S )z(The W&B URL where the run can be viewed.rn  z/runs/)r  allrz   r  r   r  r/   r/   r0   run_urlp  
   zSettings.run_urlc                 C  r  )z(The path to the workspace settings file.r   r  r   r/   r/   r0   settings_workspace{  rb  zSettings.settings_workspacec                 C  r  )z*The W&B URL where the sweep can be viewed.rn  z/sweeps/)r  r  r   r  r   r  r/   r/   r0   	sweep_url  r  zSettings.sweep_urlc                 C  s"   t | j| j d| j d| j S rc  ru  r   r/   r/   r0   sync_dir  s   zSettings.sync_dirc                 C  s   t | jd| j dS )z4Path to the append-only binary transaction log file.zrun-.wandb)r1   r  rz   r   r/   r/   r0   	sync_file  s   zSettings.sync_filec                 C  r  )Nz
latest-runr  r   r/   r/   r0   sync_symlink_latest  r{  zSettings.sync_symlink_latestc                 C  s   | j S rE   )rr  r   r/   r/   r0   rx    s   zSettings.timespecc                 C  s   | j pd}tjtj|drdtj }ndtj }tj||}t|p'dtjsCtd| ddd tjt	
 |pAdtj }tj|S )	zFull path to the wandb directory.

        The setting exposed to users as `dir=` or `WANDB_DIR` is the `root_dir`.
        We add the `__stage_dir__` to it to get the full `wandb_dir`
        rn  r  r   .zPath z. wasn't writable, using system temp directory.Fr   )rx   r+   r,   existsr.   sepaccessW_OKr   tempfile
gettempdirr-   )r   rx   __stage_dir__r,   r/   r/   r0   rv    s   


zSettings.wandb_dirc                 C  J   | j r
tj| j sdS | | j  D ]\}}|dur"t| || qdS )z,Update settings from the system config file.N)r   r+   r,   r  _load_config_fileitemssetattrr   r   r   r/   r/   r0   update_from_system_config_file     z'Settings.update_from_system_config_filec                 C  r  )z/Update settings from the workspace config file.N)r  r+   r,   r  r  r  r  r  r/   r/   r0   !update_from_workspace_config_file  r  z*Settings.update_from_workspace_config_fileenvirondict[str, Any]c           	      C  s   d}|d }ddddddd	d
ddd
}t  }| D ]A\}}||s$q||v r-|| }n||r?d|t|d   }n
|t|d  }|| jv r[|dv rW|d}|||< q| D ]\}}|durnt| || q`dS )z+Update settings from environment variables.WANDB_r   r   r   rx   r|   r}   r~   r{   r   r   rN   )
WANDB_DISABLE_SERVICEWANDB_SERVICE_TRANSPORT	WANDB_DIR
WANDB_NAMEWANDB_NOTES
WANDB_TAGSWANDB_JOB_TYPEWANDB_HTTP_TIMEOUTWANDB_FILE_PUSHER_TIMEOUTWANDB_USER_EMAILx_N)r_   r~   r#  )dictr  r   r   rf  __dict__splitr  )	r   r  
env_prefixprivate_env_prefixspecial_env_var_namesr   settingr   r   r/   r/   r0   update_from_env_vars  s@   




zSettings.update_from_env_varsc              	   C  s  | j du s
| j du rttjdusttjdurt | _ t | _| jrK| j	du s1| j	dkrKt
j| j}|d| _|d| _|d| _n*| jrh| j	durhtj| j	rh| j	| _| j	| _t | _n| jrut
d| j	 d | jdu rt | _| jptjtjptjptd	pd	}|| _t t!" | _#| j$rdS | j%p| & }|durt' }|j(pt }| j)p| *||| _)tj+tj,|tj-t ||}tj|r|| _.nd
}|| _%dS )z,Update settings from the system environment.TNrn  r,   namerootzGWANDB_NOTEBOOK_NAME should be a path to a notebook file, couldn't find r  python3z<python with no main file>)/r   r+   getenvr   	SAVE_CODEDISABLE_CODEshould_save_coderK   rE  rk   r   jupyternotebook_metadatar   r  r   r   r   r,   r  rw   r   r[   socketgethostnamer   r  _EXECUTABLErF  
executableshutilwhich
get_dockerr   image_id_from_k8srM   r   rl   _get_programr%   r  rn   rN  rv   r.   relpathrm   )r   meta_executablerl   repor  rm   r/   r/   r0   update_from_system_environment  sl   






z'Settings.update_from_system_environmentr   Nonec                 C  s.   t | D ]\}}|durt| || qdS )z"Update settings from a dictionary.N)r  r  r  )r   r   r   r   r/   r/   r0   update_from_dictC  s
   zSettings.update_from_dictc                   s*    fdd j D }|r| | dS dS )z4Update settings from another instance of `Settings`.c                   s   i | ]}|t  |qS r/   )getattr)r   fieldr   r/   r0   
<dictcomp>K  r"  z1Settings.update_from_settings.<locals>.<dictcomp>N)model_fields_setr  )r   r   dr/   r  r0   update_from_settingsI  s   zSettings.update_from_settingswandb_settings_pb2.Settingsc                 C  s  t  }| jdd D ]\}}|dkr\t|tttfr)t||}|j	j
| n2t|trOt||}| D ]\}}| D ]\}}||jj
| j
|< q?q7ntdt| d| qt|trmt||t|d qt|tr~t||t|d qt|trt||t|d qt|trt||t|d qt|tttfrt||}	|	j
| qt|trt||}
| D ]	\}}||
j
|< qqt|trt||t j|j|j
|jd q|du rqtdt| d| |S )	z3Generate a protobuf representation of the settings.T)exclude_noner   zUnsupported type z for setting r   )r  r   metricN)r   r2   
model_dumpr  r   r$  setr  r  sequencer   extendr  mapping	TypeErrortyper5   CopyFromr   rY   r   r`   r   r)   r   r&   r  r  )r   settings_protokvr  r   r   kkvvr  r  r/   r/   r0   to_protoQ  sV   











zSettings.to_protoc                 C  s  | j dkrCtj| jrCt| j}t|d }W d   n1 s#w   Y  | jdu r1|| _n| j|krCt	
d| d| j d | jdu rLt | _| j dkr| jdurt| j t| jd}|td| ji W d   dS 1 syw   Y  dS dS dS )zHandle logic for resuming runs.r@   rz   Nz!Tried to auto resume run with id z but id z is set.w)rs   r+   r,   r  r  openr9  loadrz   r   r   r'   r#   mkdir_exists_okrv  writedumps)r   fresume_run_idr/   r/   r0   handle_resume_logic  s.   



"zSettings.handle_resume_logicc                 C  sR   | j du rdS dD ]}t| |}|dur&td| d|d t| |d q	dS )zUpdate settings based on sweep context.

        When running a sweep, the project, entity, and run_id are handled externally,
        and should be ignored if they are set.
        Nro   rO   rz   	Ignoring rh  z when running a sweep.)r   r  r   r   r  r  r/   r/   r0   handle_sweep_logic  s   

zSettings.handle_sweep_logicc                 C  sN   | j sdS dD ]}t| |}|dur$td| d|d t| |d qdS )zUpdate settings based on launch context.

        When running in a launch context, the project, entity, and run_id are handled
        externally, and should be ignored if they are set.
        Nr  r  rh  z( when running from wandb launch context.)rf   r  r   r   r  r  r/   r/   r0   handle_launch_logic  s   
zSettings.handle_launch_logicurlc                 C  s$   t tjddgdd}||  dS )zValidate a URL string.httpr   T)allowed_schemesr   N)r   r   
url_schemavalidate_python)r  url_validatorr/   r/   r0   r     s   zSettings.validate_urlc              	   C  s   | j s1ttj}|dur|S zddl}|jdu r|jW S d|jj W S  t	t
fy0   Y dS w | jr7| jS | js=| jS | jdrF| jS | jS )z1Get the program that started the current process.Nr   z-m r|  )rE  r+   r  r   PROGRAM__main____spec____file__r  ImportErrorAttributeErrorrk   r   rl   r   r   )r   rl   r  r/   r/   r0   r    s&   
zSettings._get_programr  c                 C  sj   | sdS |p	t  }|sdS t j|t jt  || }t j|r3t jj||d}d|v r1dS |S dS )z=Get the relative path to the program from the root directory.N)startz../)r+   rw   r,   r.   r  r  )rl   r  full_path_to_programrelative_pathr/   r/   r0   rN    s   zSettings._get_program_relpathrA   	file_namesectionr  c                 C  s^   t  }|| ||  t }|| D ]}|| | ||< |dkr,|| d||< q|S )z?Load a config file and return the settings for a given section.r_   r#  )configparserConfigParseradd_sectionreadr  r  )r  r  parserconfigr  r/   r/   r0   r    s   

zSettings._load_config_filec                 C  sH   t | j| jgs
dS t| j}| dt| jpd dt| jp d S )z'Construct the base URL for the project.rn  r   )r  rO   ro   r   app_urlr=   r   )r   r  r/   r/   r0   r    s   (zSettings._project_url_basec                 C  s6   t   ddkrdS tj| d}dtd|i S )z<Construct the query string for project, run, and sweep URLs.r8   truern  r  ?apiKey)r   r   r  r!   r:   r	   )r   r:   r/   r/   r0   r    s   zSettings._get_url_query_stringvalRunMoment | str | Nonec                 C  s.   t | ts	| du r| S t | trt| S dS )z5Preprocess the setting for forking or resuming a run.N)r   r&   r)   from_uri)r"  r/   r/   r0   r    s
   

z Settings._runmoment_preprocessor)r*   r   )r*   rQ   )r*   rD  )r*   r5   )r*   r9   )r*   r)   )r*   r  )r*   r  )r  r  )r   r  r*   r  )r   r2   r*   r  )r*   r  )r  r)   r*   r  rE   )rl   r)   r  r9   r*   r9   )r  r)   r  r)   r*   r  )r"  r#  r*   rQ   )__name__
__module____qualname____doc__r   model_configr6   __annotations__r7   r8   r:   r;   r=   r>   r?   r   rC   rD   rI   rJ   rK   rL   rM   rN   rO   rP   rR   rS   rU   rV   rW   rZ   r[   r\   r]   r^   r_   ra   rb   rc   rd   re   rf   rg   rh   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   ru   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   loggingINFOr   r   r   r   r   r   r   r   r   r   r   r+   getpidr   r   r   r   r   r   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r  r	  r
  r  r  r  r  r  r  r  r  r'  r(  r)  r*  r-  r.  r0  r6  r;  r=  r>  r?  r@  rA  rB  rC  r   propertyrH  rM  rO  rQ  rU  rE  rX  r[  r\  r_  ra  rg  rk  rm  rr  ry  rz  r~  r  r  r  r  r  r  r  r  r  r  rw  r  r  r  r  r  r  rx  rv  r  r  r  r  r  r  r  r  r  r  staticmethodr   r  rN  r  r  r  r  r/   r/   r/   r0   r2   4   s  
 




	




























	
		!&G3

r2   T)validate_assignment)r(   r)   r*   r)   )@
__future__r   r  r9  r+  r4  r+   r   r   r   r  r  rF  r  r   typingr   r   r   urllib.parser   r   r	   version_infor   typing_extensionsgoogle.protobuf.wrappers_pb2r   r   r   r   pydanticr   r   r   r   r   r   pydantic_corer   r   r   r   r   r   wandb.apis.internalr   wandb.errorsr   wandb.protor   libr!   r"   r#   r$   
lib.gitlibr%   lib.run_momentr&   	lib.runidr'   r1   r2   r/   r/   r/   r0   <module>   sB    
 
