Skip to content

Commit

Permalink
Merge pull request #36 from hasgeek/namespace_from_url
Browse files Browse the repository at this point in the history
Added namespace_from_url()
  • Loading branch information
jace committed Nov 7, 2013
2 parents 7eb11a5 + 52a2cad commit 5d1d808
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
20 changes: 20 additions & 0 deletions coaster/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from warnings import warn
from BeautifulSoup import BeautifulSoup, Comment
import pytz
from urlparse import urlparse

from ._version import *

Expand All @@ -22,6 +23,7 @@
_punctuation_re = re.compile(ur'[\t +!#$%&()*\-/<=>?@\[\\\]^_{|}:;,.…‒–—―«»]+')
_diacritics_re = re.compile(u'[\u0300-\u036F]+')
_username_valid_re = re.compile('^[a-z0-9]([a-z0-9-]*[a-z0-9])?$')
_ipv4_re = re.compile('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')


# --- Utilities ---------------------------------------------------------------
Expand Down Expand Up @@ -453,6 +455,24 @@ def hourmin(delta):
) for delta, name in timezones]


def namespace_from_url(url):
"""
Construct a dotted namespace string from a URL.
"""
parsed = urlparse(url)
if parsed.hostname is None or parsed.hostname in ['localhost', 'localhost.localdomain'] or (
_ipv4_re.search(parsed.hostname)):
return None

namespace = parsed.hostname.split('.')
namespace.reverse()
if namespace and not namespace[0]:
namespace.pop(0)
if namespace and namespace[-1] == 'www':
namespace.pop(-1)
return '.'.join(namespace)


class LabeledEnum(object):
"""
Labeled enumerations. Declarate an enumeration with values and labels
Expand Down
15 changes: 14 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import datetime
import unittest
from coaster.utils import LabeledEnum, make_password, check_password, parse_isoformat, sanitize_html, sorted_timezones
from coaster.utils import LabeledEnum, make_password, check_password, parse_isoformat, sanitize_html, sorted_timezones, namespace_from_url


class MY_ENUM(LabeledEnum):
Expand Down Expand Up @@ -49,3 +49,16 @@ def test_sanitize_html(self):

def test_sorted_timezones(self):
self.assertTrue(isinstance(sorted_timezones(), list))

def test_namespace_from_url(self):
self.assertEqual(namespace_from_url(u'https://github.com/hasgeek/coaster'), u'com.github')
self.assertEqual(namespace_from_url(u'https://funnel.hasgeek.com/metarefresh2014/938-making-design-decisions'),
u'com.hasgeek.funnel')
self.assertEqual(namespace_from_url(u'http://www.hasgeek.com'), u'com.hasgeek')
self.assertEqual(namespace_from_url(u'www.hasgeek.com'), None)
self.assertEqual(namespace_from_url(u'This is an invalid url'), None)
# IP addresses are rejected
self.assertEqual(namespace_from_url('127.0.0.1'), None)
# Return string type is the input type
self.assertTrue(isinstance(namespace_from_url(u'https://github.com/hasgeek/coaster'), unicode))
self.assertTrue(isinstance(namespace_from_url('https://github.com/hasgeek/coaster'), str))

0 comments on commit 5d1d808

Please sign in to comment.