def runScript(log, config):
    try:

        url = config.get("ORG_INFO", "URL")
        username = config.get("ORG_INFO", "USER")
        password = config.get("ORG_INFO", "PASS")

        groups = config.get("GROUP_INFO", "GROUPS")
        iconpath = config.get("GROUP_INFO", "ICONPATH")
        if os.path.isfile(groups):
            with open(groups, "rb") as csvfile:
                """access	description	isFav	isInvitationOnly	isViewOnly	phone	snippet	sortField	sortOrder	tags	thumbnail	title"""

                agol = admin.AGOL(username, password)

                for row in csv.DictReader(csvfile, dialect="excel"):
                    if os.path.isfile(os.path.join(iconpath, row["thumbnail"])):
                        agol.createGroup(
                            title=row["title"],
                            description=row["description"],
                            tags=row["tags"],
                            snippet=row["snippet"],
                            phone=row["phone"],
                            access=row["access"],
                            sortField=row["sortField"],
                            sortOrder=row["sortOrder"],
                            isViewOnly=row["isViewOnly"],
                            isInvitationOnly=row["isInvitationOnly"],
                            thumbnail=os.path.join(iconpath, row["thumbnail"]),
                        )
                        print "Group created: " + row["title"]
                    else:
                        agol.createGroup(
                            title=row["title"],
                            description=row["description"],
                            tags=row["tags"],
                            snippet=row["snippet"],
                            phone=row["phone"],
                            access=row["access"],
                            sortField=row["sortField"],
                            sortOrder=row["sortOrder"],
                            isViewOnly=row["isViewOnly"],
                            isInvitationOnly=row["isInvitationOnly"],
                        )
                        print "Group created: " + row["title"]

        ##                    for key in row:
        ##                        print 'key=%s, value=%s' % (key, row[key])

        else:
            print "Group file not found."
            sys.exit()

    except helper.HelperError, e:
        line, filename, synerror = helper.trace()
        print "error on line: %s" % line
        print "error in file name: %s" % filename
        print "with error message: %s" % synerror
        print "Add. Error Message: %s" % e
        print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
def runScript(log,config):
    try:


        # Config File
        username = config.get( 'AGOL', 'USER')
        password = config.get('AGOL', 'PASS')

        url = config.get('FS_INFO', 'SERVICEURL')

        layers = config.get('FS_INFO', 'LAYERS')
        outputLoc = config.get('FS_INFO', 'OUTPUTLOC')

        fs = featureservice.FeatureService(url, username=username, password=password)
        fs.createReplica(replicaName="test_replica",
                           layers=layers,
                           returnAttachments=True,
                           returnAsFeatureClass=True,
                           out_path=outputLoc)



    except helper.HelperError,e:
        line, filename, synerror = helper.trace()
        print "error on line: %s" % line
        print "error in file name: %s" % filename
        print "with error message: %s" % synerror
        print "Add. Error Message: %s" % e
        print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
def runScript(log,config):
    try:


        # Config File
        username = config.get( 'AGOL', 'USER')
        password = config.get('AGOL', 'PASS')

        url = config.get('FS_INFO', 'FEATURESERVICE')
        serviceInfo = eval(config.get('FS_INFO', 'SERVICEINFO'))

        print "Config file loaded"

        fs = featureservice.FeatureService(url=url,username=username,password=password)

        for serItm in serviceInfo:
            result = fs.layers[int(serItm[0])].deleteFeatures(sql=serItm[1])
            print "Delete Complete: "  + serItm[0] + ": " + str(result)

    except helper.HelperError,e:
        line, filename, synerror = helper.trace()
        print "error on line: %s" % line
        print "error in file name: %s" % filename
        print "with error message: %s" % synerror
        print "Add. Error Message: %s" % e
        print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
def runScript(log, config):
    try:

        exampleConfigValue = config.get("SAMPLE", "SETTING")
        print exampleConfigValue

    except helper.HelperError, e:
        line, filename, synerror = helper.trace()
        print "error on line: %s" % line
        print "error in file name: %s" % filename
        print "with error message: %s" % synerror
        print "Add. Error Message: %s" % e
        print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
