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
257fd79e
Commit
257fd79e
authored
Aug 14, 2019
by
Никита Ефремов
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Removed regex handler from Parser and fixed bugs in Generators
parent
d2d01ce9
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
16 deletions
+32
-16
generators.py
dev/generators.py
+13
-0
parser.py
dev/parser.py
+19
-16
No files found.
dev/generators.py
View file @
257fd79e
...
@@ -13,6 +13,10 @@ class Generator:
...
@@ -13,6 +13,10 @@ class Generator:
with
open
(
const
.
MAPS_DIR
+
"/
%
s.map"
%
project_name
,
"w"
)
as
map_file
:
with
open
(
const
.
MAPS_DIR
+
"/
%
s.map"
%
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"
:
code
=
"permanent"
elif
code
==
"302"
:
code
=
"redirect"
with
open
(
const
.
MAPS_DIR
+
"/
%
s_
%
s_options.map"
%
(
project_name
,
code
),
"w"
)
as
map_file
:
with
open
(
const
.
MAPS_DIR
+
"/
%
s_
%
s_options.map"
%
(
project_name
,
code
),
"w"
)
as
map_file
:
map_file
.
write
(
data
)
map_file
.
write
(
data
)
with
open
(
const
.
CONFIG_DIR
+
"/
%
s.conf"
%
project_name
,
"w"
)
as
conf_file
:
with
open
(
const
.
CONFIG_DIR
+
"/
%
s.conf"
%
project_name
,
"w"
)
as
conf_file
:
...
@@ -46,6 +50,10 @@ class MapGenerator:
...
@@ -46,6 +50,10 @@ class MapGenerator:
def
generate_opt_map
(
self
,
redirect_dict
,
project_name
):
def
generate_opt_map
(
self
,
redirect_dict
,
project_name
):
result
=
[]
result
=
[]
for
code
,
redirects
in
redirect_dict
.
items
():
for
code
,
redirects
in
redirect_dict
.
items
():
if
code
==
"301"
:
code
=
"permanent"
elif
code
==
"302"
:
code
=
"redirect"
data
=
self
.
opt_start_line
%
(
project_name
,
code
)
data
=
self
.
opt_start_line
%
(
project_name
,
code
)
for
arg1
,
arg2
in
redirects
:
for
arg1
,
arg2
in
redirects
:
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg1
,
arg2
)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg1
,
arg2
)
...
@@ -66,6 +74,11 @@ class ConfigGenerator:
...
@@ -66,6 +74,11 @@ class ConfigGenerator:
def
generate_config
(
self
,
project_name
,
options
):
def
generate_config
(
self
,
project_name
,
options
):
data
=
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
project_name
,
project_name
,
"break"
))
+
"}
\n
"
data
=
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
project_name
,
project_name
,
"break"
))
+
"}
\n
"
for
code
in
options
:
for
code
in
options
:
if
code
==
"permanent"
:
data
+=
(
self
.
opt_start_line
%
(
project_name
,
"permanent"
))
+
(
self
.
opt_rewrite_line
%
(
project_name
,
project_name
,
"permanent"
,
"permanent"
))
+
"}
\n
"
elif
code
==
"redirect"
:
data
+=
(
self
.
opt_start_line
%
(
project_name
,
"redirect"
))
+
(
self
.
opt_rewrite_line
%
(
project_name
,
project_name
,
"redirect"
,
"redirect"
))
+
"}
\n
"
else
:
data
+=
(
self
.
opt_start_line
%
(
project_name
,
code
))
+
(
self
.
opt_rewrite_line
%
(
project_name
,
project_name
,
code
,
"break"
))
+
(
self
.
option_line
%
code
)
data
+=
(
self
.
opt_start_line
%
(
project_name
,
code
))
+
(
self
.
opt_rewrite_line
%
(
project_name
,
project_name
,
code
,
"break"
))
+
(
self
.
option_line
%
code
)
return
data
return
data
...
...
dev/parser.py
View file @
257fd79e
...
@@ -17,8 +17,14 @@ class MapLineParser:
...
@@ -17,8 +17,14 @@ class MapLineParser:
def
parse_line
(
self
,
line
,
prefix
,
i
):
def
parse_line
(
self
,
line
,
prefix
,
i
):
line
=
line
.
strip
()
line
=
line
.
strip
()
return_code
,
options
=
0
,
[]
return_code
,
options
=
0
,
[]
import
pdb
"""
pdb
.
set_trace
()
return_codes:
-1: blank line
0 : default redirect
1 : option redirect
2 : absolute url
3 : regexp
"""
if
line
.
startswith
(
"#"
)
or
len
(
line
)
==
0
:
if
line
.
startswith
(
"#"
)
or
len
(
line
)
==
0
:
return
-
1
,
None
,
None
return
-
1
,
None
,
None
...
@@ -31,29 +37,26 @@ class MapLineParser:
...
@@ -31,29 +37,26 @@ class MapLineParser:
elif
len
(
line
)
>
2
:
elif
len
(
line
)
>
2
:
return_code
=
1
return_code
=
1
options
=
[
option
[
1
:
-
1
]
for
option
in
line
[
2
:]]
options
=
[
option
[
1
:
-
1
]
for
option
in
line
[
2
:]]
print
(
line
[
0
])
import
pdb
pdb
.
set_trace
()
if
line
[
0
]
.
startswith
(
"~"
):
line
[
0
]
=
"~"
+
prefix
+
line
[
0
][
1
:]
if
not
self
.
url_regexp
.
fullmatch
(
line
[
0
]):
# not url - regexp
if
line
[
0
]
.
startswith
(
"//"
):
# if new URI relative to the root
try
:
re
.
compile
(
line
[
0
])
re
.
compile
(
line
[
1
])
except
re
.
error
:
raise
self
.
RegexpTestError
(
"Can
\'
t compile regular expressions {expression1} {expression2} in "
"
%
s:{line_num}"
.
format
(
expression1
=
line
[
0
],
expression2
=
line
[
1
],
line_num
=
i
),
re
.
error
)
elif
line
[
0
]
.
startswith
(
"//"
):
# if new URI relative to the root
line
[
0
]
=
line
[
0
][
1
:]
# cutting out extra '/' at the beginning
line
[
0
]
=
line
[
0
][
1
:]
# cutting out extra '/' at the beginning
line
[
1
]
=
prefix
+
line
[
1
]
line
[
1
]
=
prefix
+
line
[
1
]
elif
line
[
1
]
.
startswith
(
"//"
):
elif
line
[
1
]
.
startswith
(
"//"
):
line
[
1
]
=
line
[
1
][
1
:]
line
[
1
]
=
line
[
1
][
1
:]
line
[
0
]
=
prefix
+
line
[
0
]
line
[
0
]
=
prefix
+
line
[
0
]
if
not
line
[
0
]
.
startswith
(
"~"
)
else
line
[
0
]
elif
self
.
url_absolute
.
fullmatch
(
line
[
1
]):
elif
self
.
url_absolute
.
fullmatch
(
line
[
1
]):
line
[
0
]
=
prefix
+
line
[
0
]
line
[
0
]
=
prefix
+
line
[
0
]
options
=
[
'301'
]
return_code
=
1
else
:
# default url
else
:
# default url
line
[
0
]
=
prefix
+
line
[
0
]
line
[
0
]
=
prefix
+
line
[
0
]
if
not
line
[
0
]
.
startswith
(
"~"
)
else
line
[
0
]
line
[
1
]
=
prefix
+
line
[
1
]
line
[
1
]
=
prefix
+
line
[
1
]
return
return_code
,
line
[
0
],
line
[
1
],
options
return
return_code
,
line
[
0
],
line
[
1
],
options
...
@@ -126,7 +129,7 @@ class ConfigReader:
...
@@ -126,7 +129,7 @@ class ConfigReader:
elif
return_code
==
1
:
elif
return_code
==
1
:
opt_
=
option
[
0
][
0
]
if
option
else
option
opt_
=
option
[
0
][
0
]
if
option
else
option
if
opt_
in
res
[
1
]
.
keys
():
if
opt_
in
res
[
1
]
.
keys
():
res
[
1
][
opt_
]
.
append
[(
request_url
,
redirect_url
)]
res
[
1
][
opt_
]
.
append
([
request_url
,
redirect_url
])
else
:
else
:
res
[
1
][
opt_
]
=
[(
request_url
,
redirect_url
)]
res
[
1
][
opt_
]
=
[(
request_url
,
redirect_url
)]
...
...
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