I have a python toolbox which crawls directories, or optionally a single mxd, and writes out various properties of the layers in them to a csv file, mostly interested in the data source. This works great, except...,.
- I check for group layers to skip those. The tool does skip top level group layers, but not group layers nested in other group layers. How can I skip those?
- I originally set a default location and file name for the output csv file, which the user can change. This works if the file doesn't already exist, but if it does, that parameter gets a big red X when the tool is run. The file is set to 'w', so I assumed it would just overwrite the existing file. Is it possible to do what I thought would happen?
- In the self.description in the __init__ function, I'd like to break the text on different lines and paragraphs. But no matter how many \n's I add, the tool ignores them when run, and collapses the text into one block. Is there a way to accomplish this?
Thanks, relevant code below.
for mxd_path in mxd_list:
try:
mxd = arcpy.mapping.MapDocument(mxd_path)
dflist = arcpy.mapping.ListDataFrames(mxd)
for df in dflist:
for lyr in arcpy.mapping.ListLayers(mxd, '', df):
if not lyr.isGroupLayer:
nm = lyr.name
ds = '-- X --'
if lyr.supports('dataSource'):
ds = lyr.dataSource
lyr_attributes = [mxd_path, nm, ds]
writer.writerow(lyr_attributes)
else:
if not lyr.isGroupLayer:
arcpy.AddMessage('\nLayer {0} does not support the dataSource property'.format(nm))
unsupported.append(nm)
lngnm = lyr.longName
except Exception as e:
arcpy.AddError('EXCEPTION: {0}\t{1}\t{2}'.format(mxd_path, lngnm, e))
continue
writer.writerow([''])
del mxd