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
405757dd
Commit
405757dd
authored
May 10, 2019
by
Никита Ефремов
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added .conf and .map file generators and updated RedirectorParser
parent
578c37f3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
41 deletions
+49
-41
generators.py
core/generators.py
+30
-0
parser.py
core/parser.py
+19
-41
No files found.
core/generators.py
0 → 100644
View file @
405757dd
class
MapGenerator
:
def
__init__
(
self
,
logger
=
None
):
self
.
logger
=
logger
self
.
start_lines
=
[
"map $uri $
%
s-redirect {
\n
"
,
"
\n
map $uri $
%
s-option-redirect {
\n
"
,
"
\n
map $uri $
%
s-option {
\n
"
]
self
.
endline
=
"
\n
}"
def
generate_map
(
self
,
redirects_sorted
,
project_name
):
data
=
""
for
i
,
map_type
in
enumerate
(
self
.
start_lines
):
data
+=
map_type
%
project_name
for
arg1
,
arg2
in
redirects_sorted
[
i
]:
data
+=
"
\t
%
s
\t
%
s;"
%
(
arg1
,
arg2
)
data
+=
self
.
endline
return
data
class
ConfigGenerator
:
def
__init__
(
self
):
self
.
start_line
=
"if($
%
s-redirect) {
\n
"
self
.
rewrite_line
=
"
\t
rewrite ^/
%
s/(.^)$
%
s redirect;
\n
}"
def
generate_conf
(
self
,
project_name
):
return
self
.
start_line
%
project_name
+
self
.
rewrite_line
%
project_name
core/parser.py
View file @
405757dd
...
@@ -16,22 +16,19 @@ class RedirectorParser:
...
@@ -16,22 +16,19 @@ class RedirectorParser:
def
_parse_line
(
self
,
line
,
prefix
,
i
):
def
_parse_line
(
self
,
line
,
prefix
,
i
):
if
not
line
.
startswith
(
"#"
):
if
not
line
.
startswith
(
"#"
):
line
=
line
.
split
()
line
=
line
.
split
()
url_type
,
options
=
[]
,
[]
return_code
,
options
=
0
,
[]
if
len
(
line
)
==
0
:
if
len
(
line
)
==
0
:
return
[
"skip"
]
,
None
,
None
return
-
1
,
None
,
None
elif
len
(
line
)
<
2
:
elif
len
(
line
)
<
2
:
raise
self
.
ParseLineError
(
"Error on
%
s:{line};
\n
Not enough arguments to parse!"
.
format
(
line
=
i
))
raise
self
.
ParseLineError
(
"Error on
%
s:{line};
\n
Not enough arguments to parse!"
.
format
(
line
=
i
))
elif
len
(
line
)
>
2
:
elif
len
(
line
)
>
2
:
url_type
.
append
(
"options"
)
return_code
=
1
options
=
[
option
[
1
:
-
1
]
for
option
in
line
[
2
:]]
options
=
[
option
[
1
:
-
1
]
for
option
in
line
[
2
:]]
else
:
url_type
.
append
(
"no-options"
)
# not url - regexp
# not url - regexp
if
not
self
.
url_regexp
.
fullmatch
(
line
[
0
]):
if
not
self
.
url_regexp
.
fullmatch
(
line
[
0
]):
try
:
try
:
re
.
compile
(
line
[
0
])
re
.
compile
(
line
[
0
])
re
.
compile
(
line
[
1
])
re
.
compile
(
line
[
1
])
url_type
.
append
(
"regexp"
)
except
re
.
error
:
except
re
.
error
:
raise
self
.
RegexpTestError
(
"Can
\'
t compile regular expressions {expression1} {expression2} in "
raise
self
.
RegexpTestError
(
"Can
\'
t compile regular expressions {expression1} {expression2} in "
"
%
s:{line_num}"
.
format
(
expression1
=
line
[
0
],
expression2
=
line
[
1
],
"
%
s:{line_num}"
.
format
(
expression1
=
line
[
0
],
expression2
=
line
[
1
],
...
@@ -39,20 +36,17 @@ class RedirectorParser:
...
@@ -39,20 +36,17 @@ class RedirectorParser:
# if new URI relative to the root
# if new URI relative to the root
elif
line
[
0
]
.
startswith
(
"//"
):
elif
line
[
0
]
.
startswith
(
"//"
):
line
[
0
]
=
line
[
0
][
1
:]
# cutting out extra '/' at the beginning
line
[
0
]
=
line
[
0
][
1
:]
# cutting out extra '/' at the beginning
url_type
.
append
(
"root"
)
elif
line
[
1
]
.
startswith
(
"//"
):
elif
line
[
1
]
.
startswith
(
"//"
):
line
[
1
]
=
line
[
1
][
1
:]
line
[
1
]
=
line
[
1
][
1
:]
url_type
.
append
(
"root"
)
elif
self
.
url_absolute
.
fullmatch
(
line
[
1
]):
elif
self
.
url_absolute
.
fullmatch
(
line
[
1
]):
url_type
.
append
(
"absolute"
)
pass
# default url
# default url
else
:
else
:
line
[
0
]
=
prefix
+
line
[
0
]
line
[
0
]
=
prefix
+
line
[
0
]
line
[
1
]
=
prefix
+
line
[
1
]
line
[
1
]
=
prefix
+
line
[
1
]
url_type
.
append
(
'plain'
)
return
return_code
,
line
[
0
],
line
[
1
],
options
return
url_type
,
line
[
0
],
line
[
1
],
options
else
:
else
:
return
[
"skip"
]
,
None
,
None
return
-
1
,
None
,
None
@staticmethod
@staticmethod
def
_parse_yaml
(
file_dir
):
def
_parse_yaml
(
file_dir
):
...
@@ -77,39 +71,32 @@ class RedirectorParser:
...
@@ -77,39 +71,32 @@ class RedirectorParser:
print
(
level
.
upper
()
+
":
\n
"
+
message
)
print
(
level
.
upper
()
+
":
\n
"
+
message
)
def
_parse_map
(
self
,
map_file
,
yaml_file
):
def
_parse_map
(
self
,
map_file
,
yaml_file
):
res
=
[[],
[],
[],
[],
[],
[]]
res
=
[[],
[],
[]]
res_prefix
=
None
try
:
try
:
for
map_path
,
prefix
in
self
.
_parse_yaml
(
yaml_file
):
for
map_path
,
prefix
in
self
.
_parse_yaml
(
yaml_file
):
if
map_path
==
map_file
:
if
map_path
==
map_file
:
# ???: Is the last directory of map_path a project's name?
res_prefix
=
prefix
.
split
(
"/"
)[
-
1
]
with
open
(
map_file
,
"r"
)
as
file
:
with
open
(
map_file
,
"r"
)
as
file
:
for
i
,
line
in
enumerate
(
file
):
for
i
,
line
in
enumerate
(
file
):
request_url
,
redirect_url
,
options
=
None
,
None
,
[]
request_url
,
redirect_url
,
options
=
None
,
None
,
[]
try
:
try
:
type_
,
request_url
,
redirect_url
,
*
options
=
self
.
_parse_line
(
line
,
prefix
,
i
)
return_code
,
request_url
,
redirect_url
,
*
options
=
self
.
_parse_line
(
line
,
prefix
,
i
)
except
self
.
RegexpTestError
as
e
:
except
self
.
RegexpTestError
as
e
:
self
.
log
(
e
.
message
%
map_file
)
self
.
log
(
e
.
message
%
map_file
)
except
self
.
ParseLineError
as
e
:
except
self
.
ParseLineError
as
e
:
self
.
log
(
e
.
message
%
map_file
)
self
.
log
(
e
.
message
%
map_file
)
if
not
request_url
or
not
redirect_url
or
type_
[
0
]
==
"skip"
:
if
not
request_url
or
not
redirect_url
or
return_code
==
-
1
:
continue
continue
else
:
else
:
if
type_
[
1
]
==
"plain"
:
if
return_code
==
0
:
if
type_
[
0
]
==
"no-options"
:
res
[
0
]
.
append
((
request_url
,
redirect_url
))
res
[
0
]
.
append
((
request_url
,
redirect_url
))
elif
return_code
==
1
:
else
:
res
[
1
]
.
append
((
request_url
,
redirect_url
))
res
[
1
]
.
append
((
request_url
,
redirect_url
,
*
options
))
res
[
2
]
.
append
((
request_url
,
options
))
elif
type_
[
1
]
==
"regexp"
:
return
res
,
res_prefix
if
type_
[
0
]
==
"no-options"
:
res
[
2
]
.
append
((
request_url
,
redirect_url
))
else
:
res
[
3
]
.
append
((
request_url
,
redirect_url
,
*
options
))
elif
type_
[
1
]
==
"absolute"
:
if
type_
[
0
]
==
"no-options"
:
res
[
4
]
.
append
((
request_url
,
redirect_url
))
else
:
res
[
5
]
.
append
((
request_url
,
redirect_url
,
*
options
))
return
res
except
yaml
.
YAMLError
as
e
:
except
yaml
.
YAMLError
as
e
:
self
.
log
(
"Error occured while reading
%
s"
%
yaml_file
+
str
(
e
))
self
.
log
(
"Error occured while reading
%
s"
%
yaml_file
+
str
(
e
))
...
@@ -131,13 +118,4 @@ class RedirectorParser:
...
@@ -131,13 +118,4 @@ class RedirectorParser:
"""
"""
def
__init__
(
self
,
message
):
def
__init__
(
self
,
message
):
self
.
message
=
message
self
.
message
=
messages
def
main
():
parser
=
RedirectorParser
()
pp
(
parser
.
_parse_map
(
"tests/test.map"
,
"tests/test.yaml"
))
if
__name__
==
"__main__"
:
main
()
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