Commits

Artur Barseghyan committed 6db2b71 Draft

tests added; tiny changes to the module; version update

Comments (0)

Files changed (14)

+python setup.py install
 except:
     readme = ''
 
-version = '0.3'
+version = '0.4'
 
 data_dir = "src/tld/res"
 data = [os.path.join(data_dir, f) for f in os.listdir(data_dir)]

source_install.sh

+pip install -e hg+https://bitbucket.org/barseghyanartur/tld#egg=tld

src/tld/__init__.py

 __title__ = 'tld'
-__version__ = '0.3'
-__build__ = 0x000003
+__version__ = '0.4'
+__build__ = 0x000004
 __author__ = 'Artur Barseghyan'
 __all__ = ('get_tld',)
 
 __title__ = 'tld.conf'
-__version__ = '0.3'
-__build__ = 0x000003
+__version__ = '0.4'
+__build__ = 0x000004
 __author__ = 'Artur Barseghyan'
-__all__ = ('get_setting', 'settings',)
+__all__ = ('get_setting', 'set_setting', 'settings',)
+
+from tld import defaults
 
 class Settings(object):
+    """
+    Settings registry.
+    """
     def __init__(self):
-        self._settings = {
-            # Source path of Mozilla's effective TLD names file.
-            'NAMES_SOURCE_URL': 'http://mxr.mozilla.org/mozilla/source/netwerk/dns/src/effective_tld_names.dat?raw=1',
+        self._settings = {}
 
-            # Relative path to store the local copy of Mozilla's effective TLD names file.
-            'NAMES_LOCAL_PATH': 'res/effective_tld_names.dat.txt',
+    def set(self, name, value):
+        """
+        Override default settings.
 
-            # Debug flag.
-            'DEBUG': False
-        }
-
-    def set(name, value):
+        :param str name:
+        :param mixed value:
+        """
         self._settings[name] = value
 
     def get(self, name, default=None):
+        """
+        Gets a variable from local settings.
+
+        :param str name:
+        :param mixed default: Default value.
+        :return mixed:
+        """
         if self._settings.has_key(name):
-            return self._settings[name]
+            return self._settings.get(name, default)
+        elif hasattr(defaults, name):
+            return getattr(defaults, name, default)
         else:
             return default
 
 settings = Settings()
 
 get_setting = settings.get
+
+set_setting = settings.set

src/tld/defaults.py

+__title__ = 'tld.defaults'
+__version__ = '0.4'
+__build__ = 0x000004
+__author__ = 'Artur Barseghyan'
+__all__ = ('NAMES_SOURCE_URL', 'NAMES_LOCAL_PATH', 'DEBUG')
+
+# Source path of Mozilla's effective TLD names file.
+NAMES_SOURCE_URL = 'http://mxr.mozilla.org/mozilla/source/netwerk/dns/src/effective_tld_names.dat?raw=1'
+
+# Relative path to store the local copy of Mozilla's effective TLD names file.
+NAMES_LOCAL_PATH =  'res/effective_tld_names.dat.txt'
+
+DEBUG = False

src/tld/exceptions.py

 __title__ = 'tld.exceptions'
-__version__ = '0.3'
-__build__ = 0x000003
+__version__ = '0.4'
+__build__ = 0x000004
 __author__ = 'Artur Barseghyan'
 __all__ = ('TldIOError', 'TldDomainNotFound', 'TldBadUrl')
 
-from tld.settings import NAMES_LOCAL_PATH as TLD_NAMES_LOCAL_PATH
+from tld.conf import get_setting
 
 _ = lambda x: x
 
     Supposed to be thrown when problems with reading/writing occur.
     """
     def __init__(self, msg=None):
+        TLD_NAMES_LOCAL_PATH = get_setting('NAMES_LOCAL_PATH')
         if msg is None:
             msg = _("Can't read from or write to the %s file!") % TLD_NAMES_LOCAL_PATH
         super(TldIOError, self).__init__(msg)

src/tld/settings.py

 __title__ = 'tld.settings'
-__version__ = '0.3'
-__build__ = 0x000003
+__version__ = '0.4'
+__build__ = 0x000004
 __author__ = 'Artur Barseghyan'
 __all__ = ('NAMES_SOURCE_URL', 'NAMES_LOCAL_PATH', 'DEBUG')
 
+import warnings
+warnings.warn("""tld.settings is deprecated; use tld.conf.get_setting function instead.""",
+              DeprecationWarning)
+
 from tld.conf import get_setting
 
 NAMES_SOURCE_URL = get_setting('NAMES_SOURCE_URL')
+__title__ = 'tld.tests'
+__version__ = '0.4'
+__build__ = 0x000004
+__author__ = 'Artur Barseghyan'
+__all__ = ('TldTest',)
+
+import unittest
+
+from tld.utils import get_tld, update_tld_names
+from tld.conf import get_setting, set_setting
+from tld import defaults
+
+_ = lambda x: x
+
+TRACK_TIME = True
+
+def track_time(func):
+    """
+    Prints some useful info.
+    """
+    if not TRACK_TIME:
+        return func
+
+    def inner(self, *args, **kwargs):
+        #timer = simple_timer.Timer() # Start timer
+
+        result = func(self, *args, **kwargs)
+
+        #timer.stop() # Stop timer
+
+        print '\n%s' % func.__name__
+        print '============================'
+        print '""" %s """' % func.__doc__.strip()
+        print '----------------------------'
+        if result is not None: print result
+        #print 'done in %s seconds' % timer.duration
+
+        return result
+    return inner
+
+class TldTest(unittest.TestCase):
+    """
+    Tld tests.
+    """
+    def setUp(self):
+        self.good_patterns = [
+            'http://www.google.co.uk',
+            'http://www.v2.google.co.uk',
+            'http://www.me.congresodelalengua3.ar'
+        ]
+
+        self.bad_patterns = [
+            '/index.php?a=1&b=2',
+            'v2.www.google.com',
+            'http://www.tld.doesnotexist'
+        ]
+
+    @track_time
+    def test_0_tld_names_loaded(self):
+        """
+        Test if tld names are loaded.
+        """
+        get_tld('http://www.google.co.uk')
+        from tld.utils import tld_names
+        res = len(tld_names) > 0
+        self.assertEqual(res, True)
+        return res
+
+    @track_time
+    def test_1_update_tld_names(self):
+        """
+        Test updating the tld names (re-fetch mozilla source).
+        """
+        res = update_tld_names(fail_silently=True)
+        self.assertEqual(res, True)
+        return res
+
+    @track_time
+    def test_2_good_patterns_pass(self):
+        """
+        Test good URL patterns.
+        """
+        res = []
+        for url in self.good_patterns:
+            r = get_tld(url, fail_silently=True)
+            self.assertNotEqual(r, None)
+            res.append(r)
+        return res
+
+    @track_time
+    def test_3_bad_patterns_pass(self):
+        """
+        Test bad URL patterns.
+        """
+        res = []
+        for url in self.bad_patterns:
+            r = get_tld(url, fail_silently=True)
+            self.assertEqual(r, None)
+            res.append(r)
+        return res
+
+    @track_time
+    def test_4_override_settings(self):
+        """
+        Testing settings override.
+        """
+        def override_settings():
+            return get_setting('DEBUG')
+
+        self.assertEqual(defaults.DEBUG, override_settings())
+
+        set_setting('DEBUG', True)
+
+        self.assertEqual(True, override_settings())
+
+        return override_settings()
+
+if __name__ == '__main__':
+    unittest.main()

src/tld/tests.py

-__title__ = 'tld.tests'
-__version__ = '0.3'
-__build__ = 0x000003
-__author__ = 'Artur Barseghyan'
-
-from tld.utils import get_tld
-
-_ = lambda x: x
-
-if __name__ == '__main__':
-    # Testing good patterns
-    for url in ['http://www.google.co.uk', 'http://www.v2.google.co.uk', 'http://www.me.congresodelalengua3.ar']:
-        print '******** Testing the URL: %s' % url
-        print get_tld(url)
-
-    # Testing the bad patterns
-    for url in ['/index.php?a=1&b=2', 'v2.www.google.com', 'http://www.tld.doesnotexist']:
-        print '******** Testing the URL: %s' % url
-        try:
-            print get_tld(url)
-        except Exception, e:
-            print e

src/tld/update.py

 __title__ = 'tld.update'
-__version__ = '0.3'
-__build__ = 0x000003
+__version__ = '0.4'
+__build__ = 0x000004
 __author__ = 'Artur Barseghyan'
 
 from tld.utils import update_tld_names
 __title__ = 'tld.utils'
-__version__ = '0.3'
-__build__ = 0x000003
+__version__ = '0.4'
+__build__ = 0x000004
 __author__ = 'Artur Barseghyan'
 __all__ = ('update_tld_names', 'get_tld')
 
 import urllib2
 import os
 
-from tld.settings import NAMES_SOURCE_URL as TLD_NAMES_SOURCE_URL, NAMES_LOCAL_PATH as TLD_NAMES_LOCAL_PATH, DEBUG
+from tld.conf import get_setting
 from tld.exceptions import TldIOError, TldDomainNotFound, TldBadUrl
 
 PROJECT_DIR = lambda base : os.path.abspath(os.path.join(os.path.dirname(__file__), base).replace('\\','/'))
 
 tld_names = []
 
-def update_tld_names():
+def update_tld_names(fail_silently=False):
     """
     Updates the local copy of TLDs file.
+
+    :param bool fail_silently: If set to True, no exceptions is raised on failure but boolean False returned.
+    :return bool: True on success, False on failure.
     """
+    TLD_NAMES_SOURCE_URL = get_setting('NAMES_SOURCE_URL')
+    TLD_NAMES_LOCAL_PATH = get_setting('NAMES_LOCAL_PATH')
     try:
         remote_file = urllib2.urlopen(TLD_NAMES_SOURCE_URL)
         local_file = open(PROJECT_DIR(TLD_NAMES_LOCAL_PATH), 'w')
         local_file.close()
         remote_file.close()
     except Exception, e:
+        if fail_silently:
+            return False
         raise TldIOError(e)
 
     return True
     :param fail_silently: If set to True, no exceptions are raised and None is returned on failure.
     :return: String with top level domain or None on failure.
     """
+    TLD_NAMES_LOCAL_PATH = get_setting('NAMES_LOCAL_PATH')
+
     def init(retry_count=0):
         """
         Build the ``tlds`` list if empty. Recursive.
     domain_name = urlparse(url).netloc
 
     if not domain_name:
-        raise TldBadUrl(url=url)
+        if fail_silently:
+            return None
+        else:
+            raise TldBadUrl(url=url)
 
     domain_parts = domain_name.split('.')
 
+python src/tld/test.py
+pip uninstall tld
+rm build -rf
+rm dist -rf
+rm src/tld.egg-info -rf