0
votes

I´ve written a script that connects to Sharepoint and downloads the metadata from all the new files added into a dataframe.

I have developed it in Spyder and it works just fine. But after I compile it into an .exe file with pyinstaller and I run it, I keep getting the following error: maximum recursion depth exceeded

I´m using Windows10 and Python 3.6.10 (conda)

from office365.runtime.auth.clientCredential import ClientCredential
from office365.sharepoint.client_context import ClientContext

import pandas as pd

clien_id = XXX
cient_secret = XXX
site_url = XXX

ctx = ClientContext.connect_with_credentials(site_url, ClientCredential(client_id, client_secret))

lib = ctx.web.lists.get_by_title("REO")

doc = 1

while cuenta_error < 10:
                    try: 
                        item = lib.get_item_by_id(doc)
                        ctx.load(item)
                        ctx.execute_query()
                        id_documento = "{0}".format(item.properties["IdDocumentoSistemaOrigen"])
                        id_sharepoint = "{0}".format(item.properties["Id"])
                        ts_alta = datetime.strptime(str(datetime.now()), '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M:%S')
                        origen = "{0}".format(item.properties["AplicacionOrigen"])
                        tipo_doc_SH = "{0}".format(item.properties["TipoDocumento"]['Label'])
                        fecha_alta_origen = datetime.strptime("{0}".format(item.properties["Created"])[0:10], '%Y-%m-%d').strftime('%Y-%m-%d')
                        id_inmueble = "{0}".format(item.properties["IdActivoPrinex"])
                        dni = "{0}".format(item.properties["NumeroIdentificacion"])
                        id_promocion = "{0}".format(item.properties["CodigoPromocion"])
                        file = item.file
                        ctx.load(file)
                        ctx.execute_query()
                        nombre_documento = "{0}".format(file.properties["Name"])
                        fecha_envio = None
                        IAObjeto = None
                        #print(id_documento)
                        doc +=1
                        cuenta_error = 0
                        fecha_envio = None
                        ind_error = None
                        to_append = [id_documento, id_sharepoint, tipo_documento, ts_alta, nombre_documento,
                                origen, tipo_doc_SH, fecha_alta_origen, id_inmueble, fecha_envio, ind_error,dni, id_promocion, IAObjeto]
                        a_series = pd.Series(to_append, index = df.columns)
                        df = df.append(a_series, ignore_index=True)
                        export_doc +=1
                    except Exception as e:
                        print(e)

I have tryied with sys.setrecursionlimit(1500) but I still get the same error and with sys.setrecursionlimit(1000**6) the code end up crashing.

Does anybody have any suggestion about how to fix this?

1

1 Answers

0
votes

Good Morning

Finally I figured out the problem

ClientContext works in a recursive way and after a few calls crush,

I edited my code so I call the context each time I call a new item and now it's working.

I created two functions that I call every time I want to get the metadata from each file. With this functions I close the context and open it again after each call, probably is not the best way, but I run out of ideas and this work.

from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext
import pandas as pd
from datetime import datetime, date

def call_lib(site_url,client_id, client_secret,serv):
    ctx = ClientContext.connect_with_credentials(site_url, ClientCredential(client_id, client_secret))
    if serv == 'PRN':
        lib = ctx.web.lists.get_by_title("REO")
    elif serv == 'GIA':
        lib = ctx.web.lists.get_by_title("DESINVERSIÓN") 
    return ctx, lib

def get_metadata_from_sh (ctx,lib,docnum):
     item = lib.get_item_by_id(docnum)
     ctx.load(item)
     ctx.execute_query()
     id_documento = "{0}".format(item.properties["IdDocumentoSistemaOrigen"])
     id_sharepoint = "{0}".format(item.properties["Id"])
     ts_alta = datetime.strptime(str(datetime.now()), '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M:%S')
     origen = "{0}".format(item.properties["AplicacionOrigen"])
     tipo_doc_SH = "{0}".format(item.properties["TipoDocumento"]['Label'])
     fecha_alta_origen = datetime.strptime("{0}".format(item.properties["Created"])[0:10], '%Y-%m-%d').strftime('%Y-%m-%d')
     id_inmueble = "{0}".format(item.properties["IdActivoPrinex"])
     dni = "{0}".format(item.properties["NumeroIdentificacion"])
     id_promocion = "{0}".format(item.properties["CodigoPromocion"])
     file = item.file
     ctx.load(file)
     ctx.execute_query()
     nombre_documento = "{0}".format(file.properties["Name"])
     fecha_envio = None
     IAObjeto = None
     
     return id_documento, id_sharepoint, ts_alta, origen, tipo_doc_SH, fecha_alta_origen, id_inmueble, nombre_documento, fecha_envio, dni, int(id_promocion), IAObjeto

ctx, lib = call_lib(site_url,client_id, client_secret,serv_origen[i]) 
id_documento, id_sharepoint, ts_alta, origen, tipo_doc_SH, fecha_alta_origen, id_inmueble, nombre_documento, fecha_envio, dni,id_promocion, IAObjeto = get_metadata_from_sh (ctx,lib,doc)