<s> import logging <EOL> import os <EOL> import re <EOL> import commons <EOL> logging . getLogger ( "<STR_LIT>" ) . setLevel ( logging . DEBUG ) <EOL> def match_in_re ( name , patterns ) : <EOL> for p in patterns : <EOL> re_obj = re . compile ( p ) <EOL> if re_obj . match ( name ) : <EOL> return True <EOL> return False <EOL> def get_files_list ( path , ignore_patterns = [ '<STR_LIT>' , '<STR_LIT>' ] , match_patterns = None ) : <EOL> """<STR_LIT>""" <EOL> files = [ ] <EOL> for pending_filename in os . listdir ( path ) : <EOL> if ignore_patterns : <EOL> if match_in_re ( name = pending_filename , patterns = ignore_patterns ) : <EOL> continue <EOL> if match_patterns : <EOL> if not match_in_re ( name = pending_filename , patterns = match_patterns ) : <EOL> continue <EOL> files . append ( pending_filename ) <EOL> return files <EOL> def fix_pattern ( p ) : <EOL> try : <EOL> re . compile ( p ) <EOL> except : <EOL> if p . startswith ( "<STR_LIT>" ) and p . count ( "<STR_LIT>" ) == <NUM_LIT:1> : <EOL> suffix = p . split ( "<STR_LIT>" ) [ - <NUM_LIT:1> ] <EOL> fixed = "<STR_LIT>" % suffix <EOL> return fixed <EOL> else : <EOL> msg = "<STR_LIT>" % p <EOL> logging . error ( msg ) <EOL> return None <EOL> return p <EOL> def parse_work_path ( file_name , folder_pages_full_path , req_path ) : <EOL> if os . path . exists ( file_name ) : <EOL> return file_name <EOL> elif file_name . find ( "<STR_LIT:/>" ) != - <NUM_LIT:1> : <EOL> par = os . path . dirname ( file_name ) <EOL> if os . path . exists ( par ) : <EOL> return par <EOL> file_full_path = os . path . join ( folder_pages_full_path , par ) <EOL> if os . path . exists ( file_full_path ) : <EOL> return file_full_path <EOL> default_work_path = os . path . join ( folder_pages_full_path , req_path ) <EOL> if req_path : <EOL> par = os . path . dirname ( default_work_path ) <EOL> if os . path . exists ( par ) : <EOL> return par <EOL> return default_work_path <EOL> def cat_files ( work_path , files ) : <EOL> all = "<STR_LIT>" <EOL> for filename in files : <EOL> full_path = os . path . join ( work_path , filename ) <EOL> chunk = filename + "<STR_LIT:\n>" <EOL> with open ( full_path ) as f : <EOL> buf = commons . strutils . safeunicode ( f . read ( ) ) <EOL> buf = "<STR_LIT>" . join ( buf . split ( "<STR_LIT:\n>" ) ) <EOL> buf = commons . strutils . rstrips ( buf , "<STR_LIT:U+0020>" ) <EOL> chunk += "<STR_LIT>" + buf <EOL> chunk += "<STR_LIT:\n>" <EOL> all += chunk <EOL> return all <EOL> def macro_zw2md_cat ( text , folder_pages_full_path , req_path , ** view_settings ) : <EOL> shebang_p = "<STR_LIT>" <EOL> code_p = '<STR_LIT>' <EOL> code_block_p = "<STR_LIT>" % ( shebang_p , code_p ) <EOL> p_obj = re . compile ( code_block_p , re . MULTILINE ) <EOL> def code_repl ( match_obj ) : <EOL> code = match_obj . group ( '<STR_LIT:code>' ) <EOL> code = code . split ( "<STR_LIT:\n>" ) [ <NUM_LIT:1> ] <EOL> if code . startswith ( "<STR_LIT>" ) : <EOL> p = '<STR_LIT>' <EOL> m = re . match ( p , code , re . UNICODE | re . MULTILINE ) <EOL> file_name = m . group ( "<STR_LIT>" ) <EOL> file_name = file_name . strip ( ) <EOL> work_path = parse_work_path ( file_name = file_name , folder_pages_full_path = folder_pages_full_path , req_path = req_path ) <EOL> match_pattern = file_name . strip ( "<STR_LIT:'>" ) . strip ( "<STR_LIT>" ) <EOL> match_pattern = commons . strutils . lstrips ( match_pattern , work_path ) <EOL> match_pattern = commons . strutils . strips ( match_pattern , "<STR_LIT:/>" ) <EOL> match_pattern = fix_pattern ( match_pattern ) <EOL> files = get_files_list ( path = work_path , match_patterns = [ match_pattern ] ) <EOL> buf = cat_files ( work_path = work_path , files = files ) <EOL> return buf <EOL> buf_fixed = "<STR_LIT>" % code <EOL> return buf_fixed <EOL> return p_obj . sub ( code_repl , text ) <EOL> def test_fix_pattern ( ) : <EOL> auto_patterns = ( <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ) <EOL> for e , g in auto_patterns : <EOL> assert fix_pattern ( e ) == g <EOL> def test_macro_cat ( ) : <EOL> text = """<STR_LIT>""" <EOL> req_path = "<STR_LIT>" <EOL> folder_pages_full_path = "<STR_LIT>" <EOL> result = macro_zw2md_cat ( text = text , folder_pages_full_path = folder_pages_full_path , req_path = req_path ) <EOL> result = commons . strutils . strips ( result , "<STR_LIT:\n>" ) <EOL> print repr ( result ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_fix_pattern ( ) <EOL> test_macro_cat ( ) </s>
