-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxreplace.py
executable file
·139 lines (119 loc) · 4.79 KB
/
xreplace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#! /usr/bin/env python3
# sudo mkdir -p -m 775 /usr/local/bin
# sudo ln -s /Volumes/HOCTAP/GIT/GitHub/xreplace/xreplace.py /usr/local/bin/xreplace
# pip3 install xlrd==1.2.0
# pip3 install pandas
# pip3 install openpyxl
# pip3 install python-dotenv
import os
import sys
from dotenv import load_dotenv
import openpyxl
import pandas as pd
import xml.etree.ElementTree as ET
SCRIPTNAME = __file__
SCRIPT = os.readlink(SCRIPTNAME);
SCRIPTPATH = os.path.dirname(SCRIPT);
CURRENTDIR = os.getcwd();
FILE_ENV = '.env'
if not os.path.exists(FILE_ENV) :
FILE_ENV = SCRIPTPATH + '/.env'
load_dotenv(FILE_ENV)
extList = os.getenv('FILE_EXTENSION').split(' ')
fileRegex = os.getenv('FILE_REGEX')
if len(sys.argv) >= 2 and sys.argv[1] :
fileRegex = sys.argv[1];
if not os.path.exists(fileRegex) :
fileRegex = SCRIPTPATH + '/' + fileRegex
class xreplace:
def __init__(self):
if fileRegex.endswith('.xml'):
self.xreplaceFromXML()
if fileRegex.endswith('.xlsx'):
self.xreplaceFromExcel()
def xreplaceFromXML(self):
tree = ET.parse(fileRegex)
root = tree.getroot()
dataDict = []
for item in root:
findValue = item.find('find').text
replaceValue = item.find('replace').text
dataDict.append({ 'Find': findValue, 'Replace': replaceValue })
self.findAndReplace(dataDict)
def xreplaceFromExcel(self):
xlsx = pd.ExcelFile(fileRegex)
sheetName = xlsx.sheet_names
fieldset = ''
if 'File' in sheetName :
fieldset = 'File'
dataframe = xlsx.parse(fieldset, header=None)
dataList = dataframe.to_dict('list')
dataDict = []
for item in dataList:
extList = dataList[item]
if 'Find' in sheetName and 'Replace' in sheetName :
fieldset = 'Sheet'
findSheet = xlsx.parse('Find')
# findData = findSheet.to_dict('records')
replaceSheet = xlsx.parse('Replace')
# replaceData = replaceSheet.to_dict('records')
dataframe = pd.concat([findSheet, replaceSheet], axis=1)
dataDict = dataframe.to_dict('records')
self.findAndReplace(dataDict, fieldset)
if 'Yreplace' in sheetName :
fieldset = 'Yreplace'
dataframe = xlsx.parse(fieldset, header=None)
dataList = dataframe.to_dict('list')
dataDict = []
for item in dataList:
values = dataList[item]
cleanedList = [x for x in values if str(x) != 'nan']
if len(cleanedList) > 1 :
dataDict.append({ 'Find': cleanedList[0], 'Replace': cleanedList[1] })
self.findAndReplace(dataDict, fieldset)
if 'Xreplace' in sheetName :
fieldset = 'Xreplace'
dataframe = xlsx.parse(fieldset)
dataDict = dataframe.to_dict('records')
self.findAndReplace(dataDict, fieldset)
if fieldset == '':
# parse with parame will return frist sheet
dataframe = xlsx.parse()
dataDict = dataframe.to_dict('records')
self.findAndReplace(dataDict)
def findAndReplace(self, dataDict, fieldset=''):
# for item in dataDict:
# for x in item:
# print(f"key: {x}, value: {item[x]}")
fileList = []
for root, dirs, files in os.walk(CURRENTDIR):
for file in files:
# print(os.path.basename(fileRegex))
#append the file name to the list
# print(os.path.join(root,file))
fileName, fileExt = os.path.splitext(file)
if fileExt in extList:
if file[0] != '.' and file != os.path.basename(fileRegex):
fileList.append(os.path.join(root,file))
totalModified = 0
for pathFile in fileList :
with open(pathFile, 'r') as file :
fileData = file.read()
fileData2 = fileData
for item in dataDict:
findX = item['Find']
replaceX = item['Replace']
if pd.isna(findX) == True:
continue
if(pd.isna(replaceX) == True):
replaceX = ""
print(f"{findX} delete!")
fileData2 = fileData2.replace(findX, replaceX)
# Write the file out again
if fileData != fileData2 :
with open(pathFile, 'w') as file2:
file2.write(fileData2)
print(f"File {pathFile} modified!")
totalModified = totalModified + 1
print(f"Total {fieldset} files modified: {totalModified}")
xreplace()