Skip to content

Instantly share code, notes, and snippets.

@lastforkbender
Last active December 19, 2023 07:44
Show Gist options
  • Select an option

  • Save lastforkbender/727fa34385096551a492f714dea877f7 to your computer and use it in GitHub Desktop.

Select an option

Save lastforkbender/727fa34385096551a492f714dea877f7 to your computer and use it in GitHub Desktop.
Staqtapp's .tqpt/.tpqt glb-var smart source splitter
#______________________________________________________________________________________
def stpx_split_source(new_source_name: str) -> bool:
# < called from the stpx.py pro module as @x_splitsource(newFileName) >
# Staqtapp v1.02.458 - PySqTpp_Stpx.py - stpx_split_source()
# email: 5deg.blk.blt.cecil(@)gmail
# github: https://github.com/lastforkbender/staqtapp
# contact: https://pastebin.com/eumqiBAx
# Staqtapp's Splitter Description:
# ***** this function is exclusive to the stpx pro module only *****
# a smart .tqpt glb-var source // .tpqt glb-var lock source file splitter, this
# method splits .tqpt core glb variable source & it's data by half and finds
# any .tpqt lock/key file if associated @stpx_removevars() when called,
# that then performs the process of separate .tpqt lock/key index source
# away from any original .tpqt source connected to the .tqpt vars source;
# min. variable stack needed for this type split is 42 or greater var stacks
# (the involved parsing @stpx_remove_vars() call pasted below this)
try:
mdlPthX = os.path.dirname(os.path.abspath(__file__))
# all stpx.py function calls rely on set path folder/file in x_stpx.gz listings
# only time a parameter of folder dir and file name is @stpx_set_path()
pth = StpxSrvc.stpx_get_path(mdlPthX + '/stpx/x_stpx.gz')
# first, create or edit gzip setting entry :sts<...> to inform @stpx_remove-
# vars() is being called from this split-tqpt-source method focused, when
# @stpx_remove_vars() reads the gzip setting as :sts<ren> then makes
# element copies of what gets removed of a .tqpt lock/key var/fnc source
src = StpxSrvc.stpx_map('gzr', mdlPthX + '/stpx/x_stpx.gz', None)
mtch = re.findall(rb':sts<.*?>', src)
if len(mtch) > 0:
StpxSrvc.stpx_map('gzw', mdlPthX + '/stpx/x_stpx.gz', src.replace(mtch[0], b':sts<ren>'))
else:
StpxSrvc.stpx_map('gzw', mdlPthX + '/stpx/x_stpx.gz', src + b':sts<ren>')
# get the .tqpt glb variables source as b-string, split @ newlines(staqtapp
# doesn't allow newline chars in it's data glb var declares read or write)
src = StpxSrvc.stpx_map('tmr', pth, None).split(b'\n')
# remove last & first elements of now list(.tqpt header/ending data)
# keeping a hdr copy, this may be a static set .tqpt glb vars source...
src.pop(len(src)-1)
qpHdr = src[0]
src.pop(0)
# returns None if this is not met by a min. glb vars stack length, because
# there isn't really much of a logical reason to do splitting of a .tqpt glb
# vars source file of such lesser entries since staqtapp uses mmap, how-
# ever staqtapp does not use regular expressions to read @qp(...): vars
# data declares and is unrestricted of how much data can be placed in
# those data declare tags----also unrestricted to a total of @qp(...): used,
# that makes count 42 once again a casual/reasonable number of return
if len(src) >= 42:
srcLen = len(src)
vrnLst = []
hPnt = srcLen/2
ptrn = re.compile(rb'^<.*?=')
# get variables' names list for stpx_removevars() call; will also
# remove .tpqt lock/key function blocks if present associated
x = hPnt
while x < srcLen:
mtch = ptrn.findall(src[x])
if len(mtch) > 0:
vrnLst.append(mtch[0].replace(b'<', b'').strip(b'='))
x+=1
else:
# if this happens......the .tqpt glb var source is corrupted
return None
# the delete vars call on the current gzip set path of .tqpt file;
# stpx_remove_vars() knows call is coming from here at check
# of the gzip entry that was just changed, if this .tqpt file has a
# associated lock function .tpqt file, then will be a transferred
# to a temp named .tpqt file of the lock/key blocks removed if
# any, otherwise stpx_removevars rechanged gzip entry to null
StpxSrvc.stpx_remove_vars(False, vrnLst)
# now we can make the new split .tqpt glb var source, without
# loss of important .tpqt var locks/keys between this splitting:
# add @qpHdr first to the new .tqpt vars list from a same as is
qtlLst = [qpHdr]
for hPnt in range(srcLen):
qtlLst.append(src[hPnt])
qtlLst.append(b'!!!END_TQPT_FILE(-DO-NOT-EDIT-OR-REMOVE-)!!!')
pth[1] = new_source_name
# write the new split .tqpt source to same set x path directory
StpxSrvc.stpx_map('twb', pth, b'\n'.join(qtlLst))
qtlLst = None
# is there a new .tpqt lock/key fnc source for this new .tqpt source?
src = StpxSrvc.stpx_map('gzr', mdlPthX + '/stpx/x_stpx.gz', None)
if src.find(b':sts<den>') > -1:
# rename temp named new .tpqt lock fnc source to same new name
tmpNm = os.path.join(pth[0], '_tpqt_.ren')
newNm = os.path.join(pth[0], pth[1] + '.tpqt')
os.rename(tmpNm, newNm)
return True
else:
return False
except Exception as e:
print("staqtapp stpx error: ", e)
#______________________________________________________________________________________
def stpx_remove_vars(is_backup: bool, var_names) -> bool:
# ...a very sensitive straight-shot .tqpt/.tpqt complex parsing, do not edit
try:
mdlPthX = os.path.dirname(os.path.abspath(__file__))
pth = StpxSrvc.stpx_get_path(mdlPthX + '/stpx/x_stpx.gz')
vrNmsLen = None
vrSrc = None
src = None
rplc = False
if isinstance(var_names, str) == True:
vrSrc = var_names
var_names = [vrSrc]
if is_backup == True:
StpxSrvc.stpx_backups(True, 'tqpt', pth)
src = StpxSrvc.stpx_map('tmr', pth, None)
vrNmsLen = len(var_names)
for x in range(vrNmsLen):
vrSrc = re.findall(rb'\n<' + re.escape(str.encode(var_names[x])) + rb'=.*?>', src)
if len(vrSrc[0]) > len(var_names[x])+5:
src = src.replace(vrSrc[0], b'')
if rplc == False: rplc = True
if rplc == True:
StpxSrvc.stpx_map('twb', pth, src)
if os.path.isfile(pth[0] + '/' + pth[1] + '.tpqt') == True:
trgSts = False
stsLst = []
src = StpxSrvc.stpx_map('gzr', mdlPthX + '/stpx/x_stpx.gz', None)
if src.find(b':sts<ren>') > -1: trgSts = True
src = StpxSrvc.stpx_map('lmr', pth, None).split(b'>')
src.pop(len(src)-1)
rplcLck = False
rmvLck = False
for lx in range(vrNmsLen):
if rmvLck == False:
for lxl in range(len(src)):
vrSrc = re.findall(rb'<:' + re.escape(str.encode(var_names[lx])) + rb'=(?s:.*?).*:', src[lxl])
if len(vrSrc) > 0:
if trgSts == True:
stsLst.append(vrSrc[0])
if len(src) > 1:
src.pop(lxl)
if rplcLck == False: rplcLck = True
else:
rmvLck = True
break
else:
break
if rplcLck == True and rmvLck == False:
vrNmsLen = len(src)
for xr in range(vrNmsLen):
if src[xr].find(b'\n<') > -1:
src[xr] = src[xr].replace(b'\n<', b'')
src[xr] = b'<' + src[xr]
src[xr] = src[xr] + b'>\n'
src = b''.join(src)
StpxSrvc.stpx_map('lwb', pth, src + b'!!!END_TPQT_FILE(-DO-NOT-EDIT-OR-REMOVE-)!!!')
elif rmvLck == True:
dirF = pth[0] + "/" + pth[1] + ".tpqt"
if os.path.isfile(dirF):
os.remove(dirF)
if trgSts == True:
if len(stsLst) > 0:
vrNmsLen = len(stsLst)
for nx in range(vrNmsLen):
stsLst[nx] = stsLst[nx] + b'>\n'
stsLst = b''.join(stsLst)
with open(pth[0] + '/_tpqt_.ren', mode='wb') as fObjStsW:
fObjStsW.write(stsLst + b'!!!END_TPQT_FILE(-DO-NOT-EDIT-OR-REMOVE-)!!!')
fObjStsW.close()
fObjStsW = None
src = StpxSrvc.stpx_map('gzr', mdlPthX + '/stpx/x_stpx.gz', None)
StpxSrvc.stpx_map('gzw', mdlPthX + '/stpx/x_stpx.gz', src.replace(b':sts<ren>', b':sts<den>'))
else:
src = StpxSrvc.stpx_map('gzr', mdlPthX + '/stpx/x_stpx.gz', None)
StpxSrvc.stpx_map('gzw', mdlPthX + '/stpx/x_stpx.gz', src.replace(b':sts<ren>', b':sts<null>'))
return rplc
except Exception as e:
print("staqtapp stpx error: ", e)
#______________________________________________________________________________________
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment