Kumar McMillan avatar Kumar McMillan committed f4c2efb

Yikes. Much code cleanup for pep8 and started preparing sync to update packages

Comments (0)

Files changed (3)

onpython3yet/packages/tests.py

-"""
-This file demonstrates two different styles of tests (one doctest and one
-unittest). These will both pass when you run "manage.py test".
-
-Replace these with more appropriate tests for your application.
-"""
-
-from django.test import TestCase
-
-class SimpleTest(TestCase):
-    def test_basic_addition(self):
-        """
-        Tests that 1 + 1 always equals 2.
-        """
-        self.failUnlessEqual(1 + 1, 2)
-
-__test__ = {"doctest": """
-Another way to test that 1 + 1 is equal to 2.
-
->>> 1 + 1 == 2
-True
-"""}
-

onpython3yet/packages/tests/test_views.py

 import packages.views
 
 class TestViews(DjangoTestCase):
-    
+
     def tearDown(self):
         for pr in PackageRequirement.objects.all():
             pr.delete()
         for p in Package.objects.all():
             p.delete()
         fudge.clear_expectations()
-    
+
     @fudge.with_fakes
     def test_fetch_package_and_requirements(self):
-        
+
         fake_xmlrpclib = fudge.Fake('xmlrpclib').provides('Server')
         fake_server = fake_xmlrpclib.returns_fake()
         fake_search = (fake_server.expects('search')
                             .with_args({'name':'Adamanteus'})
-                            .returns([{'name': 'Adamanteus', 'version': '0.5', 
-                                        'summary': 'Database Backups with Version Control'}])
+                            .returns([{'name': 'Adamanteus', 'version': '0.5',
+                                       'summary': ('Database Backups with '
+                                                   'Version Control')}])
                             .next_call()
                             .with_args({'name':'pymongo'})
                             .returns([{'name': 'pymongo', 'version': '0.5'}])
                             .next_call()
                             .with_args({'name':'mercurial'})
-                            .returns([{'name': 'Mercurial', 'version': '0.5'}])
+                            .returns([{'name': 'Mercurial',
+                                       'version': '0.5'}])
                         )
-        # fake_releases = fake_server.expects('package_releases').returns(['0.5'])
+        # fake_releases =
+        #   fake_server.expects('package_releases').returns(['0.5'])
         fake_data = (fake_server.expects('release_data')
                             .with_args('Adamanteus', '0.5')
                             .returns({
-                                'requires': ['pymongo', 'mercurial'],                            
+                                'requires': ['pymongo', 'mercurial'],
                                 'classifiers': [
-                                    # pretend that this package supports python 3:
+                                    # pretend that this package supports
+                                    # python 3:
                                     'Programming Language :: Python :: 3.0'
                                 ]
                             })
                             .with_args('Mercurial', '0.5')
                             .returns({'requires': []})
                     )
-        
-        with fudge.patched_context(packages.views, "xmlrpclib", fake_xmlrpclib):
-                
-            response = self.client.get(reverse('packages.fetch', args=('Adamanteus',)))
-            self.assertRedirects(response, reverse('packages.show', args=('Adamanteus',)))
-        
+
+        with fudge.patched_context(packages.views, "xmlrpclib",
+                                   fake_xmlrpclib):
+
+            response = self.client.get(reverse('packages.fetch',
+                                       args=('Adamanteus',)))
+            self.assertRedirects(response, reverse('packages.show',
+                                 args=('Adamanteus',)))
+
             package = Package.objects.filter(name='Adamanteus')[0]
             self.assertEqual(package.current_version, '0.5')
             self.assertEqual(package.supports_python_3, True)
-            self.assertEqual(package.summary, "Database Backups with Version Control")
+            self.assertEqual(package.summary,
+                             "Database Backups with Version Control")
             self.assertEqual(package.description, None)
-        
-            reqs = sorted([r.required_package.name for r in package.requirements.all()])
+
+            reqs = sorted([r.required_package.name for r in
+                           package.requirements.all()])
             self.assertEqual(reqs, [u'Mercurial', u'pymongo'])
