
    5i                    T    d dl mZ d dlZd dlZd dlZd dlZddlmZ ddedfdZddZ	y)	    )annotationsN   )err_exitTc                   t         j                  dk(  }t         j                  }	t        j                  j
                  dk(  }
t        j                  j
                  dk(  }|rd}t        |t              s: |d       n1|r-|dk(  rd}n'|dk(  rd	}n|rd
}n |dj                  |             nd}| r| dk(  rd}nId}| }nDt        j                         }|	 |d       n%d}t         j                  j                  ||z         }dk(  rt         j                  j                  |      rlt         j                  j                  |      r |dj                  |             t         j                  j                  |      r |dj                  |             |rF|dk(  rt        |       y|dk(  r't!        |d      5 }|j#                  |       ddd       y |d       y|r|n|d   j%                         t'        fd|D              s |d       |
s|rdnd}|rdnd}|rt        |      nd}t(        j*                  }d}d|i}|r||d<   ||	||d}|dk(  rMt!        |fi |5 }t)        j,                  |fi |}|j/                          |j1                  |       ddd       y|dk(  rFt)        j,                  t        j2                  fi |}|j/                          |j1                  |       y |d       y# 1 sw Y   yxY w# 1 sw Y   yxY w)aj  
    arg_output: str
    A string representing the output file path.
    When it's "-", output is written to stdout.
    This can be directly set as args.output from argparse when calling the function (e.g within dataset_utilities)

    arg_delim: str
    A string representing the delimiter. Defaults to "," when not specified.
    It also determines the file suffix when writing to file.
    This can be set to args.delimiter from argparse when the method is called

    arg_sql: bool
    A boolean representing whether the output_listdict_or_string is a SQL query (string) or not.
    This can be args.sql from argparse

    output_listdict_or_string: 'list of dicts' or 'str' depending on whether arg_sql is False or True, respectively
    This is expected to be the response from vizserver
    if arg_sql is True, this is expected to be a string representing the SQL query
    if arg_sql is False, this is expected to be a list of dicts representing the output of a SQL query
    if output_listdict_or_string is a list of dicts, all dicts must have the same keys which will be used as column names

    save_uncommon_delim_to_txt: bool
    Set this to False if you want to error out when any delimiter other than "," or "	" is specified

    output_file_name: str
    This is expected to be a record_name which will be used when arg_output is not specified
    Do not append a suffix to this string
    output_file_name is mandatory when arg_output is not specified

    By default delimiter is set "," and file suffix is csv (when writing to file)

    None values are written as empty strings by default (csv.DictWriter behavior)

    ntr      z.sqlz!Expected SQL query to be a string,z.csv	z.tsvz.txtzUnsupported delimiter: {}-STDOUTFILENzNo output filename specifiedz1{} already exists. Please specify a new file pathz1{} is a directory. Please specify a new file pathwz8Unexpected error occurred while writing SQL query outputr   c              3  h   K   | ])  }t        |j                               t              k(   + y w)N)setkeys).0iCOLUMN_NAMESs     z/home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/dxpy/cli/output_handling.py	<genexpr>z*write_expression_output.<locals>.<genexpr>~   s*      
34CMS..
s   /2z(All rows must have the same column nameswb "modenewline)
fieldnames	delimiterlineterminatorquoting	quotecharz.Unexpected error occurred while writing output)osnamelinesepsysversion_infomajor
isinstancestrformatgetcwdpathjoinexistsisfileisdirprintopenwriter   allcsvQUOTE_MINIMAL
DictWriterwriteheader	writerowsstdout)
arg_output	arg_delimarg_sqloutput_listdict_or_stringsave_uncommon_delim_to_txtoutput_file_nameerror_handlercolnamesIS_OS_WINDOWSOS_SPECIFIC_LINE_SEPARATORIS_PYTHON_2IS_PYTHON_3SUFFIXWRITE_METHOD
OUTPUT_DIRf
WRITE_MODENEWLINE	DELIMITERQUOTING
QUOTE_CHAR
write_argsdictwriter_paramsr   r   s                           @r   write_expression_outputrQ   	   s   Z GGtOM!#""((A-K""((A-K3S9=>	F$F)9@@KL#L!L) YY[
#.
 "L!ww||J8H68QRv77>>*+ww~~./GNN(
 ww}}-.GNN( 8#+,V#&, 3123 3 TU #L4Q7<<>L 
8Q
 
 DE(MTs
#"&/C	NS	##
 J

 $+Jy! '"8#
 6!&5*5 7NN1:(9:567 7
 X%szz?->?AMMOKK12 JKc3 3L7 7s   4K48L 4K= L	c                    t        | t              rt        j                  | dd      }|S t	        dt
        j                         y)zPretty-prints the provided JSON object.

    Args:
        json_dict: A string containing valid JSON data.

    Returns:
        Returns a string with formatted JSON or None if there's an error.
    T   )	sort_keysindentzWARNING: Invalid JSON provided.)fileN)r'   dictjsondumpsr0   r$   stderr)	json_dictformatted_jsons     r   pretty_print_jsonr]      s8     )T"IaH/cjjA    )r[   rW   returnr(   )

__future__r   r$   r4   r!   rX   
exceptionsr   rQ   r]    r^   r   <module>rc      s2    " 
 
 	  !  $[L~r^   