change WIP test
This commit is contained in:
parent
16368c13bb
commit
9e7e1b27fd
@ -21,14 +21,16 @@ class WPChange:
|
||||
|
||||
def fromFile(self, files=[], number_thread=1, max_thread=1):
|
||||
divFiles = int(len(files) / max_thread)
|
||||
currentRangeFiles = int(divFiles * (number_thread+1))
|
||||
currentRangeFiles = int(divFiles * (number_thread))
|
||||
firstRange = int(currentRangeFiles - divFiles)
|
||||
self._logger.debug("{0} : index : {1}".format(self._name,number_thread))
|
||||
|
||||
|
||||
self._logger.debug("{0} : first range : {1}".format(self._name,firstRange))
|
||||
self._logger.debug("{0} : last range : {1}".format(self._name,currentRangeFiles))
|
||||
|
||||
for i in range(firstRange, currentRangeFiles):
|
||||
|
||||
if os.path.exists(files[i]):
|
||||
self._logger.info("{0} : ({1}/{2}) File is being processed : {3}".format(self._name, i+1, currentRangeFiles + 1, files[i]))
|
||||
self._change(files[i])
|
||||
@ -72,12 +74,27 @@ class WPChange:
|
||||
## Change path img file
|
||||
|
||||
def _change(self, file):
|
||||
try:
|
||||
with open(file, 'r') as f:
|
||||
content = f.read()
|
||||
soup = BeautifulSoup(content, self._parser)
|
||||
img = soup.find_all("img")
|
||||
for i in img:
|
||||
src = i.get("src")
|
||||
self._logger.info(src)
|
||||
o = urlparse(src)
|
||||
if len(o.netloc) > 0:
|
||||
self._logger.info("{0} : Change source {1} /dists/img/{2}/{3}".format(self._name, src, o.netloc, o.path))
|
||||
content = content.replace(src, "/dists/img/{0}/{1}".format(o.netloc, o.path))
|
||||
try:
|
||||
with open(file, "w") as f:
|
||||
self._logger.info("{0} : File write : {1}".format(self._name, file))
|
||||
f.write(content)
|
||||
except Exception as ex:
|
||||
self._logger.error("{0} : Error for write file {1} : {2}".format(self._name, file, ex))
|
||||
|
||||
except Exception as ex:
|
||||
self._logger.error("{0} : Error for read file {1} : {2}".format(self._name, file, ex))
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user