
    ,/i.                     ~    d dl Z d dlZd dlZd dlmZmZmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZ  G d de	      Zy)	    N)DictAnyOptionalList)BaseCommandCommandError)settings)Q)SourceManualc                       e Zd ZdZd Zd Zy)Commanda  Two-way sync for SourceManual configs with the 'Configuration' folder.
Usage: manage.py reload_config_from_folder [apply|export] [--name <portal>[,p2]] [--all] [--only selectors,rules,...] [--dry-run]
- apply: files -> DB (update database from JSON files)
- export: DB -> files (write current DB configs to JSON files)
Folder structure: Configuration/<portal>/{selector.json,rules.json,transferred.json,inactive.json,actions.json}.c                 @   |j                  ddddgd       |j                  ddd	d
t               |j                  dddd       |j                  dddd       |j                  dddd       |j                  dddd       |j                  dddt               y )Naction?applyexportzOperation mode: 'apply' updates DB from files, 'export' writes files from DB. If omitted, defaults to apply (or --export if provided).)nargschoiceshelpz--namez-nnamesz`Comma-separated portal names to update (matches SourceManual.name or linked SourceNetwork.name).)destr   typez--all
update_allz-Update all portals found under Configuration/
store_true)r   r   r   z	--dry-rundry_runz0Do not write to DB, only print intended changes.z--exportexport_to_fileszdExport current DB configs to Configuration/<portal> JSON files instead of applying from files to DB.z--overwrite	overwritezZWhen exporting, overwrite existing JSON files (otherwise they will be updated regardless).z--onlyonlyz`Limit fields to process (comma-separated). Allowed: selectors,rules,transferred,inactive,actions)add_argumentstr)selfparsers     M/var/www/extractly/extractly/management/commands/reload_config_from_folder.pyadd_argumentszCommand.add_arguments   s    h' Z	 	 	
 	s 	 	
 	@	 	 	
 	C	 	 	
 	"w	 	 	
 	m	 	 	
 	s	 	 	
    c                    t        t        dd       }|sat        j                  j	                  t        j                  j                  t        j                  j                  t              dddd            }t        j                  j                  |d      }t        j                  j                  |      st        d|       |j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d	      }	|j                  d
      }
|j                  d      }g d}d }|r|j                  d      D cg c]1  }|j                         s|j                         j                         3 }}|D cg c]	  }||vs| }}|r/t        ddj                  |       ddj                  |             dddddd}||	rdnd}n	 g }|rt        j                  |      D cg c]B  }t        j                  j                  t        j                  j                  ||            sA|D }}|s| j                  j!                  | j"                  j%                  d             y |s|	st        d      |r=|j                  d      D cg c]#  }|j                         s|j                         % }}|s|	s|st        d      d}|dk(  r|rt&        j(                  j+                         }nY|st        d      t-               }|D ]  }|t-        |      t-        |      z  z  }  t&        j(                  j/                  |      }|j1                         s5| j                  j!                  | j"                  j%                  d             y |D ]  }|j2                  xs5 |j4                  r|j4                  j2                  nd xs d |j6                   }t        j                  j                  ||j                               }|r;| j                  j!                  | j"                  j%                  d!| d"|              nt        j8                  |d#$       |j:                  xs i |j<                  xs i |j>                  xs i |j@                  xs i |jB                  xs g d}|7|jE                         D ci c]  \  }}|j                  |      |v s|| }}}|jE                         D ]  \  }}t        j                  j                  ||      }|r<| j                  j!                  | j"                  jG                  d%| d&| d'             d	 tI        |d(d)*      5 } tK        jL                  || d+d,-       d d d          | j                  j!                  | j"                  jG                  d0             y |D ]]  }"t        j                  j                  ||"      }t        j                  j                  |      s9| j                  j!                  | j"                  j%                  d1| d2             |i }#ddddddd3}$|$jE                         D ]|  \  }}%||%|vrt        j                  j                  ||      }t        j                  jS                  |      sM	 tI        |d4d)*      5 } tK        jT                  |       |#|%<   d d d        ~ |#s:| j                  j!                  | j"                  j%                  d8|" d9             Rt&        j(                  j+                         }|j/                  t-        |"      t-        |"      z        }|jY                         }&|&dk(  r:| j                  j!                  | j"                  j%                  d:|" d9             |D ]q  }g }'|#jE                         D ](  \  }%}(t        ||%      })|)|(k7  s|'j[                  |%       * |'sF| j                  j!                  | j"                  jG                  d;|j2                   d<|" d=             | j                  j!                  | j"                  j%                  d>|j2                   d<|" d?dj                  |'                    |rt        j                  j                  |d@      }*t        j8                  |*d#$       t\        j\                  j_                         ja                  dA      }+	 t        |dBd       |j2                  |j4                  r|j4                  j2                  nd |j:                  xs i |j<                  xs i |j>                  xs i |j@                  xs i |jB                  xs g dC},t        j                  j                  |*|" dD|j6                   dD|+ dE      }-tI        |-d(d)*      5 }.tK        jL                  |,|.d+d,-       d d d        |'D ]  }%tc        ||%|#|%           |je                  tg        ti        |'            G       |dHz  }t ` |rD| j                  j!                  | j"                  jG                  dItk        |       dJ|              y | j                  j!                  | j"                  jG                  dKtk        |       dL|              y c c}w c c}w c c}w c c}w c c}}w # 1 sw Y   KxY w# tN        $ rE}!| j                  j!                  | j"                  jQ                  d.| d/|!              Y d }!~!,d }!~!ww xY w# 1 sw Y   YxY w# tJ        jV                  $ rF}!| j                  j!                  | j"                  jQ                  d5| d/|! d6             Y d }!~!-d }!~!wtN        $ rF}!| j                  j!                  | j"                  jQ                  d7| d/|! d6             Y d }!~!zd }!~!ww xY w# 1 sw Y   xY w# tN        $ rE}!| j                  j!                  | j"                  j%                  dF| d/|!              Y d }!~!Td }!~!ww xY w)MNBASE_DIRz..Configurationz#Configuration folder not found at: r   r   r   r   r   r   r   )	selectorsrulestransferredinactiveactions,zInvalid --only values: z, z. Allowed: r)   r*   r+   r,   r-   )selector.json
rules.jsontransferred.jsoninactive.jsonactions.jsonr   r   z<No portal folders found under Configuration/. Nothing to do.z.Provide --name <portal>[,portal2] or use --allz1No valid portal names parsed from --name argumentr   )name__iexact)source__name__iexactz(No SourceManual entries found to export.portal_zWould export configs for 'z' to T)exist_okzWould write z for ''wzutf-8)encodingF   )ensure_asciiindentzFailed to write z: zExport complete.zPortal folder missing: z. Skipping.)r/   zselectors.jsonr0   r1   r2   r3   rzJSON parse error in z. Skipping this file.zError reading z(No known config files found for portal 'z'. Skipping.z'No SourceManual found for portal name 'zNo changes for z
 (portal 'z').z	Updating z') fields: _backupsz%Y%m%d_%H%M%Sid)r@   namesource_namer)   r*   r+   r,   r-   _z.jsonzBackup failed for )update_fields   z%Dry run complete. Portals processed: z. Records with changes: z$Update complete. Portals processed: z. Records updated: )6getattrr	   ospathabspathjoindirname__file__isdirr   getsplitstriplowerlistdirstdoutwritestyleWARNINGr   objectsallr
   filterexistsrA   sourcer@   makedirsr)   r*   r+   r,   r-   itemsSUCCESSopenjsondump	ExceptionERRORisfileloadJSONDecodeErrorcountappenddatetimenowstrftimesetattrsavelistsetlen)/r!   argsoptionsbase_dirconfig_rootr   r   r   	names_argr   r   only_argallowed_fieldsonly_fieldsfinvalidfile_to_fieldportalsdntotal_updatedqsqpsmportal_name
