AttributeError: LayerObject: Get attribute dataSource does not exist

6223
4
12-20-2010 02:07 PM
JamesGustine
New Contributor III
All,

I am trying to get this script to loop thru a directory and change the datasources of specific feature classes in all mxds. I've been successful with the replaceworkspaces method, but am hitting a wall with the replacedatasources method.

I have posted 2 scripts....The BatchReplaceWorkspaces_tool.py is a bit rough but works well. It will go thru folders and subfolders changing specified workspaces. It saves maps back to 9.3 and mirrors the source directory structure.

I have attempted a similar tact with the replacedatasources method in the BatchReplaceDataSources_tool.py script, but to no avail.

I am recieving the following error with the latter script:

Traceback (most recent call last):
  File "C:/scripts/Python/Testing/BatchReplaceDataSources_tool.py", line 28, in <module>
    if lyr.dataSource == OldDS:
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 70, in _get
    return convertArcObjectToPythonObject(getattr(self._arc_object, attr_name))
AttributeError: LayerObject: Get attribute dataSource does not exist

Wanted to see if anyone sees anything I don't.

Thanks
James
0 Kudos
4 Replies
by Anonymous User
Not applicable
Original User: jvondracek

According to the ArcGIS 10 Help for the dataSource property:
[INDENT]Returns the complete path for the layer's data source. It includes the workspacePath and the datasetName properties combined. Not all layers support the dataSource property (for example, annotation classes, Web services), so it is good practice to test for this ahead of time using the supports method.[/INDENT]

So, if you have any group layers or whatever in your MXD, such layer will not have a dataSource property but your code assumes that it does.  In your for loop before you test to see if the dataSource for the current layer is the "old datasource", you might want to check to see whether the current layer is one that supports this property before you attempt to access it.
JamesGustine
New Contributor III
Nice, that fixed the error. I also had the feature dataset included in the new workspace string variable. Which is not neccesary.

Thanks alot- James
0 Kudos
KenGalliher1
Esri Contributor

Adding to Geonet Admin's answer, I used hasattr(class, 'prop') to check each incoming layer had the datasource property.

                     
for df in arcpy.mapping.ListDataFrames(mxd):
     brknList = arcpy.mapping.ListBrokenDataSources(mxd)                     
     for bk in brknList:                         
          if hasattr(bk, "dataSource"):                             
               bk_list.append(bk.dataSource)
KenGalliher1
Esri Contributor

Yes.  Good catch.  Thanks.

0 Kudos