def runScript(log, config):
    try:

        # Config File
        username = config.get("AGOL", "USER")
        password = config.get("AGOL", "PASS")
        dataToField = eval(config.get("LOCAL_DATA", "DATATOFIELD"))
        url = config.get("FS_INFO", "URL")
        sql = config.get("FS_INFO", "SQL")

        print "Config file loaded"
        out_path = arcpy.env.scratchGDB + r"\temp1"
        # out_path=r'C:\temp\temp.gdb\temp1'

        fs = layer.FeatureLayer(url=url, username=username, password=password)
        if fs == None:
            print "Cannot find Service, exiting"
            sys.exit()

        fc = fs.query(where=sql, returnFeatureClass=True, out_fc=out_path)
        spJoinTemp = "in_memory\join"

        for itm in dataToField:
            if arcpy.Exists(itm[0]):
                desc = arcpy.Describe(itm[0])
                if desc.shapeType == "Point":
                    arcpy.SpatialJoin_analysis(
                        fc, itm[0], spJoinTemp, "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT", "#", "#"
                    )

                    print "Spatial Join Complete"
                    # Join the spatial join to the source data and copy the customer count information
                    helper.JoinAndCalc(fc, "OBJECTID", spJoinTemp, "TARGET_FID", [("Join_Count", itm[1])])

                elif desc.shapeType == "Polyline":

                    arcpy.SpatialJoin_analysis(
                        fc,
                        itm[0],
                        spJoinTemp,
                        "JOIN_ONE_TO_ONE",
                        "KEEP_ALL",
                        "SHAPE_Length 'SHAPE_Length' false true true 8 Double 0 0 ,First,#,"
                        + itm[0]
                        + ",SHAPE_Length,-1,-1;SUMLEN 'SUMLEN' true true false 50 Long 0 0 ,Sum,#,"
                        + itm[0]
                        + ",SHAPE_Length,-1,-1",
                        "INTERSECT",
                        "#",
                        "#",
                    )
                    helper.JoinAndCalc(fc, "OBJECTID", spJoinTemp, "TARGET_FID", [("SUMLEN", itm[1])])
            else:
                print itm[0] + " does not exist"
        ##        cursor = arcpy.UpdateCursor(fc)
        ##        row = cursor.next()
        ##        while row:
        ##            row.setValue("EStatus", 'Analyzed')
        ##            cursor.updateRow(row)
        ##            row = cursor.next()
        arcpy.CalculateField_management(fc, "EStatus", "'Analyzed'", "PYTHON_9.3", "")

        fs.deleteFeatures(sql=sql)
        fs.addFeatures(fc=fc)
        print "Scipt Complete"
        arcpy.Delete_management(out_path)

    except helper.HelperError, e:
        line, filename, synerror = helper.trace()
        print "error on line: %s" % line
        print "error in file name: %s" % filename
        print "with error message: %s" % synerror
        print "Add. Error Message: %s" % e
        print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
        fs.deleteFeatures(sql=sql)
        fs.addFeatures(fc=fc)
        print "Scipt Complete"
        arcpy.Delete_management(out_path)

    except helper.HelperError, e:
        line, filename, synerror = helper.trace()
        print "error on line: %s" % line
        print "error in file name: %s" % filename
        print "with error message: %s" % synerror
        print "Add. Error Message: %s" % e
        print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")

    except arcpy.ExecuteError:

        line, filename, synerror = helper.trace()
        print "error on line: %s" % line
        print "error in file name: %s" % filename
        print "with error message: %s" % synerror
        print "ArcPy Error Message: %s" % arcpy.GetMessages(2)
        print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")

    except:
        line, filename, synerror = helper.trace()
        print ("error on line: %s" % line)
        print ("error in file name: %s" % filename)
        print ("with error message: %s" % synerror)
        print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")


