def check_translate_budget(num_proposed: int) -> bool: """ Goal: block translations that would trigger the lockout. Create & maintain a persistient file that contains timestamps and # of requests, to know if my proposed number will exceed the budget. If it would still be under budget, then update the log assuming that the proposed requests will happen. options: TRANSLATE_BUDGET_MAX_REQUESTS, TRANSLATE_BUDGET_TIMEFRAME :param num_proposed: number of times I want to contact the google API :return: bool True = go ahead, False = stop """ # first, get path to persistient storage file, also creates an empty file if it doesn't exist recordpath = core.get_persistient_storage_path("translate_record.txt") # then read the file into memory, quietly record = core.read_file_to_csvlist(recordpath, quiet=True) # discard all request records that are older than <timeframe> now = time() i = 0 while i < len(record): if (now - record[i][0]) > (TRANSLATE_BUDGET_TIMEFRAME * 60 * 60): # print("debug: discard", record[i]) record.pop(i) else: i += 1 # then interpret the file: how many requests happened in the past <timeframe> requests_in_timeframe = sum([entry[1] for entry in record]) core.MY_PRINT_FUNC( "... you have used {} / {} translation requests within the last {:.4} hrs..." .format(int(requests_in_timeframe), int(TRANSLATE_BUDGET_MAX_REQUESTS), TRANSLATE_BUDGET_TIMEFRAME)) # make the decision if (requests_in_timeframe + num_proposed) <= TRANSLATE_BUDGET_MAX_REQUESTS: # this many translations is OK! go ahead! # write this transaction into the record record.append([now, num_proposed]) core.write_csvlist_to_file(recordpath, record, quiet=True) return True else: # cannot do the translate, this would exceed the budget # bonus value: how long until enough records expire that i can do this? if num_proposed >= TRANSLATE_BUDGET_MAX_REQUESTS: core.MY_PRINT_FUNC( "BUDGET: you cannot make this many requests all at once") else: to_be_popped = 0 idx = 0 for idx in range(len(record)): to_be_popped += record[idx][1] if (requests_in_timeframe + num_proposed - to_be_popped) <= TRANSLATE_BUDGET_MAX_REQUESTS: break # when record[idx] becomes too old, then the current proposed number will be okay waittime = record[idx][0] + (TRANSLATE_BUDGET_TIMEFRAME * 60 * 60) - now # convert seconds to minutes waittime = round(waittime / 60) core.MY_PRINT_FUNC( "BUDGET: you must wait %d minutes before you can do %d more translation requests with Google" % (waittime, num_proposed)) return False
def write_vmdtext(vmdtext_filename: str, nicelist: vmdstruct.Vmd): # assume the output filename has already been validated as unused, etc cleanname = core.get_clean_basename(vmdtext_filename) + ".txt" core.MY_PRINT_FUNC("Begin formatting VMD-as-text file '%s'" % cleanname) rawlist = format_nicelist_as_rawlist(nicelist) # done formatting! core.MY_PRINT_FUNC("Begin writing VMD-as-text file '%s'" % cleanname) core.MY_PRINT_FUNC("...total size = %s lines" % len(rawlist)) core.write_csvlist_to_file(vmdtext_filename, rawlist) core.MY_PRINT_FUNC("Done writing VMD-as-text file '%s'" % cleanname) return
def main(moreinfo=True): # prompt PMX name core.MY_PRINT_FUNC("Please enter name of PMX input file:") input_filename_pmx = core.MY_FILEPROMPT_FUNC(".pmx") pmx = pmxlib.read_pmx(input_filename_pmx, moreinfo=moreinfo) realbones = pmx.bones # get bones realmorphs = pmx.morphs # get morphs modelname_jp = pmx.header.name_jp modelname_en = pmx.header.name_en bonelist_out = [["modelname_jp", "'" + modelname_jp + "'"], ["modelname_en", "'" + modelname_en + "'"], ["bonename_jp", "bonename_en"]] morphlist_out = [["modelname_jp", "'" + modelname_jp + "'"], ["modelname_en", "'" + modelname_en + "'"], ["morphname_jp", "morphname_en"]] # in both lists, idx0 = name_jp, idx1 = name_en bonelist_pairs = [[a.name_jp, a.name_en] for a in realbones] morphlist_pairs = [[a.name_jp, a.name_en] for a in realmorphs] bonelist_out += bonelist_pairs morphlist_out += morphlist_pairs # write out output_filename_bone = "%s_bone_names.txt" % input_filename_pmx[0:-4] # output_filename_bone = output_filename_bone.replace(" ", "_") output_filename_bone = core.get_unused_file_name(output_filename_bone) core.MY_PRINT_FUNC("...writing result to file '%s'..." % output_filename_bone) core.write_csvlist_to_file(output_filename_bone, bonelist_out, use_jis_encoding=False) output_filename_morph = "%s_morph_names.txt" % input_filename_pmx[0:-4] output_filename_morph = core.get_unused_file_name(output_filename_morph) core.MY_PRINT_FUNC("...writing result to file '%s'..." % output_filename_morph) core.write_csvlist_to_file(output_filename_morph, morphlist_out, use_jis_encoding=False) core.MY_PRINT_FUNC("Done!") return None