Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
Move sanitize_path to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
rbw committed Mar 12, 2018
1 parent 0d7a933 commit 0084ff3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
5 changes: 3 additions & 2 deletions flask_journey/blueprint_bundle.py
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-

from flask import Blueprint

from .utils import sanitize_path
from .exceptions import InvalidBlueprint
from . import journey


class BlueprintBundle(object):
Expand All @@ -12,7 +13,7 @@ class BlueprintBundle(object):
"""

def __init__(self, path='/', description=''):
self.path = journey.Journey.sanitize_path(path)
self.path = sanitize_path(path)
self.description = description
self.blueprints = []

Expand Down
28 changes: 3 additions & 25 deletions flask_journey/journey.py
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-

import re
from .utils import sanitize_path

from .exceptions import (
IncompatibleBundle, InvalidPath, NoBundlesAttached,
IncompatibleBundle, NoBundlesAttached,
MissingBlueprints, InvalidBundlesType, ConflictingPath
)

Expand Down Expand Up @@ -97,28 +97,6 @@ def routes_simple(self):

return routes

@staticmethod
def sanitize_path(path):
"""Performs sanitation of the path after validating
:param path: path to sanitize
:return: path
:raises:
- InvalidPath if the path doesn't start with a slash
"""

if path == '/': # Nothing to do, just return
return path

if path[:1] != '/':
raise InvalidPath('The path must start with a slash')

# Deduplicate slashes in path
path = re.sub(r'/+', '/', path)

# Strip trailing slashes and return
return path.rstrip('/')

def _bundle_exists(self, path):
"""Checks if a bundle exists at the provided path
Expand Down Expand Up @@ -163,7 +141,7 @@ def _register_blueprint(self, app, bp, bundle_path, child_path, description):
:return: Dict with info about the blueprint
"""

base_path = self.sanitize_path(self._journey_path + bundle_path + child_path)
base_path = sanitize_path(self._journey_path + bundle_path + child_path)

app.register_blueprint(bp, url_prefix=base_path)

Expand Down
27 changes: 26 additions & 1 deletion flask_journey/utils.py
@@ -1,11 +1,36 @@
# -*- coding: utf-8 -*-

import re

from functools import wraps
from flask import jsonify, request
from marshmallow import ValidationError, Schema
from .exceptions import IncompatibleSchema
from furl import furl

from .exceptions import IncompatibleSchema, InvalidPath


def sanitize_path(path):
"""Performs sanitation of the path after validating
:param path: path to sanitize
:return: path
:raises:
- InvalidPath if the path doesn't start with a slash
"""

if path == '/': # Nothing to do, just return
return path

if path[:1] != '/':
raise InvalidPath('The path must start with a slash')

# Deduplicate slashes in path
path = re.sub(r'/+', '/', path)

# Strip trailing slashes and return
return path.rstrip('/')


def _validate_schema(obj):
"""Ensures the passed schema instance is compatible
Expand Down

0 comments on commit 0084ff3

Please sign in to comment.