Changeset 3241dd2 in sasview


Ignore:
Timestamp:
Sep 25, 2014 10:33:41 AM (10 years ago)
Author:
Jeff Krzywon <jeffery.krzywon@…>
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, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
a91bc5d
Parents:
72cdbec
Message:

Fixed the issue as described in ticket 269: "Failure to assign model when using attached data file" and made the change for ticket 264: "Remove .TIF extension from Load Data file extension filter."

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/sans/dataloader/readers/associations.py

    r5777106 r3241dd2  
    9191    logging.info("register_readers is now obsolete: use read_associations()") 
    9292    import abs_reader 
    93     import cansas_reader 
    9493    import ascii_reader 
    9594    import cansas_reader 
     
    9897    import IgorReader 
    9998    import red2d_reader 
    100     import tiff_reader 
     99    #import tiff_reader 
    101100    import nexus_reader 
    102101 
    103102    registry_function(abs_reader) 
    104     registry_function(cansas_reader) 
    105103    registry_function(ascii_reader) 
    106104    registry_function(cansas_reader) 
     
    109107    registry_function(IgorReader) 
    110108    registry_function(red2d_reader) 
    111     registry_function(tiff_reader) 
     109    #registry_function(tiff_reader) 
    112110    registry_function(nexus_reader) 
    113111     
  • src/sans/dataloader/readers/cansas_constants.py

    rac5b69d r3241dd2  
    3737                if name != "SASentry": 
    3838                    return_me.current_level = \ 
    39                             return_me.current_level.get("children").get(name, "") 
     39                        return_me.current_level.get("children").get(name, "") 
    4040                    if return_me.current_level == "": 
    4141                        return_me.current_level = \ 
     
    4444                    cl_datatype = return_me.current_level.get("storeas", "") 
    4545                    cl_units_optional = \ 
    46                                 return_me.current_level.get("units_required", "") 
    47                     # Where are how to store the variable for the given namespace 
    48                     # CANSAS_CONSTANTS tree is hierarchical, so is no value, inherit 
    49                     return_me.ns_variable = cl_variable if cl_variable != "" else \ 
    50                                         return_me.ns_variable 
    51                     return_me.ns_datatype = cl_datatype if cl_datatype != "" else \ 
    52                                         return_me.ns_datatype 
     46                             return_me.current_level.get("units_required", "") 
     47                    # Where are how to store the variable for the given 
     48                    # namespace CANSAS_CONSTANTS tree is hierarchical, so  
     49                    # is no value, inherit 
     50                    return_me.ns_variable = cl_variable if cl_variable != "" \ 
     51                        else return_me.ns_variable 
     52                    return_me.ns_datatype = cl_datatype if cl_datatype != "" \ 
     53                        else return_me.ns_datatype 
    5354                    return_me.ns_optional = cl_units_optional if \ 
    54                                         cl_units_optional != return_me.ns_optional \ 
     55                        cl_units_optional != return_me.ns_optional \ 
    5556                                        else return_me.ns_optional 
    5657            except AttributeError: 
     
    6162     
    6263     
    63     """ 
    64     CANSAS_NS holds the base namespace and the default schema file information 
    65     """ 
     64    def get_namespace_map(self): 
     65        """ 
     66        Helper method to get the names namespace list 
     67        """ 
     68        return self.names 
     69     
     70     
     71    # CANSAS_NS holds the base namespace and default schema file information 
    6672    CANSAS_NS = { 
    67                  "1.0" : { 
    68                           "ns" : "cansas1d/1.0",  
    69                           "schema" : "cansas1d_v1_0.xsd" 
    70                           }, 
    71                  "1.1" : { 
    72                           "ns" : "urn:cansas1d:1.1",  
    73                           "schema" : "cansas1d_v1_1.xsd" 
    74                           } 
     73                 "1.0" :  
     74                 { 
     75                  "ns" : "cansas1d/1.0",  
     76                  "schema" : "cansas1d_v1_0.xsd" 
     77                  }, 
     78                 "1.1" :  
     79                 { 
     80                  "ns" : "urn:cansas1d:1.1",  
     81                  "schema" : "cansas1d_v1_1.xsd" 
     82                  } 
    7583                 } 
    7684     
    7785     
    78     """ 
    79     The constants below hold information on where to store the CanSAS data when 
    80     loaded in using sasview 
    81     """ 
     86    # The constants below hold information on where to store the CanSAS data 
     87    # when loaded in using sasview 
    8288    META_DATA = "{0}.meta_data[\"{2}\"] = \"{1}\"" 
    8389    ANY = { 
     
    8995    SASPROCESS_TERM = { 
    9096                       "variable" : None, 
    91                        "attributes" : { 
    92                                        "unit" : {"variable" : None}, 
    93                                        "name" : {"variable" : None} 
    94                                        } 
     97                       "attributes" :  
     98                       { 
     99                        "unit" : {"variable" : None}, 
     100                        "name" : {"variable" : None} 
     101                        } 
    95102                       } 
    96103    SASPROCESS_SASPROCESSNOTE = { 
     
    103110                                "name" : {"variable" : "{0}.name = \'{1}\'"}, 
    104111                               "date" : {"variable" : "{0}.date = \'{1}\'"}, 
    105                                "description" : {"variable" : \ 
    106                                                "{0}.description = \'{1}\'"}, 
     112                               "description" :  
     113                               {"variable" : "{0}.description = \'{1}\'"}, 
    107114                               "term" : SASPROCESS_TERM, 
    108115                               "SASprocessnote" : SASPROCESS_SASPROCESSNOTE, 
     
    112119    RUN = { 
    113120           "variable" : "{0}.run.append(\"{1}\")", 
    114            "attributes" : {"name" : {"variable" : \ 
    115                                      "{0}.run_name[node_value] = \"{1}\""}} 
     121           "attributes" :  
     122           {"name" : {"variable" : "{0}.run_name[node_value] = \"{1}\""}} 
    116123           } 
    117124    SASDATA_IDATA_Q = { 
    118125                       "variable" : "{0}.x = numpy.append({0}.x, {1})", 
    119126                       "unit" : "x_unit", 
    120                        "attributes" : { 
    121                                        "unit" : { 
    122                                                  "variable" : \ 
    123                                                     "{0}._xunit = \"{1}\"", 
    124                                                     "storeas" : "content" 
    125                                                     } 
    126                                        } 
     127                       "attributes" :  
     128                       { 
     129                        "unit" :  
     130                        { 
     131                         "variable" : "{0}._xunit = \"{1}\"", 
     132                         "storeas" : "content" 
     133                         } 
     134                        }, 
    127135                       } 
    128136    SASDATA_IDATA_I = { 
    129137                       "variable" : "{0}.y = numpy.append({0}.y, {1})", 
    130138                       "unit" : "y_unit", 
    131                        "attributes" : { 
    132                                        "unit" : { 
    133                                                  "variable" : \ 
    134                                                     "{0}._yunit = \"{1}\"", 
    135                                                     "storeas" : "content" 
    136                                                 } 
    137                                        } 
     139                       "attributes" :  
     140                       { 
     141                        "unit" :  
     142                        { 
     143                         "variable" : "{0}._yunit = \"{1}\"", 
     144                         "storeas" : "content" 
     145                         } 
     146                        }, 
    138147                       } 
    139148    SASDATA_IDATA_IDEV = { 
    140                           "variable" : \ 
    141                           "{0}.dy = numpy.append({0}.dy, {1})", 
     149                          "variable" : "{0}.dy = numpy.append({0}.dy, {1})", 
    142150                          "unit" : "y_unit", 
    143                           "attributes" : { 
    144                                           "unit" : { 
    145                                                     "variable" : META_DATA, 
    146                                                     "storeas" : "content" 
    147                                                     } 
    148                                           }, 
     151                          "attributes" :  
     152                          { 
     153                           "unit" :  
     154                           { 
     155                            "variable" : META_DATA, 
     156                            "storeas" : "content" 
     157                            } 
     158                           }, 
    149159                          } 
    150160    SASDATA_IDATA_QDEV = { 
    151                           "variable" : \ 
    152                           "{0}.dx = numpy.append({0}.dx, {1})", 
     161                          "variable" : "{0}.dx = numpy.append({0}.dx, {1})", 
    153162                          "unit" : "x_unit", 
    154                           "attributes" : { 
    155                                           "unit" : { 
    156                                                     "variable" : META_DATA, 
    157                                                     "storeas" : "content" 
    158                                                     } 
    159                                           }, 
     163                          "attributes" :  
     164                          { 
     165                           "unit" :  
     166                           { 
     167                            "variable" : META_DATA, 
     168                            "storeas" : "content" 
     169                            } 
     170                           }, 
    160171                          } 
    161172    SASDATA_IDATA_DQL = { 
    162                          "variable" : \ 
    163                          "{0}.dxl = numpy.append({0}.dxl, {1})", 
     173                         "variable" : "{0}.dxl = numpy.append({0}.dxl, {1})", 
    164174                         "unit" : "x_unit", 
    165                          "attributes" : { 
    166                                          "unit" : { 
    167                                                    "variable" : META_DATA, 
    168                                                 "storeas" : "content" 
    169                                                 } 
    170                                          }, 
     175                         "attributes" :  
     176                         { 
     177                          "unit" :  
     178                          { 
     179                           "variable" : META_DATA, 
     180                           "storeas" : "content" 
     181                           } 
     182                          }, 
    171183                         } 
    172184    SASDATA_IDATA_DQW = { 
    173                          "variable" : \ 
    174                          "{0}.dxw = numpy.append({0}.dxw, {1})", 
     185                         "variable" : "{0}.dxw = numpy.append({0}.dxw, {1})", 
    175186                         "unit" : "x_unit", 
    176                          "attributes" : { 
    177                                          "unit" : { 
    178                                                    "variable" : META_DATA, 
    179                                                    "storeas" : "content" 
    180                                                    } 
    181                                          }, 
     187                         "attributes" :  
     188                         { 
     189                          "unit" :  
     190                          { 
     191                           "variable" : META_DATA, 
     192                           "storeas" : "content" 
     193                           } 
     194                          }, 
    182195                         } 
    183196    SASDATA_IDATA_QMEAN = { 
     
    228241                                 "variable" : "{0}.wavelength.append({1})", 
    229242                                 "unit" : "wavelength_unit", 
    230                                  "attributes" : { 
    231                                                  "unit" : { 
    232                                                             "variable" : "{0}.wavelength_unit = \"{1}\"", 
    233                                                             "storeas" : "content" 
    234                                                             } 
    235                                                  } 
     243                                 "attributes" :  
     244                                 { 
     245                                  "unit" :  
     246                                  { 
     247                                   "variable" : \ 
     248                                    "{0}.wavelength_unit = \"{1}\"", 
     249                                   "storeas" : "content" 
     250                                   } 
     251                                  } 
    236252                                 } 
    237253    SASTRANSSPEC_TDATA_T = { 
    238254                            "variable" : "{0}.transmission.append({1})", 
    239255                            "unit" : "transmission_unit", 
    240                             "attributes" : { 
    241                                             "unit" : { 
    242                                                       "variable" : "{0}.transmission_unit = \"{1}\"", 
    243                                                       "storeas" : "content" 
    244                                                       } 
    245                                             } 
     256                            "attributes" :  
     257                            { 
     258                             "unit" :  
     259                             { 
     260                              "variable" : "{0}.transmission_unit = \"{1}\"", 
     261                              "storeas" : "content" 
     262                              } 
     263                             } 
    246264                            } 
    247265    SASTRANSSPEC_TDATA_TDEV = { 
     
    249267                                    "{0}.transmission_deviation.append({1})", 
    250268                               "unit" : "transmission_deviation_unit", 
    251                                 "attributes" : { 
    252                                                 "unit" : { 
    253                                                           "variable" : "{0}.transmission_deviation_unit = \"{1}\"", 
    254                                                           "storeas" : "content" 
    255                                                           } 
    256                                                 } 
     269                               "attributes" : 
     270                               { 
     271                                "unit" : 
     272                                { 
     273                                 "variable" : \ 
     274                                    "{0}.transmission_deviation_unit = \"{1}\"", 
     275                                 "storeas" : "content" 
     276                                 } 
     277                                } 
    257278                               } 
    258279    SASTRANSSPEC_TDATA = { 
     
    272293                                  "<any>" : ANY, 
    273294                                  }, 
    274                     "attributes" : {"name" : {"variable" : \ 
    275                                               "{0}.name = \"{1}\""}, 
    276                                     "timestamp" : {"variable" : \ 
    277                                                    "{0}.timestamp = \"{1}\""}, 
    278                                     } 
     295                    "attributes" :  
     296                    { 
     297                     "name" : 
     298                     { 
     299                      "variable" : "{0}.name = \"{1}\""}, 
     300                      "timestamp" :  
     301                      { 
     302                       "variable" : "{0}.timestamp = \"{1}\"" 
     303                       }, 
     304                     } 
    279305                    } 
    280306    SASSAMPLE_THICK = { 
     
    282308                       "unit" : "sample.thickness_unit", 
    283309                       "storeas" : "float", 
    284                        "attributes" : { 
    285                                        "units" : { 
    286                                                   "variable" : "{0}.sample.thickness_unit = \"{1}\"", 
    287                                                   "storeas" : "content" 
    288                                                   } 
    289                                        }, 
     310                       "attributes" :  
     311                       { 
     312                        "unit" :  
     313                        { 
     314                         "variable" : "{0}.sample.thickness_unit = \"{1}\"", 
     315                         "storeas" : "content" 
     316                         } 
     317                        }, 
    290318                       } 
    291319    SASSAMPLE_TRANS = { 
     
    297325                      "unit" : "sample.temperature_unit", 
    298326                      "storeas" : "float", 
    299                       "attributes" : { 
    300                                       "units" : { 
    301                                                  "variable" : "{0}.sample.temperature_unit = \"{1}\"", 
    302                                                  "storeas" : "content" 
    303                                                  } 
    304                                       }, 
     327                      "attributes" :  
     328                      { 
     329                       "unit" :  
     330                       { 
     331                        "variable" : "{0}.sample.temperature_unit = \"{1}\"", 
     332                        "storeas" : "content" 
     333                        } 
     334                       }, 
    305335                      } 
    306336    SASSAMPLE_POS_ATTR = { 
    307                           "units" : { 
     337                          "unit" : { 
    308338                                     "variable" : \ 
    309339                                        "{0}.sample.position_unit = \"{1}\"", 
     
    338368                     } 
    339369    SASSAMPLE_ORIENT_ATTR = { 
    340                              "units" : { 
    341                                         "variable" : "{0}.sample.orientation_unit = \"{1}\"", 
    342                                         "storeas" : "content" 
    343                                         } 
     370                             "unit" :  
     371                             { 
     372                              "variable" : \ 
     373                                    "{0}.sample.orientation_unit = \"{1}\"", 
     374                              "storeas" : "content" 
     375                              } 
    344376                             } 
    345377    SASSAMPLE_ORIENT_ROLL = { 
     
    370402                        } 
    371403    SASSAMPLE = { 
    372                  "attributes" : {"name" : {\ 
    373                                     "variable" : "{0}.sample.name = \"{1}\""},}, 
     404                 "attributes" :  
     405                    {"name" : {"variable" : "{0}.sample.name = \"{1}\""},}, 
    374406                 "variable" : None, 
    375407                 "children" : { 
     
    387419    SASINSTR_SRC_BEAMSIZE_ATTR = { 
    388420                                  "unit" : \ 
    389                                     "{0}.source.beam_size_unit = \"{1}\"", 
     421                                        "{0}.source.beam_size_unit = \"{1}\"", 
    390422                                  "storeas" : "content" 
    391423                                  } 
     
    409441                               } 
    410442    SASINSTR_SRC_BEAMSIZE = { 
    411                              "attributes" : {"name" : {"variable" : \ 
    412                                         "{0}.source.beam_size_name = \"{1}\""}}, 
     443                             "attributes" :  
     444                                {"name" : {"variable" : \ 
     445                                    "{0}.source.beam_size_name = \"{1}\""}}, 
    413446                             "variable" : None, 
    414447                             "children" : { 
     
    422455                       "unit" : "source.wavelength_unit", 
    423456                       "storeas" : "float", 
    424                        "attributes" : { 
    425                                        "unit" : { 
    426                                                  "variable" : "{0}.source.wavelength_unit = \"{1}\"", 
    427                                                  "storeas" : "content" 
    428                                                  }, 
    429                                        } 
     457                       "attributes" :  
     458                       { 
     459                        "unit" :  
     460                        { 
     461                         "variable" : "{0}.source.wavelength_unit = \"{1}\"", 
     462                         "storeas" : "content" 
     463                         }, 
     464                        } 
    430465                       } 
    431466    SASINSTR_SRC_WL_MIN = { 
     
    433468                           "unit" : "source.wavelength_min_unit", 
    434469                           "storeas" : "float", 
    435                            "attributes" : { 
    436                                            "unit" : { 
    437                                                      "variable" : "{0}.source.wavelength_min_unit = \"{1}\"",   
    438                                                      "storeas" : "content" 
    439                                                      }, 
    440                                            } 
     470                           "attributes" :  
     471                           { 
     472                            "unit" :  
     473                            { 
     474                             "variable" : \ 
     475                                "{0}.source.wavelength_min_unit = \"{1}\"",   
     476                             "storeas" : "content" 
     477                             }, 
     478                            } 
    441479                           } 
    442480    SASINSTR_SRC_WL_MAX = { 
     
    444482                           "unit" : "source.wavelength_max_unit", 
    445483                           "storeas" : "float", 
    446                            "attributes" : { 
    447                                            "unit" : { 
    448                                                      "variable" : "{0}.source.wavelength_max_unit = \"{1}\"",   
    449                                                      "storeas" : "content" 
    450                                                      }, 
    451                                            } 
     484                           "attributes" :  
     485                           { 
     486                            "unit" :  
     487                            { 
     488                             "variable" : \ 
     489                                "{0}.source.wavelength_max_unit = \"{1}\"",   
     490                             "storeas" : "content" 
     491                             }, 
     492                            } 
    452493                           } 
    453494    SASINSTR_SRC_WL_SPR = { 
     
    455496                           "unit" : "source.wavelength_spread_unit", 
    456497                           "storeas" : "float", 
    457                            "attributes" : { 
    458                                            "unit" : { 
    459                                                      "variable" : "{0}.source.wavelength_spread_unit = \"{1}\"",   
    460                                                      "storeas" : "content" 
    461                                                      }, 
    462                                            } 
     498                           "attributes" :  
     499                           { 
     500                            "unit" :  
     501                            { 
     502                             "variable" : \ 
     503                                "{0}.source.wavelength_spread_unit = \"{1}\"",   
     504                             "storeas" : "content" 
     505                             }, 
     506                            } 
    463507                           } 
    464508    SASINSTR_SRC = { 
     
    503547                            } 
    504548    SASINSTR_COLL_APER_SIZE = { 
    505                                "attributes" : {"unit" : {"variable" : \ 
    506                                                     "{0}.size_unit = \"{1}\""}}, 
     549                               "attributes" :  
     550                               {"unit" : {"variable" : \ 
     551                                            "{0}.size_unit = \"{1}\""}}, 
    507552                               "children" : { 
    508553                                             "storeas" : "float", 
     
    514559    SASINSTR_COLL_APER_DIST = { 
    515560                               "storeas" : "float", 
    516                                "attributes" : { 
    517                                                "storeas" : "content", 
    518                                                "unit" : {"variable" : \ 
    519                                                 "{0}.distance_unit = \"{1}\""}}, 
     561                               "attributes" :  
     562                               { 
     563                                "storeas" : "content", 
     564                                "unit" : {"variable" : \ 
     565                                            "{0}.distance_unit = \"{1}\""} 
     566                                }, 
    520567                               "variable" : "{0}.distance = {1}", 
    521568                               "unit" : "distance_unit", 
     
    535582                          } 
    536583    SASINSTR_COLL = { 
    537                      "attributes" : {"name" : {"variable" : \ 
    538                                                "{0}.name = \"{1}\""}}, 
     584                     "attributes" :  
     585                     {"name" : {"variable" : "{0}.name = \"{1}\""}}, 
    539586                     "variable" : None, 
    540                      "children" : { 
    541                                    "length" : { 
    542                                             "variable" : "{0}.length = {1}", 
    543                                             "unit" : "length_unit", 
    544                                             "storeas" : "float", 
    545                                             "attributes" : { 
    546                                                             "storeas" : \ 
    547                                                                 "content", 
    548                                                             "unit" : {"variable" : "{0}.length_unit = \"{1}\""} 
    549                                                             }, 
    550                                             }, 
    551                                    "aperture" : SASINSTR_COLL_APER, 
    552                                    }, 
     587                     "children" :  
     588                     { 
     589                      "length" :  
     590                      { 
     591                       "variable" : "{0}.length = {1}", 
     592                       "unit" : "length_unit", 
     593                       "storeas" : "float", 
     594                       "attributes" :  
     595                       { 
     596                        "storeas" : "content", 
     597                        "unit" : {"variable" : "{0}.length_unit = \"{1}\""} 
     598                        }, 
     599                       }, 
     600                      "aperture" : SASINSTR_COLL_APER, 
     601                      }, 
    553602                     } 
    554603    SASINSTR_DET_SDD = { 
    555604                        "variable" : "{0}.distance = {1}", 
    556605                        "unit" : "distance_unit", 
    557                         "attributes" : { 
    558                                         "unit" : { 
    559                                                   "variable" : \ 
    560                                                   "{0}.distance_unit = \"{1}\"", 
    561                                                   "storeas" : "content" 
    562                                                   } 
    563                                         }, 
     606                        "attributes" :  
     607                        { 
     608                         "unit" :  
     609                         { 
     610                          "variable" : "{0}.distance_unit = \"{1}\"", 
     611                          "storeas" : "content" 
     612                          } 
     613                         }, 
    564614                        } 
    565615    SASINSTR_DET_OFF_ATTR = { 
     
    622672                         "variable" : "{0}.beam_center.x = {1}", 
    623673                         "unit" : "beam_center_unit", 
    624                          "attributes" : { 
    625                                          "unit" : \ 
    626                                             "{0}.beam_center_unit = \"{1}\"", 
    627                                          "storeas" : "content" 
    628                                          } 
     674                         "attributes" :  
     675                         { 
     676                          "unit" : "{0}.beam_center_unit = \"{1}\"", 
     677                          "storeas" : "content" 
     678                          } 
    629679                         } 
    630680    SASINSTR_DET_BC_Y = { 
    631681                         "variable" : "{0}.beam_center.y = {1}", 
    632682                         "unit" : "beam_center_unit", 
    633                          "attributes" : { 
    634                                          "unit" : \ 
    635                                             "{0}.beam_center_unit = \"{1}\"", 
    636                                          "storeas" : "content" 
    637                                          } 
     683                         "attributes" :  
     684                         { 
     685                          "unit" : "{0}.beam_center_unit = \"{1}\"", 
     686                          "storeas" : "content" 
     687                          } 
    638688                         } 
    639689    SASINSTR_DET_BC_Z = { 
    640690                         "variable" : "{0}.beam_center.z = {1}", 
    641691                         "unit" : "beam_center_unit", 
    642                          "attributes" : { 
    643                                          "unit" : \ 
    644                                             "{0}.beam_center_unit = \"{1}\"", 
    645                                          "storeas" : "content" 
    646                                          } 
     692                         "attributes" :  
     693                         { 
     694                          "unit" : "{0}.beam_center_unit = \"{1}\"", 
     695                          "storeas" : "content" 
     696                          } 
    647697                         } 
    648698    SASINSTR_DET_BC = { 
     
    657707                        "variable" : "{0}.pixel_size.x = {1}", 
    658708                        "unit" : "pixel_size_unit", 
    659                         "attributes" : { 
    660                                         "unit" : \ 
    661                                            "{0}.pixel_size_unit = \"{1}\"", 
    662                                         "storeas" : "content" 
    663                                         } 
     709                        "attributes" :  
     710                        { 
     711                         "unit" : "{0}.pixel_size_unit = \"{1}\"", 
     712                         "storeas" : "content" 
     713                         } 
    664714                        } 
    665715    SASINSTR_DET_PIXEL_Y = { 
    666716                        "variable" : "{0}.pixel_size.y = {1}", 
    667717                        "unit" : "pixel_size_unit", 
    668                         "attributes" : { 
    669                                         "unit" : \ 
    670                                            "{0}.pixel_size_unit = \"{1}\"", 
    671                                         "storeas" : "content" 
    672                                         } 
     718                        "attributes" :  
     719                        { 
     720                         "unit" : "{0}.pixel_size_unit = \"{1}\"", 
     721                         "storeas" : "content" 
     722                         } 
    673723                        } 
    674724    SASINSTR_DET_PIXEL_Z = { 
    675725                        "variable" : "{0}.pixel_size.z = {1}", 
    676726                        "unit" : "pixel_size_unit", 
    677                         "attributes" : { 
    678                                         "unit" : \ 
    679                                            "{0}.pixel_size_unit = \"{1}\"", 
    680                                         "storeas" : "content" 
    681                                         } 
     727                        "attributes" :  
     728                        { 
     729                         "unit" : "{0}.pixel_size_unit = \"{1}\"", 
     730                         "storeas" : "content" 
     731                         } 
    682732                        } 
    683733    SASINSTR_DET_PIXEL = { 
     
    692742                         "variable" : "{0}.slit_length = {1}", 
    693743                         "unit" : "slit_length_unit", 
    694                          "attributes" : { 
    695                                          "unit" : { 
    696                                                    "variable" : "{0}.slit_length_unit = \"{1}\"", 
    697                                                    "storeas" : "content" 
    698                                                    } 
    699                                          } 
     744                         "attributes" :  
     745                         { 
     746                          "unit" :  
     747                          { 
     748                           "variable" : "{0}.slit_length_unit = \"{1}\"", 
     749                           "storeas" : "content" 
     750                           } 
     751                          } 
    700752                         } 
    701753    SASINSTR_DET = { 
     
    703755                    "variable" : None, 
    704756                    "attributes" : { 
    705                                     "name" : { 
    706                                               "storeas" : "content", 
    707                                               "variable" : "{0}.name = \"{1}\"", 
    708                                               } 
     757                                    "name" :  
     758                                    { 
     759                                     "storeas" : "content", 
     760                                     "variable" : "{0}.name = \"{1}\"", 
     761                                     } 
    709762                                    }, 
    710763                    "children" : { 
     
    723776    SASINSTR = { 
    724777                "variable" : None, 
    725                 "children" : { 
    726                               "variable" : None, 
    727                               "name" : {"variable" : \ 
    728                                            "{0}.instrument = \"{1}\""}, 
    729                               "SASsource" : SASINSTR_SRC, 
    730                               "SAScollimation" : SASINSTR_COLL, 
    731                               "SASdetector" : SASINSTR_DET, 
    732                               }, 
     778                "children" :  
     779                { 
     780                 "variable" : None, 
     781                 "name" : {"variable" : "{0}.instrument = \"{1}\""}, 
     782                 "SASsource" : SASINSTR_SRC, 
     783                 "SAScollimation" : SASINSTR_COLL, 
     784                 "SASdetector" : SASINSTR_DET, 
     785                 }, 
    733786                } 
    734787    CANSAS_FORMAT = { 
    735                      "SASentry" : { 
    736                                    "units_optional" : True, 
    737                                    "variable" : None, 
    738                                    "storeas" : "content", 
    739                                    "attributes" : {"name" : {"variable" : \ 
    740                                         "{0}.run_name[node_value] = \"{1}\""}}, 
    741                                    "children" : { 
    742                                                  "Title" : TITLE, 
    743                                                  "Run" : RUN, 
    744                                                  "SASdata" : SASDATA, 
    745                                                  "SAStransmission_spectrum" : \ 
    746                                                     SASTRANSSPEC, 
    747                                                  "SASsample" : SASSAMPLE, 
    748                                                  "SASinstrument" : SASINSTR, 
    749                                                  "SASprocess" : SASPROCESS, 
    750                                                  "SASnote" : SASNOTE, 
    751                                                  "<any>" : ANY, 
    752                                                  } 
    753                                    } 
     788                     "SASentry" :  
     789                     { 
     790                      "units_optional" : True, 
     791                      "variable" : None, 
     792                      "storeas" : "content", 
     793                      "attributes" : {"name" : {"variable" : \ 
     794                                    "{0}.run_name[node_value] = \"{1}\""}}, 
     795                      "children" : { 
     796                                    "Title" : TITLE, 
     797                                    "Run" : RUN, 
     798                                    "SASdata" : SASDATA, 
     799                                    "SAStransmission_spectrum" : SASTRANSSPEC, 
     800                                    "SASsample" : SASSAMPLE, 
     801                                    "SASinstrument" : SASINSTR, 
     802                                    "SASprocess" : SASPROCESS, 
     803                                    "SASnote" : SASNOTE, 
     804                                    "<any>" : ANY, 
     805                                    } 
     806                      } 
    754807                     } 
    755     
     808 
     809 
    756810class CurrentLevel: 
    757811    """ 
     
    769823        self.ns_datatype = "content" 
    770824        self.ns_optional = True 
     825         
     826    def get_current_level(self): 
     827        """ 
     828        Helper method to get the current_level map 
     829        """ 
     830        return self.current_level 
     831     
     832    def get_data_type(self): 
     833        """ 
     834        Helper method to get the ns_datatype label 
     835        """ 
     836        return self.ns_datatype 
     837     
     838    def get_variable(self): 
     839        """ 
     840        Helper method to get the ns_variable label 
     841        """ 
     842        return self.ns_variable 
  • src/sans/dataloader/readers/cansas_reader.py

    r51af54b r3241dd2  
    3535PREPROCESS = "xmlpreprocess" 
    3636ENCODING = "encoding" 
     37RUN_NAME_DEFAULT = "None" 
    3738HAS_CONVERTER = True 
    3839try: 
    3940    from sans.data_util.nxsunit import Converter 
    40 except: 
     41except ImportError: 
    4142    HAS_CONVERTER = False 
    4243 
     
    7475# Called by outside packages: 
    7576#    sans.perspectives.fitting.pagestate 
    76 def write_node(doc, parent, name, value, attr={}): 
     77def write_node(doc, parent, name, value, attr=None): 
    7778    """ 
    7879    :param doc: document DOM 
     
    8485    :return: True if something was appended, otherwise False 
    8586    """ 
     87    if attr is None: 
     88        attr = {} 
    8689    if value is not None: 
    8790        node = doc.createElement(name) 
     
    103106    ##CanSAS version - defaults to version 1.0 
    104107    cansas_version = "1.0" 
     108    base_ns = "{cansas1d/1.0}" 
    105109     
    106110    logging = [] 
     
    116120    allow_all = True 
    117121     
     122     
    118123    def __init__(self): 
    119124        ## List of errors 
    120125        self.errors = [] 
     126        self.encoding = None 
     127         
    121128         
    122129    def is_cansas(self, ext="xml"): 
    123130        """ 
    124131        Checks to see if the xml file is a CanSAS file 
     132         
     133        :param ext: The file extension of the data file 
    125134        """ 
    126135        if self.validate_xml(): 
     
    134143        return False 
    135144     
    136     def read(self, xml): 
    137         """ 
    138         Validate and read in an xml file in the canSAS format. 
    139          
    140         :param xml: A canSAS file path in proper XML format 
    141         """ 
    142         # X - Q value; Y - Intensity (Abs) 
    143         x_vals = numpy.empty(0) 
    144         y_vals = numpy.empty(0) 
    145         dx_vals = numpy.empty(0) 
    146         dy_vals = numpy.empty(0) 
    147         dxl = numpy.empty(0) 
    148         dxw = numpy.empty(0) 
     145     
     146    def load_file_and_schema(self, xml_file): 
     147        """ 
     148        Loads the file and associates a schema, if a known schema exists 
     149         
     150        :param xml_file: The xml file path sent to Reader.read 
     151        """ 
     152        base_name = xml_reader.__file__ 
     153        base_name = base_name.replace("\\","/") 
     154        base = base_name.split("/sans/")[0] 
     155         
     156        # Load in xml file and get the cansas version from the header 
     157        self.set_xml_file(xml_file) 
     158        self.cansas_version = self.xmlroot.get("version", "1.0") 
     159         
     160        # Generic values for the cansas file based on the version 
     161        cansas_defaults = CANSAS_NS.get(self.cansas_version, "1.0") 
     162        schema_path = "{0}/sans/dataloader/readers/schema/{1}".format\ 
     163                (base, cansas_defaults.get("schema")).replace("\\", "/") 
     164         
     165        # Link a schema to the XML file. 
     166        self.set_schema(schema_path) 
     167        return cansas_defaults 
     168                 
     169     
     170    def read(self, xml_file): 
     171        """ 
     172        Validate and read in an xml_file file in the canSAS format. 
     173         
     174        :param xml_file: A canSAS file path in proper XML format 
     175        """ 
    149176         
    150177        # output - Final list of Data1D objects 
    151178        output = [] 
    152         # ns - Namespace hierarchy for current xml object 
     179        # ns - Namespace hierarchy for current xml_file object 
    153180        ns_list = [] 
    154181         
    155182        # Check that the file exists 
    156         if os.path.isfile(xml): 
    157             basename = os.path.basename(xml) 
     183        if os.path.isfile(xml_file): 
     184            basename = os.path.basename(xml_file) 
    158185            _, extension = os.path.splitext(basename) 
    159186            # If the file type is not allowed, return nothing 
    160187            if extension in self.ext or self.allow_all: 
    161188                # Get the file location of  
    162                 base_name = xml_reader.__file__ 
    163                 base_name = base_name.replace("\\","/") 
    164                 base = base_name.split("/sans/")[0] 
    165                  
    166                 # Load in xml file and get the cansas version from the header 
    167                 self.set_xml_file(xml) 
    168                 root = self.xmlroot 
    169                 if root is None: 
    170                     root = {} 
    171                 self.cansas_version = root.get("version", "1.0") 
    172                  
    173                 # Generic values for the cansas file based on the version 
    174                 cansas_defaults = CANSAS_NS.get(self.cansas_version, "1.0") 
    175                 schema_path = "{0}/sans/dataloader/readers/schema/{1}".format\ 
    176                         (base, cansas_defaults.get("schema")).replace("\\", "/") 
    177                  
    178                 # Link a schema to the XML file. 
    179                 self.set_schema(schema_path) 
     189                cansas_defaults = self.load_file_and_schema(xml_file) 
    180190                 
    181191                # Try to load the file, but raise an error if unable to. 
     
    184194                    if self.is_cansas(extension): 
    185195                        # Get each SASentry from XML file and add it to a list. 
    186                         entry_list = root.xpath('/ns:SASroot/ns:SASentry', 
    187                                 namespaces={'ns': cansas_defaults.get("ns")}) 
     196                        entry_list = self.xmlroot.xpath( 
     197                                '/ns:SASroot/ns:SASentry',  
     198                                namespaces = {'ns': cansas_defaults.get("ns")}) 
    188199                        ns_list.append("SASentry") 
    189200                         
    190201                        # If multiple files, modify the name for each is unique 
    191                         multiple_files = len(entry_list) - 1 
    192202                        increment = 0 
    193                         name = basename 
    194203                        # Parse each SASentry item 
    195204                        for entry in entry_list: 
    196205                            # Define a new Data1D object with zeroes for  
    197206                            # x_vals and y_vals 
    198                             data1d = Data1D(x_vals, y_vals, dx_vals, dy_vals) 
    199                             data1d.dxl = dxl 
    200                             data1d.dxw = dxw 
     207                            data1d = Data1D(numpy.empty(0), numpy.empty(0), \ 
     208                                            numpy.empty(0), numpy.empty(0)) 
     209                            data1d.dxl = numpy.empty(0) 
     210                            data1d.dxw = numpy.empty(0) 
    201211                             
    202212                            # If more than one SASentry, increment each in order 
    203                             if multiple_files: 
     213                            name = basename 
     214                            if len(entry_list) - 1 > 0: 
    204215                                name += "_{0}".format(increment) 
    205216                                increment += 1 
     
    220231                            del extras[:] 
    221232                             
    222                             # Final cleanup 
    223                             # Remove empty nodes, verify array sizes are correct 
    224                             for error in self.errors: 
    225                                 return_value.errors.append(error) 
    226                             del self.errors[:] 
    227                             numpy.trim_zeros(return_value.x) 
    228                             numpy.trim_zeros(return_value.y) 
    229                             numpy.trim_zeros(return_value.dy) 
    230                             size_dx = return_value.dx.size 
    231                             size_dxl = return_value.dxl.size 
    232                             size_dxw = return_value.dxw.size 
    233                             if size_dxl == 0 and size_dxw == 0: 
    234                                 return_value.dxl = None 
    235                                 return_value.dxw = None 
    236                                 numpy.trim_zeros(return_value.dx) 
    237                             elif size_dx == 0: 
    238                                 return_value.dx = None 
    239                                 size_dx = size_dxl 
    240                                 numpy.trim_zeros(return_value.dxl) 
    241                                 numpy.trim_zeros(return_value.dxw) 
     233                            return_value = self._final_cleanup(return_value) 
    242234                            output.append(return_value) 
    243235                    else: 
    244                         value = self.find_invalid_xml() 
    245                         output.append("Invalid XML at: {0}".format(value)) 
     236                        output.append("Invalid XML at: {0}".format(\ 
     237                                                    self.find_invalid_xml())) 
    246238                except: 
    247239                    # If the file does not match the schema, raise this error 
    248                     raise RuntimeError, "%s cannot be read" % xml 
     240                    raise RuntimeError, "%s cannot be read" % xml_file 
    249241                return output 
    250242        # Return a list of parsed entries that dataloader can manage 
    251243        return None 
     244     
     245     
     246    def _final_cleanup(self, data1d): 
     247        """ 
     248        Final cleanup of the Data1D object to be sure it has all the 
     249        appropriate information needed for perspectives 
     250         
     251        :param data1d: Data1D object that has been populated 
     252        """ 
     253        # Final cleanup 
     254        # Remove empty nodes, verify array sizes are correct 
     255        for error in self.errors: 
     256            data1d.errors.append(error) 
     257        del self.errors[:] 
     258        numpy.trim_zeros(data1d.x) 
     259        numpy.trim_zeros(data1d.y) 
     260        numpy.trim_zeros(data1d.dy) 
     261        size_dx = data1d.dx.size 
     262        size_dxl = data1d.dxl.size 
     263        size_dxw = data1d.dxw.size 
     264        if size_dxl == 0 and size_dxw == 0: 
     265            data1d.dxl = None 
     266            data1d.dxw = None 
     267            numpy.trim_zeros(data1d.dx) 
     268        elif size_dx == 0: 
     269            data1d.dx = None 
     270            size_dx = size_dxl 
     271            numpy.trim_zeros(data1d.dxl) 
     272            numpy.trim_zeros(data1d.dxw) 
     273        return data1d 
     274                             
    252275     
    253276    def _create_unique_key(self, dictionary, name, numb = 0): 
     
    268291     
    269292     
    270     def _unit_conversion(self, new_current_level, attr, data1d, \ 
    271                                     tagname, node_value, optional = True): 
     293    def _unit_conversion(self, node, new_current_level, data1d, \ 
     294                                                tagname, node_value): 
    272295        """ 
    273296        A unit converter method used to convert the data included in the file 
     
    279302        :param data1d: Where the values will be saved 
    280303        :param node_value: The value of the current dom node 
    281         :param optional: Boolean that says if the units are required 
    282         """ 
     304        """ 
     305        attr = node.attrib 
    283306        value_unit = '' 
    284307        if 'unit' in attr and new_current_level.get('unit') is not None: 
     
    291314                local_unit = attr['unit'] 
    292315                if local_unit.lower() != default_unit.lower() and \ 
    293                     local_unit is not None and local_unit.lower() != "none" and\ 
    294                      default_unit is not None: 
     316                    local_unit is not None and local_unit.lower() != "none" \ 
     317                        and default_unit is not None: 
    295318                    if HAS_CONVERTER == True: 
    296319                        try: 
     
    299322                            data_conv_q = Converter(attr['unit']) 
    300323                            value_unit = default_unit 
    301                             exec "node_value = data_conv_q(node_value, units=data1d.{0})".format(unitname) 
    302                         except KeyError as e: 
     324                            i_string = "node_value = data_conv_q" 
     325                            i_string += "(node_value, units=data1d.{0})" 
     326                            exec i_string.format(unitname) 
     327                        except KeyError: 
    303328                            err_msg = "CanSAS reader: could not convert " 
    304                             err_msg += "{0} unit {1}; ".format(tagname, local_unit) 
    305                             intermediate = "err_msg += \"expecting [{1}]  {2}\".format(data1d.{0}, sys.exc_info()[1])".format(unitname, "{0}", "{1}") 
    306                             exec intermediate 
     329                            err_msg += "{0} unit {1}; " 
     330                            err_msg = err_msg.format(tagname, local_unit) 
     331                            intermediate = "err_msg += " + \ 
     332                                        "\"expecting [{1}]  {2}\"" + \ 
     333                                        ".format(data1d.{0}, " + \ 
     334                                        "sys.exc_info()[1])" 
     335                            exec intermediate.format(unitname, "{0}", "{1}") 
    307336                            self.errors.append(err_msg) 
    308337                            raise ValueError(err_msg) 
    309                             return 
    310338                        except: 
    311339                            err_msg = \ 
     
    320348                        self.errors.append(err_msg) 
    321349                        raise ValueError, err_msg 
    322                         return 
    323350                else: 
    324351                    value_unit = local_unit 
    325352            except: 
    326353                err_msg = "CanSAS reader: could not convert " 
    327                 err_msg += "Q unit [%s]; " % attr['unit'], 
    328                 exec "err_msg += \"expecting [%s]\n  %s\" % (data1d.{0}, sys.exc_info()[1])".format(unitname) 
     354                err_msg += "Q unit [%s]; " % attr['unit'] 
     355                intermediate = "err_msg += \"expecting [%s]\n  %s\" % " + \ 
     356                            "(data1d.{0}, sys.exc_info()[1])" 
     357                exec intermediate.format(unitname) 
    329358                self.errors.append(err_msg) 
    330359                raise ValueError, err_msg 
    331                 return 
    332360        elif 'unit' in attr: 
    333361            value_unit = attr['unit'] 
     
    335363        return node_value, value_unit 
    336364     
    337     def _parse_entry(self, dom, names=["SASentry"], data1d=None, extras=[]): 
    338         """ 
    339         Parse a SASEntry - new recursive method for parsing the dom of 
    340             the CanSAS data format. This will allow multiple data files 
    341             and extra nodes to be read in simultaneously. 
    342          
    343         :param dom: dom object with a namespace base of names 
    344         :param names: A list of element names that lead up to the dom object 
    345         :param data1d: The data1d object that will be modified 
    346         :param extras: Any values that should go into meta_data when data1d 
    347             is not a Data1D object 
    348         """ 
    349          
    350         # A portion of every namespace entry 
     365     
     366    def _check_for_empty_data(self, data1d): 
     367        """ 
     368        Creates an empty data set if no data is passed to the reader 
     369         
     370        :param data1d: presumably a Data1D object 
     371        """ 
    351372        if data1d == None: 
     373            self.errors = [] 
    352374            x_vals = numpy.empty(0) 
    353375            y_vals = numpy.empty(0) 
     
    359381            data1d.dxl = dxl 
    360382            data1d.dxw = dxw 
     383        return data1d 
     384     
     385     
     386    def _handle_special_cases(self, tagname, data1d, children): 
     387        """ 
     388        Handle cases where the data type in Data1D is a dictionary or list 
     389         
     390        :param tagname: XML tagname in use 
     391        :param data1d: The original Data1D object 
     392        :param children: Child nodes of node 
     393        :param node: existing node with tag name 'tagname' 
     394        """ 
     395        if tagname == "SASdetector": 
     396            data1d = Detector() 
     397        elif tagname == "SAScollimation": 
     398            data1d = Collimation() 
     399        elif tagname == "SAStransmission_spectrum": 
     400            data1d = TransmissionSpectrum() 
     401        elif tagname == "SASprocess": 
     402            data1d = Process() 
     403            for child in children: 
     404                if child.tag.replace(self.base_ns, "") == "term": 
     405                    term_attr = {} 
     406                    for attr in child.keys(): 
     407                        term_attr[attr] = \ 
     408                            ' '.join(child.get(attr).split()) 
     409                    if child.text is not None: 
     410                        term_attr['value'] = \ 
     411                            ' '.join(child.text.split()) 
     412                    data1d.term.append(term_attr) 
     413        elif tagname == "aperture": 
     414            data1d = Aperture() 
     415        if tagname == "Idata" and children is not None: 
     416            data1d = self._check_for_empty_resolution(data1d, children) 
     417        return data1d 
     418     
     419     
     420    def _check_for_empty_resolution(self, data1d, children): 
     421        """ 
     422        A method to check all resolution data sets are the same size as I and Q 
     423        """ 
     424        dql_exists = False 
     425        dqw_exists = False 
     426        dq_exists = False 
     427        di_exists = False 
     428        for child in children: 
     429            tag = child.tag.replace(self.base_ns, "") 
     430            if tag == "dQl": 
     431                dql_exists = True 
     432            if tag == "dQw": 
     433                dqw_exists = True 
     434            if tag == "Qdev": 
     435                dq_exists = True 
     436            if tag == "Idev": 
     437                di_exists = True 
     438        if dqw_exists and dql_exists == False: 
     439            data1d.dxl = numpy.append(data1d.dxl, 0.0) 
     440        elif dql_exists and dqw_exists == False: 
     441            data1d.dxw = numpy.append(data1d.dxw, 0.0) 
     442        elif dql_exists == False and dqw_exists == False \ 
     443                                            and dq_exists == False: 
     444            data1d.dx = numpy.append(data1d.dx, 0.0) 
     445        if di_exists == False: 
     446            data1d.dy = numpy.append(data1d.dy, 0.0) 
     447        return data1d 
     448     
     449     
     450    def _restore_original_case(self,  
     451                               tagname_original,  
     452                               tagname,  
     453                               save_data1d,  
     454                               data1d): 
     455        """ 
     456        Save the special case data to the appropriate location and restore 
     457        the original Data1D object 
     458         
     459        :param tagname_original: Unmodified tagname for the node 
     460        :param tagname: modified tagname for the node 
     461        :param save_data1d: The original Data1D object 
     462        :param data1d: If a special case was handled, an object of that type 
     463        """ 
     464        if tagname_original == "SASdetector": 
     465            save_data1d.detector.append(data1d) 
     466        elif tagname_original == "SAScollimation": 
     467            save_data1d.collimation.append(data1d) 
     468        elif tagname == "SAStransmission_spectrum": 
     469            save_data1d.trans_spectrum.append(data1d) 
     470        elif tagname_original == "SASprocess": 
     471            save_data1d.process.append(data1d) 
     472        elif tagname_original == "aperture": 
     473            save_data1d.aperture.append(data1d) 
     474        else: 
     475            save_data1d = data1d 
     476        return save_data1d 
     477     
     478     
     479    def _handle_attributes(self, node, data1d, cs_values, tagname): 
     480        """ 
     481        Process all of the attributes for a node 
     482        """ 
     483        attr = node.attrib 
     484        if attr is not None: 
     485            for key in node.keys(): 
     486                try: 
     487                    node_value, unit = self._get_node_value(node, cs_values, \ 
     488                                                        data1d, tagname) 
     489                    cansas_attrib = \ 
     490                    cs_values.current_level.get("attributes").get(key) 
     491                    attrib_variable = cansas_attrib.get("variable") 
     492                    if key == 'unit' and unit != '': 
     493                        attrib_value = unit 
     494                    else: 
     495                        attrib_value = node.attrib[key] 
     496                    store_attr = attrib_variable.format("data1d", \ 
     497                                                    attrib_value, key) 
     498                    exec store_attr 
     499                except AttributeError: 
     500                    pass     
     501        return data1d 
     502     
     503     
     504    def _get_node_value(self, node, cs_values, data1d, tagname): 
     505        """ 
     506        Get the value of a node and any applicable units 
     507         
     508        :param node: The XML node to get the value of 
     509        :param cs_values: A CansasConstants.CurrentLevel object 
     510        :param attr: The node attributes 
     511        :param dataid: The working object to be modified 
     512        :param tagname: The tagname of the node 
     513        """ 
     514        #Get the text from the node and convert all whitespace to spaces 
     515        units = '' 
     516        node_value = node.text 
     517        if node_value == "": 
     518            node_value = None 
     519        if node_value is not None: 
     520            node_value = ' '.join(node_value.split()) 
     521        
     522        # If the value is a float, compile with units. 
     523        if cs_values.ns_datatype == "float": 
     524            # If an empty value is given, set as zero. 
     525            if node_value is None or node_value.isspace() \ 
     526                                    or node_value.lower() == "nan": 
     527                node_value = "0.0" 
     528            #Convert the value to the base units 
     529            node_value, units = self._unit_conversion(node, \ 
     530                        cs_values.current_level, data1d, tagname, node_value) 
     531             
     532        # If the value is a timestamp, convert to a datetime object 
     533        elif cs_values.ns_datatype == "timestamp": 
     534            if node_value is None or node_value.isspace(): 
     535                pass 
     536            else: 
     537                try: 
     538                    node_value = \ 
     539                        datetime.datetime.fromtimestamp(node_value) 
     540                except ValueError: 
     541                    node_value = None 
     542        return node_value, units 
     543     
     544     
     545    def _parse_entry(self, dom, names=None, data1d=None, extras=None): 
     546        """ 
     547        Parse a SASEntry - new recursive method for parsing the dom of 
     548            the CanSAS data format. This will allow multiple data files 
     549            and extra nodes to be read in simultaneously. 
     550         
     551        :param dom: dom object with a namespace base of names 
     552        :param names: A list of element names that lead up to the dom object 
     553        :param data1d: The data1d object that will be modified 
     554        :param extras: Any values that should go into meta_data when data1d 
     555            is not a Data1D object 
     556        """ 
     557         
     558        if extras is None: 
     559            extras = [] 
     560        if names is None or names == []: 
     561            names = ["SASentry"] 
     562         
     563        data1d = self._check_for_empty_data(data1d) 
    361564                             
    362         base_ns = "{0}{1}{2}".format("{", \ 
     565        self.base_ns = "{0}{1}{2}".format("{", \ 
    363566                            CANSAS_NS.get(self.cansas_version).get("ns"), "}") 
    364         unit = '' 
    365567        tagname = '' 
    366568        tagname_original = '' 
     
    370572            try: 
    371573                # Get the element name and set the current names level 
    372                 tagname = node.tag.replace(base_ns, "") 
     574                tagname = node.tag.replace(self.base_ns, "") 
    373575                tagname_original = tagname 
    374576                if tagname == "fitting_plug_in" or tagname == "pr_inversion" or\ 
     
    376578                    continue 
    377579                names.append(tagname) 
    378                 attr = node.attrib 
    379580                children = node.getchildren() 
    380581                if len(children) == 0: 
     
    383584                 
    384585                # Look for special cases 
    385                 if tagname == "SASdetector": 
    386                     data1d = Detector() 
    387                 elif tagname == "SAScollimation": 
    388                     data1d = Collimation() 
    389                 elif tagname == "SAStransmission_spectrum": 
    390                     data1d = TransmissionSpectrum() 
    391                 elif tagname == "SASprocess": 
    392                     data1d = Process() 
    393                     for child in node: 
    394                         if child.tag.replace(base_ns, "") == "term": 
    395                             term_attr = {} 
    396                             for attr in child.keys(): 
    397                                 term_attr[attr] = \ 
    398                                     ' '.join(child.get(attr).split()) 
    399                             if child.text is not None: 
    400                                 term_attr['value'] = \ 
    401                                     ' '.join(child.text.split()) 
    402                             data1d.term.append(term_attr) 
    403                 elif tagname == "aperture": 
    404                     data1d = Aperture() 
    405                 if tagname == "Idata" and children is not None: 
    406                     dql = 0 
    407                     dqw = 0 
    408                     for child in children: 
    409                         tag = child.tag.replace(base_ns, "") 
    410                         if tag == "dQl": 
    411                             dql = 1 
    412                         if tag == "dQw": 
    413                             dqw = 1 
    414                     if dqw == 1 and dql == 0: 
    415                         data1d.dxl = numpy.append(data1d.dxl, 0.0) 
    416                     elif dql == 1 and dqw == 0: 
    417                         data1d.dxw = numpy.append(data1d.dxw, 0.0) 
    418                                  
     586                data1d = self._handle_special_cases(tagname, data1d, children) 
     587                 
    419588                # Get where to store content 
    420589                cs_values = CONSTANTS.iterate_namespace(names) 
     
    427596                     
    428597                #Get the information from the node 
    429                 node_value = node.text 
    430                 if node_value == "": 
    431                     node_value = None 
    432                 if node_value is not None: 
    433                     node_value = ' '.join(node_value.split()) 
    434                  
    435                 # If the value is a float, compile with units. 
    436                 if cs_values.ns_datatype == "float": 
    437                     # If an empty value is given, store as zero. 
    438                     if node_value is None or node_value.isspace() \ 
    439                                             or node_value.lower() == "nan": 
    440                         node_value = "0.0" 
    441                     node_value, unit = self._unit_conversion(\ 
    442                                 cs_values.current_level, attr, data1d, \ 
    443                                 tagname, node_value, cs_values.ns_optional) 
    444                 # If the value is a timestamp, convert to a datetime object 
    445                 elif cs_values.ns_datatype == "timestamp": 
    446                     if node_value is None or node_value.isspace(): 
    447                         pass 
    448                     else: 
    449                         try: 
    450                             node_value = \ 
    451                                 datetime.datetime.fromtimestamp(node_value) 
    452                         except ValueError: 
    453                             node_value = None 
     598                node_value, _ = self._get_node_value(node, cs_values, \ 
     599                                                            data1d, tagname) 
     600                 
    454601                # If appending to a dictionary (meta_data | run_name) 
    455602                # make sure the key is unique 
     
    481628                    exec store_me 
    482629                # Get attributes and process them 
    483                 if attr is not None: 
    484                     for key in node.keys(): 
    485                         try: 
    486                             cansas_attrib = \ 
    487                             cs_values.current_level.get("attributes").get(key) 
    488                             attrib_variable = cansas_attrib.get("variable") 
    489                             if key == 'unit' and unit != '': 
    490                                 attrib_value = unit 
    491                             else: 
    492                                 attrib_value = node.attrib[key] 
    493                             store_attr = attrib_variable.format("data1d", \ 
    494                                                             attrib_value, key) 
    495                             exec store_attr 
    496                         except AttributeError as e: 
    497                             pass 
    498              
    499             except TypeError as e: 
     630                data1d = self._handle_attributes(node, data1d, cs_values, \ 
     631                                                 tagname) 
     632                 
     633            except TypeError: 
    500634                pass 
    501             except Exception as e: 
     635            except Exception as excep: 
    502636                exc_type, exc_obj, exc_tb = sys.exc_info() 
    503637                fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] 
    504                 print(e, exc_type, fname, exc_tb.tb_lineno, tagname, exc_obj) 
     638                print(excep, exc_type, fname, exc_tb.tb_lineno, \ 
     639                      tagname, exc_obj) 
    505640            finally: 
    506641                # Save special cases in original data1d object 
    507642                # then restore the data1d 
    508                 if tagname_original == "SASdetector": 
    509                     save_data1d.detector.append(data1d) 
    510                 elif tagname_original == "SAScollimation": 
    511                     save_data1d.collimation.append(data1d) 
    512                 elif tagname == "SAStransmission_spectrum": 
    513                     save_data1d.trans_spectrum.append(data1d) 
    514                 elif tagname_original == "SASprocess": 
    515                     save_data1d.process.append(data1d) 
    516                 elif tagname_original == "aperture": 
    517                     save_data1d.aperture.append(data1d) 
    518                 else: 
    519                     save_data1d = data1d 
     643                save_data1d = self._restore_original_case(tagname_original, \ 
     644                                                tagname, save_data1d, data1d) 
    520645                if tagname_original == "fitting_plug_in" or \ 
    521646                    tagname_original == "invariant" or \ 
     
    529654         
    530655     
    531     def _to_xml_doc(self, datainfo): 
    532         """ 
    533         Create an XML document to contain the content of a Data1D 
    534          
    535         :param datainfo: Data1D object 
    536         """ 
    537         if not issubclass(datainfo.__class__, Data1D): 
    538             raise RuntimeError, "The cansas writer expects a Data1D instance" 
    539          
    540         # Get PIs and create root element 
     656    def _get_pi_string(self): 
     657        """ 
     658        Creates the processing instructions header for writing to file 
     659        """ 
    541660        pis = self.return_processing_instructions() 
    542661        if len(pis) > 0: 
    543662            pi_tree = self.create_tree(pis[0]) 
    544663            i = 1 
    545             for i in range(1,len(pis) - 1): 
     664            for i in range(1, len(pis) - 1): 
    546665                pi_tree = self.append(pis[i], pi_tree) 
    547666            pi_string = self.to_string(pi_tree) 
    548667        else: 
    549668            pi_string = "" 
    550          
    551         # Define namespaces and create SASroot object 
     669        return pi_string 
     670     
     671     
     672    def _create_main_node(self): 
     673        """ 
     674        Creates the primary xml header used when writing to file 
     675        """ 
    552676        xsi = "http://www.w3.org/2001/XMLSchema-instance" 
    553677        version = self.cansas_version 
    554         ns = CANSAS_NS.get(version).get("ns") 
     678        n_s = CANSAS_NS.get(version).get("ns") 
    555679        if version == "1.1": 
    556680            url = "http://www.cansas.org/formats/1.1/" 
    557681        else: 
    558682            url = "http://svn.smallangles.net/svn/canSAS/1dwg/trunk/" 
    559         schema_location = "{0} {1}cansas1d.xsd".format(ns, url) 
     683        schema_location = "{0} {1}cansas1d.xsd".format(n_s, url) 
    560684        attrib = {"{" + xsi + "}schemaLocation" : schema_location, 
    561685                  "version" : version} 
    562         nsmap = {'xsi' : xsi, None: ns} 
    563          
    564         main_node = self.create_element("{" + ns + "}SASroot", \ 
     686        nsmap = {'xsi' : xsi, None: n_s} 
     687         
     688        main_node = self.create_element("{" + n_s + "}SASroot", \ 
    565689                                               attrib = attrib, \ 
    566690                                               nsmap = nsmap) 
    567          
    568         # Create ElementTree, append SASroot and apply processing instructions 
    569         base_string = pi_string + self.to_string(main_node) 
    570         base_element = self.create_element_from_string(base_string) 
    571         doc = self.create_tree(base_element) 
    572          
    573         # Create SASentry Element 
    574         entry_node = self.create_element("SASentry") 
    575         root = doc.getroot() 
    576         root.append(entry_node) 
    577          
    578         # Add Title to SASentry 
    579         self.write_node(entry_node, "Title", datainfo.title) 
    580          
    581         # Add Run to SASentry 
     691        return main_node 
     692     
     693     
     694    def _write_run_names(self, datainfo, entry_node): 
     695        """ 
     696        Writes the run names to the XML file 
     697         
     698        :param datainfo: The Data1D object the information is coming from 
     699        :param entry_node: lxml node ElementTree object to be appended to 
     700        """ 
    582701        if datainfo.run == None or datainfo.run == []: 
    583             RUN_NAME_DEFAULT = "None" 
    584702            datainfo.run.append(RUN_NAME_DEFAULT) 
    585703            datainfo.run_name[RUN_NAME_DEFAULT] = RUN_NAME_DEFAULT 
     
    590708                runname = {'name': datainfo.run_name[item]} 
    591709            self.write_node(entry_node, "Run", item, runname) 
    592          
    593         # Data info 
     710             
     711     
     712    def _write_data(self, datainfo, entry_node): 
     713        """ 
     714        Writes the I and Q data to the XML file 
     715         
     716        :param datainfo: The Data1D object the information is coming from 
     717        :param entry_node: lxml node ElementTree object to be appended to 
     718        """ 
    594719        node = self.create_element("SASdata") 
    595720        self.append(node, entry_node) 
    596721         
    597722        for i in range(len(datainfo.x)): 
    598             pt = self.create_element("Idata") 
    599             node.append(pt) 
    600             self.write_node(pt, "Q", datainfo.x[i], {'unit': datainfo.x_unit}) 
     723            point = self.create_element("Idata") 
     724            node.append(point) 
     725            self.write_node(point, "Q", datainfo.x[i], \ 
     726                             {'unit': datainfo.x_unit}) 
    601727            if len(datainfo.y) >= i: 
    602                 self.write_node(pt, "I", datainfo.y[i], 
     728                self.write_node(point, "I", datainfo.y[i], 
    603729                            {'unit': datainfo.y_unit}) 
    604730            if datainfo.dy != None and len(datainfo.dy) > i: 
    605                 self.write_node(pt, "Idev", datainfo.dy[i], 
     731                self.write_node(point, "Idev", datainfo.dy[i], 
    606732                            {'unit': datainfo.y_unit}) 
    607733            if datainfo.dx != None and len(datainfo.dx) > i: 
    608                 self.write_node(pt, "Qdev", datainfo.dx[i], 
     734                self.write_node(point, "Qdev", datainfo.dx[i], 
    609735                            {'unit': datainfo.x_unit}) 
    610736            if datainfo.dxw != None and len(datainfo.dxw) > i: 
    611                 self.write_node(pt, "dQw", datainfo.dxw[i], 
     737                self.write_node(point, "dQw", datainfo.dxw[i], 
    612738                            {'unit': datainfo.x_unit}) 
    613739            if datainfo.dxl != None and len(datainfo.dxl) > i: 
    614                 self.write_node(pt, "dQl", datainfo.dxl[i], 
     740                self.write_node(point, "dQl", datainfo.dxl[i], 
    615741                            {'unit': datainfo.x_unit}) 
    616742         
    617         # Transmission Spectrum Info 
     743     
     744     
     745    def _write_trans_spectrum(self, datainfo, entry_node): 
     746        """ 
     747        Writes the transmission spectrum data to the XML file 
     748         
     749        :param datainfo: The Data1D object the information is coming from 
     750        :param entry_node: lxml node ElementTree object to be appended to 
     751        """ 
    618752        for i in range(len(datainfo.trans_spectrum)): 
    619753            spectrum = datainfo.trans_spectrum[i] 
     
    624758                node.setAttribute("timestamp", spectrum.timestamp) 
    625759            for i in range(len(spectrum.wavelength)): 
    626                 pt = self.create_element("Tdata") 
    627                 node.append(pt) 
    628                 self.write_node(pt, "Lambda", spectrum.wavelength[i],  
     760                point = self.create_element("Tdata") 
     761                node.append(point) 
     762                self.write_node(point, "Lambda", spectrum.wavelength[i],  
    629763                           {'unit': spectrum.wavelength_unit}) 
    630                 self.write_node(pt, "T", spectrum.transmission[i],  
     764                self.write_node(point, "T", spectrum.transmission[i],  
    631765                           {'unit': spectrum.transmission_unit}) 
    632766                if spectrum.transmission_deviation != None \ 
    633767                and len(spectrum.transmission_deviation) >= i: 
    634                     self.write_node(pt, "Tdev", \ 
     768                    self.write_node(point, "Tdev", \ 
    635769                               spectrum.transmission_deviation[i], \ 
    636770                               {'unit': spectrum.transmission_deviation_unit}) 
    637  
    638         # Sample info 
     771     
     772     
     773    def _write_sample_info(self, datainfo, entry_node): 
     774        """ 
     775        Writes the sample information to the XML file 
     776         
     777        :param datainfo: The Data1D object the information is coming from 
     778        :param entry_node: lxml node ElementTree object to be appended to 
     779        """ 
    639780        sample = self.create_element("SASsample") 
    640781        if datainfo.sample.name is not None: 
     
    681822        for item in datainfo.sample.details: 
    682823            self.write_node(sample, "details", item) 
    683          
    684         # Instrument info 
     824     
     825     
     826    def _write_instrument(self, datainfo, entry_node): 
     827        """ 
     828        Writes the instrumental information to the XML file 
     829         
     830        :param datainfo: The Data1D object the information is coming from 
     831        :param entry_node: lxml node ElementTree object to be appended to 
     832        """ 
    685833        instr = self.create_element("SASinstrument") 
    686834        self.append(instr, entry_node) 
    687          
    688835        self.write_node(instr, "name", datainfo.instrument) 
    689          
    690         #   Source 
     836        return instr 
     837     
     838     
     839    def _write_source(self, datainfo, instr): 
     840        """ 
     841        Writes the source information to the XML file 
     842         
     843        :param datainfo: The Data1D object the information is coming from 
     844        :param instr: instrument node  to be appended to 
     845        """ 
    691846        source = self.create_element("SASsource") 
    692847        if datainfo.source.name is not None: 
     
    728883                   datainfo.source.wavelength_spread, 
    729884                   {"unit": datainfo.source.wavelength_spread_unit}) 
    730          
    731         #   Collimation 
     885     
     886     
     887    def _write_collimation(self, datainfo, instr):     
     888        """ 
     889        Writes the collimation information to the XML file 
     890         
     891        :param datainfo: The Data1D object the information is coming from 
     892        :param instr: lxml node ElementTree object to be appended to 
     893        """ 
    732894        if datainfo.collimation == [] or datainfo.collimation == None: 
    733895            coll = Collimation() 
     
    742904                       {"unit": item.length_unit}) 
    743905             
    744             for apert in item.aperture: 
    745                 ap = self.create_element("aperture") 
    746                 if apert.name is not None: 
    747                     self.write_attribute(ap, "name", str(apert.name)) 
    748                 if apert.type is not None: 
    749                     self.write_attribute(ap, "type", str(apert.type)) 
    750                 self.append(ap, coll) 
     906            for aperture in item.aperture: 
     907                apert = self.create_element("aperture") 
     908                if aperture.name is not None: 
     909                    self.write_attribute(apert, "name", str(aperture.name)) 
     910                if aperture.type is not None: 
     911                    self.write_attribute(apert, "type", str(aperture.type)) 
     912                self.append(apert, coll) 
    751913                 
    752914                size = self.create_element("size") 
    753                 if apert.size_name is not None: 
     915                if aperture.size_name is not None: 
    754916                    self.write_attribute(size,  
    755917                                                "name",  
    756                                                 str(apert.size_name)) 
    757                 written = self.write_node(size, "x", apert.size.x, 
    758                                      {"unit": apert.size_unit}) 
    759                 written = written | self.write_node(size, "y", apert.size.y, 
    760                                                {"unit": apert.size_unit}) 
    761                 written = written | self.write_node(size, "z", apert.size.z, 
    762                                                {"unit": apert.size_unit}) 
     918                                                str(aperture.size_name)) 
     919                written = self.write_node(size, "x", aperture.size.x, 
     920                                     {"unit": aperture.size_unit}) 
     921                written = written | self.write_node(size, "y", aperture.size.y, 
     922                                               {"unit": aperture.size_unit}) 
     923                written = written | self.write_node(size, "z", aperture.size.z, 
     924                                               {"unit": aperture.size_unit}) 
    763925                if written == True: 
    764                     self.append(size, ap) 
    765                  
    766                 self.write_node(ap, "distance", apert.distance, 
    767                            {"unit": apert.distance_unit}) 
    768  
    769         #   Detectors 
     926                    self.append(size, apert) 
     927                 
     928                self.write_node(apert, "distance", aperture.distance, 
     929                           {"unit": aperture.distance_unit}) 
     930     
     931     
     932    def _write_detectors(self, datainfo, instr): 
     933        """ 
     934        Writes the detector information to the XML file 
     935         
     936        :param datainfo: The Data1D object the information is coming from 
     937        :param inst: lxml instrument node to be appended to 
     938        """ 
    770939        if datainfo.detector == None or datainfo.detector == []: 
    771940            det = Detector() 
     
    827996            if written == True: 
    828997                self.append(pix, det) 
    829              
    830         # Processes info 
     998     
     999     
     1000    def _write_process_notes(self, datainfo, entry_node): 
     1001        """ 
     1002        Writes the process notes to the XML file 
     1003         
     1004        :param datainfo: The Data1D object the information is coming from 
     1005        :param entry_node: lxml node ElementTree object to be appended to 
     1006         
     1007        """ 
    8311008        for item in datainfo.process: 
    8321009            node = self.create_element("SASprocess") 
     
    8441021            if len(item.notes) == 0: 
    8451022                self.write_node(node, "SASprocessnote", "") 
    846                  
    847         # Note info 
     1023     
     1024     
     1025    def _write_notes(self, datainfo, entry_node): 
     1026        """ 
     1027        Writes the notes to the XML file and creates an empty note if none 
     1028        exist 
     1029         
     1030        :param datainfo: The Data1D object the information is coming from 
     1031        :param entry_node: lxml node ElementTree object to be appended to 
     1032         
     1033        """ 
    8481034        if len(datainfo.notes) == 0: 
    8491035            node = self.create_element("SASnote") 
     
    8541040                self.write_text(node, item) 
    8551041                self.append(node, entry_node) 
    856                  
    857          
    858         # Return the document, and the SASentry node associated with 
    859         #      the data we just wrote 
    860         # If the calling function was not the cansas reader, return a minidom 
    861         #      object rather than an lxml object.         
     1042     
     1043     
     1044    def _check_origin(self, entry_node, doc): 
     1045        """ 
     1046        Return the document, and the SASentry node associated with 
     1047        the data we just wrote. 
     1048        If the calling function was not the cansas reader, return a minidom 
     1049        object rather than an lxml object. 
     1050         
     1051        :param entry_node: lxml node ElementTree object to be appended to 
     1052        :param doc: entire xml tree 
     1053        """ 
    8621054        frm = inspect.stack()[1] 
    8631055        mod_name = frm[1].replace("\\", "/").replace(".pyc", "") 
     
    8661058        mod_name = mod[1] 
    8671059        if mod_name != "dataloader/readers/cansas_reader": 
    868             string = self.to_string(doc, pp=False) 
     1060            string = self.to_string(doc, pretty_print=False) 
    8691061            doc = parseString(string) 
    8701062            node_name = entry_node.tag 
    8711063            node_list = doc.getElementsByTagName(node_name) 
    8721064            entry_node = node_list.item(0) 
     1065        return entry_node 
     1066     
     1067     
     1068    def _to_xml_doc(self, datainfo): 
     1069        """ 
     1070        Create an XML document to contain the content of a Data1D 
     1071         
     1072        :param datainfo: Data1D object 
     1073        """ 
     1074        if not issubclass(datainfo.__class__, Data1D): 
     1075            raise RuntimeError, "The cansas writer expects a Data1D instance" 
     1076         
     1077        # Get PIs and create root element 
     1078        pi_string = self._get_pi_string() 
     1079         
     1080        # Define namespaces and create SASroot object 
     1081        main_node = self._create_main_node() 
     1082         
     1083        # Create ElementTree, append SASroot and apply processing instructions 
     1084        base_string = pi_string + self.to_string(main_node) 
     1085        base_element = self.create_element_from_string(base_string) 
     1086        doc = self.create_tree(base_element) 
     1087         
     1088        # Create SASentry Element 
     1089        entry_node = self.create_element("SASentry") 
     1090        root = doc.getroot() 
     1091        root.append(entry_node) 
     1092         
     1093        # Add Title to SASentry 
     1094        self.write_node(entry_node, "Title", datainfo.title) 
     1095         
     1096        # Add Run to SASentry 
     1097        self._write_run_names(datainfo, entry_node) 
     1098         
     1099        # Add Data info to SASEntry 
     1100        self._write_data(datainfo, entry_node) 
     1101         
     1102        # Transmission Spectrum Info 
     1103        self._write_trans_spectrum(datainfo, entry_node) 
     1104         
     1105        # Sample info 
     1106        self._write_sample_info(datainfo, entry_node) 
     1107         
     1108        # Instrument info 
     1109        instr = self._write_instrument(datainfo, entry_node) 
     1110         
     1111        #   Source 
     1112        self._write_source(datainfo, instr) 
     1113         
     1114        #   Collimation 
     1115        self._write_collimation(datainfo, instr) 
     1116 
     1117        #   Detectors 
     1118        self._write_detectors(datainfo, instr) 
    8731119             
     1120        # Processes info 
     1121        self._write_process_notes(datainfo, entry_node) 
     1122                 
     1123        # Note info 
     1124        self._write_notes(datainfo, entry_node)  
     1125         
     1126        # Return the document, and the SASentry node associated with 
     1127        #      the data we just wrote 
     1128        # If the calling function was not the cansas reader, return a minidom 
     1129        #      object rather than an lxml object.         
     1130        entry_node = self._check_origin(entry_node, doc) 
     1131         
    8741132        return doc, entry_node 
    8751133     
     
    9011159        doc, _ = self._to_xml_doc(datainfo) 
    9021160        # Write the file 
    903         fd = open(filename, 'w') 
     1161        file_ref = open(filename, 'w') 
    9041162        if self.encoding == None: 
    9051163            self.encoding = "UTF-8" 
    906         doc.write(fd, encoding=self.encoding, 
     1164        doc.write(file_ref, encoding=self.encoding, 
    9071165                  pretty_print=True, xml_declaration=True) 
    908         fd.close() 
     1166        file_ref.close() 
    9091167     
    9101168     
     
    9501208                                           conv(value, units=local_unit)) 
    9511209                        except: 
    952                             exc_type, exc_value, exc_traceback = sys.exc_info() 
     1210                            _, exc_value, _ = sys.exc_info() 
    9531211                            err_mess = "CanSAS reader: could not convert" 
    9541212                            err_mess += " %s unit [%s]; expecting [%s]\n  %s" \ 
  • src/sans/dataloader/readers/defaults.xml

    r5777106 r3241dd2  
    99        <FileType extension='.asc' reader='IgorReader'/> 
    1010        <FileType extension='.dat' reader='red2d_reader'/> 
     11        <!-- 
     12        Removed the tiff_reader for now. To reinstate, remove the XML comment 
    1113        <FileType extension='.tif' reader='tiff_reader'/> 
     14        --> 
    1215        <FileType extension='.abs' reader='abs_reader'/> 
    1316        <FileType extension='.d1d' reader='hfir1d_reader'/> 
  • src/sans/dataloader/readers/xml_reader.py

    rac5b69d r3241dd2  
    1515############################################################################# 
    1616 
     17import logging 
    1718from lxml import etree 
    1819from lxml.builder import E 
     
    3738    processing_instructions = None 
    3839     
    39     def __init__(self, xml = None, schema = None, root = None): 
     40    def __init__(self, xml = None, schema = None): 
    4041        self.xml = xml 
    4142        self.schema = schema 
    4243        self.processing_instructions = {} 
    4344        if xml is not None: 
    44             self.set_xml_file(xml, root) 
     45            self.set_xml_file(xml) 
    4546        else: 
    4647            self.xmldoc = None 
     
    6162        return self.xmldoc 
    6263     
    63     def set_xml_file(self, xml, root = None): 
     64    def set_xml_file(self, xml): 
    6465        """ 
    6566        Set the XML file and parse 
     
    6970            self.xmldoc = etree.parse(self.xml, parser = PARSER) 
    7071            self.xmlroot = self.xmldoc.getroot() 
     72        except etree.XMLSyntaxError as xml_error: 
     73            logging.info(xml_error) 
    7174        except Exception: 
    7275            self.xml = None 
     
    8184            self.schema = schema 
    8285            self.schemadoc = etree.parse(self.schema, parser = PARSER) 
     86        except etree.XMLSyntaxError as xml_error: 
     87            logging.info(xml_error) 
    8388        except Exception: 
    8489            self.schema = None 
     
    105110        try: 
    106111            first_error = schema.assertValid(self.xmldoc) 
    107         except etree.DocumentInvalid as e: 
    108             first_error = str(e) 
     112        except etree.DocumentInvalid as err: 
     113            first_error = str(err) 
    109114        return first_error 
    110115     
     
    116121        self.set_schema(self.schema) 
    117122         
    118     def to_string(self, elem, pp=False, encoding=None): 
     123    def to_string(self, elem, pretty_print=False, encoding=None): 
    119124        """ 
    120125        Converts an etree element into a string 
    121126        """ 
    122         return etree.tostring(elem, pretty_print = pp, encoding = encoding) 
     127        return etree.tostring(elem, pretty_print = pretty_print, \ 
     128                              encoding = encoding) 
    123129     
    124130    def break_processing_instructions(self, string, dic): 
  • test/sansdataloader/test/utest_cansas.py

    rac5b69d r3241dd2  
    22    Unit tests for the new recursive cansas reader 
    33""" 
     4import logging 
    45import warnings 
    56warnings.simplefilter("ignore") 
     
    5152            name = self.get_number_of_entries(dictionary, name, i) 
    5253        return name 
     54     
     55     
     56    def test_invalid_xml(self): 
     57        """ 
     58        Should fail gracefully and send a message to logging.info() 
     59        """ 
     60        invalid = StringIO.StringIO('<a><c></b></a>') 
     61        reader = XMLreader(invalid) 
    5362        
    5463 
     
    203212            ## find the processing instructions and make into a dictionary 
    204213            dic = self.get_processing_instructions(reader) 
    205             self.assertTrue(dic == {'xml-stylesheet': 'type="text/xsl" href="cansas1d.xsl" '}) 
     214            self.assertTrue(dic == {'xml-stylesheet': \ 
     215                                    'type="text/xsl" href="cansas1d.xsl" '}) 
    206216             
    207217            xml = "<test><a><b><c></c></b></a></test>" 
Note: See TracChangeset for help on using the changeset viewer.