if __name__ == "__main__":
def runScript(log,config):
    try:


        # Config File

        username = config.get( 'AGOL', 'USER')
        password = config.get('AGOL', 'PASS')
        LocalParcels = config.get('LOCAL_DATA', 'LOCALPARCELS')
        CommunityParcelsLocalCopy = config.get('LOCAL_DATA', 'COMMUNITYPARCELSLOCALCOPY')
        url = config.get('FS_INFO', 'URL')
        deleteSQL = config.get('FS_INFO', 'DELETESQL')

        print "Loading Configuration File"
        arcpy.AddMessage("Loading Configuration File")

        if arcpy.Exists(LocalParcels) == False:
            print "Please specify a input parcel layer (LocalParcels=) in the configuration file, exiting"
            arcpy.AddMessage("Please specify a input parcel layer in the configuration file, exiting")
            sys.exit()

        if arcpy.Exists(CommunityParcelsLocalCopy) == False:
            print "Please specify a input community parcel layer (CommunityParcelsLocalCopy=) in the configuration file, exiting"
            arcpy.AddMessage("Please specify a input parcel layer in the configuration file, exiting")
            sys.exit()

        if username == "":
	        print "Please specify a ArcGIS Online Username (username =)in the configuration file, exiting"
	        arcpy.AddMessage(username)
	        sys.exit()

	    if password == "":
	        print "Please specify a ArcGIS Online password (password =)in the configuration file, exiting"
	        arcpy.AddMessage(password)
	        sys.exit()

	    if deleteSQL == "":
	        print "Please specify a SQL query (CVTTXDSCRP = 'some value') in the configuration file, exiting"
	        arcpy.AddMessage("Please specify a SQL query (CVTTXDSCRP = 'some value') in the configuration file, exiting")
	        sys.exit()


        fs = layer.FeatureLayer (url=url,username=username,password=password)
        if fs == None:
            print "Cannot find or connect to service, make sure service is accessible"
            arcpy.AddMessage("Cannot find or connect to service, make sure service is accessible")
            sys.exit()

        # Delete existing dataset that matches the community parcel schema
        arcpy.management.TruncateTable(CommunityParcelsLocalCopy)
        print "Cleaning up local parcel data"

        # Append new parcels into the community parcels schema, field map your data into the community schema.  Add local data field names after the "#" in the list.
        # For example, for STATEAREA "STATEAREA" true true false 50 Text 0 0 ,First,#,LocalParcels,TotalAcres,-1,-1  The local Parcels field name from STATEDAREA (community parcels schema) is TotalAcres.
        Field_Map = arcpy.GetParameterAsText(0)
        arcpy.Append_management(LocalParcels,CommunityParcelsLocalCopy,"NO_TEST",
            """
                LOWPARCELID "LOWPARCELID" true true false 50 Text 0 0 ,First,#;
                PARCELID "PARCELID" true true false 50 Text 0 0 ,First,#;
                FLOORDESIG "FLOORDESIG" true true false 50 Text 0 0 ,First,#;
                SHAPE_Length "SHAPE_Length" false true true 8 Double 0 0 ,First,#;
                SHAPE_Area "SHAPE_Area" false true true 8 Double 0 0 ,First,#;
                STATEAREA "STATEAREA" true true false 50 Text 0 0 ,First,#;
                CNVYNAME "CNVYNAME" LocalParcels,Subdivisio;
                USEDCD "USEDCD" true true false 50 Text 0 0 ,First,#;
                USEDSCRP "USEDSCRP" LocalParcels,PropType;
                CVTTXDSCRP "CVTTXDSCRP" true true false 50 Text 0 0 ,First,#;
                IMPROVED "IMPROVED" true true false 50 Text 0 0 ,First,#;
                OWNTYPE "OWNTYPE" true true false 50 Text 0 0 ,First,#;
                SITEADRESS "SITEADRESS" true true false 50 Text 0 0 ,First,#;
                OWNERNME1 "OWNERNME1" true true false 50 Text 0 0 ,First,#;
                OWNERNME2 "OWNERNME2" true true false 50 Text 0 0 ,First,#;
                PSTLADRESS "PSTLADRESS" true true false 50 Text 0 0 ,First,#;
                USPSBOX "USPSBOX" true true false 50 Text 0 0 ,First,#;
                PSTLCITY "PSTLCITY" true true false 50 Text 0 0 ,First,#;
                PSTLSTATE "PSTLSTATE" true true false 50 Text 0 0 ,First,#;
                PSTLZIP "PSTLZIP" true true false 50 Text 0 0 ,First,#;
                PSTLINTER "PSTLINTER" true true false 50 Text 0 0 ,First,#;
                LNDVALUE "LNDVALUE" true true false 50 Text 0 0 ,First,#;
                IMPVALUE "IMPVALUE" true true false 50 Text 0 0 ,First,#;
                CNTASSDVAL "CNTASSDVAL" true true false 50 Text 0 0 ,First,#;
                CNTTXBLVAL "CNTTXBLVAL" true true false 50 Text 0 0 ,First,#;
                SALEPRICE "SALEPRICE" true true false 50 Text 0 0 ,First,#;
                SALEDATE "SALEDATE" true true false 50 Text 0 0 ,First,#;
                LOCALFIPS "LOCALFIPS" true true false 50 Text 0 0 ,First,#;
                STCOFIPS "STCOFIPS" true true false 50 Text 0 0 ,First,#;
                GNISID "GNISID" true true false 50 Text 0 0 ,First,#;
                LASTEDITOR "LASTEDITOR" true true false 50 Text 0 0 ,First,#;
                LASTUPDATE "LASTUPDATE" true true false 50 Text 0 0 ,First,#;
                COUNTYNAME "COUNTYNAME" true true false 50 Text 0 0 ,First,#""","#")

        print "Mapping Local Parcel data to Community Parcel Schema"
        print "Community Parcel Update to ArcGIS Online Started, please be patient"
        arcpy.AddMessage("Mapping Local Parcel data to Community Parcel Schema")
        arcpy.AddMessage("Community Parcel Update to ArcGIS Online Started, please be patient")

        ##    # Calculate the CVTTXDSCRP to the County Name
        ##        arcpy.CalculateField_management(CommunityParcelsLocalCopy, "CVTTXDSCRP", "\"5\"", "PYTHON", "")
        ##        print "Set County Code information"
        ##        arcpy.AddMessage("Calculate County Code Description")

        # Add Attribute Index
        ##        arcpy.AddIndex_management(CommunityParcelsLocalCopy, "CVTTXDSCRP", "CountyDescriptionIndex", "NON_UNIQUE", "ASCENDING")
        ##        print "Add Attribute Indexes"
        ##        arcpy.AddMessage("Add Attribute Indexes - Performance")


        fs.objectIdField()
        value1=fs.query(sql=deleteSQL,returnIDsOnly=true)
        myids=value1 ['objectIds']

        minId = min(myids)
        i = 0
        maxId = max(myids)

        print minId
        print maxId
        chunkSize = 1000

        while (i <= len(myids)):
            #print myids[i:i+1000]
            oids = ",".join(str(e) for e in myids[i:i+chunkSize])
            #print oids
            if oids == '':
                continue
            else:
                fs.applyEdits(deleteFeatures=oids)
            i+=chunkSize
            print i
            #print len(myids)
            #print i/ float(len(myids))
            print "Completed: {0:2f}%".format( i/ float(len(myids))*100)
            arcpy.AddMessage("Deleted: {0:2f}%".format ( i/ float(len(myids))*100))
        print "Community Parcels upload Started"
        arcpy.AddMessage("Community Parcels upload started, please be patient, may take +- 5 minutes per 80,000 parcels.  For future consideration, please run tool during non-peak internet usage")
        fs.addFeatures(fc=CommunityParcelsLocalCopy)

    except helper.HelperError,e:
        line, filename, synerror = helper.trace()
        print "error on line: %s" % line
        print "error in file name: %s" % filename
        print "with error message: %s" % synerror
        print "Add. Error Message: %s" % e
        print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
