# -*- coding: utf-8 -*-
__author__ = 'nickl-'
from base import BaseAdapter
from subprocess import Popen
from subprocess import PIPE
from string import strip
class Npm(BaseAdapter):
    adapter_command = 'npm'

    def search(self, query):
        response = Popen(self.adapter_command + ' search -q ' + query, shell=True, stdout=PIPE).communicate()[0]
        lst = list(self.__parse_search(line) for line in response.splitlines() if ' - ' in line)
        if lst:
            return dict(lst)
        return {}

    def __parse_search(self, result):
        return map(strip, (self.adapter_command + ':' + result).split(' - ', 1))

    def install(self, query):
        print '\n'
        Popen(self.adapter_command + ' install ' + query, shell=True).wait()
        return {}

@@ -150,16 +150,42 @@
 t strip%0A
+from re import match, sub%0A
 class Np
@@ -458,20 +458,105 @@
 if '
- - ' in line
+npm http' not in line and not bool(match('%5ENAME%5Cs+DESCRIPTION%5Cs+AUTHOR%5Cs+DATE%5Cs+KEYWORDS', line))
 )%0A  
@@ -593,19 +593,48 @@
 rn dict(
-lst
+%5B(k,v) for k,v in lst if k != 0%5D
 )%0A      
@@ -697,78 +697,470 @@
    r
-eturn map(strip, (self.adapter_command + ':' + result).split(' - ', 1)
+ = match('%5E(%5B%5Cw-%5D*)%5Cs+(%5Cw.*)=(.+)%5Cs+(%5Cd%5Cd%5Cd%5Cd%5B%5Cd%5C-%5C: %5D*)%5Cs+(%5Cw.*)$', result)%0A        if r and len(r.groups()) == 5:%0A            r = map(strip, list(r.groups()))%0A            pkg = ''.join(%5Bself.adapter_command,':', r.pop(0)%5D)%0A            r%5B1%5D = ' '.join(('Author:', r%5B1%5D, r%5B2%5D))%0A            r%5B2%5D = r%5B0%5D%0A            r%5B3%5D = 'Tags: ' + ', '.join(r%5B3%5D.split(' '))%0A            del r%5B0%5D%0A            desc = '%5Cn'.join(r)%0A            return (pkg, desc)%0A        return (0,0
 )%0A%0A 

Implement npm search result parcing