-        
-            self.assertEqual(Package.objects.filter(name='pymongo').count(), 1)
-            self.assertEqual(Package.objects.filter(name='Mercurial').count(), 1)
-        
+
+            self.assertEqual(Package.objects.filter(name='pymongo').count(),
+                             1)
+            self.assertEqual(Package.objects.filter(name='Mercurial').count(),
+                             1)
+
         # make sure the second pass uses existing objects:
-        response = self.client.get(reverse('packages.fetch', args=('Adamanteus',)))
-        self.assertRedirects(response, reverse('packages.show', args=('Adamanteus',)))
-        
+        response = self.client.get(reverse('packages.fetch',
+                                   args=('Adamanteus',)))
+        self.assertRedirects(response, reverse('packages.show',
+                             args=('Adamanteus',)))
+
         self.assertEqual(Package.objects.filter(name='Adamanteus').count(), 1)
         self.assertEqual(Package.objects.filter(name='pymongo').count(), 1)
         self.assertEqual(Package.objects.filter(name='Mercurial').count(), 1)
-    
+
     @fudge.with_fakes
     def test_fetch_urlencoded_package(self):
-        
+
         fake_xmlrpclib = fudge.Fake('xmlrpclib').provides('Server')
         fake_server = fake_xmlrpclib.returns_fake()
         fake_search = (fake_server.expects('search')
                             .with_args({'name':'Amara XML Toolkit'})
-                            .returns([{'name': 'Amara XML Toolkit', 'version': '0.5'}])
+                            .returns([{'name': 'Amara XML Toolkit',
+                                       'version': '0.5'}])
                         )
-        # fake_releases = fake_server.expects('package_releases').returns(['0.5'])
+        # fake_releases =
+        #       fake_server.expects('package_releases').returns(['0.5'])
         fake_data = (fake_server.expects('release_data')
                             .with_args('Amara XML Toolkit', '0.5')
                             .returns({'requires': []})
                     )
-        
-        with fudge.patched_context(packages.views, "xmlrpclib", fake_xmlrpclib):
-            # note that django automatically urldecodes but it doesn't hurt to check!
-            response = self.client.get(reverse('packages.fetch', 
-                                            args=(urllib.quote('Amara XML Toolkit'),)))
-    
+
+        with fudge.patched_context(packages.views, "xmlrpclib",
+                                   fake_xmlrpclib):
+            # note that django automatically urldecodes but
+            # it doesn't hurt to check!
+            response = self.client.get(reverse('packages.fetch',
+                                args=(urllib.quote('Amara XML Toolkit'),)))
+
     @fudge.with_fakes
     def test_skip_missing_requirements(self):
-        
+
         fake_xmlrpclib = fudge.Fake('xmlrpclib').provides('Server')
         fake_server = fake_xmlrpclib.returns_fake()
         fake_search = (fake_server.expects('search')
                             .with_args({'name':'Adamanteus'})
-                            .returns([{'name': 'Adamanteus', 'version': '0.5'}])
+                            .returns([{'name': 'Adamanteus',
+                                       'version': '0.5'}])
                             .next_call()
                             .with_args({'name':'bogus'})
                             # force a missing requirement:
-                            .returns([{'name': 'other', 'version': '0.1'}, {'name': 'other2', 'version': '0.1'}])
+                            .returns([{'name': 'other', 'version': '0.1'},
+                                      {'name': 'other2', 'version': '0.1'}])
                         )
-        # fake_releases = fake_server.expects('package_releases').returns(['0.5'])
+        # fake_releases =
+        #       fake_server.expects('package_releases').returns(['0.5'])
         fake_data = (fake_server.expects('release_data')
                             .with_args('Adamanteus', '0.5')
                             .returns({
                                 'requires': ['bogus']
                             })
                     )
-        
-        with fudge.patched_context(packages.views, "xmlrpclib", fake_xmlrpclib):
-                
-            response = self.client.get(reverse('packages.fetch', args=('Adamanteus',)))
-            self.assertRedirects(response, reverse('packages.show', args=('Adamanteus',)))
-        
+
+        with fudge.patched_context(packages.views, "xmlrpclib",
+                                   fake_xmlrpclib):
+
+            response = self.client.get(reverse('packages.fetch',
+                                       args=('Adamanteus',)))
+            self.assertRedirects(response, reverse('packages.show',
+                                 args=('Adamanteus',)))
+
             package = Package.objects.filter(name='Adamanteus')[0]
-        
-            reqs = sorted([r.required_package.name for r in package.requirements.all()])
+
+            reqs = sorted([r.required_package.name for r in
+                           package.requirements.all()])
             self.assertEqual(reqs, [])
-    
+
     @fudge.with_fakes
     def test_skip_requirements_when_no_results(self):
-        
+
         fake_xmlrpclib = fudge.Fake('xmlrpclib').provides('Server')
         fake_server = fake_xmlrpclib.returns_fake()
         fake_search = (fake_server.expects('search')
                             .with_args({'name':'Adamanteus'})
-                            .returns([{'name': 'Adamanteus', 'version': '0.5'}])
+                            .returns([{'name': 'Adamanteus',
+                                       'version': '0.5'}])
                             .next_call()
                             .with_args({'name':'bogus'})
                             # force no results:
                             .returns([])
                         )
-        # fake_releases = fake_server.expects('package_releases').returns(['0.5'])
+        # fake_releases =
+        #   fake_server.expects('package_releases').returns(['0.5'])
         fake_data = (fake_server.expects('release_data')
                             .with_args('Adamanteus', '0.5')
                             .returns({
                                 'requires': ['bogus']
                             })
                     )
-        
-        with fudge.patched_context(packages.views, "xmlrpclib", fake_xmlrpclib):
-                
-            response = self.client.get(reverse('packages.fetch', args=('Adamanteus',)))
-            self.assertRedirects(response, reverse('packages.show', args=('Adamanteus',)))
-        
+
+        with fudge.patched_context(packages.views, "xmlrpclib",
+                                   fake_xmlrpclib):
+
+            response = self.client.get(reverse('packages.fetch',
+                                       args=('Adamanteus',)))
+            self.assertRedirects(response, reverse('packages.show',
+                                 args=('Adamanteus',)))
+
             package = Package.objects.filter(name='Adamanteus')[0]
-        
-            reqs = sorted([r.required_package.name for r in package.requirements.all()])
+
+            reqs = sorted([r.required_package.name for r in
+                           package.requirements.all()])
             self.assertEqual(reqs, [])
-    
+
     @fudge.with_fakes
     def test_package_cannot_require_itself(self):
-        
+
         fake_xmlrpclib = fudge.Fake('xmlrpclib').provides('Server')
         fake_server = fake_xmlrpclib.returns_fake()
         fake_search = (fake_server.expects('search')
                             .times_called(1)
                             .with_args({'name':'Adamanteus'})
-                            .returns([{'name': 'Adamanteus', 'version': '0.5'}])
+                            .returns([{'name': 'Adamanteus',
+                                       'version': '0.5'}])
                         )
-        # fake_releases = fake_server.expects('package_releases').returns(['0.5'])
+        # fake_releases =
+        #   fake_server.expects('package_releases').returns(['0.5'])
         fake_data = (fake_server.expects('release_data')
                             .with_args('Adamanteus', '0.5')
                             .returns({
                                 'requires': ['Adamanteus']
                             })
                     )
-        
-        with fudge.patched_context(packages.views, "xmlrpclib", fake_xmlrpclib):
-                
-            response = self.client.get(reverse('packages.fetch', args=('Adamanteus',)))
-            self.assertRedirects(response, reverse('packages.show', args=('Adamanteus',)))
-        
+
+        with fudge.patched_context(packages.views, "xmlrpclib",
+                                   fake_xmlrpclib):
+
+            response = self.client.get(reverse('packages.fetch',
+                                       args=('Adamanteus',)))
+            self.assertRedirects(response, reverse('packages.show',
+                                 args=('Adamanteus',)))
+
             package = Package.objects.filter(name='Adamanteus')[0]
-            
-            reqs = sorted([r.required_package.name for r in package.requirements.all()])
+
+            reqs = sorted([r.required_package.name for r in
+                           package.requirements.all()])
             self.assertEqual(reqs, [])
 
-        
+

onpython3yet/packages/views.py

     """The package was not found on PyPI."""
 
 def fetch(request, package_name):
