<s> from cffi import FFI <EOL> import os <EOL> INCLUDE = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> if '<STR_LIT>' in os . environ : <EOL> cpaths = os . getenv ( '<STR_LIT>' ) . split ( os . pathsep ) <EOL> INCLUDE += [ os . path . join ( p , '<STR_LIT>' ) for p in cpaths ] <EOL> BLOCKS = { '<STR_LIT:{>' : '<STR_LIT:}>' , '<STR_LIT:(>' : '<STR_LIT:)>' } <EOL> def header_files ( ) : <EOL> for dir in INCLUDE : <EOL> if os . path . exists ( dir ) : <EOL> break <EOL> return { fn : os . path . join ( dir , fn ) for fn in os . listdir ( dir ) } <EOL> def functions ( hfiles ) : <EOL> lines = [ ] <EOL> for fn , path in hfiles . items ( ) : <EOL> with open ( path ) as f : <EOL> cont = '<STR_LIT>' <EOL> for ln in f : <EOL> if cont == '<STR_LIT:U+002C>' : <EOL> lines . append ( ln ) <EOL> cont = '<STR_LIT>' <EOL> if cont in BLOCKS : <EOL> lines . append ( ln ) <EOL> if BLOCKS [ cont ] in ln : <EOL> cont = '<STR_LIT>' <EOL> if not ( ln . startswith ( '<STR_LIT>' ) <EOL> or ln . startswith ( '<STR_LIT>' ) ) : <EOL> continue <EOL> lines . append ( ln ) <EOL> cont = ln . strip ( ) [ - <NUM_LIT:1> ] <EOL> return '<STR_LIT>' . join ( ln [ <NUM_LIT:10> : ] if ln . startswith ( '<STR_LIT>' ) else ln for ln in lines ) <EOL> def symbols ( ffi ) : <EOL> nanomsg = ffi . dlopen ( '<STR_LIT>' ) <EOL> lines = [ ] <EOL> for i in range ( <NUM_LIT> ) : <EOL> val = ffi . new ( '<STR_LIT>' ) <EOL> name = nanomsg . nn_symbol ( i , val ) <EOL> if name == ffi . NULL : <EOL> break <EOL> name = ffi . string ( name ) . decode ( ) <EOL> name = name [ <NUM_LIT:3> : ] if name . startswith ( '<STR_LIT>' ) else name <EOL> lines . append ( '<STR_LIT>' % ( name , val [ <NUM_LIT:0> ] ) ) <EOL> return '<STR_LIT:\n>' . join ( lines ) + '<STR_LIT:\n>' <EOL> def create_module ( ) : <EOL> hfiles = header_files ( ) <EOL> hfiles . pop ( '<STR_LIT>' , None ) <EOL> ffi = FFI ( ) <EOL> ffi . cdef ( functions ( hfiles ) ) <EOL> ffi . set_source ( '<STR_LIT>' , '<STR_LIT:\n>' . join ( '<STR_LIT>' % fn for fn in hfiles ) , <EOL> libraries = [ '<STR_LIT>' ] , include_dirs = INCLUDE ) <EOL> return ffi <EOL> ffi = create_module ( ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:w>' ) as f : <EOL> f . write ( symbols ( ffi ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> ffi . compile ( ) </s>
