import os

from django.conf import settings

from launchpadlib.launchpad import Launchpad


def pull(package):
    
    cachedir = getattr(settings, 'LAUNCHPAD_CACHE_DIR', os.path.join(settings.PROJECT_ROOT, 'lp-cache'))
    launchpad = Launchpad.login_anonymously('djangopackages.com', 'production', cachedir)
    repo_name = package.repo_name().replace('https://code.launchpad.net/','')

    branch = launchpad.branches.getByUrl(url='lp:%s' % repo_name)


    package.repo_description = branch.description or ''
    package.repo_forks = len(branch.project.getBranches())
    package.repo_watchers = len(branch.subscribers)
    package.participants = branch.owner.name

    return package

import os

from django.conf import settings
from launchpadlib.launchpad import Launchpad

from package.handlers.base_handler import BaseHandler


class LaunchpadHandler(BaseHandler):
    title = 'Launchpad'
    url = 'https://code.launchpad.net'
    user_url = 'https://launchpad.net/~%s'
    repo_regex = r'https://code.launchpad.net/[\w\-\_]+/([\w\-\_]+)/[\w\-\_]+/{0,1}'
    slug_regex = r'https://code.launchpad.net/[\w\-\_]+/([\w\-\_]+)/[\w\-\_]+/{0,1}'

    def pull(self, package):
        cachedir = getattr(settings, 'LAUNCHPAD_CACHE_DIR', os.path.join(settings.PROJECT_ROOT, 'lp-cache'))
        launchpad = Launchpad.login_anonymously('djangopackages.com', 'production', cachedir)
        repo_name = package.repo_name()

        branch = launchpad.branches.getByUrl(url='lp:%s' % repo_name)

        package.repo_description = branch.description or ''
        package.repo_forks = len(branch.project.getBranches())
        package.repo_watchers = len(branch.subscribers)
        package.participants = branch.owner.name

        return package

Refactor Launchpad handler to use BaseHandler.
Refactor Launchpad handler to use BaseHandler.

  import os
  
  from django.conf import settings
- 
  from launchpadlib.launchpad import Launchpad
  
+ from package.handlers.base_handler import BaseHandler
- 
- def pull(package):
-     
-     cachedir = getattr(settings, 'LAUNCHPAD_CACHE_DIR', os.path.join(settings.PROJECT_ROOT, 'lp-cache'))
-     launchpad = Launchpad.login_anonymously('djangopackages.com', 'production', cachedir)
-     repo_name = package.repo_name().replace('https://code.launchpad.net/','')
- 
-     branch = launchpad.branches.getByUrl(url='lp:%s' % repo_name)
  
  
-     package.repo_description = branch.description or ''
-     package.repo_forks = len(branch.project.getBranches())
-     package.repo_watchers = len(branch.subscribers)
-     package.participants = branch.owner.name
+ class LaunchpadHandler(BaseHandler):
+     title = 'Launchpad'
+     url = 'https://code.launchpad.net'
+     user_url = 'https://launchpad.net/~%s'
+     repo_regex = r'https://code.launchpad.net/[\w\-\_]+/([\w\-\_]+)/[\w\-\_]+/{0,1}'
+     slug_regex = r'https://code.launchpad.net/[\w\-\_]+/([\w\-\_]+)/[\w\-\_]+/{0,1}'
  
-     return package
+     def pull(self, package):
+         cachedir = getattr(settings, 'LAUNCHPAD_CACHE_DIR', os.path.join(settings.PROJECT_ROOT, 'lp-cache'))
+         launchpad = Launchpad.login_anonymously('djangopackages.com', 'production', cachedir)
+         repo_name = package.repo_name()
  
+         branch = launchpad.branches.getByUrl(url='lp:%s' % repo_name)
+ 
+         package.repo_description = branch.description or ''
+         package.repo_forks = len(branch.project.getBranches())
+         package.repo_watchers = len(branch.subscribers)
+         package.participants = branch.owner.name
+ 
+         return package
+ 
Refactor Launchpad handler to use BaseHandler.
## Code Before:
import os

from django.conf import settings

from launchpadlib.launchpad import Launchpad


def pull(package):
    
    cachedir = getattr(settings, 'LAUNCHPAD_CACHE_DIR', os.path.join(settings.PROJECT_ROOT, 'lp-cache'))
    launchpad = Launchpad.login_anonymously('djangopackages.com', 'production', cachedir)
    repo_name = package.repo_name().replace('https://code.launchpad.net/','')

    branch = launchpad.branches.getByUrl(url='lp:%s' % repo_name)


    package.repo_description = branch.description or ''
    package.repo_forks = len(branch.project.getBranches())
    package.repo_watchers = len(branch.subscribers)
    package.participants = branch.owner.name

    return package

## Instruction:
Refactor Launchpad handler to use BaseHandler.
## Code After:
import os

from django.conf import settings
from launchpadlib.launchpad import Launchpad

from package.handlers.base_handler import BaseHandler


class LaunchpadHandler(BaseHandler):
    title = 'Launchpad'
    url = 'https://code.launchpad.net'
    user_url = 'https://launchpad.net/~%s'
    repo_regex = r'https://code.launchpad.net/[\w\-\_]+/([\w\-\_]+)/[\w\-\_]+/{0,1}'
    slug_regex = r'https://code.launchpad.net/[\w\-\_]+/([\w\-\_]+)/[\w\-\_]+/{0,1}'

    def pull(self, package):
        cachedir = getattr(settings, 'LAUNCHPAD_CACHE_DIR', os.path.join(settings.PROJECT_ROOT, 'lp-cache'))
        launchpad = Launchpad.login_anonymously('djangopackages.com', 'production', cachedir)
        repo_name = package.repo_name()

        branch = launchpad.branches.getByUrl(url='lp:%s' % repo_name)

        package.repo_description = branch.description or ''
        package.repo_forks = len(branch.project.getBranches())
        package.repo_watchers = len(branch.subscribers)
        package.participants = branch.owner.name

        return package
