import os, arcpy, codecs, shutil, glob, sys import xml.etree.ElementTree as ET def Message(msg): print str(msg) arcpy.AddMessage(str(msg)) def AdjustSite(Site_Folder, name, user, image): _dir = os.path.dirname(sys.argv[0]) site_dir = os.path.join(_dir, 'Sites') temp_dir = os.path.join(_dir, 'OtherTemplates') ws = os.path.join(site_dir, Site_Folder) tags = ['Permissions','PrintTemplates','Security','Viewers','Workflows'] site = os.path.join(ws, 'Site.xml') shutil.copy(site, site[:-4] + '_Copy.xml') trees = ET.parse(site) doc = trees.getroot() elms = [elm.tag for elm in doc] with codecs.open(site, 'r', encoding='utf-8') as f: orig = f.readlines()[:-1] # Iterate through tags and add if necessary for xtag in tags: if xtag not in elms: with codecs.open(os.path.join(temp_dir, '%s.xml'%xtag),'r', encoding='utf-8') as rd: for l in rd.readlines(): orig.append(l.replace('VIEWER_XXX',name).replace('USER_XXX',user).replace('ZZZZ',Site_Folder[:4])) Message('Added %s tag' %xtag) # Copy Reports reports = os.path.join(temp_dir, 'Reports') #-------------------------------------------------------------------------------------------------- # Temporary: Delete Reports if exists <----------- Remove this after testing rep_fold = os.path.join(ws, 'Reports') if os.path.exists(rep_fold): arcpy.Delete_management(rep_fold) #-------------------------------------------------------------------------------------------------- shutil.copytree(reports, os.path.join(ws, 'Reports')) shutil.copy(image, os.path.join(ws, 'Reports',os.path.basename(image))) Message('Copied Reports') # Rename Print templates for rpx in glob.glob(os.path.join(ws,'Reports','*PrintTemplate*.rpx')): os.rename(rpx, rpx.replace('XXXX',Site_Folder[:4])) # Write out new Site.xml orig.append('</Site>') with codecs.open(site, 'w', encoding='utf-8') as wr: wr.writelines(orig) Message('Updated Site.xml for %s' %Site_Folder) return if __name__ == '__main__': # Test Run # Stand alone params - comment out for script tool run Site_Folder = 'ConfigTest' name = 'ConfigViewer' user = 'calebma' image = os.path.join(os.getcwd(), r'Logo\Zimmerman.png') AdjustSite(Site_Folder, name, user, image) ## # Get args ## # script tool params - un-comment this to run as script tool ## argv = tuple(arcpy.GetParameterAsText(i) for i in range(arcpy.GetArgumentCount())) ## ## AdjustSite(*argv)
<type 'exceptions.UnicodeDecodeError'>: 'ascii' codec can't decode byte 0xae in position 14: ordinal not in range(128)
Solved! Go to Solution.
Here you go Caleb -
It's a dumb pet trick: I printed your tuple argv and it turns out that is what it's tripping on in passing from the ArcGIS tool interface to python (if I stated that correctly) --- at any rate, the 'encoding' was lost in translation (or not translated, more exactly), so to speak.
I did this, and it 'corrected' itself:
argv = tuple(str(arcpy.GetParameterAsText(i)) for i in range(arcpy.GetArgumentCount()))