Skip to content

Commit 0c5cda8

Browse files
authored
Merge pull request #74 from dwolfson/v5.2
Addresses Issue: Separate path from file name in glossary/import and export #71
2 parents b44865f + f0f84d3 commit 0c5cda8

File tree

6 files changed

+67
-19
lines changed

6 files changed

+67
-19
lines changed

pyegeria/commands/cat/glossary_actions.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
EGERIA_WIDTH = os.environ.get("EGERIA_WIDTH", 200)
4949
EGERIA_JUPYTER = os.environ.get("EGERIA_JUPYTER", False)
5050
EGERIA_HOME_GLOSSARY_GUID = os.environ.get("EGERIA_HOME_GLOSSARY_GUID", None)
51+
EGERIA_GLOSSARY_PATH = os.environ.get("EGERIA_GLOSSARY_PATH", None)
5152

5253

5354
@click.command("create-glossary")
@@ -265,8 +266,11 @@ def delete_term(server, url, userid, password, timeout, term_guid):
265266

266267

267268
@click.command("import-terms")
268-
@click.option("--glossary-name", help="Name of Glossary", required=True)
269-
@click.option("--file-name", help="Path of CSV file", required=True)
269+
@click.option("--glossary_name", help="Name of Glossary", required=True)
270+
@click.option("--file_name", help="Path of CSV file", required=True)
271+
@click.option(
272+
"--file_path", help="Path of CSV file", default=EGERIA_GLOSSARY_PATH, required=False
273+
)
270274
@click.option(
271275
"--verbose",
272276
is_flag=True,
@@ -288,6 +292,7 @@ def delete_term(server, url, userid, password, timeout, term_guid):
288292
@click.option("--timeout", default=60, help="Number of seconds to wait")
289293
def import_terms(
290294
glossary_name: str,
295+
file_path: str,
291296
file_name: str,
292297
verbose: bool,
293298
upsert: bool,
@@ -302,7 +307,11 @@ def import_terms(
302307
token = m_client.create_egeria_bearer_token()
303308
try:
304309
result = m_client.load_terms_from_file(
305-
glossary_name, file_name, upsert=upsert, verbose=verbose
310+
glossary_name,
311+
file_name,
312+
file_path=file_path,
313+
upsert=upsert,
314+
verbose=verbose,
306315
)
307316

308317
click.echo(
@@ -319,25 +328,30 @@ def import_terms(
319328

320329
@click.command("export-terms")
321330
@click.option(
322-
"--glossary-guid",
331+
"--glossary_guid",
323332
default=EGERIA_HOME_GLOSSARY_GUID,
324333
help="GUID of Glossary to export",
325334
required=True,
326335
)
327-
@click.option("--file-name", help="Path of CSV file", required=True)
336+
@click.option("--file_name", help="Path of CSV file", required=True)
337+
@click.option(
338+
"--file_path", help="Path of CSV file", default=EGERIA_GLOSSARY_PATH, required=False
339+
)
328340
@click.option("--server", default=EGERIA_VIEW_SERVER, help="Egeria view server to use")
329341
@click.option(
330342
"--url", default=EGERIA_VIEW_SERVER_URL, help="URL of Egeria platform to connect to"
331343
)
332344
@click.option("--userid", default=EGERIA_USER, help="Egeria user")
333345
@click.option("--password", default=EGERIA_USER_PASSWORD, help="Egeria user password")
334346
@click.option("--timeout", default=60, help="Number of seconds to wait")
335-
def export_terms(glossary_guid: str, file_name, server, url, userid, password, timeout):
347+
def export_terms(
348+
glossary_guid: str, file_name, file_path, server, url, userid, password, timeout
349+
):
336350
"""Export the glossary specified"""
337351
m_client = EgeriaTech(server, url, user_id=userid, user_pwd=password)
338352
token = m_client.create_egeria_bearer_token()
339353
try:
340-
result = m_client.export_glossary_to_csv(glossary_guid, file_name)
354+
result = m_client.export_glossary_to_csv(glossary_guid, file_name, file_path)
341355

342356
click.echo(
343357
f"Exported {result} terms from glossary: {glossary_guid} into {file_name}"

pyegeria/commands/cli/egeria.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ def glossary_group(ctx):
879879
)
880880
@click.option(
881881
"--glossary-guid",
882-
default=None,
882+
default=os.environ.get("EGERIA_HOME_GLOSSARY_GUID", None),
883883
help="Optionally restrict search to glossary with the specified guid",
884884
)
885885
@click.option(

pyegeria/commands/cli/egeria_cat.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@
129129
)
130130
@click.option(
131131
"--width",
132-
default=os.environ.get("EGERIA_WIDTH", "200"),
132+
default=os.environ.get("EGERIA_WIDTH", 200),
133+
type=int,
133134
help="Screen width, in characters, to use",
134135
)
135136
@click.option(
@@ -311,7 +312,7 @@ def glossary_group(ctx):
311312
)
312313
@click.option(
313314
"--glossary-guid",
314-
default=os.environ.get("EGERIA_HOME_GLOSSARY_GUID"),
315+
default=os.environ.get("EGERIA_HOME_GLOSSARY_GUID", None),
315316
help="Optionally restrict search to glossary with the specified guid",
316317
)
317318
@click.option(

pyegeria/glossary_manager_omvs.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,7 @@ def load_terms_from_file(
15351535
self,
15361536
glossary_name: str,
15371537
filename: str,
1538+
file_path: str = os.environ.get("EGERIA_GLOSSARY_PATH", None),
15381539
upsert: bool = True,
15391540
verbose: bool = True,
15401541
) -> List[dict] | None:
@@ -1544,6 +1545,9 @@ def load_terms_from_file(
15441545
----------
15451546
glossary_name : str
15461547
Name of the glossary to import terms into.
1548+
file_path: str, default is EGERIA_GLOSSARY_PATH if specified or None
1549+
If EGERIA_GLOSSARY_PATH environment variable is set, then it will be used in forming the
1550+
prepended to the filename parameter to form the full path to the file.
15471551
filename: str
15481552
Path to the file to import terms from. File is assumed to be in CSV format. The path
15491553
is relative to where the python method is being called from.
@@ -1609,16 +1613,25 @@ def load_terms_from_file(
16091613
"Version Identifier",
16101614
"Status",
16111615
}
1616+
1617+
if file_path:
1618+
full_file_path = os.path.join(file_path, filename)
1619+
else:
1620+
full_file_path = filename
1621+
1622+
if not os.path.isfile(full_file_path):
1623+
raise FileNotFoundError(
1624+
f"Did not find file with path {file_path} and name {filename}"
1625+
)
16121626
# process file
1613-
with open(filename, mode="r") as file:
1627+
with open(full_file_path, mode="r") as file:
16141628
# Create a CSV reader object
16151629
csv_reader = csv.DictReader(file)
16161630
headers = csv_reader.fieldnames
16171631
term_info = []
16181632
# check that the column headers are known
16191633
if all(header in term_properties for header in headers) is False:
16201634
raise InvalidParameterException("Invalid headers in CSV File")
1621-
sys.exit(1)
16221635

16231636
# process each row and validate values
16241637
for row in csv_reader:
@@ -1759,7 +1772,10 @@ def load_terms_from_file(
17591772
return
17601773

17611774
async def _async_export_glossary_to_csv(
1762-
self, glossary_guid: str, target_file: str
1775+
self,
1776+
glossary_guid: str,
1777+
target_file: str,
1778+
file_path: str = os.environ.get("EGERIA_GLOSSARY_PATH", None),
17631779
) -> int:
17641780
"""Export all the terms in a glossary to a CSV file. Async version
17651781
@@ -1769,6 +1785,9 @@ async def _async_export_glossary_to_csv(
17691785
Identity of the glossary to export.
17701786
target_file: str
17711787
Complete file name with path and extension to export to.
1788+
file_path: str, default is EGERIA_GLOSSARY_PATH if specified or None
1789+
If EGERIA_GLOSSARY_PATH environment variable is set, then it will be used in forming the
1790+
prepended to the filename parameter to form the full path to the file.
17721791
17731792
Returns:
17741793
int: Number of rows exported.
@@ -1787,8 +1806,12 @@ async def _async_export_glossary_to_csv(
17871806
"Version Identifier",
17881807
"Status",
17891808
]
1809+
if file_path:
1810+
full_file_path = os.path.join(file_path, target_file)
1811+
else:
1812+
full_file_path = target_file
17901813

1791-
with open(target_file, mode="w") as file:
1814+
with open(full_file_path, mode="w") as file:
17921815
csv_writer = csv.DictWriter(file, fieldnames=header)
17931816
csv_writer.writeheader()
17941817
count = 0
@@ -1822,7 +1845,12 @@ async def _async_export_glossary_to_csv(
18221845
count += 1
18231846
return count
18241847

1825-
def export_glossary_to_csv(self, glossary_guid: str, target_file: str) -> int:
1848+
def export_glossary_to_csv(
1849+
self,
1850+
glossary_guid: str,
1851+
target_file: str,
1852+
file_path: str = os.environ.get("EGERIA_GLOSSARY_PATH", None),
1853+
) -> int:
18261854
"""Export all the terms in a glossary to a CSV file.
18271855
18281856
Parameters:
@@ -1831,14 +1859,17 @@ def export_glossary_to_csv(self, glossary_guid: str, target_file: str) -> int:
18311859
Identity of the glossary to export.
18321860
target_file: str
18331861
Complete file name with path and extension to export to.
1862+
file_path: str, default is EGERIA_GLOSSARY_PATH if specified or None
1863+
If EGERIA_GLOSSARY_PATH environment variable is set, then it will be used in forming the
1864+
prepended to the filename parameter to form the full path to the file.
18341865
18351866
Returns:
18361867
int: Number of rows exported.
18371868
"""
18381869

18391870
loop = asyncio.get_event_loop()
18401871
response = loop.run_until_complete(
1841-
self._async_export_glossary_to_csv(glossary_guid, target_file)
1872+
self._async_export_glossary_to_csv(glossary_guid, target_file, file_path)
18421873
)
18431874

18441875
return response

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pyegeria"
3-
version = "5.2.0.42.4"
3+
version = "5.2.0.42.8"
44
license = 'Apache 2.0'
55
authors = ["Dan Wolfson <[email protected]>"]
66
readme = "README.md"

tests/test_glossary_manager_omvs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,9 @@ def test_load_terms_from_csv(self):
585585
)
586586
token = g_client.create_egeria_bearer_token(self.good_user_3, "secret")
587587
glossary = "example"
588-
file_name = "/Users/dwolfson/localGit/egeria-v5-1/egeria-workspaces/exchange/loading-bay/glossary/upsert-example.om-terms"
589-
response = g_client.load_terms_from_file(glossary, file_name, True)
588+
file_path = "/Users/dwolfson/localGit/egeria-v5-1/egeria-workspaces/exchange/loading-bay/glossary"
589+
file_name = "pets.om-terms"
590+
response = g_client.load_terms_from_file(glossary, file_name, file_path)
590591
print(f"type is {type(response)}")
591592
if type(response) is list:
592593
print("\n\n" + json.dumps(response, indent=4))
@@ -598,6 +599,7 @@ def test_load_terms_from_csv(self):
598599
InvalidParameterException,
599600
PropertyServerException,
600601
UserNotAuthorizedException,
602+
FileNotFoundError,
601603
) as e:
602604
print_exception_response(e)
603605
assert False, "Invalid request"

0 commit comments

Comments
 (0)