<s> from __future__ import absolute_import , division , print_function <EOL> import os <EOL> import json <EOL> from glue . core import Subset <EOL> DISPATCH = { } <EOL> def save_page ( page , page_number , label , subset ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> result [ '<STR_LIT>' ] = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : len ( page ) } <EOL> result [ '<STR_LIT:name>' ] = str ( label ) <EOL> result [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> d = page [ <NUM_LIT:0> ] . _data [ <NUM_LIT:0> ] <EOL> unselected = dict ( opacity = d . style . alpha , <EOL> size = d . style . markersize / <NUM_LIT:2> , <EOL> color = d . style . color ) <EOL> result [ '<STR_LIT>' ] = dict ( unselected = unselected ) <EOL> if subset is not None : <EOL> s = subset . style <EOL> selected = dict ( opacity = s . alpha , size = s . markersize / <NUM_LIT:2> , color = s . color ) <EOL> result [ '<STR_LIT>' ] [ '<STR_LIT>' ] = selected <EOL> result [ '<STR_LIT>' ] = { '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' % page_number } <EOL> result [ '<STR_LIT>' ] = result [ '<STR_LIT>' ] <EOL> result [ '<STR_LIT>' ] = list ( map ( save_plot , page , range ( len ( page ) ) ) ) <EOL> return result <EOL> def save_plot_base ( plot , index ) : <EOL> result = { } <EOL> result [ '<STR_LIT>' ] = [ <NUM_LIT:0> , index ] <EOL> return result <EOL> def save_plot ( plot , index ) : <EOL> typ = type ( plot ) <EOL> return DISPATCH [ typ ] ( plot , index ) <EOL> def save_scatter ( plot , index ) : <EOL> """<STR_LIT>""" <EOL> result = save_plot_base ( plot , index ) <EOL> props = plot . properties <EOL> result [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> result [ '<STR_LIT>' ] = dict ( columnName = props [ '<STR_LIT>' ] . label , <EOL> range = [ props [ '<STR_LIT>' ] , props [ '<STR_LIT>' ] ] ) <EOL> result [ '<STR_LIT>' ] = dict ( columnName = props [ '<STR_LIT>' ] . label , <EOL> range = [ props [ '<STR_LIT>' ] , props [ '<STR_LIT>' ] ] ) <EOL> return result <EOL> def save_histogram ( plot , index ) : <EOL> """<STR_LIT>""" <EOL> result = save_plot_base ( plot , index ) <EOL> props = plot . properties <EOL> result [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> result [ '<STR_LIT>' ] = dict ( columnName = props [ '<STR_LIT>' ] . label , <EOL> bins = props [ '<STR_LIT>' ] , <EOL> range = [ props [ '<STR_LIT>' ] , props [ '<STR_LIT>' ] ] ) <EOL> return result <EOL> def stage_subsets ( application ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for page in application . viewers : <EOL> subset = None <EOL> for viewer in page : <EOL> for layer_artist in viewer . layers : <EOL> if not layer_artist . visible : <EOL> continue <EOL> s = layer_artist . layer <EOL> if not isinstance ( s , Subset ) : <EOL> continue <EOL> if subset is not None and s is not subset : <EOL> return None <EOL> if subset is None : <EOL> subset = s <EOL> result . append ( subset ) <EOL> return tuple ( result ) <EOL> def can_save_d3po ( application ) : <EOL> """<STR_LIT>""" <EOL> dc = application . session . data_collection <EOL> if len ( dc ) != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> data = dc [ <NUM_LIT:0> ] <EOL> for tab in application . viewers : <EOL> for viewer in tab : <EOL> if not isinstance ( viewer , DISPATCH . keys ( ) ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if sum ( len ( tab ) for tab in application . viewers ) == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if stage_subsets ( application ) is None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def make_data_file ( data , subsets , path ) : <EOL> """<STR_LIT>""" <EOL> from astropy . table import Table , Column <EOL> data_path = os . path . join ( path , '<STR_LIT>' ) <EOL> t = Table ( [ data [ c ] for c in data . components ] , <EOL> names = [ c . label for c in data . components ] ) <EOL> for i , subset in enumerate ( subsets ) : <EOL> if subset is None : <EOL> continue <EOL> c = Column ( data = subset . to_mask ( ) . astype ( '<STR_LIT:i>' ) , name = '<STR_LIT>' % i ) <EOL> t . add_column ( c ) <EOL> t . write ( data_path , format = '<STR_LIT:ascii>' , delimiter = '<STR_LIT:U+002C>' ) <EOL> def save_d3po ( application , path ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( path ) and not os . path . isdir ( path ) : <EOL> os . unlink ( path ) <EOL> if not os . path . exists ( path ) : <EOL> os . mkdir ( path ) <EOL> data = application . session . data_collection [ <NUM_LIT:0> ] <EOL> subsets = stage_subsets ( application ) <EOL> viewers = application . viewers <EOL> make_data_file ( data , subsets , path ) <EOL> result = { } <EOL> result [ '<STR_LIT:filename>' ] = '<STR_LIT>' <EOL> result [ '<STR_LIT:title>' ] = "<STR_LIT>" % data . label <EOL> result [ '<STR_LIT>' ] = list ( map ( save_page , application . viewers , <EOL> range ( len ( viewers ) ) , <EOL> application . tab_names , <EOL> subsets ) ) <EOL> state_path = os . path . join ( path , '<STR_LIT>' ) <EOL> with open ( state_path , '<STR_LIT:w>' ) as outfile : <EOL> json . dump ( result , outfile , indent = <NUM_LIT:2> ) <EOL> html_path = os . path . join ( path , '<STR_LIT>' ) <EOL> with open ( html_path , '<STR_LIT:w>' ) as outfile : <EOL> outfile . write ( HTML ) <EOL> launch ( path ) <EOL> def launch ( path ) : <EOL> """<STR_LIT>""" <EOL> from SocketServer import TCPServer <EOL> from SimpleHTTPServer import SimpleHTTPRequestHandler <EOL> from random import randrange <EOL> from socket import error <EOL> import webbrowser <EOL> from threading import Thread <EOL> os . chdir ( path ) <EOL> while True : <EOL> try : <EOL> PORT = randrange ( <NUM_LIT> , <NUM_LIT> ) <EOL> server = TCPServer ( ( "<STR_LIT>" , PORT ) , SimpleHTTPRequestHandler , False ) <EOL> server . allow_reuse_address = True <EOL> server . server_bind ( ) <EOL> break <EOL> except error : <EOL> pass <EOL> print ( '<STR_LIT>' % PORT ) <EOL> server . server_activate ( ) <EOL> thread = Thread ( target = server . serve_forever ) <EOL> thread . setDaemon ( True ) <EOL> thread . start ( ) <EOL> webbrowser . open ( '<STR_LIT>' % PORT ) <EOL> def setup ( ) : <EOL> from glue . config import exporters <EOL> exporters . add ( '<STR_LIT>' , save_d3po , can_save_d3po , outmode = '<STR_LIT>' ) <EOL> HTML = """<STR_LIT>""" <EOL> try : <EOL> from glue . viewers . scatter . qt import ScatterWidget <EOL> from glue . viewers . histogram . qt import HistogramWidget <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> DISPATCH [ ScatterWidget ] = save_scatter <EOL> DISPATCH [ HistogramWidget ] = save_histogram </s>
