Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gitlab-pages-shortlinks
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
FS Info TU Dortmund
Administration
gitlab-pages-shortlinks
Commits
28acbc6d
Commit
28acbc6d
authored
Oct 10, 2022
by
Luca
Browse files
Options
Downloads
Patches
Plain Diff
Add comments to vanitymapv2
parent
9d941655
No related branches found
No related tags found
1 merge request
!7
Add comments to vanitymapv2
Pipeline
#105328
failed
Oct 10, 2022
Stage: quality
Stage: test
Stage: build
Changes
2
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
shortlinks/generator.py
+5
-5
5 additions, 5 deletions
shortlinks/generator.py
shortlinks/utils.py
+34
-12
34 additions, 12 deletions
shortlinks/utils.py
with
39 additions
and
17 deletions
shortlinks/generator.py
+
5
−
5
View file @
28acbc6d
...
...
@@ -13,7 +13,7 @@ from shortlinks.utils import (
class
ShortlinkGenerator
:
logger
=
logging
.
getLogger
(
"
shortlinks.ShortlinkGenerator
"
)
link_mappings
:
dict
[
str
,
str
]
=
{}
link_mappings
:
dict
[
str
,
dict
[
str
,
str
]
]
=
{}
vanity_file
:
TextIOWrapper
ignore_urls
:
List
[
str
]
...
...
@@ -40,8 +40,8 @@ class ShortlinkGenerator:
def
generate
(
self
,
out_path
:
str
)
->
None
:
flat_map
=
flatten_links
(
self
.
link_mappings
)
export_vanity_map_json
(
out_path
,
self
.
link_mappings
)
for
path
,
url
in
flat_map
.
items
():
if
url
:
for
path
,
data
in
flat_map
.
items
():
if
data
:
self
.
logger
.
info
(
f
"
Writing redirect for
{
path
}
"
)
check_url
(
url
,
self
.
ignore_urls
)
write_redirect_html
(
out_path
,
path
,
url
)
check_url
(
data
[
'
url
'
]
,
self
.
ignore_urls
)
write_redirect_html
(
out_path
,
path
,
data
[
'
url
'
]
)
This diff is collapsed.
Click to expand it.
shortlinks/utils.py
+
34
−
12
View file @
28acbc6d
...
...
@@ -13,8 +13,14 @@ logger = logging.getLogger("shortlinks.utils")
def
export_vanity_map_json
(
out_path
:
str
,
link_mappings
:
dict
[
str
,
str
])
->
None
:
serialized_map
=
json
.
dumps
(
link_mappings
)
os
.
makedirs
(
out_path
,
exist_ok
=
True
)
with
open
(
os
.
path
.
join
(
out_path
,
"
vanitymap.json
"
),
"
w
"
)
as
file
:
with
open
(
os
.
path
.
join
(
out_path
,
"
vanitymap
v2
.json
"
),
"
w
"
)
as
file
:
file
.
write
(
serialized_map
)
old_format
=
{}
for
path
in
link_mappings
:
old_format
[
path
]
=
link_mappings
[
path
][
'
url
'
]
serialized_old_format
=
json
.
dumps
(
old_format
)
with
open
(
os
.
path
.
join
(
out_path
,
"
vanitymap.json
"
),
"
w
"
)
as
file
:
file
.
write
(
serialized_old_format
)
def
check_url
(
url
:
str
,
ignore_list
:
list
[
str
])
->
bool
:
...
...
@@ -29,33 +35,49 @@ def check_url(url: str, ignore_list: list[str]) -> bool:
return
False
def
read_file
(
file
:
TextIOWrapper
)
->
dict
[
str
,
str
]:
def
read_file
(
file
:
TextIOWrapper
)
->
dict
[
str
,
dict
[
str
,
str
]
]:
_link_mappings
=
{}
for
line
in
file
:
if
line
.
startswith
(
"
#
"
)
or
len
(
line
.
strip
())
==
0
:
continue
_path
,
_url
,
*
_
=
line
.
split
()
# parse comment if given
parsed_line
=
line
.
replace
(
"
\n
"
,
""
).
split
(
maxsplit
=
2
)
_path
=
parsed_line
[
0
]
_url
=
parsed_line
[
1
]
if
len
(
parsed_line
)
==
3
:
_comment
=
parsed_line
[
2
]
else
:
_comment
=
""
if
_path
in
_link_mappings
:
raise
ValueError
(
f
"
Redirect target for
{
_path
}
already defined
"
)
_link_mappings
[
_path
]
=
_url
# Set comment content
if
_comment
.
startswith
(
"
\"
"
)
and
_comment
.
endswith
(
"
\"
"
):
_comment
=
_comment
[
1
:
len
(
_comment
)
-
1
]
else
:
_comment
=
_path
[
1
:]
_link_mappings
[
_path
]
=
{
'
url
'
:
_url
,
'
comment
'
:
_comment
,
'
group
'
:
_path
}
return
_link_mappings
def
flatten_links
(
link_mappings
:
dict
[
str
,
str
])
->
dict
[
str
,
Optional
[
str
]]:
_flat_mappings
:
dict
[
str
,
Optional
[
str
]]
=
link_mappings
.
copy
()
# type: ignore
def
flatten_links
(
link_mappings
:
dict
[
str
,
dict
[
str
,
str
]
])
->
dict
[
str
,
Optional
[
dict
[
str
,
str
]
]]:
_flat_mappings
:
dict
[
str
,
Optional
[
dict
[
str
,
str
]
]]
=
link_mappings
.
copy
()
# type: ignore
_remaining_rounds
=
10
while
_remaining_rounds
!=
0
:
found
=
False
for
key
,
value
in
_flat_mappings
.
items
():
if
value
and
value
.
startswith
(
"
/
"
):
if
value
is
not
None
and
value
[
'
url
'
]
.
startswith
(
"
/
"
):
found
=
True
logger
.
info
(
f
"
Detected internal link:
{
key
}
->
{
value
}
"
)
if
value
not
in
_flat_mappings
.
keys
():
logger
.
error
(
f
"
Target
{
value
}
not found!
"
)
logger
.
info
(
f
"
Detected internal link:
{
key
}
->
{
value
[
'
url
'
]
}
"
)
if
value
[
'
url
'
]
not
in
_flat_mappings
.
keys
():
logger
.
error
(
f
"
Target
{
value
[
'
url
'
]
}
not found!
"
)
_flat_mappings
[
key
]
=
None
else
:
logger
.
info
(
f
"
Replacing
{
value
}
with
{
_flat_mappings
[
value
]
}
"
)
_flat_mappings
[
key
]
=
_flat_mappings
[
value
]
logger
.
info
(
f
"
Replacing
{
value
[
'
url
'
]
}
with
{
_flat_mappings
[
value
[
'
url
'
]][
'
url
'
]
}
"
)
# Replace comment if default comment and new would not be default
if
_flat_mappings
[
key
][
'
comment
'
]
==
key
[
1
:]
and
\
_flat_mappings
[
value
[
'
url
'
]][
'
comment
'
]
!=
value
[
'
url
'
][
1
:]:
_flat_mappings
[
key
][
'
comment
'
]
=
_flat_mappings
[
value
[
'
url
'
]][
'
comment
'
]
_flat_mappings
[
key
][
'
url
'
]
=
_flat_mappings
[
value
[
'
url
'
]][
'
url
'
]
_remaining_rounds
=
_remaining_rounds
-
1
if
not
found
:
logger
.
info
(
"
No (remaining) internal links found, quitting loop
"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment