Ignore:
Timestamp:
Sep 27, 2017 10:50:02 AM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
fca1f50
Parents:
69a6897 (diff), 83db1cc (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'ticket-887-reorg' into ticket-853-fit-gui-to-calc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sasgui/perspectives/fitting/report_dialog.py

    r7432acb r69a6897  
    3838        # number of images of plot 
    3939        self.nimages = len(self.report_list[2]) 
    40  
    41         if self.report_list[2] is not None: 
    42             # put image path in the report string 
    43             if len(self.report_list[2]) == 1: 
    44                 self.report_html = self.report_list[0] % \ 
    45                                     "memory:img_fit0.png" 
    46             elif len(self.report_list[2]) == 2: 
    47                 self.report_html = self.report_list[0] % \ 
    48                                     ("memory:img_fit0.png", 
    49                                      "memory:img_fit1.png") 
    50             # allows up to three images 
    51             else: 
    52                 self.report_html = self.report_list[0] % \ 
    53                                     ("memory:img_fit0.png", 
    54                                      "memory:img_fit1.png", 
    55                                      "memory:img_fit2.png") 
    56         else: 
    57             self.report_html = self.report_list[0] 
     40        self.report_html = self.report_list[0] 
    5841        # layout 
    5942        self._setup_layout() 
     
    7457 
    7558        fName = dlg.GetPath() 
     59        basename = os.path.splitext(fName)[0] 
    7660        ext_num = dlg.GetFilterIndex() 
     61        dlg.Destroy() 
     62 
     63        if ext_num == 0 and self.index_offset == 0:  # has pdf 
     64            ext = ".pdf" 
     65        elif ext_num == 1 - self.index_offset: 
     66            ext = ".html" 
     67        elif ext_num == 2 - self.index_offset: 
     68            ext = ".txt" 
     69        else: 
     70            logger.warn("unknown export format in report dialog") 
     71            return 
     72        filename = basename + ext 
     73 
     74        # save figures 
     75        pictures = [] 
     76        for num in range(self.nimages): 
     77            pic_name = basename + '_img%s.png' % num 
     78            # save the image for use with pdf writer 
     79            self.report_list[2][num].savefig(pic_name) 
     80            pictures.append(pic_name) 
     81 
     82        # translate png references int html from in-memory name to on-disk name 
     83        html = self.report_html.replace("memory:img_fit", basename+'_img') 
    7784 
    7885        #set file extensions 
    7986        img_ext = [] 
    80         pic_fname = [] 
    81         #PDF 
    82         if ext_num == (0 + 2 * self.index_offset): 
    83             # TODO: Sort this case out 
    84             ext = '.pdf' 
     87        if ext == ".pdf": 
     88            # write pdf as a pdf file 
     89            pdf = self.HTML2PDF(data=html, filename=filename) 
    8590 
    86             fName = os.path.splitext(fName)[0] + ext 
    87             dlg.Destroy() 
    88             #pic (png) file path/name 
    89             for num in range(self.nimages): 
    90                 im_ext = '_img%s.png' % num 
    91                 #img_ext.append(im_ext) 
    92                 pic_name = os.path.splitext(fName)[0] + im_ext 
    93                 pic_fname.append(pic_name) 
    94                 # save the image for use with pdf writer 
    95                 self.report_list[2][num].savefig(pic_name) 
     91            # delete images used to create the pdf 
     92            for pic_name in pictures: 
     93                os.remove(pic_name) 
    9694 
    97             #put the image path in html string 
    98             report_frame = self.report_list[0] 
    99             #put image name strings into the html file 
    100             #Note:The str for pic_fname shouldn't be removed. 
    101             if self.nimages == 1: 
    102                 html = report_frame % str(pic_fname[0]) 
    103             elif self.nimages == 2: 
    104                 html = report_frame % (str(pic_fname[0]), str(pic_fname[1])) 
    105             elif self.nimages == 3: 
    106                 html = report_frame % (str(pic_fname[0]), str(pic_fname[1]), 
    107                                           str(pic_fname[2])) 
    108  
    109             # make/open file in case of absence 
    110             f = open(fName, 'w') 
    111             f.close() 
    112             # write pdf as a pdf file 
    113             pdf = self.HTML2PDF(data=html, filename=fName) 
    114  
    115             #open pdf 
     95            #open pdf viewer 
    11696            if pdf: 
    11797                try: 
    118                     #Windows 
    119                     os.startfile(str(fName)) 
    120                 except: 
    121                     try: 
    122                         #Mac 
     98                    if os.name == 'nt':  # Windows 
     99                        os.startfile(fName) 
     100                    elif sys.platform == "darwin":  # Mac 
    123101                        os.system("open %s" % fName) 
    124                     except: 
    125                         #DO not open 
    126                         pass 
    127             #delete image file 
    128             for num in range(self.nimages): 
    129                 os.remove(pic_fname[num]) 
    130             return 
    131         #HTML + png(graph) 
    132         elif ext_num == (1 - self.index_offset): 
    133             ext = '.html' 
    134             for num in range(self.nimages): 
    135                 img_ext.append('_img4html%s.png' % num) 
    136             report_frame = self.report_list[0] 
    137         #TEXT + pdf(graph) 
    138         elif ext_num == (2 - self.index_offset): 
    139             ext = '.txt' 
    140             # changing the image extension actually changes the image 
    141             # format on saving 
    142             for num in range(self.nimages): 
    143                 img_ext.append('_img4txt%s.pdf' % num) 
    144             report = self.report_list[1] 
    145         else: 
    146             return 
     102                except Exception as exc: 
     103                    # cannot open pdf 
     104                    logging.error(str(exc)) 
    147105 
    148         #file name 
    149         fName = os.path.splitext(fName)[0] + ext 
    150         dlg.Destroy() 
     106        elif ext == ".html": 
     107            with open(filename, 'w') as f: 
     108                f.write(html) 
    151109 
    152         #pic (png) file path/name 
    153         for num in range(self.nimages): 
    154             pic_name = os.path.splitext(fName)[0] + img_ext[num] 
    155             pic_fname.append(pic_name) 
    156         #put the image path in html string 
    157         if ext_num == (1 - self.index_offset): 
    158             if self.nimages == 1: 
    159                 report = report_frame % os.path.basename(pic_fname[0]) 
    160             elif self.nimages == 2: 
    161                 report = report_frame % (os.path.basename(pic_fname[0]), 
    162                                          os.path.basename(pic_fname[1])) 
    163             elif self.nimages == 3: 
    164                 report = report_frame % (os.path.basename(pic_fname[0]), 
    165                                          os.path.basename(pic_fname[1]), 
    166                                          os.path.basename(pic_fname[2])) 
    167         f = open(fName, 'w') 
    168         f.write(report) 
    169         f.close() 
     110        elif ext == ".txt": 
     111            with open(filename, 'w') as f: 
     112                f.write(self.report_list[1]) 
     113 
    170114        self.Update() 
    171         #save png file using pic_fname 
    172         for num in range(self.nimages): 
    173             self.report_list[2][num].savefig(pic_fname[num]) 
Note: See TracChangeset for help on using the changeset viewer.