portal_dir	files_mapfndatafilenamerH   fheportalpayloadsmappingfieldrg   changesnew_valold_val
backup_dir	timestampbackup_payloadbackup_pathbfs/                                                  r#   handlezCommand.handleC   s
   8Z6wwrww||BGGOOH4MtUY[_ae'fgHggll8_=ww}}[)!D[MRSS 'H 5";;|4
I.#*;;w#7	 ',= >!++k2	")++f"5 V6>nnS6IW6IQWWY1779??,6IKW"-I+Q.1Hq+GI"%<TYYw=O<PP[\`\e\eft\u[v#wxx )! -'%
 >!0XgF "$**["9i"9QRWW]]277<<XcefKg=hq"9Gi!!$**"4"45s"tu_"#STT.7ooc.BP.Baggi1779.BPog"#VWW X!))--/ &'WXXC A*QA-FFFA !!))00399;!!$**"4"45_"`a!wwfRYY299>>DfW^_a_d_d^eUfWW\\+{7H7H7JK
KK%%djj&8&8;UVaUbbghrgs9t&uvKK
T: &(\\%7R"$((.b(*(<"%'[[%6B$&JJ$4"	 *:C//:K t:Khb$}O`O`acOdhsOsT:KI t&/oo&7NHd77<<
H=D))$***<*<|H:U[\g[hhi=j*kl \!$g>" IIdBU1M ? '8) > KKdjj001CDE Fk6:J77==,!!$**"4"47NzlZe5f"gh')H "-"-%$1!+ )G $+==?%*uK/Gww||J977>>$'o!$g>".2iimHUO ? $3 !!$**"4"47_`f_ggs5t"uv %%))+B1&1A64RRSBHHJEz!!$**"4"47^_e^ffr5s"tu&.nn&6NE7%b%0G')u- '7
 KK%%djj&8&8)"''*VHCH'J K!!$**"4"4y
6(+diiPWFXEYZ#\ ]   WW\\+zB
J6$--113<<_M	Z%b$5 "9;ryy~~%'\\%7R!#R')~~';$&KK$52#%::#3	&N #%'',,zfXQruugQykY^;_"`Kk3AR		."5QRS B %EBx7 % d3w<&89"[ U r KKdjj003XY\]dYeXff~  @M  N  2O  P  QKKdjj003WX[\cXdWeex  zG  yH  2I  J  Kk XI* j QR !u ?>$ \))$***:*:=MdVSUVWUX;Y*Z[[\< ?>// u))$***:*:=QRVQWWYZ[Y\\q;r*stt$ o))$***:*:^D6QSTUSVVk;l*mnnod BA  ZKK%%djj&8&8;MbTQSTUSV9W&XYYZs   k* k	kk>Akk"k8kk.k+k)9kk)<m
l:#mC	o=o0*o=k&!k))	l72:l22l7:m?mo-;no-';o((o-0o:5o==	q:qqN)__name__
__module____qualname__r   r$   r    r%   r#   r   r      s    	{ 	+
ZKKr%   r   )r`   rG   ri   typingr   r   r   r   django.core.management.baser   r   django.confr	   django.db.modelsr
   extractly.modelsr   r   r   r%   r#   <module>r      s0     	  , , A    )AKk AKr%   