Skip to content

Commit

Permalink
Merge pull request #4 from aerocyber/dev
Browse files Browse the repository at this point in the history
Remove the validators dependency by using regex
  • Loading branch information
aerocyber authored May 8, 2023
2 parents 80c0d8d + 673aed2 commit 83a71b0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 75 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"licenser.license": "MIT",
"python.formatting.provider": "autopep8"
"python.formatting.provider": "none",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ See [LICENSE file](https://github.com/aerocyber/ospyata/blob/main/LICENSE)
### Support

Financially: [Buy me a coffee](https://www.buymeacoffee.com/aerocyber)

Code/Documentation: [Open a PR](https://github.com/aerocyber/ospyata/pulls)

Error reporting: [Open an issue](https://github.com/aerocyber/ospyata/issues)
7 changes: 0 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
attrs==22.2.0
autopep8==2.0.2
decorator==5.1.1
jsonschema==4.17.3
pycodestyle==2.10.0
pyrsistent==0.19.3
validators==0.20.0
40 changes: 20 additions & 20 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,33 @@
here = pathlib.Path(__file__).parent.resolve()

# Get the long description from the README file
long_description = (here / 'README.md').read_text(encoding='utf-8')
long_description = (here / "README.md").read_text(encoding="utf-8")


setup(
name='ospyata',
name="ospyata",
license="MIT License",
version='3.1.1',
description='Python library for the open source bookmark app Osmata.',
version="3.1.3",
description="Python library for the open source bookmark app Osmata.",
long_description=long_description,
long_description_content_type='text/markdown',
url='https://github.com/aerocyber/ospyata',
author='aerocyber',
long_description_content_type="text/markdown",
url="https://github.com/aerocyber/ospyata",
author="aerocyber",
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3 :: Only',
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3 :: Only",
],
keywords='osmata, development, osmata-bindings, osmata-python-bindings, bookmarks, ospyata',
package_dir={'': 'src'},
packages=find_packages(where='src'),
python_requires='>=3.11, <4',
install_requires=['validators'],
keywords="osmata, development, osmata-bindings, osmata-python-bindings, bookmarks, ospyata",
package_dir={"": "src"},
packages=find_packages(where="src"),
python_requires=">=3.11, <4",
install_requires=[], # Thanks to django's regex, validators is no longer required.
project_urls={
'Bug Reports': 'https://github.com/aerocyber/ospyata/issues',
'Source': 'https://github.com/aerocyber/ospyata/',
"Bug Reports": "https://github.com/aerocyber/ospyata/issues",
"Source": "https://github.com/aerocyber/ospyata/",
},
)
2 changes: 1 addition & 1 deletion src/ospyata/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT

__version__ = '3.0.0'
__version__ = '3.1.3'
__spec_version__ = '3.0.0'
84 changes: 38 additions & 46 deletions src/ospyata/osmata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# https://opensource.org/licenses/MIT

import json
import validators
import re
from typing import List
import pathlib

Expand All @@ -14,7 +14,6 @@ class OspyataException(Exception):


class Osmata:

def __init__(self):
self.db = {} # Initialize the datastructure.

Expand Down Expand Up @@ -42,10 +41,7 @@ def push(self, name: str, url: str, categories: List[str] = []):
_msg = url + " exists in db."
raise OspyataException(_msg)
else:
self.db[name] = {
"URL": url,
"Categories": categories
}
self.db[name] = {"URL": url, "Categories": categories}

def pop(self, name: str):
"""Delete a record from db matching name.
Expand All @@ -67,37 +63,7 @@ def validate_omio(self, dat: str):
Args:
dat (str): The omio string.
"""
# schema = {
# "$schema": "https://json-schema.org/draft/2020-12/schema",
# "$id": "https://example.com/product.schema.json",
# "title": "Osmations",
# "description": "A record of all bookmarks.",
# "type": "object",
# "properties": {
# "Name": {
# "description": "The unique identifier for a record.",
# "type": "string"
# },
# "URL": {
# "description": "URL associated with the Name.",
# "type": "string"
# },
# "Categories": {
# "description": "Tags for the Record",
# "type": "array",
# "items": {
# "type": "string"
# }
# }
# },
# "required": ["Name", "URL"]
# }
# try:
# validate(instance=dat, schema=schema)
# except Exception as e:
# return False
# else:
# return True

data = json.loads(dat)
_keys = data.keys()
_urls = []
Expand All @@ -124,14 +90,12 @@ def validate_omio(self, dat: str):
return False
return True



def dumpOmio(self):
return json.dumps(self.db)

def loadOmio(self, omio_path):
if pathlib.Path(omio_path).exists():
f = open(omio_path, 'r')
f = open(omio_path, "r")
data = f.read()
f.close()
if self.validate_omio(dat=data):
Expand All @@ -152,7 +116,8 @@ def check_existance(self, name=False, url=False):
if name == False:
if url == False:
raise OspyataException(
"Neither name nor url is present for existance checking.")
"Neither name nor url is present for existance checking."
)
else:
_names = self.db.keys()
for _name in _names:
Expand All @@ -176,8 +141,35 @@ def validate_url(self, url):
Args:
url (str): Url
"""
try:
if validators.url(url.strip()):
return True
except Exception as e:
raise e
# Regex source: https://github.com/django/django/blob/main/django/core/validators.py#L69
regex = re.compile(
r"^(?:[a-z0-9.+-]*)://" # scheme is validated separately
r"(?:[^\s:@/]+(?::[^\s:@/]*)?@)?" # user:pass authentication
r"(?:" + r"(?:0|25[0-5]|2[0-4][0-9]|1[0-9]?[0-9]?|[1-9][0-9]?)"
r"(?:\.(?:0|25[0-5]|2[0-4][0-9]|1[0-9]?[0-9]?|[1-9][0-9]?)){3}"
+ "|"
+ r"\[[0-9a-f:.]+\]"
+ "|"
+ "("
+ r"[a-z"
+ "\u00a1-\uffff"
+ r"0-9](?:[a-z"
+ "\u00a1-\uffff"
+ r"0-9-]{0,61}[a-z"
+ "\u00a1-\uffff"
+ r"0-9])?"
+ r"(?:\.(?!-)[a-z"
+ "\u00a1-\uffff"
+ r"0-9-]{1,63}(?<!-))*"
+ r"\." # dot
r"(?!-)" # can't start with a dash
r"(?:[a-z" + "\u00a1-\uffff" + "-]{2,63}" # domain label
r"|xn--[a-z0-9]{1,59})" # or punycode label
r"(?<!-)" # can't end with a dash
r"\.?" + "|localhost)" + ")" # may have a trailing dot
r"(?::[0-9]{1,5})?" # port
r"(?:[/?#][^\s]*)?" # resource path
r"\Z",
re.IGNORECASE,
)
return re.match(regex, url) is not None

0 comments on commit 83a71b0

Please sign in to comment.