How can i replace/substitute a certain block of code in a file? [closed]

I am working on a python program that searches source files for a certain comment block that mentions the description of the program and what it does. I have been successfully able to extract the block of code (given my current implementation provided below), and the block of code looks like this….

//################ 
//# [file id number] 
//################# 
//# [file name] 
//################# 
//################# 
//# {file description} 
//# {file description continues...} 
//# {file description continues...} 
//# {file description continues...}
//################# 
//# [some more comments] 
//# [some more comments] 
//# [some more comments] 
//##################

However, I need to take the old comment block and replace it with a new comment block that shares similar elements to the new one. For example, I need to slightly alter the file name, and I need to remove the file id number portion altogether. However, I need to keep the file description and “some more comments” portion of the comment block. Given my current implementation of the python program, how can build the new comment block and replace it in the file. Is there a certain function that allows for you to replace certain lines in a source file, because i have kept track of the line offsets of the original comment block as i need it in that exact location? Anything helps!! Thank you!! Here is the code below...

def replace(filename: Path):
  with open(filename) as f: 
  contents = f.read()

  pattern1 = re.compile(r^"//[\s#]+?$")
  pattern2 = re.compile(r^"(?!//!)(^//.*?]\W].*?$)")
  match = "" #empty string
  matches = [] #empty list
  counter = 0 #keeps track of the line number each matched string in the file


  lines = contents.splitlines()
  templineNums = [] #list stores the line number of each line match, however, 
      there may be more matches then needed 
  lineNums = [] #list stores the line number of each correct line match we want 
      for the comment block

  for i in lines: 
       counter = counter + 1
       if (pattern1.match() or pattern2.match()): #checks to see if the regex matches are true
           templineNums.append(counter) 
           match = i + "\n"

  else: 
       if match: 
           matches.append(match) #appends the matched line we want to list 
  match = "" #resets the match to blank string

  linecounter = 0 #keeps track of how many lines are in the code block 
  stringlist = [] #will keep track of each individual string in the comment 
     block 
  stringLines = matches[0].splitlines() #this stores the entire comment block 
     which is located at index 0. the other indices do not matter
  for i in stringLines: 
  linecounter = linecounter + 1
  stringList.append(i) 

  lineNums = templineNums[0:linecounter] #stores the line number for each line 
        in the comment block 
  lineOffset1 = lineNums[0] #stores the value of the beginning line of the 
         comment block 
  lineOffset2 = lineNums[-1] #stores the last line of the comment block


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation