Last active
April 9, 2024 03:33
-
-
Save lastforkbender/8ac666aea7b15e006b59f8bd9f87cdc5 to your computer and use it in GitHub Desktop.
Console app for __slots__ programming assistances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Slotster v1.0.41 | |
| # This console app module's features are for assist on slots class building. The | |
| # goal is to have common assist and advanced ones: Obfuscation, threading, etc. | |
| # Staqtapp's modules are required for this console app: | |
| # https://github.com/lastforkbender/staqtapp | |
| import stpp | |
| from collections import deque | |
| import multiprocessing | |
| #from DUN3 import dun3_aux_lmb_rtn | |
| import os | |
| import re | |
| #__________________________________________________________________________________ | |
| SLTR_PTH = os.path.dirname(os.path.abspath(__file__)) + '/slotster' | |
| #__________________________________________________________________________________ | |
| class SlotsterCnsl: | |
| __slots__ = ( '_cmds', '_fncs', '_sSetVarNms', '_sSmp', '_rLstA', '_rLstB', '_rLstC', '_rLstD', '_rLstE', '_rStrA', '_rStrB', '_rStrC', '_rStrD', '_rIntA', '_rIntB', '_rIntC', '_rIntD', '_rIntE', '_rBoolA', '_rBoolB', '_rBoolC', '_rBoolD', '_rBoolE' ) | |
| def __init__(self): | |
| self._cmds = {} | |
| self._fncs = {} | |
| self._sSetVarNms = set('_abcdefghijklmnopqurstvwxyzABCDEFGHIJKLMNOPQURSTVWXYZ0123456789') | |
| def add_cmd(self, cmd, fnc): | |
| self._cmds[cmd] = fnc | |
| def add_fnc(self, nme, fnc): | |
| self._fncs[nme] = fnc | |
| def run(self, cmd): | |
| if cmd in self._cmds: | |
| args = input(self._cmds[cmd]['prmp']).split() | |
| self._fncs[self._cmds[cmd]['fnct']](*args) | |
| else: | |
| print(' |■ invalid command, type help for a list of all commands') | |
| #__________________________________________________________________________________ | |
| class SltsrFncs(SlotsterCnsl): | |
| __slots__ = ( '_sf_rLstA', '_sf_rLstB', '_sf_rLstC', '_sf_rLstD', '_sf_rStrA', '_sf_rStrB', '_sf_rStrC', '_sf_rStrD', '_sf_rIntA', '_sf_rIntB', '_sf_rIntC', '_sf_rIntD', '_sf_rBoolA', '_sf_rBoolB', '_sf_rBoolC', '_sf_rBoolD') | |
| def slots_module_path(self, args): | |
| # Sets, changes & sets or removes the set default module file path. | |
| if args != 'r': | |
| if not os.path.isfile(args): raise Exception(f'invalid file path ({args})') | |
| else: | |
| if not stpp.findvar(False, 'sltr_mdl_pth', SLTR_PTH, 'slotster'): | |
| stpp.addvar('sltr_mdl_pth', f'@qp({args}):', SLTR_PTH, 'slotster') | |
| stpp.lockvar('sltr_mdl_pth', ['slots_module_path'], f'{SLTR_PTH}/slotster.tqpt') | |
| self.prant(f'module path set ({args})') | |
| else: | |
| if os.path.isfile(f'{SLTR_PTH}/slotster.tpqt'): | |
| if stpp.keyvar(True, 'sltr_mdl_pth', 'slots_module_path', f'{SLTR_PTH}/slotster.tqpt'): | |
| if stpp.findvar(False, 'sltr_mdl_pth', SLTR_PTH, 'slotster'): | |
| stpp.changevar('sltr_mdl_pth', f'@qp({args}):', SLTR_PTH, 'slotster') | |
| self.prant(f'module path changed & set ({args})') | |
| else: | |
| stpp.addvar('sltr_mdl_pth', f'@qp({args}):', SLTR_PTH, 'slotster') | |
| self.prant(f'module path changed & set ({args})') | |
| else: raise Exception('staqtapp variable edit denied') | |
| else: raise Exception('staqtapp variable edit denied') | |
| else: | |
| if not os.path.isfile(f'{SLTR_PTH}/slotster.tpqt'): raise Exception('no module path has been set to remove') | |
| else: | |
| if stpp.keyvar(True, 'sltr_mdl_pth', 'slots_module_path', f'{SLTR_PTH}/slotster.tqpt'): | |
| if stpp.findvar(False, 'sltr_mdl_pth', SLTR_PTH, 'slotster'): | |
| stpp.changevar('sltr_mdl_pth', '@qp(null):', SLTR_PTH, 'slotster') | |
| self.prant('set module path removed') | |
| else: | |
| stpp.addvar('sltr_mdl_pth', '@qp(null):', SLTR_PTH, 'slotster') | |
| self.prant('set module path removed') | |
| else: raise Exception('staqtapp variable edit denied') | |
| #__________________________________________________________________________________ | |
| def create_module_path(self, args): | |
| # Sets the default create/build module folder path. | |
| if not os.path.isdir(args): raise Exception(f'invalid folder path ({args})') | |
| else: | |
| if not stpp.findvar(False, 'sltr_crt_pth', SLTR_PTH, 'slotster'): stpp.addvar('sltr_crt_pth', f'@qp({args}):', SLTR_PTH, 'slotster') | |
| else: stpp.changevar('sltr_crt_pth', f'@qp({args}):', SLTR_PTH, 'slotster') | |
| self.prant(f'create module folder path set ({args})') | |
| #__________________________________________________________________________________ | |
| def show_slots_variables_names(self, args): | |
| # Prints to console found slots variables names. | |
| # __slots__ in use----> (_rStrB) | |
| sCls = SlotsterCnsl() | |
| if args == 'r': | |
| if not os.path.isfile(f'{SLTR_PTH}/slotster.tqpt'): self.prant('slotster setting file missing, no set module path to run search') | |
| else: | |
| if not stpp.findvar(False, 'sltr_mdl_pth', SLTR_PTH, 'slotster'): self.prant('no set module path found, type smp to set a default module path') | |
| else: | |
| sCls._rStrB = self.current_module_path(False,True) | |
| if not os.path.isfile(sCls._rStrB): self.prant('current set module path is not a valid file') | |
| else: | |
| self.show_slots_variables_names_ext(sCls._rStrB) | |
| self.reset_slots(True,True,True,True) | |
| else: | |
| if not os.path.isfile(args): self.prant(f'invalid file path ({args})') | |
| else: | |
| self.show_slots_variables_names_ext(args) | |
| self.reset_slots(True,True,True,True) | |
| #__________________________________________________________________________________ | |
| def show_slots_variables_names_ext(self, mPth): | |
| # Fitter method for show_slots_variables_names | |
| # __slots__ in use----> (_rLstC, _rStrC) | |
| sCls = SlotsterCnsl() | |
| with open(mPth, mode='r') as mSrcObj: sCls._rStrC = mSrcObj.read() | |
| self.prant('doing search for slots variables...') | |
| if len(sCls._rStrC) > 41: | |
| sCls._rLstC = self.get_slots_variables_list(sCls._rStrC) | |
| if len(sCls._rLstC) > 0: | |
| for k in range(len(sCls._rLstC)): | |
| self.prant(f'\n __slots__ variables names list({k+1}):') | |
| sCls._rStrC = sCls._rLstC[k].split(',') | |
| for l in range(len(sCls._rStrC)): self.prant(f' {sCls._rStrC[l]}') | |
| else: self.prant('no slots variables found') | |
| else: self.prant('module source length not a valid length') | |
| #__________________________________________________________________________________ | |
| def change_slots_variable_name(self, args): | |
| # Replaces a typed slots var name with a new slots var name also typed. | |
| # __slots__ in use----> (_rLstA, _rStrA, _rStrB, _rIntE, _rBoolA) | |
| if args.find('='): | |
| sCls = SlotsterCnsl() | |
| sCls._rLstA = args.split('=') | |
| if len(sCls._rLstA) == 2: | |
| sCls._rBoolA = True | |
| for v in range(len(sCls._rLstA)): | |
| if not set(sCls._rLstA[v]).issubset(sCls._sSetVarNms): | |
| sCls._rBoolA = False | |
| break | |
| if sCls._rBoolA: | |
| if stpp.findvar(False, 'sltr_mdl_pth', SLTR_PTH, 'slotster'): | |
| sCls._rStrA = stpp.loadvar_str('sltr_mdl_pth', SLTR_PTH, 'slotster') | |
| if sCls._rStrA != 'null': | |
| with open(sCls._rStrA, mode='r') as mSrcObj: sCls._rStrB = mSrcObj.read() | |
| sCls._rIntE = self.rename_slots_variables_names(True, sCls._rStrB, sCls._rLstA) | |
| if sCls._rIntE == -1: self.prant('no slots variables declare line(s) found') | |
| elif sCls._rIntE == -2: self.prant('no slots variables found..........') | |
| elif sCls._rIntE == -3: self.prant(f'slots var "{sCls._rLstA[1]}" was not found') | |
| elif sCls._rIntE == -4: self.prant(f'non-allowed edit, new slots var name "{sCls._rLstA[0]}" already exist!') | |
| else: self.prant('set module path is null, svc requires a set module path') | |
| else: self.prant('no set module path for svc, type smp to set a module path') | |
| else: self.prant('invalid chars for variable naming') | |
| else: self.prant('svc requires two variable names') | |
| else: self.prant('equal = separator missing or missing two variable names...') | |
| self.reset_slots(True,True,True,True) | |
| #__________________________________________________________________________________ | |
| def slots_vars_assignment_usage_track(self, args): | |
| # Shows a list order of slots variables usage for assignment = lines. | |
| # ☆__slots__ in use----> (_sf_rLstA, _sf_rStrA, _sf_rStrB) | |
| if args != 'r': | |
| if not os.path.isfile(args): raise Exception(f'invalid file path ({args})') | |
| self._sf_rStrA = args | |
| if args == 'r': | |
| if not stpp.findvar(False, 'sltr_mdl_pth', SLTR_PTH, 'slotster'): self.prant('no set module path, type smp to set a module path') | |
| else: self._sf_rStrA = self.current_module_path(False,True) | |
| if self._sf_rStrA != 'null': | |
| with open(self._sf_rStrA, mode='r') as mSrcObj: self._sf_rStrB = mSrcObj.read() | |
| self._sf_rLstA = self.get_slots_variables_list(self._sf_rStrB) | |
| if self._sf_rLstA != -1 and self._sf_rLstA != -2: | |
| self.scan_slots_assignment_usage(self._sf_rLstA, self._sf_rStrB) | |
| else: | |
| if self._sf_rLstA == -1: self.prant('no slots variables declare line(s) found') | |
| else: self.prant('no slots variables found..........') | |
| else: self.prant('set module path read null, type smp to set a module path') | |
| self.reset_sf_slots(True,True,True,True) | |
| #__________________________________________________________________________________ | |
| def slots_vars_functions_usage_track(self, args): | |
| # | |
| # ☆__slots__ in use----> (_sf_rStrA, _sf_rStrB) | |
| if stpp.findvar(False, 'sltr_mdl_pth', SLTR_PTH, 'slotster'): | |
| self._sf_rStrA = self.current_module_path(False,True) | |
| if self._sf_rStrA != 'null': | |
| with open(self._sf_rStrA, mode='r') as mSrcObj: self._sf_rStrB = mSrcObj.read() | |
| if self._sf_rStrB.find(args) > -1: self.scan_slots_functions_usage(self._sf_rStrB, str(args)) | |
| else: self.prant(f'the typed function name "{args}" was not found') | |
| else: self.prant('set module path read null, type smp to set a module path') | |
| else: self.prant('a set module path is required for svf, type smp to set a module path') | |
| self.reset_sf_slots(True,True,True,True) | |
| #__________________________________________________________________________________ | |
| def commands_help(self): | |
| print('\n Slotster v1.0.41 Commands:') | |
| print('\n || smp: sets or removes the default module file path used by commands') | |
| print(' || cmp: sets the default create module file path used by commands') | |
| print(' || svn: prints search results for all found slots variables names') | |
| print(' || sva: prints slots vars assignment(s) from set or typed module path') | |
| print(' || svf: scans, comments and prints slots vars list in defined function') | |
| print(' || svc: changes a slots variable name to new name throughout set module ') | |
| print(' || res: restores the set working module source to a previous state') | |
| print('\n') | |
| #__________________________________________________________________________________ | |
| #__________________________________________________________________________________ | |
| #__________________________________________________________________________________ | |
| #__________________________________________________________________________________ | |
| def rename_slots_variables_names(self, fromSvc, src, varNmsLst): | |
| # Replaces all slots var name(s) specified in set module to new slots var name(s). | |
| # Handles full scope replacement for multiple __slots__ vars declaring list found. | |
| # __slots__ in use----> (_rLstB, _rLstC, _rLstD, _rIntA, _rIntB, _rIntC, _rIntD, _rBoolB, _rBoolC, _rBoolD) | |
| sCls = SlotsterCnsl() | |
| sCls._rLstB = self.get_slots_variables_list(src) | |
| if sCls._rLstB == -1 or sCls._rLstB == -2: | |
| return sCls._rLstB | |
| else: | |
| sCls._rBoolD = True | |
| sCls._rIntA = len(varNmsLst) | |
| for sl in range(len(sCls._rLstB)): | |
| sCls._rLstC = sCls._rLstB[sl].split(',') | |
| sCls._rIntC = len(sCls._rLstC) | |
| sCls._rIntB = 0 | |
| while sCls._rIntB < sCls._rIntA: | |
| sCls._rBoolB = False | |
| sCls._rBoolC = False | |
| for svn in range(sCls._rIntC): | |
| if sCls._rLstC[svn] == varNmsLst[sCls._rIntB+1]: sCls._rBoolB = True | |
| if sCls._rLstC[svn] == varNmsLst[sCls._rIntB]: sCls._rBoolC = True | |
| if sCls._rBoolB: | |
| if not sCls._rBoolC: | |
| if sCls._rBoolD: | |
| sCls._rBoolD = False | |
| self.backup_src('SLTRCRREDT', None, 'sltrsrc', src, True, False) | |
| sCls._rIntD = 0 | |
| while sCls._rIntD < sCls._rIntA: | |
| src = re.sub(varNmsLst[sCls._rIntD+1], varNmsLst[sCls._rIntD], src) | |
| self.prant(f'replaced all slots vars ({varNmsLst[sCls._rIntD+1]}) with ({varNmsLst[sCls._rIntD]})') | |
| sCls._rIntD+=2 | |
| else: | |
| if fromSvc: | |
| return -4 | |
| else: | |
| if fromSvc and sl == len(sCls._rLstB)-1: | |
| return -3 | |
| sCls._rIntB+=2 | |
| with open(self.current_module_path(False,True), mode='w') as wrtSrcObj: wrtSrcObj.write(src) | |
| return 1 | |
| #__________________________________________________________________________________ | |
| def get_slots_variables_list(self, src): | |
| # Returns list of all __slots__ variables names declared of @src. | |
| # -1 return no __slots__ declare line found, or -2 search for slots vars exhausted. | |
| # __slots__ in use----> (_rLstA, _rBoolA) | |
| sCls = SlotsterCnsl() | |
| sCls._rLstA = None | |
| sCls._rBoolA = False | |
| if src.find('__slots__') > -1: | |
| sCls._rLstA = re.findall(r'__slots__\s*=\s*(.*?)\)', src) | |
| if len(sCls._rLstA) > 0: sCls._rBoolA = True | |
| else: | |
| sCls._rLstA = re.findall(r'__slots__\s*=\s*(.*?)\]', src) | |
| if len(sCls._rLstA) > 0: sCls._rBoolA = True | |
| if sCls._rBoolA: | |
| for s in range(len(sCls._rLstA)): | |
| sCls._rLstA[s] = sCls._rLstA[s].replace(' ','').replace('"','') | |
| sCls._rLstA[s] = sCls._rLstA[s].replace("'",'').replace('(','') | |
| sCls._rLstA[s] = sCls._rLstA[s].replace('[','') | |
| return sCls._rLstA | |
| else: | |
| sCls._rLstA = self.scan_src(src, True, -1, '__slots__', ')') | |
| if not len(sCls._rLstA) > 0: sCls._rLstA = self.scan_src(src, True, -1, '__slots__', ']') | |
| if len(sCls._rLstA) > 0: | |
| for s in range(len(sCls._rLstA)): | |
| sCls._rLstA[s] = sCls._rLstA[s].replace('__slots__','').replace(' ','').replace('"','') | |
| sCls._rLstA[s] = sCls._rLstA[s].replace('\n','').replace("'",'').replace('(','').replace(')','') | |
| sCls._rLstA[s] = sCls._rLstA[s].replace('=','').replace('[','').replace(']','') | |
| return sCls._rLstA | |
| else: | |
| return -2 | |
| else: | |
| return -1 | |
| #__________________________________________________________________________________ | |
| def scan_slots_assignment_usage(self, svLst, src): | |
| # Scans & prints to console the slots vars assignment '=' lines where any found. | |
| # ☆__slots__ in use----> (_sf_rLstB, _sf_rLstC, _sf_rLstD, _sf_rIntA, _sf_rIntB, _sf_rIntC, _sf_BoolA) | |
| for sl in range(len(svLst)): | |
| self._sf_rLstB = svLst[sl].split(',') | |
| self._sf_rIntA = len(self._sf_rLstB) | |
| for a in range(self._sf_rIntA): | |
| self._sf_rLstC = self.scan_src(src, True, -1, f'{self._sf_rLstB[a]} =', '\n') | |
| self._sf_rIntB = len(self._sf_rLstC) | |
| self._sf_rLstD = self.scan_src(src, True, -1, f'{self._sf_rLstB[a]}=', '\n') | |
| self._sf_rIntC = len(self._sf_rLstD) | |
| print('\n') | |
| self._sf_rBoolA = False | |
| self.dsg_prant(f'[ {self._sf_rLstB[a]} "=" assignment(s) ]:\n',1) | |
| if self._sf_rIntB > 0: | |
| self._sf_rBoolA = True | |
| for p in range(self._sf_rIntB): | |
| if len(self._sf_rLstC[p]) > 54: self.prant(self._sf_rLstC[p].replace('\n','')[0:55]+'...') | |
| else: self.prant(self._sf_rLstC[p].replace('\n','')) | |
| if self._sf_rIntC > 0: | |
| self._sf_rBoolA = True | |
| for e in range(self._sf_rIntC): | |
| if len(self._sf_rLstD[e]) > 54: self.prant(self._sf_rLstD[e].replace('\n','')[0:55]+'...') | |
| else: self.prant(self._sf_rLstD[e].replace('\n','')) | |
| if not self._sf_rBoolA: | |
| self.prant('no assignment "=" instructions found..........') | |
| #__________________________________________________________________________________ | |
| def scan_slots_functions_usage(self, src, fncNm): | |
| # Scans and prints found slots variables use in a defined function block. | |
| # (Search assumes the function ends by a keyword of 'def' or 'class'; or | |
| # search has no more text length to search for any chars in set module.) | |
| # ☆__slots__ in use----> (_sf_rLstA, _sf_rLstB, _sf_rLstC, _sf_rIntA, _sf_rIntB, _sf_rIntC) | |
| self._sf_rIntA = src.find(f'def {fncNm}(') | |
| if self._sf_rIntA > -1: | |
| self._sf_rLstA = self.get_slots_variables_list(src) | |
| if self._sf_rLstA != -1 and self._sf_rLstA != -2: | |
| self._sf_rIntB = src.find('def', self._sf_rIntA+2) | |
| self._sf_rIntC = src.find('class', self._sf_rIntA+2) | |
| if self._sf_rIntB < 0 and self._sf_rIntC > -1: self._sf_rIntB = self._sf_rIntC | |
| elif self._sf_rIntB < 0 and self._sf_rIntC < 0: self._sf_rIntB = len(src)-1 | |
| elif self._sf_rIntB > -1 and self._sf_rIntC > -1: | |
| if self._sf_rIntB > self._sf_rIntC: self._sf_rIntB = self._sf_rIntC | |
| src = src[self._sf_rIntA:self._sf_rIntB] | |
| self._sf_rIntC = len(self._sf_rLstA) | |
| self._sf_rLstC = [] | |
| for sl in range(self._sf_rIntC): | |
| self._sf_rLstB = self._sf_rLstA[sl].split(',') | |
| self._sf_rIntA = len(self._sf_rLstB) | |
| for svn in range(self._sf_rIntA): | |
| if src.find(self._sf_rLstB[svn]) > -1: self._sf_rLstC.append(self._sf_rLstB[svn]) | |
| if len(self._sf_rLstC) > 0: | |
| self.dsg_prant(f'[ "{fncNm}" slots variables in use ]:',1) | |
| for s in range(len(self._sf_rLstC)): self.prant(self._sf_rLstC[s]) | |
| else: self.prant(f'no found slots vars use for function "{fncNm}"') | |
| else: | |
| if self._sf_rLstA == -1: self.prant('no slots variables declare line(s) found') | |
| else: self.prant('no slots variables found..........') | |
| else: self.prant('"{fncNm}" is not a defined function of set module path, no "def" keyword found') | |
| #__________________________________________________________________________________ | |
| def scan_src(self, src, mlt, sPos, bPtrn, eChr): | |
| # Returns scan source(s) list for a starting pattern to a ending char. | |
| # __slots__ in use----> (_rLstB, _rStrA, _rIntA, _rIntB, _rIntC, _rBoolB, _rBoolC) | |
| sCls = SlotsterCnsl() | |
| sCls._rLstB = [] | |
| dqRtn = None | |
| sCls._rIntC = len(src) | |
| sCls._rBoolC = False | |
| while True: | |
| sCls._rBoolB = False | |
| sCls._rIntA = 0 | |
| if sPos < 0: sCls._rIntB = src.find(bPtrn) | |
| else: | |
| if sCls._rBoolC == False: sCls._rIntB = sPos | |
| else: sCls._rIntB = src.find(bPtrn, sPos) | |
| if sCls._rIntB > -1: | |
| for sCls._rStrA in src: | |
| if sCls._rBoolB == False and sCls._rIntA == sCls._rIntB: | |
| sCls._rBoolB = True | |
| dqRtn = deque() | |
| dqRtn.append(sCls._rStrA) | |
| elif sCls._rBoolB == True: | |
| if sCls._rStrA == eChr: | |
| dqRtn.append(sCls._rStrA) | |
| sCls._rLstB.append(''.join(dqRtn)) | |
| if mlt == True: | |
| sCls._rBoolC = True | |
| sPos = sCls._rIntA | |
| break | |
| else: | |
| return sCls._rLstB | |
| else: dqRtn.append(sCls._rStrA) | |
| sCls._rIntA+=1 | |
| if sCls._rIntA == sCls._rIntC: | |
| return sCls._rLstB | |
| else: | |
| return sCls._rLstB | |
| #__________________________________________________________________________________ | |
| def rgx_src(self, dsg, ptrn, rplc, src, keep, rtrn): | |
| # Search or search and replace methods with regex. | |
| # [dsg(1)=points list, dsg(2)=findall, dsg(3)=find&replace] | |
| stvn = None | |
| rtrnLst = [] | |
| block = False | |
| if dsg == 1: | |
| m = re.finditer(ptrn, src) | |
| for x in m: | |
| rtrnLst.append(x.start()) | |
| rtrnLst.append(x.end()) | |
| elif dsg == 2: rtrnLst = re.findall(ptrn, src) | |
| elif dsg == 3: | |
| block = True | |
| if not os.path.isfile(f'{SLTR_PTH}/backups/source/SLTRORGSRC.{self.current_module_path(False,False)}.sltrsrc'): | |
| self.backup_src('SLTRORGSRC', None, 'sltrsrc', src, True, False) | |
| self.backup_src('SLTRCRREDT', None, 'sltrsrc', src, True, False) | |
| src = re.sub(ptrn, rplc, src) | |
| #====================================================================== | |
| if keep and not block: | |
| if dsg == 1: stvn = 'sltr_srch_pnts' | |
| elif dsg == 2: stvn = 'sltr_srch_fnda' | |
| if os.path.isfile(f'{SLTR_PTH}/slotster.tqpt'): | |
| if stpp.findvar(False, stvn, SLTR_PTH, 'slotster'): | |
| if stpp.keyvar(True, stvn, 'rgx_src', f'{SLTR_PTH}/slotster.tqpt'): | |
| if len(rtrnLst) > 0: stpp.changevar(stvn, f'@qp({rtrnLst}):', SLTR_PTH, 'slotster') | |
| else: stpp.changevar(stvn, '@qp(null):', SLTR_PTH, 'slotster') | |
| else: | |
| return -2 | |
| else: | |
| if len(rtrnLst) > 0: stpp.addvar(stvn, f'@qp({rtrnLst}):', SLTR_PTH, 'slotster') | |
| else: stpp.addvar(stvn, '@qp(null):', SLTR_PTH, 'slotster') | |
| stpp.lockvar(stvn, ['rgx_src'], f'{SLTR_PTH}/slotster.tqpt') | |
| else: | |
| return -1 | |
| if rtrn and not block: | |
| return rtrnLst | |
| elif rtrn and dsg == 3: | |
| return src | |
| elif not rtrn and dsg == 3: | |
| with open(self.current_module_path(False,True), mode='w') as wrtSrcObj: wrtSrcObj.write(src) | |
| #__________________________________________________________________________________ | |
| def backup_src(self, bName, fName, ext, src, fromSmp, clear): | |
| # Writes backups of working sources, removes backup files or other. | |
| if not clear: | |
| if not os.path.isdir(f'{SLTR_PTH}/backups/source'): | |
| if not os.path.isdir(f'{SLTR_PTH}/backups'): | |
| os.makedirs(f'{SLTR_PTH}/backups') | |
| os.makedirs(f'{SLTR_PTH}/backups/source') | |
| else: os.makedirs(f'{SLTR_PTH}/backups/source') | |
| if fromSmp: fName = self.current_module_path(False,False) | |
| with open(f'{SLTR_PTH}/backups/source/{bName}.{fName}.{ext}', mode='w') as bckupSrcObj: bckupSrcObj.write(src) | |
| else: | |
| pass | |
| #__________________________________________________________________________________ | |
| def restore_module_source_state(self, args): | |
| # Restores the previous module source state before now changes or | |
| # restores the original module source state on the first backup done. | |
| # __slots__ in use----> (_rStrA, _rStrB, _rStrC, _rStrD) | |
| sCls = SlotsterCnsl() | |
| if args == 'o' or args == 'r': | |
| if stpp.findvar(False, 'sltr_mdl_pth', SLTR_PTH, 'slotster'): | |
| sCls._rStrC = self.current_module_path(False,True) | |
| if sCls._rStrC != 'null': | |
| sCls._rStrD = self.current_module_path(False,False) | |
| if args == 'r': sCls._rStrA = f'{SLTR_PTH}/backups/source/SLTRCRREDT.{sCls._rStrD}.sltrsrc' | |
| else: sCls._rStrA = f'{SLTR_PTH}/backups/source/SLTRORGSRC.{sCls._rStrD}.sltrsrc' | |
| if os.path.isfile(sCls._rStrA): | |
| with open(sCls._rStrA, 'r') as rSrcObj: sCls._rStrB = rSrcObj.read() | |
| sCls._rStrA = sCls._rStrC.split('/') | |
| with open(f'{SLTR_PTH}/backups/source/{sCls._rStrA[len(sCls._rStrA)-1]}', 'w') as wSrcObj: wSrcObj.write(sCls._rStrB) | |
| os.replace(f'{SLTR_PTH}/backups/source/{sCls._rStrA[len(sCls._rStrA)-1]}', sCls._rStrC) | |
| if args == 'r': self.prant('restore of previous module source state finished') | |
| else: self.prant('restore of original module source state finished') | |
| else: | |
| if args == 'r': self.prant('no found ~SLTRCRREDT~ current backup source file for restore!') | |
| else: self.prant('no found ~SLTRORGSRC~ original backup source file for restore!') | |
| else: self.prant('no current module path set for restore, set module path read null.......') | |
| else: self.prant('no current module path set for restore, type smp to set a module path') | |
| self.reset_slots(False,True,False,False) | |
| #__________________________________________________________________________________ | |
| def current_module_path(self, prant, fullPath): | |
| # Returns a printable string for current set module paths feedback. | |
| # __slots__ in use----> (static: _sSmp) | |
| if prant: | |
| if not os.path.isfile(f'{SLTR_PTH}/slotster.tpqt'): | |
| return '□ | current set module path: null |' | |
| else: | |
| if not stpp.findvar(False, 'sltr_mdl_pth', SLTR_PTH, 'slotster'): | |
| return '□ | current set module path: not set |' | |
| else: | |
| return f'□ | current set module path: {stpp.loadvar_str("sltr_mdl_pth", SLTR_PTH, "slotster")} |' | |
| else: | |
| sCls = SlotsterCnsl() | |
| sCls._sSmp = stpp.loadvar_str('sltr_mdl_pth', SLTR_PTH, 'slotster') | |
| if fullPath or sCls._sSmp == 'null': | |
| return sCls._sSmp | |
| else: | |
| sCls._sSmp = sCls._sSmp.split('/') | |
| sCls._sSmp = sCls._sSmp[len(sCls._sSmp)-1] | |
| sCls._sSmp = sCls._sSmp.replace('.','_').split('_') | |
| sCls._sSmp.pop(len(sCls._sSmp)-1) | |
| if len(sCls._sSmp) > 1: sCls._sSmp = '-'.join(sCls._sSmp) | |
| else: sCls._sSmp = sCls._sSmp[0] | |
| return sCls._sSmp | |
| #__________________________________________________________________________________ | |
| def current_create_module_path(self): | |
| # Returns a printable string for current set create module folder path feedback. | |
| rtrn = None | |
| if not os.path.isfile(f'{SLTR_PTH}/slotster.tpqt'): rtrn = '□ | current set create module folder path: null |' | |
| else: | |
| if not stpp.findvar(False, 'sltr_crt_pth', SLTR_PTH, 'slotster'): rtrn = '□ | current set create module folder path: not set |' | |
| else: rtrn = f'□ | current set create module folder path: {stpp.loadvar_str("sltr_crt_pth", SLTR_PTH, "slotster")} |' | |
| return rtrn | |
| #__________________________________________________________________________________ | |
| def reset_slots(self, l, s, i, b): | |
| # Resets __slots__ class variables to None. | |
| sCls = SlotsterCnsl() | |
| if l: | |
| sCls._rLstA = None | |
| sCls._rLstB = None | |
| sCls._rLstC = None | |
| sCls._rLstD = None | |
| sCls._rLstE = None | |
| if s: | |
| sCls._rStrA = None | |
| sCls._rStrB = None | |
| sCls._rStrC = None | |
| sCls._rStrD = None | |
| if i: | |
| sCls._rIntA = None | |
| sCls._rIntB = None | |
| sCls._rIntC = None | |
| sCls._rIntD = None | |
| sCls._rIntE = None | |
| if b: | |
| sCls._rBoolA = None | |
| sCls._rBoolB = None | |
| sCls._rBoolC = None | |
| sCls._rBoolD = None | |
| sCls._rBoolE = None | |
| #__________________________________________________________________________________ | |
| def reset_sf_slots(self, l, s, i, b): | |
| # Resets __slots__ sf class variables to None. | |
| if l: | |
| self._sf_rLstA = None | |
| self._sf_rLstB = None | |
| self._sf_rLstC = None | |
| self._sf_rLstD = None | |
| if s: | |
| self._sf_rStrA = None | |
| self._sf_rStrB = None | |
| self._sf_rStrC = None | |
| self._sf_rStrD = None | |
| if i: | |
| self._sf_rIntA = None | |
| self._sf_rIntB = None | |
| self._sf_rIntC = None | |
| self._sf_rIntD = None | |
| if b: | |
| self._sf_rBoolA = None | |
| self._sf_rBoolB = None | |
| self._sf_rBoolC = None | |
| self._sf_rBoolD = None | |
| #__________________________________________________________________________________ | |
| def dsg_prant(self, txt, dsg): | |
| # Prints custom command run results print. | |
| if dsg == 1: print(f' |□ {txt}') | |
| #__________________________________________________________________________________ | |
| def prant(self, txt): | |
| # Prints a standard command run results print. | |
| print(f' |■ {txt}') | |
| #__________________________________________________________________________________ | |
| #def show_random_lambda(self, args): | |
| # Prints a random built complex lambda function to console and stores it. | |
| # __slots__ in use----> (_rStrA, _rIntA) | |
| #sCls = SlotsterCnsl() | |
| #sCls._rStrA = dun3_aux_lmb_rtn(True, int(args)) | |
| #sCls._rIntA = 0 | |
| #while True: | |
| #sCls._rIntA+=1 | |
| #if not stpp.findvar(False, f'sltr_lambda_{sCls._rIntA}', SLTR_PTH, 'slotster'): | |
| #break | |
| #stpp.addvar(f'sltr_lambda_{sCls._rIntA}', f'@qp({sCls._rStrA}):', SLTR_PTH, 'slotster') | |
| #self.prant(sCls._rStrA) | |
| #sCls._rStrA = None | |
| #sCls._rIntA = None | |
| #__________________________________________________________________________________ | |
| def TEST(self, args): | |
| # TEST METHOD | |
| if args == 'r': | |
| sCls = SlotsterCnsl() | |
| #mPth = stpp.loadvar_str("sltr_mdl_pth", SLTR_PTH, "slotster") | |
| #mSrc = None | |
| #with open(mPth, mode='r') as mSrcObj: mSrc = mSrcObj.read() | |
| #============================================================ | |
| #__________________________________________________________________________________ | |
| def main(): | |
| # Sets the commands & functions, enters main console app loop. | |
| try: | |
| stpp.makesource(False, True, False, None, 'slotster') | |
| except Exception as e: | |
| pass | |
| cls_cnsl = SlotsterCnsl() | |
| cls_fncs = SltsrFncs() | |
| #--------ADD COMMANDS---------------------------------------------------- | |
| cls_cnsl.add_cmd('smp',{'fnct': 'smp', 'prmp': ' □|smp|\n □:type module path to save or "r" to remove: '}) | |
| cls_cnsl.add_cmd('cmp',{'fnct': 'cmp', 'prmp': ' □|cmp|\n □:type folder path to save: '}) | |
| cls_cnsl.add_cmd('svn',{'fnct': 'svn', 'prmp': ' □|svn|\n □:type module path or "r" to run: '}) | |
| cls_cnsl.add_cmd('sva',{'fnct': 'sva', 'prmp': ' □|sva|\n □:type module path or "r" to run: '}) | |
| cls_cnsl.add_cmd('svf',{'fnct': 'svf', 'prmp': ' □|svf|\n □:type function name to scan for slots use: '}) | |
| cls_cnsl.add_cmd('svc',{'fnct': 'svc', 'prmp': '□|svc|\n □:<new var name>=<var name>(no spaces): '}) | |
| cls_cnsl.add_cmd('res',{'fnct': 'res', 'prmp': ' □|res|\n □:type "r" to restore or "o" for restore to original: '}) | |
| #cls_cnsl.add_cmd('lmb',{'fnct': 'lmb', 'prmp': ' □|lmb|\n □:number of lambda params(max=8): '}) | |
| cls_cnsl.add_cmd('test',{'fnct': 'test', 'prmp': '\n□TEST: '}) | |
| #--------ADD FUNCTIONS----------------------------------------------------- | |
| cls_cnsl.add_fnc('smp', cls_fncs.slots_module_path) | |
| cls_cnsl.add_fnc('cmp', cls_fncs.create_module_path) | |
| cls_cnsl.add_fnc('svn', cls_fncs.show_slots_variables_names) | |
| cls_cnsl.add_fnc('sva', cls_fncs.slots_vars_assignment_usage_track) | |
| cls_cnsl.add_fnc('svf', cls_fncs.slots_vars_functions_usage_track) | |
| cls_cnsl.add_fnc('svc', cls_fncs.change_slots_variable_name) | |
| cls_cnsl.add_fnc('res', cls_fncs.restore_module_source_state) | |
| #cls_cnsl.add_fnc('lmb', cls_fncs.show_random_lambda) | |
| cls_cnsl.add_fnc('test', cls_fncs.TEST) | |
| #__________________________________________________________________________________ | |
| cl = ['help','exit','smp','cmp'] | |
| lpStrt = True | |
| tmp = None | |
| while True: | |
| if lpStrt: | |
| lpStrt = False | |
| print('■|□__________________________________________________□|■') | |
| print('□|■__________________________________________________■|□') | |
| print('■|□__________________________________________________□|■') | |
| print('□|■ Slotster v1.0.41 (type help for commands list) ■|□') | |
| cmmd = input('□: ') | |
| if cmmd == cl[0] or cmmd == cl[1]: | |
| if cmmd == cl[0]: cls_fncs.commands_help() | |
| else: | |
| break | |
| else: | |
| if cmmd == cl[2] or cmmd == cl[3]: | |
| if cmmd == cl[2]: print(cls_fncs.current_module_path(True, None)) | |
| elif cmmd == cl[3]: print(cls_fncs.current_create_module_path()) | |
| try: | |
| cls_cnsl.run(cmmd) | |
| except Exception as cnsl_err: | |
| tmp = str(cnsl_err).replace(' ',"").split(':') | |
| if tmp[len(tmp)-1] == "'args'": tmp = None | |
| else: | |
| cls_fncs.reset_slots(True,True,True,True) | |
| cls_fncs.reset_sf_slots(True,True,True,True) | |
| print(f' |■ error: {cnsl_err}') | |
| #__________________________________________________________________________________ | |
| if __name__ == '__main__': | |
| main() | |
| #__________________________________________________________________________________ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment