-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespaces-to-namespace-prefixes-file-generator.py
113 lines (79 loc) · 3.54 KB
/
namespaces-to-namespace-prefixes-file-generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# #%L
# Alfresco HX Insight Connector
# %%
# Copyright (C) 2024 Alfresco Software Limited
# %%
# This file is part of the Alfresco software.
# If the software was purchased under a paid Alfresco license, the terms of
# the paid license agreement will prevail. Otherwise, the software is
# provided under the following open source license terms:
#
# Alfresco is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Alfresco is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
# #L%
import argparse
import base64
import json
import requests
PAGE_SIZE = 100
ENDPOINT = "/alfresco/api/-default-/public/alfresco/versions/1/types"
def get_cli_arguments():
parser = argparse.ArgumentParser(description='Generate namespace prefix mappings for an ACS Repository.')
parser.add_argument('host', help='Alfresco repository host (ex: localhost:8080)')
parser.add_argument('username', help='User (ex: admin)')
parser.add_argument('password', help='Password (ex: admin)')
return parser.parse_args()
def get_auth_token(console_args):
return base64.b64encode(f"{console_args.username}:{console_args.password}".encode()).decode()
def is_status_success(status):
return 200 <= status < 300
def get_namespace_to_prefix_mapping(type_info):
model_info = type_info["entry"]["model"]
return (model_info["namespaceUri"], model_info["namespacePrefix"])
def get_namespace_to_prefix_mappings(types_info):
return dict(map(get_namespace_to_prefix_mapping, types_info))
def get_types_info(host, token, page):
skip_count = (page - 1) * PAGE_SIZE
url = f"http://{host}{ENDPOINT}?skipCount={skip_count}&maxItems={PAGE_SIZE}"
headers = {
'Accept': 'application/json',
'Authorization': f'Basic {token}'
}
response = requests.request("GET", url, headers=headers, data={})
if not is_status_success(response.status_code):
print(response.text)
exit(1)
response_json = response.json()
total_items = response_json["list"]["pagination"]["totalItems"]
fetched_items = response_json["list"]["pagination"]["skipCount"] + response_json["list"]["pagination"]["count"]
has_next_page = fetched_items < total_items
return {
"has_next_page": has_next_page,
"next_page": lambda: get_types_info(host, token, page + 1),
"types": get_namespace_to_prefix_mappings(response.json()["list"]["entries"])
}
if __name__ == "__main__":
console_args = get_cli_arguments()
prefix_map = {}
host = console_args.host
token = get_auth_token(console_args)
types_info_response = get_types_info(host, token, 1)
while True:
prefix_map.update(types_info_response["types"])
if not types_info_response["has_next_page"]:
break
types_info_response = types_info_response["next_page"]()
out_filename = "namespace-prefixes.json"
with open(out_filename, "w") as namespaces_file:
json.dump({"prefixUriMap": prefix_map}, namespaces_file, indent=2)
print(f"Done! You'll find generated mapping in {out_filename} file")