Skip to content

Instantly share code, notes, and snippets.

@repen
Created October 18, 2020 17:46
Show Gist options
  • Save repen/16499bc5a5e1cf66f5d3ddb12a4c3dd1 to your computer and use it in GitHub Desktop.
Save repen/16499bc5a5e1cf66f5d3ddb12a4c3dd1 to your computer and use it in GitHub Desktop.
sssss
import configparser, time, re, os
from tools import log as lo, listdir_fullpath
class Text:
def __init__(self, text, path):
self.text = text
self.path = path
def __del__(self):
# with open(self.path + ".edit", "w") as f:
with open(self.path, "w", encoding="utf8") as f:
f.write( self.text )
def get_val(self):
return self.text
def set_val(self, text):
self.text = text
class UpdateNum:
def __init__(self, template, koef):
self.template = template
self.koef = koef
left, right = self.template[0], self.template[-1]
self.pattern = re.compile( "\{0}{1}\{2}".format( left, r"(\d+(?:\.\d+)?)", right ) )
def __change(self, *args):
match = args[0]
full = match.group(0)
number_str = match.group(1)
if "." in number_str:
number = int( float( number_str ) * KOEF )
else:
number = int( int( number_str ) * KOEF )
number_str = full[0] + str(number) + full[-1]
return number_str
def run(self, *args):
text = args[0]
result = re.sub( self.pattern, self.__change ,text.get_val() )
text.set_val( result )
return text
config = configparser.RawConfigParser()
config.read('setting.conf')
args = config['site']
#load config
PATH_DIR = args['dir']
KOEF = float(args['koef'])
VAL = args['va']
log = lo( "updBot", "main.log")
values = VAL.split("\n")
handlers = [ UpdateNum(v, KOEF) for v in values ]
def script():
dir_list = listdir_fullpath( PATH_DIR )
# dir_list = filter( lambda path : not re.search(r"edit", path), dir_list )
for path in dir_list:
log.info( "Path: %s handle", path )
with open(path, encoding="utf8") as f:
data = f.read()
text = Text( data, path )
for handler in handlers:
text = handler.run( text )
log.info("=== Done ===")
script()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment