Skip to content

Commit 26122e7

Browse files
authored
Merge pull request #140 from casework/export_local_uuid
Export local_uuid
2 parents 016ac37 + 6c1bb77 commit 26122e7

File tree

3 files changed

+8
-135
lines changed

3 files changed

+8
-135
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ case_sparql_select output.md input.sparql input.json [input-2.json ...]
122122

123123
### `local_uuid`
124124

125-
This [module](case_utils/local_uuid.py) provides a wrapper UUID generator, `local_uuid()`. Its main purpose is making example data generate consistent identifiers, and intentionally includes mechanisms to make it difficult to activate this mode without awareness of the caller.
125+
_Migration:_ Functionality previously in [`case_utils.local_uuid`](case_utils/local_uuid.py) has been exported to [`cdo-local-uuid`](https://github.com/Cyber-Domain-Ontology/CDO-Utility-Local-UUID). A future `case-utils` release will drop this re-export.
126126

127127

128128
### Built versions

case_utils/local_uuid.py

Lines changed: 6 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -15,146 +15,18 @@
1515
# We would appreciate acknowledgement if the software is used.
1616

1717
"""
18-
This library is a wrapper for uuid, provided to generate repeatable UUIDs if requested.
19-
20-
The function local_uuid() should be used in code where a user could be expected to opt in to non-random UUIDs.
18+
This library was a wrapper for uuid, provided to generate repeatable UUIDs if requested. It is now a temporary re-export of functionality migrated to cdo_local_uuid.
2119
"""
2220

2321
__version__ = "0.4.0"
2422

2523
__all__ = ["configure", "local_uuid"]
2624

27-
import logging
28-
import os
29-
import pathlib
30-
import sys
31-
import typing
32-
import uuid
3325
import warnings
3426

35-
DEMO_UUID_BASE: typing.Optional[str] = None
36-
37-
DEMO_UUID_COUNTER: int = 0
38-
39-
_logger = logging.getLogger(pathlib.Path(__file__).name)
40-
41-
42-
def configure() -> None:
43-
"""
44-
This function is part of setting up _demo_uuid() to generate non-random UUIDs. See _demo_uuid() documentation for further setup notes.
45-
"""
46-
global DEMO_UUID_BASE
47-
48-
if os.getenv("DEMO_UUID_REQUESTING_NONRANDOM") == "NONRANDOM_REQUESTED":
49-
warnings.warn(
50-
"Environment variable DEMO_UUID_REQUESTING_NONRANDOM is deprecated. See case_utils.local_uuid._demo_uuid for usage notes on its replacement, CASE_DEMO_NONRANDOM_UUID_BASE. Proceeding with random UUIDs.",
51-
FutureWarning,
52-
)
53-
return
54-
55-
env_base_dir_name = os.getenv("CASE_DEMO_NONRANDOM_UUID_BASE")
56-
if env_base_dir_name is None:
57-
return
58-
59-
base_dir_original_path = pathlib.Path(env_base_dir_name)
60-
if not base_dir_original_path.exists():
61-
warnings.warn(
62-
"Environment variable CASE_DEMO_NONRANDOM_UUID_BASE is expected to refer to an existing directory. Proceeding with random UUIDs.",
63-
RuntimeWarning,
64-
)
65-
return
66-
if not base_dir_original_path.is_dir():
67-
warnings.warn(
68-
"Environment variable CASE_DEMO_NONRANDOM_UUID_BASE is expected to refer to a directory. Proceeding with random UUIDs.",
69-
RuntimeWarning,
70-
)
71-
return
72-
73-
# Component: An emphasis this is an example.
74-
demo_uuid_base_parts = ["example.org"]
75-
76-
# Component: Present working directory, relative to CASE_DEMO_NONRANDOM_UUID_BASE if that environment variable is an ancestor of pwd.
77-
base_dir_resolved_path = base_dir_original_path.resolve()
78-
srcdir_original_path = pathlib.Path(os.getcwd())
79-
srcdir_resolved_path = srcdir_original_path.resolve()
80-
# _logger.debug("base_dir_resolved_path = %r.", base_dir_resolved_path)
81-
# _logger.debug("srcdir_resolved_path = %r.", srcdir_resolved_path)
82-
try:
83-
srcdir_relative_path = srcdir_resolved_path.relative_to(base_dir_resolved_path)
84-
# _logger.debug("srcdir_relative_path = %r.", srcdir_relative_path)
85-
demo_uuid_base_parts.append(str(srcdir_relative_path))
86-
except ValueError:
87-
# If base_dir is not an ancestor directory of srcdir, default to srcdir.
88-
# _logger.debug("PWD is not relative to base path.")
89-
demo_uuid_base_parts.append(str(srcdir_resolved_path))
90-
91-
# Component: Command of argument vector.
92-
env_venv_name = os.getenv("VIRTUAL_ENV")
93-
if env_venv_name is None:
94-
demo_uuid_base_parts.append(sys.argv[0])
95-
else:
96-
command_original_path = pathlib.Path(sys.argv[0])
97-
command_resolved_path = command_original_path.resolve()
98-
venv_original_path = pathlib.Path(env_venv_name)
99-
venv_resolved_path = venv_original_path.resolve()
100-
try:
101-
command_relative_path = command_resolved_path.relative_to(
102-
venv_resolved_path
103-
)
104-
# _logger.debug("command_relative_path = %r.", command_relative_path)
105-
demo_uuid_base_parts.append(str(command_relative_path))
106-
except ValueError:
107-
# _logger.debug("Command path is not relative to virtual environment path.")
108-
demo_uuid_base_parts.append(str(command_resolved_path))
109-
110-
if len(sys.argv) > 1:
111-
# Component: Arguments of argument vector.
112-
demo_uuid_base_parts.extend(sys.argv[1:])
113-
114-
# _logger.debug("demo_uuid_base_parts = %r.", demo_uuid_base_parts)
115-
116-
DEMO_UUID_BASE = "/".join(demo_uuid_base_parts)
117-
118-
119-
def _demo_uuid() -> str:
120-
"""
121-
This function generates a repeatable UUID, drawing on non-varying elements of the environment and process call for entropy.
122-
123-
This function is not intended to be called outside of this module. Instead, local_uuid() should be called.
124-
125-
WARNING: This function was developed for use ONLY for reducing (but not eliminating) version-control edits to identifiers when generating sample data. It creates UUIDs that are decidedly NOT random, and should remain consistent on repeated calls to the importing script.
126-
127-
To prevent accidental non-random UUID usage, two setup steps need to be done before calling this function:
128-
129-
* An environment variable, CASE_DEMO_NONRANDOM_UUID_BASE, must be set to a string provided by the caller. The variable's required value is the path to some directory. The variable's recommended value is the equivalent of the Make variable "top_srcdir" - that is, the root directory of the containing Git repository, some parent of the current process's current working directory.
130-
* The configure() function in this module must be called.
131-
"""
132-
global DEMO_UUID_BASE
133-
global DEMO_UUID_COUNTER
134-
135-
if os.getenv("CASE_DEMO_NONRANDOM_UUID_BASE") is None:
136-
raise ValueError(
137-
"demo_uuid() called without CASE_DEMO_NONRANDOM_UUID_BASE in environment."
138-
)
139-
140-
if DEMO_UUID_BASE is None:
141-
raise ValueError("demo_uuid() called with DEMO_UUID_BASE unset.")
142-
143-
parts = [DEMO_UUID_BASE]
144-
145-
# Component: Incrementing counter.
146-
DEMO_UUID_COUNTER += 1
147-
parts.append(str(DEMO_UUID_COUNTER))
148-
149-
return str(uuid.uuid5(uuid.NAMESPACE_URL, "/".join(parts)))
150-
27+
from cdo_local_uuid import configure, local_uuid
15128

152-
def local_uuid() -> str:
153-
"""
154-
Generate either a UUID4, or if requested via environment configuration, a non-random demo UUID.
155-
"""
156-
global DEMO_UUID_BASE
157-
if DEMO_UUID_BASE is None:
158-
return str(uuid.uuid4())
159-
else:
160-
return _demo_uuid()
29+
warnings.warn(
30+
"case_utils.local_uuid has exported its functionality to cdo_local_uuid. Imports should be changed to use cdo_local_uuid. case_utils currently re-exports that functionality, but this will cease in a future release.",
31+
DeprecationWarning,
32+
)

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ license_files =
1919
[options]
2020
include_package_data = true
2121
install_requires =
22+
cdo-local-uuid >= 0.4.0, < 0.5.0
2223
pandas
2324
pyshacl >= 0.24.0
2425
rdflib < 8

0 commit comments

Comments
 (0)