<s> from __future__ import absolute_import <EOL> import os <EOL> import sys <EOL> import argparse <EOL> import importlib <EOL> from contextlib import contextmanager <EOL> import yaml <EOL> from blaze . interactive import data as bz_data <EOL> from odo . utils import ignoring <EOL> from . server import Server , DEFAULT_PORT <EOL> try : <EOL> import __builtin__ as builtins <EOL> except ImportError : <EOL> import builtins <EOL> __all__ = '<STR_LIT>' , '<STR_LIT>' <EOL> def _spider ( resource_path , ignore , followlinks , hidden , extra_kwargs ) : <EOL> resources = { } <EOL> for filename in ( os . path . join ( resource_path , x ) <EOL> for x in os . listdir ( resource_path ) ) : <EOL> basename = os . path . basename ( filename ) <EOL> if ( basename . startswith ( os . curdir ) and not hidden or <EOL> os . path . islink ( filename ) and not followlinks ) : <EOL> continue <EOL> if os . path . isdir ( filename ) : <EOL> new_resources = _spider ( filename , ignore = ignore , <EOL> followlinks = followlinks , <EOL> hidden = hidden , <EOL> extra_kwargs = extra_kwargs ) <EOL> if new_resources : <EOL> resources [ basename ] = new_resources <EOL> else : <EOL> with ignoring ( * ignore ) : <EOL> resources [ basename ] = bz_data ( filename , ** ( extra_kwargs or { } ) ) <EOL> return resources <EOL> def data_spider ( path , <EOL> ignore = ( ValueError , NotImplementedError ) , <EOL> followlinks = True , <EOL> hidden = False , <EOL> extra_kwargs = None ) : <EOL> """<STR_LIT>""" <EOL> return { os . path . basename ( path ) : _spider ( path , <EOL> ignore = ignore , <EOL> followlinks = followlinks , <EOL> hidden = hidden , <EOL> extra_kwargs = extra_kwargs ) } <EOL> @ contextmanager <EOL> def pushd ( path ) : <EOL> """<STR_LIT>""" <EOL> cwd = os . getcwd ( ) <EOL> os . chdir ( os . path . abspath ( path ) ) <EOL> try : <EOL> yield <EOL> finally : <EOL> os . chdir ( cwd ) <EOL> def from_yaml ( fh , <EOL> ignore = ( ValueError , NotImplementedError ) , <EOL> followlinks = True , <EOL> hidden = False , <EOL> relative_to_yaml_dir = False ) : <EOL> """<STR_LIT>""" <EOL> resources = { } <EOL> yaml_dir = os . path . dirname ( os . path . abspath ( fh . name ) ) <EOL> for name , info in yaml . load ( fh . read ( ) ) . items ( ) : <EOL> with pushd ( yaml_dir if relative_to_yaml_dir else os . getcwd ( ) ) : <EOL> try : <EOL> source = info . pop ( '<STR_LIT:source>' ) <EOL> except KeyError : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> name ) <EOL> for mod in info . pop ( '<STR_LIT>' , [ ] ) : <EOL> importlib . import_module ( mod ) <EOL> if os . path . isdir ( source ) : <EOL> resources [ name ] = data_spider ( os . path . expanduser ( source ) , <EOL> ignore = ignore , <EOL> followlinks = followlinks , <EOL> hidden = hidden , <EOL> extra_kwargs = info ) <EOL> else : <EOL> resources [ name ] = bz_data ( source , ** info ) <EOL> return resources <EOL> def _parse_args ( ) : <EOL> p = argparse . ArgumentParser ( <EOL> formatter_class = argparse . ArgumentDefaultsHelpFormatter ) <EOL> p . add_argument ( '<STR_LIT:path>' , type = argparse . FileType ( '<STR_LIT:r>' ) , nargs = '<STR_LIT:?>' , <EOL> default = None , <EOL> help = '<STR_LIT>' ) <EOL> p . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = int , default = DEFAULT_PORT , <EOL> help = '<STR_LIT>' ) <EOL> p . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , default = '<STR_LIT:127.0.0.1>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_argument ( '<STR_LIT>' , '<STR_LIT>' , nargs = '<STR_LIT:+>' , <EOL> default = [ '<STR_LIT>' ] , <EOL> help = '<STR_LIT>' ) <EOL> p . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> args = p . parse_args ( ) <EOL> if not ( args . path or args . allow_dynamic_addition ) : <EOL> msg = "<STR_LIT>" <EOL> p . error ( msg ) <EOL> return args <EOL> def _main ( ) : <EOL> args = _parse_args ( ) <EOL> ignore = tuple ( getattr ( builtins , e ) for e in args . ignored_exception ) <EOL> if args . path : <EOL> resources = from_yaml ( args . path , <EOL> ignore = ignore , <EOL> followlinks = args . follow_links , <EOL> hidden = args . hidden , <EOL> relative_to_yaml_dir = args . yaml_dir ) <EOL> else : <EOL> resources = { } <EOL> server = Server ( resources , allow_add = args . allow_dynamic_addition ) <EOL> server . run ( host = args . host , port = args . port , debug = args . debug ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _main ( ) </s>
