Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nginx-redirector
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
eterfund
nginx-redirector
Commits
4645e70c
Commit
4645e70c
authored
Apr 12, 2022
by
Soldatoff
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added the ability to use your name in yaml.
parent
e9910f24
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
21 deletions
+32
-21
generators.py
redirector/utils/generators.py
+29
-19
parser.py
redirector/utils/parser.py
+3
-2
No files found.
redirector/utils/generators.py
View file @
4645e70c
...
@@ -9,30 +9,39 @@ class Generator:
...
@@ -9,30 +9,39 @@ class Generator:
def
get_conf_dirs
(
self
):
def
get_conf_dirs
(
self
):
return
MAPS_DIR
,
CONFIG_DIR
return
MAPS_DIR
,
CONFIG_DIR
def
get_essential
(
self
,
arg_map
=
None
):
return_dict
=
{
"project_name"
:
''
,
"prefix"
:
''
,
"custom"
:
''
,
"dir_name"
:
''
}
if
arg_map
:
return_dict
[
"dir_name"
]
=
arg_map
.
split
(
"/"
)[
-
2
]
if
len
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
1
])
>
len
(
"/"
):
#prefix != "/"
return_dict
[
"prefix"
]
=
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
1
]
.
split
(
"/"
)[
-
1
])
return_dict
[
"project_name"
]
=
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
1
]
.
split
(
"/"
)[
-
1
])
elif
len
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
2
])
>
len
(
"/"
):
#name != "/" and prefix == "/"
return_dict
[
"custom"
]
=
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
2
]
.
split
(
"/"
)[
-
1
])
return_dict
[
"project_name"
]
=
return_dict
[
"custom"
]
+
"-"
+
return_dict
[
"dir_name"
]
else
:
#name == "/" and prefix == "/"
return_dict
[
"project_name"
]
=
return_dict
[
"dir_name"
]
return
return_dict
def
generate
(
self
,
redirects_data
,
project_name
,
arg_map
=
None
):
def
generate
(
self
,
redirects_data
,
project_name
,
arg_map
=
None
):
if
len
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
1
])
>
len
(
"/"
):
arg
=
self
.
get_essential
(
arg_map
)
prefix
=
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
1
]
.
split
(
"/"
)[
-
1
]
redirects_map
=
self
.
map_gen
.
generate_map
(
redirects_data
[
0
],
arg
)
redirects_map
=
self
.
map_gen
.
generate_map
(
redirects_data
[
0
],
"/{}"
.
format
(
project_name
))
redirects_with_options
=
self
.
map_gen
.
generate_opt_map
(
redirects_data
[
1
],
arg
[
"project_name"
])
#need test
else
:
conf_data
=
self
.
conf_gen
.
generate_config
(
arg
[
"project_name"
],
[
code
for
code
,
data
in
redirects_with_options
],
arg
[
"prefix"
])
prefix
=
arg_map
.
split
(
"/"
)[
-
2
]
redirects_map
=
self
.
map_gen
.
generate_map
(
redirects_data
[
0
],
project_name
)
redirects_with_options
=
self
.
map_gen
.
generate_opt_map
(
redirects_data
[
1
],
project_name
)
conf_data
=
self
.
conf_gen
.
generate_config
(
project_name
,
[
code
for
code
,
data
in
redirects_with_options
],
prefix
)
maps_dir
,
config_dir
=
self
.
get_conf_dirs
()
maps_dir
,
config_dir
=
self
.
get_conf_dirs
()
try
:
try
:
with
open
(
"{}/{}.map"
.
format
(
maps_dir
,
prefix
),
"w"
)
as
map_file
:
with
open
(
"{}/{}.map"
.
format
(
maps_dir
,
arg
[
"project_name"
]
),
"w"
)
as
map_file
:
map_file
.
write
(
redirects_map
)
map_file
.
write
(
redirects_map
)
for
code
,
data
in
redirects_with_options
:
for
code
,
data
in
redirects_with_options
:
if
code
==
"301"
:
if
code
==
"301"
:
code
=
"permanent"
code
=
"permanent"
elif
code
==
"302"
:
elif
code
==
"302"
:
code
=
"redirect"
code
=
"redirect"
with
open
(
maps_dir
+
"/
%
s_
%
s_options.map"
%
(
project_name
,
code
),
"w"
)
as
map_file
:
with
open
(
maps_dir
+
"/
%
s_
%
s_options.map"
%
(
arg
[
"project_name"
]
,
code
),
"w"
)
as
map_file
:
map_file
.
write
(
data
)
map_file
.
write
(
data
)
with
open
(
config_dir
+
"/
%
s.conf"
%
prefix
,
"w"
)
as
conf_file
:
with
open
(
config_dir
+
"/
%
s.conf"
%
arg
[
"project_name"
]
,
"w"
)
as
conf_file
:
conf_file
.
write
(
conf_data
)
conf_file
.
write
(
conf_data
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
)
print
(
e
)
...
@@ -53,11 +62,16 @@ class MapGenerator:
...
@@ -53,11 +62,16 @@ class MapGenerator:
self
.
endline
=
"
\n
}"
self
.
endline
=
"
\n
}"
def
generate_map
(
self
,
redirects
,
project_name
):
def
generate_map
(
self
,
redirects
,
arg
):
data
=
self
.
start_line
%
project_name
if
arg
[
"prefix"
]:
data
=
self
.
start_line
%
arg
[
"prefix"
]
else
:
data
=
self
.
start_line
%
arg
[
"project_name"
]
if
arg
[
"project_name"
]
==
arg
[
"prefix"
]:
arg
[
"prefix"
]
=
"/{}"
.
format
(
arg
[
"prefix"
])
for
arg1
,
arg2
in
redirects
:
for
arg1
,
arg2
in
redirects
:
# print('redirect is', arg1, arg2)
# print('redirect is', arg1, arg2)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
project_name
+
arg1
,
project_name
+
arg2
)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg
[
"prefix"
]
+
arg1
,
arg
[
"prefix"
]
+
arg2
)
data
+=
self
.
endline
data
+=
self
.
endline
return
data
return
data
...
@@ -86,11 +100,7 @@ class ConfigGenerator:
...
@@ -86,11 +100,7 @@ class ConfigGenerator:
self
.
option_line
=
"return
%
s;
\n
}
\n
"
self
.
option_line
=
"return
%
s;
\n
}
\n
"
def
generate_config
(
self
,
project_name
,
options
,
prefix
):
def
generate_config
(
self
,
project_name
,
options
,
prefix
):
if
len
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
1
])
>
len
(
"/"
):
data
=
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
"{}"
.
format
(
prefix
),
project_name
,
"redirect"
))
+
"}
\n
"
data
=
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
"/{}"
.
format
(
prefix
),
project_name
,
"redirect"
))
+
"}
\n
"
else
:
data
=
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
""
,
project_name
,
"redirect"
))
+
"}
\n
"
for
code
in
options
:
for
code
in
options
:
if
code
==
"permanent"
:
if
code
==
"permanent"
:
data
+=
(
self
.
opt_start_line
%
(
project_name
,
"permanent"
))
+
(
self
.
opt_rewrite_line
%
(
project_name
,
project_name
,
"permanent"
,
"permanent"
))
+
"}
\n
"
data
+=
(
self
.
opt_start_line
%
(
project_name
,
"permanent"
))
+
(
self
.
opt_rewrite_line
%
(
project_name
,
project_name
,
"permanent"
,
"permanent"
))
+
"}
\n
"
...
...
redirector/utils/parser.py
View file @
4645e70c
...
@@ -104,7 +104,8 @@ class ConfigReader:
...
@@ -104,7 +104,8 @@ class ConfigReader:
abs_map_path
=
map_path
abs_map_path
=
map_path
project_prefix
=
project
.
get
(
"prefix"
)
project_prefix
=
project
.
get
(
"prefix"
)
return_list
.
append
((
abs_map_path
,
project_prefix
))
custom_name
=
project
.
get
(
"name"
)
return_list
.
append
((
abs_map_path
,
project_prefix
,
custom_name
))
stream
.
close
()
stream
.
close
()
...
@@ -120,7 +121,7 @@ class ConfigReader:
...
@@ -120,7 +121,7 @@ class ConfigReader:
map_file_name
=
os
.
path
.
basename
(
map_file
)
# get filename from arg
map_file_name
=
os
.
path
.
basename
(
map_file
)
# get filename from arg
try
:
try
:
for
map_path
,
prefix
in
self
.
parse_yaml
(
yaml_file
):
for
map_path
,
prefix
,
name
in
self
.
parse_yaml
(
yaml_file
):
abs_map_path
=
get_map_path
(
map_path
,
map_dir
)
abs_map_path
=
get_map_path
(
map_path
,
map_dir
)
#if map_file_name not in abs_map_path:
#if map_file_name not in abs_map_path:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment