what is wrong with this

725
7
03-10-2020 07:43 AM
PSGeo
by
New Contributor II

hi guys,

I am using ArcMap 10.5.

I have a tool that adds the name of the location automatically based on the name of the folders (where are located the mxd and the gdb) and the date that has manual input.

The fact, I ran the script in stand-alone and it worked perfectly. However, with the python addin included it doesn't work...

I am sure it works after the "try:" but before it I don't know what am I doing wrong. Please can someone tell what is it.

cheers

Pedro

here is the code:

import arcpy
import pythonaddins
import os, sys, string
import arcpy.da
from arcpy import env # needed for env.workspace definition
import traceback # needed to log errors
import datetime
import uuid

class Island_1_upload(object):
    """Implementation for Island_1_addin.combobox (ComboBox)"""
    def __init__(self):
        self.items = []
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWW'

    def onEditChange(self, text):
        global imageDate
        imageDate = text

    def onEnter(self):
        import arcpy
        import pythonaddins
        import os, sys, string
        import arcpy.da
        from arcpy import env # needed for env.workspace definition
        import traceback # needed to log errors
        import datetime
        import uuid


        if any(char.isalpha() for char in imageDate)==1:
            pythonaddins.MessageBox('Wrong characters in the date. Please try again!', 'Warning', 0)
        elif len(imageDate)<> 10:
            pythonaddins.MessageBox('Wrong characters in the date. Please try again!', 'Warning', 0)

        else:

            try:



                startTime = datetime.datetime.now()
                print startTime


                date = imageDate


                
                error_code=0

                # Checking the length of string values - if the input parameters defined wrongly, the process will not start
                if len(date) <> 10:
                    RaiseWarning("PROBLEM: Wrong date: <"+date+">. It has to be exactly ten characters long.")
                    error_code = error_code + 1



                try:

                    datetime.datetime.strptime(date,'%Y/%m/%d')
                except ValueError:
                    RaiseWarning("PROBLEM: Wrong date format: <"+date+">. It has to be year/month/day -> yyyy/mm/dd.")
                    error_code = error_code + 1




                # mxd and environment; and gdb sources and targets‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
7 Replies
DanPatterson_Retired
MVP Esteemed Contributor

nested try-except blocks are iffy and your first one seems to be missing an except.  Did you truncate the rest of the code when copying?

0 Kudos
PSGeo
by
New Contributor II

hi,

the 'try' has the 'except'. Like i said the everything works on standalone mode after the first try... but I cannot make it work as a tool or button in the arcmap (addin).

that's why i asked what is wrong on that first part before the 'try'.

0 Kudos
JoeBorgione
MVP Esteemed Contributor

I don't see an except partner for line 41 try....  Also, line 21 shows an invalid syntax when I paste your def into spyder.

elif len(imageDate)<> 10:
## replace with
elif len(imageDate)!= 10:
That should just about do it....
0 Kudos
PSGeo
by
New Contributor II

Hi,

believe me it is there in the end of the rest of the script.

I fixed the != .

the result of all this after creating the add-in is in the picture

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Okay.  I believe you!  Try eliminating some of the code and see if your addin loads.  For example, take out the second Try except block, etc.

That should just about do it....
0 Kudos
PSGeo
by
New Contributor II

 Just cleaned... but still the same things is happening...

there is no area to write date and enter

import arcpy
import os, sys, string
import arcpy.da
from arcpy import env # needed for env.workspace definition
import traceback # needed to log errors
import datetime
import pythonaddins
import uuid

def count_records(fc):
    """ Create table from FC in order to speed up the count process, which is slow because the complexity of the GDB, return: the number of records in the FC in string format """
    fc_view = "in_memory\\"+fc + "_view"
    if arcpy.Exists(fc_view):
        arcpy.Delete_management(fc_view)
    arcpy.MakeTableView_management(fc,fc_view)
    count = str(arcpy.GetCount_management(fc_view).getOutput(0))
    arcpy.Delete_management("in_memory")
    return count

def RaiseWarning(msg):
    """ Print and log warning message """
    arcpy.AddWarning(msg)

def SysExit(msg):
    """ Print and log error message before interrupt script """
    arcpy.AddError(msg)
    script_end = datetime.datetime.now()
    sys.exit()

class upload_1(object):
    """Implementation for 20200324_upload_1_addin.combobox (ComboBox)"""
    def __init__(self):
        self.items = []
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWW'

    def onEditChange(self, text):
        global imageDate
        imageDate = text

    def onEnter(self):
        if any(char.isalpha() for char in imageDate)==1:
            pythonaddins.MessageBox('Wrong characters in the date. Please try again!', 'Warning', 0)
        elif len(imageDate)!= 10:
            pythonaddins.MessageBox('Wrong characters in the date. Please try again!', 'Warning', 0)
        else:
            try:
                startTime = datetime.datetime.now()
                print startTime
                 # Script arguments - input parameters
            ##    arcpy.GetParameterAsText(0)
                date = imageDate

                # mxd and environment; and gdb sources and targets
                mxdInicio = arcpy.mapping.MapDocument('current')
                mxdPath = mxdInicio.filePath
                issue_code = os.path.basename(mxdInicio)
                issue_code = issue_code[0:19]
                issue_code = issue_code.replace("_"," ")
                mxdPathList = mxdPath.split(os.sep)
                IssueGDB =  "\\".join(mxdPathList[:-1])
0 Kudos
JoeBorgione
MVP Esteemed Contributor

That's the only thing I would be doing; starting at the bottom and either eliminating or commenting out blocks of code and see where it finally loads into your addin; then focus your efforts on the the last block eliminated.  Trouble shooting is an iterative process.

That should just about do it....
0 Kudos