6
6
"""
7
7
import enum
8
8
import os
9
- from typing import Any , Dict , Optional , Tuple
9
+ from typing import Any , Dict , List , Optional , Tuple
10
10
11
11
import jsonschema
12
12
import yaml
40
40
_MANIFEST_SCHEMA : Dict [str , Any ] = yaml .load (schema_file , Loader = yaml .FullLoader )
41
41
assert _MANIFEST_SCHEMA
42
42
43
+ REPO_TYPE_GITHUB : str = "github"
44
+ REPO_TYPE_GITLAB : str = "gitlab"
45
+ _REPO_TYPES : List [str ] = [REPO_TYPE_GITHUB , REPO_TYPE_GITLAB ]
46
+
43
47
44
48
class TextEncoding (enum .Enum ):
45
49
"""A general text encoding format, used initially for Job text fields."""
@@ -81,14 +85,73 @@ def validate_job_schema(job_definition: Dict[str, Any]) -> Optional[str]:
81
85
return None
82
86
83
87
88
+ def get_supported_repository_types () -> List [str ]:
89
+ """Returns a copy of the supported Git repository types."""
90
+ return _REPO_TYPES [:]
91
+
92
+
93
+ def _get_github_job_doc_url (
94
+ manifest_url : str , collection : str , job_id : str , doc_url : Optional [str ]
95
+ ) -> str :
96
+ """Returns the path to the doc for a GitHub public reference,
97
+ based on the manifest URL, collection and Job ID.
98
+ """
99
+ manifest_directory_url , _ = os .path .split (manifest_url )
100
+ if doc_url and not doc_url .startswith ("https://" ):
101
+ # doc-url defined but does not start 'https://'
102
+ # the doc_url is a path within the docs directory
103
+ doc_url = f"{ manifest_directory_url } /docs/{ doc_url } "
104
+ elif doc_url is None :
105
+ # No doc-url.
106
+ # The doc is expected to be in 'docs',
107
+ # as '{job}.md' in the {collection} directory.
108
+ doc_url = f"{ manifest_directory_url } /docs/{ collection } /{ job_id } .md"
109
+ else :
110
+ # How did we get here?
111
+ assert False
112
+
113
+ return doc_url
114
+
115
+
116
+ def _get_gitlab_job_doc_url (
117
+ manifest_url : str , collection : str , job_id : str , doc_url : Optional [str ]
118
+ ) -> str :
119
+ """Returns the path to the doc for a GitLab reference,
120
+ based on the manifest URL, collection and Job ID.
121
+ """
122
+ manifest_directory_url , _ = manifest_url .split ("%2f" )
123
+ if doc_url and not doc_url .startswith ("https://" ):
124
+ # doc-url defined but does not start 'https://'
125
+ # the doc_url is a path within the docs directory
126
+ doc_url = doc_url .replace ("/" , "%2f" )
127
+ doc_url = f"{ manifest_directory_url } %2fdocs%2f{ doc_url } /raw"
128
+ elif doc_url is None :
129
+ # No doc-url.
130
+ # The doc is expected to be in 'docs',
131
+ # as '{job}.md' in the {collection} directory.
132
+ doc_url = f"{ manifest_directory_url } %2fdocs%2f{ collection } %2f{ job_id } .md/raw"
133
+ else :
134
+ # How did we get here?
135
+ assert False
136
+
137
+ return doc_url
138
+
139
+
84
140
def get_job_doc_url (
85
- collection : str , job : str , job_definition : Dict [str , Any ], manifest_url : str
141
+ repo_type : str ,
142
+ collection : str ,
143
+ job : str ,
144
+ job_definition : Dict [str , Any ],
145
+ manifest_url : str ,
86
146
) -> str :
87
147
"""Returns the Job's documentation URL for a specific Job using the JOb's
88
148
'doc-url' and manifest URL. The job manifest is expected to
89
149
have been validated, and we expect to have been given one job structure
90
150
from the job's definition, not the whole job definition file.
151
+
152
+ repo_type must be one of the supported types.
91
153
"""
154
+ assert repo_type in _REPO_TYPES
92
155
assert collection
93
156
assert job
94
157
assert isinstance (job_definition , dict )
@@ -101,12 +164,12 @@ def get_job_doc_url(
101
164
# 3. If the doc-url does not begin https:// then is
102
165
# assumed to be relative to the manifest directory URL
103
166
104
- # A typical manifest URl will look like this...
167
+ # A typical GitHub manifest URL will look like this...
105
168
#
106
169
# https://raw.githubusercontent.com/InformaticsMatters/
107
170
# virtual-screening/main/data-manager/manifest-virtual-screening.yaml
108
171
#
109
- # The base for an auto-generated doc-url (if we need it)
172
+ # And, in this case the base for an auto-generated doc-url (if we need it)
110
173
# will be: -
111
174
#
112
175
# https://raw.githubusercontent.com/InformaticsMatters/
@@ -117,21 +180,14 @@ def get_job_doc_url(
117
180
# If doc-url starts 'https://' just return it
118
181
if doc_url and doc_url .startswith ("https://" ):
119
182
return doc_url
120
-
121
- # If the doc-url is set (it's not https://)
122
- # so we assume is simply relative to the 'docs' directory
123
- # where the manifest is found.
124
- manifest_directory_url , _ = os .path .split (manifest_url )
125
- if doc_url and not doc_url .startswith ("https://" ):
126
- # doc-url defined but does not start 'https://'
127
- doc_url = f"{ manifest_directory_url } /docs/{ doc_url } "
128
- elif doc_url is None :
129
- # No doc-url.
130
- # The
131
- doc_url = f"{ manifest_directory_url } /docs/{ collection } /{ job } .md"
132
- else :
133
- # How did we get here?
134
- assert False
183
+ if doc_url is not None :
184
+ assert not doc_url .startswith ("/" )
185
+ assert not doc_url .endswith ("/" )
186
+
187
+ if repo_type == REPO_TYPE_GITHUB :
188
+ doc_url = _get_github_job_doc_url (manifest_url , collection , job , doc_url )
189
+ elif repo_type == REPO_TYPE_GITLAB :
190
+ doc_url = _get_gitlab_job_doc_url (manifest_url , collection , job , doc_url )
135
191
136
192
assert doc_url
137
193
return doc_url
0 commit comments