示例#1
0
    def simple_describe(self, s_objects=None, filename=None, path=None):
        """
            [input]
            * path - path to destination folder
            * filename - no extension
            * s_objects - (list) of sobjects names
            [output]
        """
        path = path_formatter(path)

        properties = {
            "createable",
            "custom",
            "calculated",
            "label",
            "name",
            "permissionable",
            "queryable",
            "retrieveable",
            "searchable",
            "triggerable",
            "updateable",
            "autoNumber",
            "defaultedOnCreate",
            "nillable",
            "referenceTo",
            "type",
        }
        objects_describe = pd.DataFrame()
        filename = None if filename is None else filename.split('.')[0]

        if s_objects == None:
            describe_sf = self.describe()
            objects = adjust_report(describe_sf["sobjects"])
            # print("Max Batch Size: {}".format(describe_sf["maxBatchSize"]))
            # print("Encoding: {}".format(describe_sf["encoding"]))
            s_objects = list(set(objects.name))

        for obj in s_objects:
            describe_obj = eval("self.{}.describe()".format(obj))
            df_full_object = pd.DataFrame(describe_obj["fields"])
            df_short_object = df_full_object[list(
                properties & set(df_full_object.columns))].copy()
            df_short_object["object"] = obj
            objects_describe = pd.concat([df_short_object, objects_describe],
                                         axis=0)
        if filename is not None:
            objects_describe.to_excel("{}{}.xlsx".format(path, filename),
                                      index=False)
        lod_objects_describe = objects_describe.to_dict(orient="records")
        return lod_objects_describe
示例#2
0
def save_lod_files(files, filename, path=None, start_index=0):
    """
        Saves your lods into .mtxt files
        [input]
        * files - list of lod
        * filename - name template
        * path - path to folder
        * start_index
        [output]
        * files like: filename_[\\d].mtxt
    """
    path = path_formatter(path)
    for i, target in enumerate(files):
        with open("{}{}_{}.mtxt".format(path, filename, i + start_index),
                  "w") as f:
            f.write(str(target))
示例#3
0
def read_lod_files(filenames, path=None):
    """
        Carrega os arquivos (.mtxt) da pasta especificada. Os arquivos devem ter indices sequenciais
        [input]
        * path - pasta com os arquivos
        * filenames - list with filenames (.mtxt) to be loaded. (without index, e. g. filename_0.mtxt -> filename)
        [output]
        * dicionario com o {nome dos arquivos (sem indice) : lista dos arquivos}
        Exemplo:
        read_lod_files('Home/','account') carrega account_0.mtxt,account_1.mtxt...
    """
    path = path_formatter(path)
    if type(filenames) != list:
        raise ValueError(
            "{}: filenames invalid parameter!".format("read_lod_files"))
    elif len(filenames) == 0:
        raise ValueError(
            "{}: filenames invalid parameter!".format("read_lod_files"))
    # lists all files in especified path
    dir_files = []
    for __, __, files in os.walk(path):
        for f in files:
            dir_files.append(f)
    # lists .mtxt filenames
    dir_filenames = []
    for f in dir_files:
        dir_filenames.append(re.split("_(\\d)*.mtxt", f)[0])
    # checks validity of input
    if not (set(filenames) < set(dir_filenames)):
        raise ValueError(
            "{}: the files {} were not found on the specified folder!".format(
                "read_lod_files", str(set(filenames) - set(dir_filenames))))
    filenames = list(set(filenames))  # remove duplicates
    loaded_files = {}
    for f in filenames:
        loaded_files[f] = []
    for f in dir_files:
        for nome_alvo in filenames:
            if f[:len(nome_alvo)] == nome_alvo:
                load_file = read_lod_file(path + f)
                loaded_files[nome_alvo].append(load_file)
    return loaded_files
示例#4
0
 def to_salesforce(
     self,
     lod_list,
     method,
     obj,
     path=None,
     key_map=None,
     drop=False,
     step=5000,
     suf="",
     pref="",
     start_index=0,
 ):
     """
         Sends all lods parsed to Salesforce. They must be of the same sObject and respect Salesforce's bulk api limits.
         [input]
         * lod_list - (list) of lods to be sent
         * method - ('insert'|'delete'|'upsert'|'update'|'undelete')
         * obj - (str), sObject
         * key_map - (dict), used to rename current keys to Salesforce's field api names.
         * path - (str), path to save report files
         * drop - (bool), True to drop keys that are not in key_map
         * step - (int), batch size
         * suf - (str), added to the end of the file name
         * pref - (str), added to the start of the file name
         * start_index - (int), index of the first file to be saved.
         [output]
         * (list), list of lods report
     """
     path = path_formatter(path)
     files = []
     lod_report_final = []
     for lod in lod_list:
         files.extend(lod_rename(lod, key_map, drop=drop))
     count = start_index
     for lod in files:
         if len(lod) > 0:
             filename = """{}_{}_report_{}_{}""".format(
                 pref, method, obj, suf)
             start_time = timeit.default_timer()
             print("{} #{} of {} {} started at {}, saved on {}:".format(
                 method, count, len(lod), obj,
                 timeit.time.strftime("%H:%M:%S", timeit.time.localtime()),
                 filename))
             # sends to salesforce
             report = self.lod_to_saleforce(obj, method, lod, step)
             # format response
             df_report = adjust_report(report)
             df_report = df_report.assign(taskid=df_report["id"])
             df_report.drop(columns=["id"], inplace=True)
             # reports
             lod_report = df_report.to_dict(orient="records")
             save_lod_files([lod_report], path, filename, start_index=count)
             lod_report_final.append(lod_report)
             err = df_report[~df_report.success].shape[0]
             suc = df_report[df_report.success].shape[0]
             print("\terrors:", err)
             print("\tsuccess:", suc)
             if err > 0:
                 try:
                     df_report.to_excel("{}{}_{}.xlsx".format(
                         path, filename, count))
                 except:
                     pass
                 print("\tmessages: ", set(df_report.message))
             m, s = divmod(timeit.default_timer() - start_time, 60)
             print("\texecution time: {:1.0f}min {:2.0f}s".format(m, s))
             count += 1
         else:
             print("One of the lods passed had length zero. Skipped...")
     return lod_report_final