
    RhP              	       "   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mZm	Z	 d dl
mZmZ d dlmZ d dlmc mZ d dlmc mZ d dlZd Zd Zd Zded	ed
efdZd Zd Zd Zd ZddedededefdZ ddedede!fdZ"d Z#dede!fdZ$d dZ%d Z&y)!    N)FastMailMessageSchemaConnectionConfig)HTTPExceptionstatus)JSONResponsec                      dj                  t        j                  t        j                  t        j
                  z   d            S )N    )k)joinrandomchoicesstringascii_uppercasedigits     ./var/www/html/sandstorm/dependencies/helper.pygenerate_random_textr      s+    776>>&"8"86=="HANOOr   c                 4    t        j                  dd|       }|S )Nz[^a-zA-Z0-9]_)resub)input_stringsanitized_strings     r   replace_special_charactersr      s    vvosLAr   c                 T    | j                  dg       D cg c]  }|d   	 }}|S c c}w )Nerrorsmessage)get)responseerrormessagess      r   get_error_messager%      s0    .6ll8R.HIUi IHIO Js   %	recipientsubjectbodyc           
        K   t        t        j                  d      t        j                  d      t        j                  d      dt        j                  d      ddd      }t        || g|d	
      }t	        |      }	 |j                  |       d {    y 7 # t        $ r}t        dt        |            d }~ww xY ww)NSES_ACCESS_KEY_IDSES_SECRET_ACCESS_KEYSES_SENDER_EMAILi  SES_AWS_HOSTFT)MAIL_USERNAMEMAIL_PASSWORD	MAIL_FROM	MAIL_PORTMAIL_SERVERMAIL_STARTTLSMAIL_SSL_TLSUSE_CREDENTIALShtml)r'   
recipientsr(   subtypei  )status_codedetail)	r   osgetenvr   r   send_message	Exceptionr   str)r&   r'   r(   confr    fmes          r   
send_emailrC      s     ii 34ii 78))./IIn-	D ;	G 
$B<oog&&& <CF;;<s<   A;C>B BB CB 	B>#B99B>>Cc                 n    t        |       5 }t        j                  |      cddd       S # 1 sw Y   yxY w)aj  
    Reads a JSON file and returns its content as a Python dictionary or list,
    depending on the JSON structure.

    Args:
        file_path (str): The path to the JSON file to be read.

    Returns:
        dict or list: The content of the JSON file. The type depends on the
                    structure of the JSON file (object or array at the root).
    N)openjsonload	file_pathfs     r   read_json_filerK   3   s-     
i Ayy|  s   +4c                 d    t        |       5 }|j                         cd d d        S # 1 sw Y   y xY w)N)rE   readrH   s     r   read_txt_filerN   B   s)    	i Avvx  s   &/c                 T    t        j                  d      }|j                  d|       } | S )Nz]:\s+"(.*?)"\s+httpz]: http)r   compiler   )textregexs     r   parserS   G   s&    JJ-.E99Y%DKr   c                     | y i }| d   j                         D ]   \  }}|| d   |   d   | d   |   d   d||<   " |j                  |d       S )Nurl_to_unified_indexurl_to_infotitlesnippets)urlrW   rX   )itemsr!   )search_resultsindex_to_findcitation_dictrY   indexs        r   $construct_citation_dict_from_articler_   L   sz    M$%;<BBD 

U#M237@&}5c::F 
e
 ]D11r   course_titlecontent_dirwiki_diroutput_filec           
         t        j                  dd       t        j                  dd       t        j                  dd       t        j                  dd       t        j                  d	d
t        j                         j
                   dddddd      }t        j                  |d      }dt        j                  |d      _        dt        j                  |d      _        t        j                  |d      }t        j                  |d      }t        j                  |d      }| t        j                  |d      _        t        j                  |d      }	t        j                  |	dddd      }
