
    |i/                         S r SSKrSSKrSSKrSSKJr  SSKJr  S
S jrS r	/ SQr
/ SQr\S	:X  a  SSKr\R                  " \	" 5       5        gg)a  
CSV Column Remapper for Key-Value Pair Data
Processes CSV files where identifiers and values alternate in columns.

Supports BOTH:
  - Numeric identifiers (e.g., 1200319923593372)
  - Text markers (e.g., due_on:, name:)

Example input format:
  task_id, identifier1, value1, due_on:, 2026-03-20, name:, Task Name...
  1213413361584562, 1200319923593372, Yes, due_on:, 2026-03-20, name:, Dean Away

Usage:
  python csv_remapper.py input.csv output.csv --map id1:label1 "due_on:":due_date ...
    N)Path)OrderedDictc                 F   [        U SSSS9 n[        R                  " U5      n[        U5      nSSS5        W(       d  [	        S5      eUS   n[        S USS	  5       5      (       a  US
S nOUn/ n	[        5       n
U GH  nU Vs/ s H  o(       a  UR                  5       OSPM     nnU(       a
  US   (       d  M?  US   n[        5       nXS'   UR                  5        H  nSX'   M	     S
nU[        U5      S
-
  :  Ga  UU   nUS
-   [        U5      :  a  UUS
-      OSnU(       d  US
-  nMB  [        R                  " SU5      (       a@  US;  a  UOSnUU;   a  UXU   '   O"U(       a  SU 3nUUU'   U
R                  U5        US-  nOgUR                  S5      (       aL  US;  a  UOSnUU;   a  UXU   '   O.U(       a'  UR                  S5      nUUU'   U
R                  U5        US-  nOUS
-  nU[        U5      S
-
  :  a  GM  U	R!                  U5        GM     S/[        UR                  5       5      -   nU(       a  U[#        U
5      -  nU	 H  nU H  nUU;  d  M  SUU'   M     M     [        USSSS9 n[        R$                  " UUS9nUR'                  5         UR)                  U	5        SSS5        [+        S[        U	5       S35        [+        S[        U5       SU 35        [+        SU 35        U	$ ! , (       d  f       GN= fs  snf ! , (       d  f       Ng= f)a  
Parse CSV where identifiers and values alternate in columns.

Args:
    input_file: Path to input CSV
    output_file: Path to output CSV
    identifier_labels: Dict mapping {identifier: label}
                      For text markers, include the colon (e.g., "due_on:")
    include_all: If True, include unmapped fields (default: False)

Returns:
    List of processed row dictionaries
r zutf-8)newlineencodingNzInput file is emptyr   c              3   ~   #    U  H3  n[         R                  " S [        U5      R                  5       5      v   M5     g7f)z^\d+$N)rematchstrstrip).0cells     /home/share/csv_remapper.py	<genexpr>&parse_key_value_csv.<locals>.<genexpr>1   s)     
K]T288Hc$ioo/00]s   ;=      task_idz	^\d{10,}$)Noner   field_   :w)
fieldnamesu   ✓ Processed z rowsu   ✓ Output columns (z): u   ✓ Output saved to: )opencsvreaderlist
ValueErrorallsetr   r   valueslenr   r   addendswithrstripappendsorted
DictWriterwriteheader	writerowsprint)
input_fileoutput_fileidentifier_labelsinclude_allfr   raw_rows	first_row	data_rowsprocessed_rowsextra_fieldsrowr   r   recordlabelicurrentnext_valvalue
field_namemarker_namer   fnamewriters                            r   parse_key_value_csvrD      s2    
j#rG	<A< 
= .// I

KYr]
KKKQRL		N5L8;<ttzz|+<#a&a& #y '--/EFM 0 #c(Q,!fG%&USX%5s1q5z2HQ xxg..$,L$@b//9>FW56 #)'!3J).F:& $$Z0Q !!#&&$,L$@b //9>FW56 ").."5K*/F;' $$[1QQE #c(Q,H 	f%o t t$5$<$<$>??Jf\**
 !EF" "u   ! 
k3W	=j9( 
>
 
N3~./u
56	 Z 1ZL
AB	!+
/0 
=	<& =F 
>	=s   "K;"L:7L;
L

L c                  <   [         R                  " S[         R                  SS9n U R                  SSS9  U R                  SSS9  U R                  S	S
SSSS9  U R                  SSSSS9  U R	                  5       n[        UR                  5      R                  5       (       d  [        SUR                   S35        g[        5       nUR                   H  nSU;  a  [        SU S35          gUR                  S5      (       a7  SU;   a1  UR                  SS5      nUS   SS  S-   nUS   R                  5       nO}UR                  S5      S:X  aD  UR                  S5      (       d.  UR                  SS5      nUS   S-   nUS   R                  5       nO$UR                  SS5      u  pVUR                  5       nUR                  5       X%'   M      [!        UR                  UR"                  X!R$                  5        g! [&         a  n[        SU 35         S nAgS nAff = f)NzdRemap CSV columns from identifier-value pairs. Supports numeric IDs and text markers (e.g., due_on:)a  
Examples:
  # Numeric identifiers only
  python csv_remapper.py input.csv output.csv --map 1200319923593372:status 1189842108382269:department

  # Include text markers (use quotes for colons)
  python csv_remapper.py input.csv output.csv     --map 1200319923593372:status "due_on:":due_date "name:":task_name

  # Mixed identifiers and markers
  python csv_remapper.py input.csv output.csv     --map 1200319923593372:status 1206595535112943:assigned_to "due_on:":due_date "name:":task_name
        )descriptionformatter_classepilogr/   zInput CSV file path)helpr0   zOutput CSV file pathz--mapz-m+TzoIdentifier-label pairs in format id:label. For text markers, include colon in quotes (e.g., "due_on:":due_date))nargsrequiredrI   z--include-allz-a
store_truez5Include unmapped fields (default: only mapped fields))actionrI   zError: Input file 'z' not foundr   r   zError: Invalid mapping 'z '. Use format 'identifier:label'"z":r   r   zError processing file: )argparseArgumentParserRawDescriptionHelpFormatteradd_argument
parse_argsr   r/   existsr.   r   map
startswithsplitr   countrsplitrD   r0   r2   	Exception)parserargsr1   pairparts
identifierr;   es           r   mainrb      s   $$z <<F$ +@A
,BC
~	   D   D  ''))#DOO#4K@A $d?,TF2RST ??3DDLJJtQ'Eq!"+J!HNN$EZZ_!$//#*>*>KKQ'EqCJ!HNN$E !%

3 2J#))+J(-%- 0DOOT-=-=?PRbRbc
 	  's+,s   +G9 9
HHH)120031992359337212003199235933811201654500615241118984210838226912064251583209941206595535112943zdue_on:zname:)statusapprovalcategory
departmentlocationassigned_todue_date	task_name__main__)F)__doc__r   r   rP   pathlibr   collectionsr   rD   rb   DEFAULT_IDENTIFIERSDEFAULT_LABELS__name__sysexit     r   <module>r|      sX      	   #nbHX	 	 zHHTV r{   