# -*- coding: utf-8 -*-
"""
    wakatime.languages
    ~~~~~~~~~~~~~~~~~~

    Parse dependencies from a source code file.

    :copyright: (c) 2014 Alan Hamlett.
    :license: BSD, see LICENSE for more details.
"""

import logging

from ..compat import open, import_module


log = logging.getLogger('WakaTime')


class TokenParser(object):
    """The base class for all dependency parsers. To add support for your
    language, inherit from this class and implement the :meth:`parse` method
    to return a list of dependency strings.
    """
    source_file = None
    lexer = None
    dependencies = []
    tokens = []

    def __init__(self, source_file, lexer=None):
        self.source_file = source_file
        self.lexer = lexer

    def parse(self, tokens=[]):
        """ Should return a list of dependencies.
        """
        if not tokens and not self.tokens:
            self.tokens = self._extract_tokens()
        raise Exception('Not yet implemented.')

    def append(self, dep, truncate=True):
        self._save_dependency(dep, truncate=truncate)

    def _extract_tokens(self):
        with open(self.source_file, 'r', encoding='utf-8') as fh:
            return self.lexer.get_tokens_unprocessed(fh.read(512000))

    def _save_dependency(self, dep, truncate=True):
        dep = dep.strip().split('.')[0].strip() if truncate else dep.strip()
        if dep:
            self.dependencies.append(dep)


class DependencyParser(object):
    source_file = None
    lexer = None
    parser = None

    def __init__(self, source_file, lexer):
        self.source_file = source_file
        self.lexer = lexer

        try:
            module_name = self.lexer.__module__.split('.')[-1]
            class_name = self.lexer.__class__.__name__.replace('Lexer', 'Parser', 1)
            module = import_module('.%s' % module_name, package=__package__)
            self.parser = getattr(module, class_name)
        except ImportError as ex:
            log.debug(ex)

    def parse(self):
        if self.parser:
            plugin = self.parser(self.source_file, lexer=self.lexer)
            dependencies = plugin.parse()
            return list(set(dependencies))
        return []

@@ -1087,32 +1087,59 @@
 t_tokens(self):%0A
+        if self.lexer:%0A    
         with ope
@@ -1196,24 +1196,28 @@
             
+    
 return self.
@@ -1261,16 +1261,34 @@
 512000))
+%0A        return %5B%5D
 %0A%0A    de
@@ -1668,24 +1668,51 @@
 er = lexer%0A%0A
+        if self.lexer:%0A    
         try:
@@ -1712,16 +1712,20 @@
    try:%0A
+    
         
@@ -1779,16 +1779,20 @@
 .')%5B-1%5D%0A
+    
         
@@ -1868,16 +1868,20 @@
 er', 1)%0A
+    
         
@@ -1957,24 +1957,28 @@
             
+    
 self.parser 
@@ -2015,16 +2015,20 @@
         
+    
 except I
@@ -2045,16 +2045,20 @@
  as ex:%0A
+    
         

handle unknown language when parsing dependencies