def test_write_waypoints(self): route = _Route((37.770776, -122.461689), (37.780776, -122.461689), waypoints=[(37.431257, -122.133121)]) with StringIO() as f: with _Writer(f) as writer: route.write(writer)
def get(self): '''Return the HTML map as a string (which includes one Google Map and all elements to be rendered).''' with StringIO() as f: with _Writer(f) as w: self._write_html(w) return f.getvalue()
def test_extra_dedentation(self): with StringIO() as f: with _Writer(f) as writer: writer.indent().write("Here's a sample indented line...") writer.dedent().write( "Here's another line before an extra dedent...") with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") writer.dedent() self.assertEqual( len(w), 1, "The extra dedent should raise a single warning") self.assertTrue( issubclass(w[-1].category, UserWarning), "The extra dedent should raise a 'UserWarning'") writer.write("And here's a third line after the extra dedent.") output_string = f.getvalue() EXPECTED_OUTPUT = '''\ Here's a sample indented line... Here's another line before an extra dedent... And here's a third line after the extra dedent. ''' self.assertEqual( output_string, EXPECTED_OUTPUT, _get_comparison_error_message(output_string, EXPECTED_OUTPUT))
def test_writing_multilines(self): with StringIO() as f: with _Writer(f) as writer: writer.write(''' Here's a sample line... List of random items: - First \t- Second - Third ''') output_string = f.getvalue() EXPECTED_OUTPUT = '''\ Here's a sample line... List of random items: - First - Second - Third ''' self.assertEqual( output_string, EXPECTED_OUTPUT, _get_comparison_error_message(output_string, EXPECTED_OUTPUT))
def draw(self, file): ''' Create the HTML file (which includes one Google Map and all elements to be rendered). :param file: File to write to, as a file path. ''' with open(file, 'w') as f: with _Writer(f) as w: self._write_html(w)
def test_exception_exit(self): with StringIO() as f: try: with _Writer(f) as writer: writer.write('Content') raise Exception writer.write('Unreachable content') except: self.assertEqual( f.getvalue(), '', "The written-to file should be empty if an exception is triggered" )
def _write_html(self, file): ''' Write the HTML map. Args: file (handle): File to write to. ''' with _Writer(file) as w: context = _Context() w.write(''' <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <title>{title}</title> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=visualization{key}"></script> <script type="text/javascript"> '''.format(title=self._title, key=('&key=%s' % self._apikey if self._apikey else ''))) w.indent() w.write('function initialize() {') w.indent() self._map.write(w) [drawable.write(w) for drawable in self._drawables] [marker.write(w, context) for marker in self._markers] if self._marker_dropper: self._marker_dropper.write(w, context) w.dedent() w.write('}') w.dedent() w.write(''' </script> </head> <body style="margin:0px; padding:0px;" onload="initialize()"> <div id="map_canvas" style="width: 100%; height: 100%;" /> </body> </html> ''')
def test_write(self): route = _Route((37.770776,-122.461689), (37.780776,-122.461689)) with StringIO() as f: with _Writer(f) as writer: route.write(writer)