def updateConfigFile(config, filename, rmnames): wrapper = TextWrapper() wrapper.width = 80 wrapper.break_long_words = False wrapper.break_on_hyphens = False wrap = lambda names: '\n'.join(wrapper.wrap(' '.join(names))) didChange = False for propertyName, values in config.items('glyphs'): glyphNames = values.split() propChanged = False glyphNames2 = [name for name in glyphNames if name not in rmnames] if len(glyphNames2) < len(glyphNames): print('[fontbuild.cfg] updating glyphs property', propertyName) config.set('glyphs', propertyName, wrap(glyphNames2)+'\n') didChange = True if didChange: s = StringIO() config.write(s) s = s.getvalue() s = re.sub(r'\n(\w+)\s+=\s*', '\n\\1: ', s, flags=re.M) s = re.sub(r'((?:^|\n)\[[^\]]*\])', '\\1\n', s, flags=re.M) s = re.sub(r'\n\t\n', '\n\n', s, flags=re.M) s = s.strip() + '\n' print('Writing', filename) if not dryRun: with open(filename, 'w') as f: f.write(s)
def renameConfigFile(config, filename, newNames, dryRun=False, print=print): wrapper = TextWrapper() wrapper.width = 80 wrapper.break_long_words = False wrapper.break_on_hyphens = False wrap = lambda names: '\n'.join(wrapper.wrap(' '.join(names))) didRename = False for propertyName, values in config.items('glyphs'): glyphNames = values.split() # print(propertyName, glyphNames) propChanged = False for name in glyphNames: if name in newNames: sectionChanged = True if sectionChanged: config.set('glyphs', propertyName, wrap(glyphNames) + '\n') didRename = True # config.set(section, option, value) if didRename: s = StringIO() config.write(s) s = s.getvalue() s = re.sub(r'\n(\w+)\s+=\s*', '\n\\1: ', s, flags=re.M) s = re.sub(r'((?:^|\n)\[[^\]]*\])', '\\1\n', s, flags=re.M) s = re.sub(r'\n\t\n', '\n\n', s, flags=re.M) s = s.strip() + '\n' print('Writing', filename) if not dryRun: with open(filename, 'w') as f: f.write(s)
def generate_file_rst(fname, target_dir, src_dir, plot_gallery): """ Generate the rst file for a given example. """ base_image_name = os.path.splitext(fname)[0] image_fname = '%s_%%s.png' % base_image_name this_template = rst_template last_dir = os.path.split(src_dir)[-1] # to avoid leading . in file names, and wrong names in links if last_dir == '.' or last_dir == 'examples': last_dir = '' else: last_dir += '_' short_fname = last_dir + fname src_file = os.path.join(src_dir, fname) example_file = os.path.join(target_dir, fname) shutil.copyfile(src_file, example_file) # The following is a list containing all the figure names figure_list = [] image_dir = os.path.join(target_dir, 'images') thumb_dir = os.path.join(image_dir, 'thumb') if not os.path.exists(image_dir): os.makedirs(image_dir) if not os.path.exists(thumb_dir): os.makedirs(thumb_dir) image_path = os.path.join(image_dir, image_fname) stdout_path = os.path.join(image_dir, 'stdout_%s.txt' % base_image_name) thumb_file = os.path.join(thumb_dir, fname[:-3] + '.png') if plot_gallery and fname.startswith('plot'): # generate the plot as png image if file name # starts with plot and if it is more recent than an # existing image. first_image_file = image_path % 1 if os.path.exists(stdout_path): stdout = open(stdout_path).read() else: stdout = '' if (not os.path.exists(first_image_file) or os.stat(first_image_file).st_mtime <= os.stat(src_file).st_mtime): # We need to execute the code print 'plotting %s' % fname t0 = time() import matplotlib.pyplot as plt plt.close('all') cwd = os.getcwd() try: # First CD in the original example dir, so that any file # created by the example get created in this directory orig_stdout = sys.stdout os.chdir(os.path.dirname(src_file)) my_stdout = StringIO() sys.stdout = my_stdout my_globals = {'pl': plt} execfile(os.path.basename(src_file), my_globals) sys.stdout = orig_stdout my_stdout = my_stdout.getvalue() if '__doc__' in my_globals: # The __doc__ is often printed in the example, we # don't with to echo it my_stdout = my_stdout.replace(my_globals['__doc__'], '') my_stdout = my_stdout.strip() if my_stdout: stdout = '**Script output**::\n\n %s\n\n' % ('\n '.join( my_stdout.split('\n'))) open(stdout_path, 'w').write(stdout) os.chdir(cwd) # In order to save every figure we have two solutions : # * iterate from 1 to infinity and call plt.fignum_exists(n) # (this requires the figures to be numbered # incrementally: 1, 2, 3 and not 1, 2, 5) # * iterate over [fig_mngr.num for fig_mngr in # matplotlib._pylab_helpers.Gcf.get_all_fig_managers()] for fig_num in (fig_mngr.num for fig_mngr in matplotlib. _pylab_helpers.Gcf.get_all_fig_managers()): # Set the fig_num figure as the current figure as we can't # save a figure that's not the current figure. plt.figure(fig_num) plt.savefig(image_path % fig_num) figure_list.append(image_fname % fig_num) except: print 80 * '_' print '%s is not compiling:' % fname traceback.print_exc() print 80 * '_' finally: os.chdir(cwd) sys.stdout = orig_stdout print " - time elapsed : %.2g sec" % (time() - t0) else: figure_list = [ f[len(image_dir):] for f in glob.glob(image_path % '[1-9]') ] #for f in glob.glob(image_path % '*')] # generate thumb file this_template = plot_rst_template from matplotlib import image if os.path.exists(first_image_file): image.thumbnail(first_image_file, thumb_file, 0.2) if not os.path.exists(thumb_file): # create something not to replace the thumbnail shutil.copy('images/blank_image.png', thumb_file) docstring, short_desc, end_row = extract_docstring(example_file) # Depending on whether we have one or more figures, we're using a # horizontal list or a single rst call to 'image'. if len(figure_list) == 1: figure_name = figure_list[0] image_list = SINGLE_IMAGE % figure_name.lstrip('/') else: image_list = HLIST_HEADER for figure_name in figure_list: image_list += HLIST_IMAGE_TEMPLATE % figure_name.lstrip('/') f = open(os.path.join(target_dir, fname[:-2] + 'rst'), 'w') f.write(this_template % locals()) f.flush()
def run_integration_test(input, args): input = StringIO(input.strip()) output = StringIO() run(['pawk'] + args, input, output) return output.getvalue().strip()
def __test_parser(self, parser, lossy=True): global width doc = open(self.DOC_FILE).read().decode('utf-8') assert isinstance(doc, unicode), doc expected_pxml = open(self.DOC_PXML_FILE).read().decode('utf-8') self.assertNotEqual(doc.strip(), u'', "Empty test file. "+ ("on <%s>" % self.DOC_FILE )) if self.VERBOSE: print self.DOC_FILE.ljust(width,'=') ## Compare parse trees, using pprint/pseudoxml representation warnings = StringIO() generated_tree = docutils.core.publish_parts( source=doc, source_path=self.DOC_FILE, parser=parser, settings_overrides={ 'warning_stream': warnings, #'input_encoding': 'unicode', #'output_encoding': 'unicode', #'error_encoding': 'unicode', #'error_encoding_error_handler': 'replace', #'warnings': 'test.log', }, writer_name='pprint')['whole'] warnings = warnings.getvalue() # print unified diff for PXML mismatch diff = "\n".join(list(unified_diff(expected_pxml.split('\n'), generated_tree.split('\n')))) if self.VERBOSE: out = [u''] original_out = expected_pxml.strip().split('\n') generated_out = generated_tree.strip().split('\n') out += [ (u'Original Tree ').ljust(width, '-') +u' '+ (u'Generated Tree ').ljust(width, '-') ] while original_out or generated_out: p1 = u'' p2 = u'' if original_out: p1 = original_out.pop(0) if generated_out: p2 = generated_out.pop(0) out += [ p1.ljust(width) +u' '+ p2.ljust(width) ] out += [u''] diff += "\n".join(out) # if self.VERBOSE: # print diff self.assertEqual( expected_pxml, generated_tree, "pxml tree mismatch \n "+ ("on <%s>" % self.DOC_FILE )+"\n\n"+ diff ) if warnings: self.assertFalse(warnings.strip(), "Error parsing test document\n "+ ("on <%s>" % self.DOC_FILE )+"\n\n"+ warnings) assert True
def __test_writer(self, writer, lossy=True): rst = open(self.DOC_FILE).read().decode('utf-8') if self.VERBOSE: print self.DOC_FILE.ljust(79,'=') self.assertNotEqual(rst.strip(), '', "Empty test file. "+ ("on <%s>" % self.DOC_FILE )) # Generate pseudoxml from source warnings = StringIO() original_tree = docutils.core.publish_parts( source=rst, source_path=self.DOC_FILE, #reader_name=self.TAG, settings_overrides={ 'warning_stream': warnings, # 'output_encoding': 'unicode', # 'output_encoding': 'ascii', #'input_encoding': 'unicode', }, writer_name='pseudoxml')['whole']#writer_name='dotmpe-rst') warnings = warnings.getvalue() if warnings: self.assertFalse(warnings.strip(), "Corrupt test source file: "+ ("on <%s>" % self.DOC_FILE )+"\n"+ warnings) return if self.VERBOSE: print " Original".ljust(79,'-') print original_tree # Publish the source file to rST, ie. regenerate the rST file warnings = StringIO() result = docutils.core.publish_parts( source=rst, source_path=self.DOC_FILE, #reader_name=reader_name, settings_overrides={ 'warning_stream': warnings, 'input_encoding': 'unicode', }, writer=writer)['whole']#writer_name='dotmpe-rst') if not lossy: self.assertEqual( result, rst, ("on <%s>" % self.DOC_FILE )+"\n"+ rst+'\n'+(''.rjust(79,'='))+'\n'+result ) self.assertNotEqual(result.strip(), '', "Empty generated file. "+ ("on <%s>" % self.DOC_FILE )) ## Compare parse trees, generate pseudoxml representation warnings = StringIO() generated_tree = docutils.core.publish_parts( source=result, source_path=self.DOC_FILE, #reader_name=reader_name, settings_overrides={ 'warning_stream': warnings, 'input_encoding': 'unicode', }, writer_name='pseudoxml')['whole'] warnings = warnings.getvalue() if warnings: self.assertFalse(warnings.strip(), "Error re-parsing generated file\n "+ ("on <%s>" % self.DOC_FILE )+"\n\n"+ warnings) diff = "\n".join(list(unified_diff(original_tree.split('\n'), generated_tree.split('\n')))) self.assertEqual( original_tree, generated_tree, "pxml tree mismatch \n "+ ("on <%s>" % self.DOC_FILE )+"\n\n"+ diff ) # original_tree+'\n'+(''.rjust(79,'='))+'\n'+generated_tree ) if self.VERBOSE: print " Generated".ljust(79,'-') print generated_tree print
def generate_file_rst(fname, target_dir, src_dir, plot_gallery): """ Generate the rst file for a given example. """ base_image_name = os.path.splitext(fname)[0] image_fname = '%s_%%s.png' % base_image_name this_template = rst_template last_dir = os.path.split(src_dir)[-1] # to avoid leading . in file names, and wrong names in links if last_dir == '.' or last_dir == 'examples': last_dir = '' else: last_dir += '_' short_fname = last_dir + fname src_file = os.path.join(src_dir, fname) example_file = os.path.join(target_dir, fname) shutil.copyfile(src_file, example_file) # The following is a list containing all the figure names figure_list = [] image_dir = os.path.join(target_dir, 'images') thumb_dir = os.path.join(image_dir, 'thumb') if not os.path.exists(image_dir): os.makedirs(image_dir) if not os.path.exists(thumb_dir): os.makedirs(thumb_dir) image_path = os.path.join(image_dir, image_fname) stdout_path = os.path.join(image_dir, 'stdout_%s.txt' % base_image_name) thumb_file = os.path.join(thumb_dir, fname[:-3] + '.png') if plot_gallery and fname.startswith('plot'): # generate the plot as png image if file name # starts with plot and if it is more recent than an # existing image. first_image_file = image_path % 1 if os.path.exists(stdout_path): stdout = open(stdout_path).read() else: stdout = '' if (not os.path.exists(first_image_file) or os.stat(first_image_file).st_mtime <= os.stat(src_file).st_mtime): # We need to execute the code print 'plotting %s' % fname t0 = time() import matplotlib.pyplot as plt plt.close('all') cwd = os.getcwd() try: # First CD in the original example dir, so that any file # created by the example get created in this directory orig_stdout = sys.stdout os.chdir(os.path.dirname(src_file)) my_stdout = StringIO() sys.stdout = my_stdout my_globals = {'pl': plt} execfile(os.path.basename(src_file), my_globals) sys.stdout = orig_stdout my_stdout = my_stdout.getvalue() if '__doc__' in my_globals: # The __doc__ is often printed in the example, we # don't with to echo it my_stdout = my_stdout.replace( my_globals['__doc__'], '') my_stdout = my_stdout.strip() if my_stdout: stdout = '**Script output**::\n\n %s\n\n' % ( '\n '.join(my_stdout.split('\n'))) open(stdout_path, 'w').write(stdout) os.chdir(cwd) # In order to save every figure we have two solutions : # * iterate from 1 to infinity and call plt.fignum_exists(n) # (this requires the figures to be numbered # incrementally: 1, 2, 3 and not 1, 2, 5) # * iterate over [fig_mngr.num for fig_mngr in # matplotlib._pylab_helpers.Gcf.get_all_fig_managers()] for fig_num in (fig_mngr.num for fig_mngr in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()): # Set the fig_num figure as the current figure as we can't # save a figure that's not the current figure. plt.figure(fig_num) plt.savefig(image_path % fig_num) figure_list.append(image_fname % fig_num) except: print 80 * '_' print '%s is not compiling:' % fname traceback.print_exc() print 80 * '_' finally: os.chdir(cwd) sys.stdout = orig_stdout print " - time elapsed : %.2g sec" % (time() - t0) else: figure_list = [f[len(image_dir):] for f in glob.glob(image_path % '[1-9]')] #for f in glob.glob(image_path % '*')] # generate thumb file this_template = plot_rst_template from matplotlib import image if os.path.exists(first_image_file): image.thumbnail(first_image_file, thumb_file, 0.2) if not os.path.exists(thumb_file): # create something not to replace the thumbnail shutil.copy('images/blank_image.png', thumb_file) docstring, short_desc, end_row = extract_docstring(example_file) # Depending on whether we have one or more figures, we're using a # horizontal list or a single rst call to 'image'. if len(figure_list) == 1: figure_name = figure_list[0] image_list = SINGLE_IMAGE % figure_name.lstrip('/') else: image_list = HLIST_HEADER for figure_name in figure_list: image_list += HLIST_IMAGE_TEMPLATE % figure_name.lstrip('/') f = open(os.path.join(target_dir, fname[:-2] + 'rst'), 'w') f.write(this_template % locals()) f.flush()