def runScript(log,config):
    try:

        username = config.get( 'AGOL', 'USER')
        password = config.get('AGOL', 'PASS')



        outageURL = config.get('FS_INFO', 'OUTAGESERVICE')
        outageDelSQL = config.get('FS_INFO', 'OUTAGEDELETESQL')


        outageAreaLayer = config.get('LOCAL_DATA', 'OUTAGELAYER')
        outageAreaCustOutField = config.get('LOCAL_DATA', 'CUSTOMERSOUTFIELD')
        outageAreaCustInField = config.get('LOCAL_DATA', 'CUSTOMERSINFIELD')
        outageAreaTotCustField = config.get('LOCAL_DATA', 'TOTALCUSTOMERSFIELD')
        outageAreaPercOutField = config.get('LOCAL_DATA', 'PERCENTOUTFIELD')
        outageAreaRptDate = eval(config.get('LOCAL_DATA', 'DATEFIELD'))

        eventLayer = config.get('LOCAL_DATA', 'EVENTSLAYER')

        print "Config file loaded"

        # Local variables:
        outageAreaJoinedToEvents = 'in_memory\AreaJoinedWithOutage'##'%scratchgdb%\AreaJoinedWithOutage'

        if arcpy.Exists(outageAreaLayer) == False:
            print "Join Data Does Not Exist, exiting"
            sys.exit()

        if arcpy.Exists(eventLayer) == False:
            print "Input Data Does Not Exist, exiting"
            sys.exit()

        # Process: Spatial Join


        arcpy.SpatialJoin_analysis(outageAreaLayer, eventLayer, outageAreaJoinedToEvents, 'JOIN_ONE_TO_ONE', 'KEEP_ALL',"","INTERSECT","#","#")
        print "Spatial Join Complete"

        #Join the spatial join to the source data and copy the customer count information
        helper.JoinAndCalc(outageAreaLayer,"OBJECTID",outageAreaJoinedToEvents,"TARGET_FID",[("Join_Count",outageAreaCustOutField)])

        print "Outage Counts Calculated"

        if helper.FieldExist(outageAreaLayer,[outageAreaCustInField,outageAreaTotCustField]):
            # Process: Calc Number In
            arcpy.CalculateField_management(outageAreaLayer, outageAreaCustInField, "!" + outageAreaTotCustField +"!- !" + outageAreaCustOutField + "!", "PYTHON_9.3", "")
            print "Number of people in service calculated"

        if helper.FieldExist(outageAreaLayer,[outageAreaCustInField,outageAreaTotCustField,outageAreaPercOutField]):

            # Process: Calc Percent Out
            arcpy.CalculateField_management(outageAreaLayer, outageAreaPercOutField, "calc( !" + outageAreaCustOutField +"!, !" + outageAreaTotCustField +"! )", "PYTHON_9.3", "def calc(numout,numserv):\\n  if numout == 0:\\n    return 0\\n  else:\\n    return round(float((float(numout) / float(numserv)) *100),2)")
            print "Percentage out calculated"

        if helper.FieldExist(outageAreaLayer,[outageAreaRptDate[0]]):
            # Process: Set Time
            tz = time.timezone
            dateExp = "import time\\nimport datetime\\nfrom time import mktime\\nfrom datetime import datetime\\ndef calc(dt):\\n  return datetime.fromtimestamp(mktime(time.strptime(str(dt), '" + str(outageAreaRptDate[1]) + "')) + " + str(tz) + ")"
            exp =  "calc('" + datetime.datetime.now().strftime(str(outageAreaRptDate[1])) + "')"
            arcpy.CalculateField_management(outageAreaLayer,outageAreaRptDate[0], exp, "PYTHON_9.3", dateExp)
            print "Report time set"


        fs = layer.FeatureLayer(url=outageURL,
                        username=username,
                        password=password)

        fs.deleteFeatures(sql=outageDelSQL)
        print "Outage Polygon layer cleared"

        results = fs.addFeatures(fc=outageAreaLayer)
        print "Outage Polygon layer features added"


    except helper.HelperError,e:
        line, filename, synerror = helper.trace()
        print "error on line: %s" % line
        print "error in file name: %s" % filename
        print "with error message: %s" % synerror
        print "Add. Error Message: %s" % e
        print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")