-    query = Package.objects.filter(name=package_name)
-    if query.count():
-        # already fetched it:
-        return HttpResponseRedirect(reverse('packages.show', args=(package_name,)))
-    
+
     server = xmlrpclib.Server('http://pypi.python.org/pypi')
     pypi_package = _get_pypi_package_by_name(server, package_name)
     if not pypi_package:
         return HttpResponse("%r not found on PyPI" % package_name)
-    
-    # now that we have the correct name (correct case), see if we have it again:
+
+    # now that we have the correct name (correct case),
+    # see if we have it again:
     query = Package.objects.filter(name=pypi_package['name'])
     if query.count():
         # TODO: update the package instead?
-        return HttpResponseRedirect(reverse('packages.show', args=(pypi_package['name'],)))
-    
+        return HttpResponseRedirect(reverse('packages.show',
+                                    args=(pypi_package['name'],)))
+
     _create_package_from_pypi(server, pypi_package)
-        
-    return HttpResponseRedirect(reverse('packages.show', args=(pypi_package['name'],)))
 
-def _get_pypi_package_by_name(server, package_name):    
+    return HttpResponseRedirect(reverse('packages.show',
+                                args=(pypi_package['name'],)))
+
+def _get_pypi_package_by_name(server, package_name):
     result = server.search({'name': package_name})
     if not len(result):
         return None
-        
+
     pypi_package = None
     for pkg in result:
         if pkg['name'].lower() == package_name.lower():
             break
     if not pypi_package:
         raise PackageNotFound("No exact match for %r" % package_name)
-    
+
     return pypi_package
 
-def _create_package_from_pypi(server, pypi_package):    
-            
+def _create_package_from_pypi(server, pypi_package):
+
     current_version = pypi_package['version']
     log.info("Creating: %r" % pypi_package)
     release_data = server.release_data(pypi_package['name'], current_version)
-    
+
     supports_python_3 = False
     if release_data.get('classifiers'):
         for cls in release_data['classifiers']:
             # supports 3.x
             if cls.startswith('Programming Language :: Python :: 3'):
                 supports_python_3 = True
-        
+
     saved_package = Package(
         name=pypi_package['name'],
         current_version=pypi_package['version'],
         description=pypi_package.get('description', None),
         supports_python_3=supports_python_3
     )
-    
+
     # add requirements if there are any:
     for required_pkg_name in release_data.get('requires', []):
         if required_pkg_name == saved_package.name:
             # a package cannot require itself
             # however it is possible to declare this.
             continue
-            
+
         # TODO: merge this with packages.requirements
         required_pkg_name = requirements.strip_spec(required_pkg_name)
-        
+
         try:
             query = Package.objects.filter(name=required_pkg_name)
             if query.count():
                 required_package = query[0]
             else:
-                r_pypi_package = _get_pypi_package_by_name(server, required_pkg_name)
+                r_pypi_package = _get_pypi_package_by_name(server,
+                                                           required_pkg_name)
                 if r_pypi_package:
                     log.info("Creating requirement of %r: %r" % (
                                 saved_package.name, r_pypi_package['name']))
-                    required_package = _create_package_from_pypi(server, r_pypi_package)
+                    required_package = _create_package_from_pypi(
+                                                    server, r_pypi_package)
                 else:
-                    raise PackageNotFound("No results for %r" % required_pkg_name)
-            
+                    raise PackageNotFound("No results for %r" %
+                                          required_pkg_name)
+
             requirement = PackageRequirement(
                 package=saved_package,
                 required_package=required_package
             )
             saved_package.requirements.add(requirement)
-            
+
         except PackageNotFound, exc:
             log.warning("Could not find requirement %r for package %r" % (
-                                        required_pkg_name, saved_package.name))
+                                    required_pkg_name, saved_package.name))
             # if a requirement cannot be found just ignore the requirement
             pass
-    
+
     saved_package.save()
-    
+
     return saved_package
 
 def show(request, package_name):
     if query.count():
         pkg = query[0]
     else:
-        return HttpResponseRedirect(reverse('packages.fetch', args=(package_name,)))
-    
+        return HttpResponseRedirect(reverse('packages.fetch',
+                                    args=(package_name,)))
+
     return render_to_response('packages/show.html', {'package': pkg},
             context_instance=RequestContext(request))
-    
+
 def index(request):
     return HttpResponse("package index")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.