<s> from IPython . core import ipapi <EOL> from IPython . core import macro <EOL> ip = ipapi . get ( ) <EOL> import os , pprint <EOL> def export ( filename = None ) : <EOL> lines = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> vars = ip . db . keys ( '<STR_LIT>' ) <EOL> vars . sort ( ) <EOL> varstomove = [ ] <EOL> get = ip . db . get <EOL> macros = [ ] <EOL> variables = [ ] <EOL> for var in vars : <EOL> k = os . path . basename ( var ) <EOL> v = get ( var ) <EOL> if k . startswith ( '<STR_LIT:_>' ) : <EOL> continue <EOL> if isinstance ( v , macro . Macro ) : <EOL> macros . append ( ( k , v ) ) <EOL> if type ( v ) in [ int , str , float ] : <EOL> variables . append ( ( k , v ) ) <EOL> if macros : <EOL> lines . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for k , v in macros : <EOL> lines . append ( "<STR_LIT>" % k ) <EOL> for line in v . value . splitlines ( ) : <EOL> lines . append ( '<STR_LIT:U+0020>' + repr ( line + '<STR_LIT:\n>' ) ) <EOL> lines . extend ( [ '<STR_LIT:)>' , '<STR_LIT>' ] ) <EOL> if variables : <EOL> lines . extend ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for k , v in variables : <EOL> varstomove . append ( k ) <EOL> lines . append ( '<STR_LIT>' % ( k , repr ( v ) ) ) <EOL> lines . append ( '<STR_LIT>' % ( '<STR_LIT:U+0020>' . join ( varstomove ) ) ) <EOL> bkms = ip . db . get ( '<STR_LIT>' , { } ) <EOL> if bkms : <EOL> lines . extend ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> lines . append ( "<STR_LIT>" % pprint . pformat ( bkms , indent = <NUM_LIT:2> ) ) <EOL> aliases = ip . db . get ( '<STR_LIT>' , { } ) <EOL> if aliases : <EOL> lines . extend ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for k , v in list ( aliases . items ( ) ) : <EOL> try : <EOL> lines . append ( "<STR_LIT>" % ( k , repr ( v [ <NUM_LIT:1> ] ) ) ) <EOL> except ( AttributeError , TypeError ) : <EOL> pass <EOL> env = ip . db . get ( '<STR_LIT>' ) <EOL> if env : <EOL> lines . extend ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> lines . append ( "<STR_LIT>" % pprint . pformat ( env , indent = <NUM_LIT:2> ) ) <EOL> out = '<STR_LIT:\n>' . join ( lines ) <EOL> if filename : <EOL> open ( filename , '<STR_LIT:w>' ) . write ( out ) <EOL> else : <EOL> print ( out ) </s>