t        j                  |d      }t        t        t        j                  |            d      D ]  \  }}|j                  d      s|j                  dd       j                  dd      j                         }d!| }d"| }d#| }t        j                  |
d$d%|i      }d&| d'| t        j                  |d(      _        t        j                  |d$||d)      }|t        j                  |d(      _        t        j                  j!                  ||      }t        j                  |d*|d+|d,      }t        j                  |d-d.|i        t        j"                  |d/      }t%        j&                  |      }|j)                  d01      }t+        |d2d/3      5 }|j-                  |       d d d        t/        d4|        y # 1 sw Y   xY w)5Nr
   z1http://www.imsglobal.org/xsd/imsccv1p1/imscp_v1p1lomz/http://ltsc.ieee.org/xsd/imsccv1p1/LOM/resourcelomimsccz/http://ltsc.ieee.org/xsd/imsccv1p1/LOM/manifestxsiz)http://www.w3.org/2001/XMLSchema-instancemanifestman_am  http://www.imsglobal.org/xsd/imsccv1p1/imscp_v1p1 http://www.imsglobal.org/profile/cc/ccv1p1/ccv1p1_imscp_v1p2_v1p0.xsd http://ltsc.ieee.org/xsd/imsccv1p1/LOM/resource http://www.imsglobal.org/profile/cc/ccv1p1/LOM/ccv1p1_lomresource_v1p0.xsd http://ltsc.ieee.org/xsd/imsccv1p1/LOM/manifest http://www.imsglobal.org/profile/cc/ccv1p1/LOM/ccv1p1_lommanifest_v1p0.xsd)
identifierxmlnsz	xmlns:lomzxmlns:lomimsccz	xmlns:xsizxsi:schemaLocationmetadatazIMS Common Cartridgeschemaz1.1.0schemaversionzlomimscc:lomzlomimscc:generalzlomimscc:titlezlomimscc:stringorganizationsorganizationorg_1zrooted-hierarchy)rj   	structure	resources   start.htmlr    module_item_res_itemrj   zModule z: rW   )rj   identifierrefresource
webcontent)rj   typehreffiler   zutf-8z  )indentw)encodingu    ✅ imsmanifest.xml created at: )ETregister_namespaceElementuuiduuid4hex
SubElementrQ   	enumeratesortedr;   listdirendswithreplacerW   pathr   tostringminidomparseStringtoprettyxmlrE   writeprint)r`   ra   rb   rc   rh   rl   re   generalrW   ro   orgresources_elifilename
title_textmod_iditem_idres_idmodule_itemnested_item	full_pathr~   rough_stringreparsed
pretty_xmlrJ   s                             r   create_manifestr   X   s   "QR%!RS*&WX%!LMzz*TZZ\--./DFK@Y' H  }}Xz2H-CBMM(H%*4;BMM(O,1
--.
1CmmC!34GMM'#34E3?BMM%*+0 MM(O<M
--~g\n7o
pC==;7L 

;(?!@J =8  )%%c3/77DJJL
1#!+smmC,1GH5<QCr*3Nk7+0mmKbh9ij3=k7+0 GGLL84	==z  <
 
 	h(;<+=0 ;;x1L""<0H%%T%2J	k3	1 Q	
	,[M
:; s   L??Mfolder_path	base_pathreturnc                 
   g }t        t        j                  |       d      D ]]  \  }}|j                         j	                  d      s&t        j
                  j                  ||      }|j                  d| d|d       _ |S )Nrt   ru   rw   r{   r   )idr   r   )r   r;   r   lowerr   r   r   append)r   r   rs   r   r   r   s         r   generate_resources_from_folderr      s    I K!8B 	8~~((1GGLLH5	* 
 		 r   c                 L    t        j                  dd|       j                         S )Nz\s+rx   )r   r   strip)r   s    r   normalize_spacesr      s    66&#|,2244r   md_textc                     t        j                  d| t         j                        }|D cg c]#  }|j                         s|j                         % c}S c c}w )zBSplits the markdown into sections based on top-level (#) headings.z	(?=^# .+))flags)r   split	MULTILINEr   )r   sectionsss      r   split_markdown_sectionsr      s:    xxgR\\BH'5!1779AGGI555s   AAc                 F    |st         j                  }t        ||| d      S )N)r   r    r9   content)r   HTTP_400_BAD_REQUESTr   )r    r9   s     r   errorResponser      s+    11!
 r   c                 \    t        t        j                  t        j                  | |d      S )N)r   r    datar   )r   r   HTTP_200_OK)r    r   s     r   successResponser      s,    &&((
	 r   )zimsmanifest.xml)wiki_content)i  )'rF   r;   r   r   r   base64fastapi_mailr   r   r   fastapir   r   fastapi.responsesr   xml.etree.ElementTreeetreeElementTreer   xml.dom.minidomdomr   r   r   r   r%   r?   rC   rK   rN   rS   r_   r   listr   r   r   r   r   r   r   r   <module>r      s     	   	  B B ) * " " ! ! P
< <c < <2


2C<# C<C C<3 C<UX C<L  Y] 56S 6T 6

r   