Skip to content

Commit 8eefb6a

Browse files
committed
feat: implement change_entity_label() (first working version)
# Last 3 commits: # 6d41820 feat: implement `change_entity_label()` (first working version) # 1a3943d feat: implement `change_entity_label()` (wip) # 11b3b6d feat (test_refactor_tools): add `make_temp_copy_of_file()` # Last 3 commits: # f2b9050 feat: implement `change_entity_label()` (wip) # 7af0110 feat (test_refactor_tools): add `make_temp_copy_of_file()` # 2db8f37 feat (script.py): add `--refactor-entity-label` option stub
1 parent f2b9050 commit 8eefb6a

3 files changed

Lines changed: 32 additions & 11 deletions

File tree

src/pyirk/refactor_tools.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,16 @@ def change_entity_label(key, new_label, fpath):
3838

3939
src_lines[start_idx + i] = new_defining_line
4040

41-
with open(fpath, "w", encoding="utf-8") as fp:
42-
fp.writelines(src_lines)
41+
new_src = "".join(src_lines)
42+
43+
replacements = [
44+
(f'{key}["{original_label}"]', f'{key}["{new_label}"]'),
45+
(f"{key}['{original_label}']", f"{key}['{new_label}']"),
46+
# TODO: add underscore_version
47+
]
4348

44-
IPS()
49+
for rplm in replacements:
50+
new_src = new_src.replace(*rplm)
51+
52+
with open(fpath, "w", encoding="utf-8") as fp:
53+
fp.write(new_src)

src/pyirk/script.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,8 @@ def main():
197197
if args.refactor_entity_label:
198198
from . import refactor_tools as rt
199199
assert args.inputfile is not None
200-
rt.change_entity_label(1, 2, args.inputfile)
201-
202-
IPS()
200+
assert len(args.refactor_entity_label) == 2
201+
rt.change_entity_label(*args.refactor_entity_label, args.inputfile)
203202
exit()
204203

205204
if args.version:

tests/test_refactor_tools.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import unittest
23
import tempfile
34
import shutil
@@ -17,19 +18,31 @@
1718

1819
# noinspection PyPep8Naming
1920
class Test_01_Script(HousekeeperMixin, unittest.TestCase):
20-
def test_rt_a01__(self):
21+
def test_rt_a01__change_entity_label(self):
2122

22-
tmp_mod_fpath0 = make_temp_copy_of_file(ocse_subset_agents_path)
2323
tmp_mod_fpath = make_temp_copy_of_file(ocse_subset_agents_path)
2424

2525
# change I9942["Stanford University"] to I9942["Renamed Stanford University"]
2626
rt.change_entity_label(key="I9942", new_label="Renamed Stanford University", fpath=tmp_mod_fpath)
2727

28-
# for manually checking the difference
29-
import os
30-
os.system(f"kdiff3 {tmp_mod_fpath0} {tmp_mod_fpath}")
28+
manually_checking_difference = False
29+
if manually_checking_difference:
30+
tmp_mod_fpath0 = make_temp_copy_of_file(ocse_subset_agents_path)
31+
os.system(f"kdiff3 {tmp_mod_fpath0} {tmp_mod_fpath}")
32+
33+
34+
def test_rt_a02__change_entity_label_from_cli(self):
35+
36+
tmp_mod_fpath = make_temp_copy_of_file(ocse_subset_agents_path)
37+
38+
cmd = f'pyirk --refactor-entity-label I9942 "Renamed Stanford University" {tmp_mod_fpath}'
39+
os.system(cmd)
3140

3241

42+
manually_checking_difference = False
43+
if manually_checking_difference:
44+
tmp_mod_fpath0 = make_temp_copy_of_file(ocse_subset_agents_path)
45+
os.system(f"kdiff3 {tmp_mod_fpath0} {tmp_mod_fpath}")
3346
#
3447
# Auxiliary functions:
3548
#

0 commit comments

Comments
 (0)