Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Поляков Сергей Викторович
url_shortener
Commits
dafa53ed
Commit
dafa53ed
authored
Sep 20, 2021
by
Поляков Сергей Викторович
Browse files
debugging file paths
parent
68fb6904
Pipeline
#3613
failed with stages
in 3 minutes and 8 seconds
Changes
7
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
data/db.yaml
View file @
dafa53ed
-
full_link
:
https://habr.com/ru/company/ruvds/news/t/577872/
short_link
:
ex1
-
full_link
:
https://habr.com/ru/company/ruvds/blog/576376/
short_link
:
li2
-
full_link
:
https://habr.com/ru/company/ruvds/blog/577754/
short_link
:
en3
-
full_link
:
https://habr.com/ru/company/ruvds/blog/576716/
short_link
:
zv4
-
full_link
:
https://habr.com/ru/company/ruvds/news/t/577872/
short_link
:
ex1
-
full_link
:
https://habr.com/ru/company/ruvds/blog/576376/
short_link
:
li2
-
full_link
:
https://habr.com/ru/company/ruvds/blog/577754/
short_link
:
en3
-
full_link
:
https://habr.com/ru/company/ruvds/blog/576716/
short_link
:
zv4
lib/libs.py
View file @
dafa53ed
import
random
import
os
import
json
class
Config
:
def
__init__
(
self
,
path
=
os
.
path
.
join
(
__file__
,
r
"..\..\settings.json"
)):
self
.
_path
=
path
with
open
(
self
.
_path
,
"r"
)
as
f
:
js
=
json
.
loads
(
f
.
read
())
storage
=
js
[
'storage'
]
path_to_yaml
=
js
[
'path_to_yaml'
]
self
.
storage
=
storage
self
.
path_to_yaml
=
path_to_yaml
class
LinkGenerator
:
"""Takes the last generated link and returns a new in new_link variable"""
# сделал бы с помошью Unique но в ТЗ так
def
__init__
(
self
,
last_link
:
str
):
self
.
_last_link
=
last_link
self
.
_last_number
=
int
(
self
.
_get_num
())
+
1
self
.
new_link
=
(
self
.
_random_alpha
()
+
str
(
self
.
_last_number
))
def
_get_num
(
self
):
"""Parses a string and returns only numbers"""
try
:
t
=
''
for
i
in
self
.
_last_link
:
if
i
.
isdigit
():
t
+=
i
return
t
except
Exception
as
e
:
print
(
f
'Error:
{
e
}
'
)
return
''
def
_random_alpha
(
self
):
"""Return two random letters"""
return
chr
(
random
.
randint
(
97
,
122
))
+
chr
(
random
.
randint
(
97
,
122
))
import
random
import
json
class
Config
:
"""Read json and return objects"""
def
__init__
(
self
,
path
):
"""path - path to pref.json"""
self
.
_path
=
path
with
open
(
self
.
_path
,
"r"
)
as
f
:
js
=
json
.
loads
(
f
.
read
())
storage
=
js
[
'storage'
]
name_yaml_bd
=
js
[
'name_yaml_bd'
]
self
.
storage
=
storage
self
.
name_yaml_bd
=
name_yaml_bd
class
LinkGenerator
:
"""Takes the last generated link and returns a new in new_link variable"""
# сделал бы с помошью Unique но в ТЗ так
def
__init__
(
self
,
last_link
:
str
):
self
.
_last_link
=
last_link
self
.
_last_number
=
int
(
self
.
_get_num
())
+
1
self
.
new_link
=
(
self
.
_random_alpha
()
+
str
(
self
.
_last_number
))
def
_get_num
(
self
):
"""Parses a string and returns only numbers"""
try
:
t
=
''
for
i
in
self
.
_last_link
:
if
i
.
isdigit
():
t
+=
i
return
t
except
Exception
as
e
:
print
(
f
'Error:
{
e
}
'
)
return
''
def
_random_alpha
(
self
):
"""Return two random letters"""
return
chr
(
random
.
randint
(
97
,
122
))
+
chr
(
random
.
randint
(
97
,
122
))
lib/yaml_manager.py
View file @
dafa53ed
import
os
import
yaml
yaml_conf
=
YamlConfig
()
class
YamlManager
:
def
__init__
(
self
,
path
):
self
.
_path
=
path
if
os
.
path
.
exists
(
self
.
_path
):
self
.
last_short_link
=
self
.
get_last_dict
()[
'short_link'
]
else
:
self
.
last_short_link
=
'0'
def
save
(
self
,
short_link
:
str
,
full_link
:
str
):
with
open
(
self
.
_path
,
'a'
)
as
f
:
yaml
.
dump
([{
"short_link"
:
short_link
,
"full_link"
:
full_link
}],
f
,
default_flow_style
=
False
)
def
read
(
self
):
"""Return all dict from yaml"""
try
:
with
open
(
yaml_conf
.
path_to_yaml
)
as
f
:
templates
=
yaml
.
safe_load
(
f
)
return
templates
except
:
return
None
def
get_by_key
(
self
,
key
):
try
:
for
s
in
self
.
read
():
if
key
==
s
[
'short_link'
]:
return
s
[
'full_link'
]
except
:
return
False
def
get_last_dict
(
self
):
"""Return last element from yaml"""
return
self
.
read
()[
-
1
]
\ No newline at end of file
import
os
import
yaml
class
YamlManager
:
def
__init__
(
self
,
path
):
"""path = path to db"""
self
.
_path
=
path
if
os
.
path
.
exists
(
self
.
_path
):
self
.
last_short_link
=
self
.
get_last_dict
()[
'short_link'
]
else
:
self
.
last_short_link
=
'0'
def
save
(
self
,
short_link
:
str
,
full_link
:
str
):
with
open
(
self
.
_path
,
'a'
)
as
f
:
yaml
.
dump
([{
"short_link"
:
short_link
,
"full_link"
:
full_link
}],
f
,
default_flow_style
=
False
)
def
read
(
self
):
"""Return all dict from yaml"""
try
:
with
open
(
self
.
_path
)
as
f
:
items
=
yaml
.
safe_load
(
f
)
return
items
except
:
return
None
def
get_by_key
(
self
,
key
):
try
:
for
s
in
self
.
read
():
if
key
==
s
[
'short_link'
]:
return
s
[
'full_link'
]
except
:
return
False
def
get_last_dict
(
self
):
"""Return last element from yaml"""
return
self
.
read
()[
-
1
]
main.py
View file @
dafa53ed
from
starlette.responses
import
PlainTextResponse
,
RedirectResponse
from
typing
import
Optional
from
fastapi
import
FastAPI
,
Path
,
Query
from
settings
import
YamlConfig
from
settings
import
StorageConfig
from
lib
import
YamlManager
storage_conf
=
StorageConfig
()
yaml_conf
=
YamlConfig
()
yaml
=
YamlManager
(
path
=
yaml_conf
.
path_to_yaml
)
app
=
FastAPI
()
@
app
.
get
(
"/{shorted_link}"
)
async
def
short_link
(
shorted_link
:
str
):
if
storage_conf
.
storage
==
'yaml'
:
key
=
yaml
.
get_by_key
(
shorted_link
)
if
key
:
return
RedirectResponse
(
url
=
key
)
else
:
return
'404 Not found'
from
pathlib
import
Path
from
fastapi
import
FastAPI
from
starlette.responses
import
RedirectResponse
from
lib.libs
import
Config
from
lib.yaml_manager
import
YamlManager
conf
=
Config
(
Path
(
"pref.json"
))
path_to_yaml_db
=
Path
(
"data"
,
conf
.
name_yaml_bd
)
yaml
=
YamlManager
(
path
=
path_to_yaml_db
)
app
=
FastAPI
()
@
app
.
get
(
"/{shorted_link}"
)
async
def
short_link
(
shorted_link
:
str
):
if
conf
.
storage
==
'yaml'
:
key
=
yaml
.
get_by_key
(
shorted_link
)
if
key
:
return
RedirectResponse
(
url
=
key
)
else
:
return
'404 Not found'
pref.json
0 → 100644
View file @
dafa53ed
{
"storage"
:
"yaml"
,
"name_yaml_bd"
:
"db.yaml"
}
requirements.txt
View file @
dafa53ed
argon2-cffi
==21.1.0
asgiref
==3.4.1
atomicwrites
==1.4.0
attrs
==21.2.0
backcall
==0.2.0
beautifulsoup4
==4.9.3
betterconf
==2.6.1
bleach
==4.1.0
bs4
==0.0.1
certifi
==2021.5.30
cffi
==1.14.6
charset-normalizer
==2.0.4
click
==8.0.1
colorama
==0.4.4
cycler
==0.10.0
debugpy
==1.4.1
decorator
==5.0.9
defusedxml
==0.7.1
entrypoints
==0.3
fastapi
==0.68.1
Flask
==2.0.1
greenlet
==1.1.1
h11
==0.12.0
httptools
==0.2.0
idna
==3.2
iniconfig
==1.1.1
ipykernel
==6.3.1
ipython
==7.27.0
ipython-genutils
==0.2.0
itsdangerous
==2.0.1
jedi
==0.18.0
Jinja2
==3.0.1
jsonschema
==3.2.0
jupyter-client
==7.0.2
jupyter-core
==4.7.1
jupyterlab-pygments
==0.1.2
jupyterthemes
==0.20.0
kiwisolver
==1.3.2
lesscpy
==0.15.0
lxml
==4.6.3
MarkupSafe
==2.0.1
matplotlib
==3.4.3
matplotlib-inline
==0.1.2
mistune
==0.8.4
MouseInfo
==0.1.3
nbclient
==0.5.4
nbconvert
==6.1.0
nbformat
==5.1.3
nest-asyncio
==1.5.1
notebook
==6.4.3
numpy
==1.21.2
packaging
==21.0
pandocfilters
==1.4.3
parso
==0.8.2
pathlib
==1.0.1
pickleshare
==0.7.5
Pillow
==8.3.1
pluggy
==1.0.0
ply
==3.11
prometheus-client
==0.11.0
prompt-toolkit
==3.0.20
py
==1.10.0
PyAutoGUI
==0.9.53
pycparser
==2.20
pydantic
==1.8.2
PyGetWindow
==0.0.9
Pygments
==2.10.0
PyMsgBox
==1.0.9
pyparsing
==2.4.7
pyperclip
==1.8.2
PyRect
==0.1.4
pyrsistent
==0.18.0
PyScreeze
==0.1.27
pyTelegramBotAPI
==4.0.0
pytest
==6.2.5
python-dateutil
==2.8.2
python-dotenv
==0.19.0
PyTweening
==1.0.3
pytz
==2021.1
pywin32
==301
pywinpty
==1.1.3
PyYAML
==5.4.1
pyzmq
==22.2.1
requests
==2.26.0
Send2Trash
==1.8.0
six
==1.16.0
soupsieve
==2.2.1
SQLAlchemy
==1.4.23
starlette
==0.14.2
terminado
==0.11.1
testpath
==0.5.0
toml
==0.10.2
tornado
==6.1
traitlets
==5.1.0
typing-extensions
==3.10.0.2
urllib3
==1.26.6
uvicorn
==0.15.0
watchgod
==0.7
wcwidth
==0.2.5
webencodings
==0.5.1
websockets
==10.0
Werkzeug
==2.0.1
pip
install
PyYAML
pip
install
fastapi
PyYAML
>=5.4.1
fastapi
>=0.68.1
PyYAML
==5.4.1
fastapi
==0.68.1
uvicorn
==0.15.0
starlette
~=0.14.2
pathlib
~=1.0.1
\ No newline at end of file
settings.json
deleted
100644 → 0
View file @
68fb6904
{
"name"
:
"$name$"
,
"version"
:
"$version$"
,
"dependencies"
:
{
$END$
}
}
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