Skip to content

Commit

Permalink
fixed replace_non_unique so that the prefix can be the same as the se…
Browse files Browse the repository at this point in the history
…arch string
  • Loading branch information
Theodore Lindsay committed Apr 10, 2018
1 parent 86ff386 commit e2fd543
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions figurefirst/svg_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@ def replace_non_unique(in_filename,out_filename,
prefix = ''):
"""parse the document 'in_filename' and replace all
instances of the string 'search_string' with the
unique string 'prefix_x' where x is the occurance
of the search_string"""
unique string 'prefix_x'.
Parameters
----------
in_filename : name of the file to parse
out_filename : name of the file to write. If
out_filename is the same as in_filename
then the old document will be overwritten.
search_string : target string to search and replace. Note:
when applying this function to svg layout documents
take care that you don't target strings that are
common strings in the SVG language. e.g. 'label'
prefix : the prefix will be appended with a number and used
to replace each occurance of the search string.
"""

with open(in_filename,'rt') as inf:
strdta = inf.read()
n_instances = strdta.count(search_string)
for i in range(n_instances):
strdta = strdta.replace(search_string, prefix + '_%s'%(i), 1)
slist = strdta.split(search_string)
outstr = ''.join([s + prefix + '_%s'%(i) for i,s in enumerate(slist[:-1])])
outstr += slist[-1]
with open(out_filename,'wt') as outf:
outf.write(strdta)
outf.write(outstr)

0 comments on commit e2fd543

Please sign in to comment.