You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spot.apache.org by le...@apache.org on 2019/09/11 01:40:10 UTC

[incubator-spot] 41/45: Added substitute function and fixed GetRoot to not be hardcoded.

This is an automated email from the ASF dual-hosted git repository.

leahy pushed a commit to branch asf-site
in repository https://gitbox.apache.org/repos/asf/incubator-spot.git

commit da375f8af2954f5c84e5e0da33fec3cbb0f6dfbf
Author: Mark Schoeni <ma...@gmail.com>
AuthorDate: Mon Dec 17 14:41:03 2018 -0600

    Added substitute function and fixed GetRoot to not be hardcoded.
---
 _Tools/page-massedit.py | 50 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 45 insertions(+), 5 deletions(-)

diff --git a/_Tools/page-massedit.py b/_Tools/page-massedit.py
index 7514602..9f2b55a 100644
--- a/_Tools/page-massedit.py
+++ b/_Tools/page-massedit.py
@@ -9,6 +9,13 @@ print('Use this to make changes across all the Spot pages fast (mainly menus)')
 print('This is a result of laziness; enjoy \n')
 
 
+def GetRootDir(rootName):
+    filePath = os.path.realpath(__file__)
+    pathSplit = filePath.split('\\')
+    idx = 1 + [i for i, s in enumerate(pathSplit) if rootName in s][-1]
+    return '\\'.join(pathSplit[0:idx])
+
+
 def FindFiles(_rootDir, _pattern: str):
     _fileList = []
 
@@ -47,6 +54,7 @@ def RegDelLines(_searchPattern, _files):
                 for line in oldFile:
                     pattern = re.compile(_searchPattern)
                     matches = pattern.findall(line)
+
                     if len(matches) == 0:
                         newFile.write(line)
                     else:
@@ -82,7 +90,34 @@ def RegInsertAfter(_searchPattern, _files, _line2Insert, _insertType):
         os.remove(filePath + ".old")
 
 
-rootDir = "c:\\code\\incubator-spot"  #input('enter directory:')
+def RegSubstitute(_searchPattern, _files, _text2Sub):
+    for filePath in _files:
+        baseName = os.path.basename(filePath)
+        newFileName = os.path.dirname(filePath) + "\\" + baseName + ".new"
+
+        with open(filePath, "r", encoding='utf-8') as oldFile:
+            # create swap file
+            with open(newFileName, "w", encoding='utf-8') as newFile:
+                for line in oldFile:
+                    pattern = re.compile(_searchPattern)
+                    matches = pattern.findall(line)
+
+                    if len(matches) > 0:
+                        for match in matches:
+                            newLine = line.replace(match, _text2Sub)
+
+                        newFile.write(newLine)
+                    else:
+                        newFile.write(line)
+
+
+        # Swap Files
+        os.rename(filePath, filePath + ".old")
+        os.rename(newFileName, filePath)
+        os.remove(filePath + ".old")
+
+
+rootDir = GetRootDir('incubator-spot')
 
 files = FindFiles(rootDir, 'index.html')
 
@@ -90,7 +125,7 @@ searchPattern = input("Search Pattern:")
 
 files = RegSearch(searchPattern, files)
 
-print("commands(DelLines / InsertAfter/ ...)")
+print("commands(DelLines / InsertAfter/ Sub)")
 
 while True:
     cmd = input("How to Proceed?: ")
@@ -101,7 +136,7 @@ while True:
 
     # Insert After
     elif cmd.lower() == 'insertafter':
-        insertType = input('append (app) or next line (nl)')
+        insertType = input('append (app) or next line (nl):')
 
         # normalise
         if insertType in ['app', 'append']:
@@ -115,13 +150,18 @@ while True:
         else:
             print("Invalid Insertion Type")
 
+    # Substitute
+    elif cmd.lower() == 'sub':
+        subText = input('Text to substitute with:')
+        RegSubstitute(searchPattern, files, subText)
+
     # Regex Replace
-    elif cmd == 'regex':
+    elif cmd.lower() == 'regex':
         print("Not Implemented Yet")
         #massedit.edit_files(files, ["re.sub()"])
 
     # Quit
-    elif cmd in ['q', 'quit']:
+    elif cmd in ['q', 'quit', 'exit']:
         break
 
     # ?!?