#!/usr/bin/env python import sys import os import six def inc_text(s, paths): if not s.startswith( '@' ): return None path = s[ 1: ] r = False if os.path.exists( path ): with open( path, 'rb' ) as f: try: r = f.read().decode( 'utf-8' ) paths.append( path ) except: pass return r paths = [ '-' ] is_dbl_paths = lambda : paths[ -1 ] in paths[ : -1 ] def err(s, i): sys.stderr.write( 'err {}, {} L{}\n'.format( s, paths[ -1 ], i+1 ) ) sys.exit( 1 ) cut_tail_nl = lambda s: s[ : -1 ] if s and s[ -1 ] == '\n' else s def inc_exp(s, inc_text): def inc_exp_line(i_s): (i, s) = i_s r = inc_text( s, paths ) if r == False: err( s, i ) if r != None: s = '' if not is_dbl_paths(): s = inc_exp( cut_tail_nl( r ), inc_text ) paths.pop() return s return '\n'.join( map( inc_exp_line, enumerate( s.split( '\n' ) ) ) ) if __name__ == "__main__": f = sys.stdin if six.PY2 else sys.stdin.buffer s = f.read().decode( 'utf-8' ) s = inc_exp( s, inc_text ) f = sys.stdout if six.PY2 else sys.stdout.buffer f.write( s.encode( 'utf-8' ) ) # EOF