parse bower and npm dependencies. parse dependencies from javascript files.
This commit is contained in:
parent
50a0e9efcb
commit
166f41899c
5 changed files with 176 additions and 11 deletions
|
@ -83,16 +83,20 @@ class DependencyParser(object):
|
|||
self.lexer = lexer
|
||||
|
||||
if self.lexer:
|
||||
module_name = self.lexer.__module__.split('.')[-1]
|
||||
class_name = self.lexer.__class__.__name__.replace('Lexer', 'Parser', 1)
|
||||
else:
|
||||
module_name = 'unknown'
|
||||
class_name = 'UnknownParser'
|
||||
|
||||
try:
|
||||
module = import_module('.%s' % module_name, package=__package__)
|
||||
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__)
|
||||
try:
|
||||
self.parser = getattr(module, class_name)
|
||||
except AttributeError:
|
||||
log.debug(traceback.format_exc())
|
||||
except ImportError:
|
||||
self.parser = getattr(module, class_name)
|
||||
except AttributeError:
|
||||
log.debug(traceback.format_exc())
|
||||
except ImportError:
|
||||
log.debug(traceback.format_exc())
|
||||
|
||||
def parse(self):
|
||||
if self.parser:
|
||||
|
|
|
@ -12,21 +12,55 @@
|
|||
import os
|
||||
|
||||
from . import TokenParser
|
||||
from ..compat import u
|
||||
|
||||
|
||||
FILES = {
|
||||
'bower': {'exact': False, 'dependency': 'bower'},
|
||||
'bower.json': {'exact': True, 'dependency': 'bower'},
|
||||
'component.json': {'exact': True, 'dependency': 'bower'},
|
||||
'package.json': {'exact': True, 'dependency': 'npm'},
|
||||
}
|
||||
|
||||
|
||||
class JsonParser(TokenParser):
|
||||
state = None
|
||||
level = 0
|
||||
|
||||
def parse(self, tokens=[]):
|
||||
self._process_file_name(os.path.basename(self.source_file))
|
||||
if not tokens and not self.tokens:
|
||||
self.tokens = self._extract_tokens()
|
||||
for index, token, content in self.tokens:
|
||||
self._process_token(token, content)
|
||||
return self.dependencies
|
||||
|
||||
def _process_file_name(self, file_name):
|
||||
for key, value in FILES.items():
|
||||
found = (key == file_name) if value.get('exact') else (key in file_name)
|
||||
found = (key == file_name) if value.get('exact') else (key.lower() in file_name.lower())
|
||||
if found:
|
||||
self.append(value['dependency'])
|
||||
|
||||
def _process_token(self, token, content):
|
||||
if u(token) == 'Token.Name.Tag':
|
||||
self._process_tag(token, content)
|
||||
elif u(token) == 'Token.Literal.String.Single' or u(token) == 'Token.Literal.String.Double':
|
||||
self._process_literal_string(token, content)
|
||||
elif u(token) == 'Token.Punctuation':
|
||||
self._process_punctuation(token, content)
|
||||
|
||||
def _process_tag(self, token, content):
|
||||
if content.strip('"').strip("'") == 'dependencies' or content.strip('"').strip("'") == 'devDependencies':
|
||||
self.state = 'dependencies'
|
||||
elif self.state == 'dependencies' and self.level == 2:
|
||||
self.append(content.strip('"').strip("'"))
|
||||
|
||||
def _process_literal_string(self, token, content):
|
||||
pass
|
||||
|
||||
def _process_punctuation(self, token, content):
|
||||
if content == '{':
|
||||
self.level += 1
|
||||
elif content == '}':
|
||||
self.level -= 1
|
||||
if self.state is not None and self.level <= 1:
|
||||
self.state = None
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
Parse dependencies from Python code.
|
||||
|
||||
:copyright: (c) 2013 Alan Hamlett.
|
||||
:copyright: (c) 2014 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
|
|
93
wakatime/languages/templates.py
Normal file
93
wakatime/languages/templates.py
Normal file
|
@ -0,0 +1,93 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
wakatime.languages.templates
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Parse dependencies from Templates.
|
||||
|
||||
:copyright: (c) 2014 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
from . import TokenParser
|
||||
from ..compat import u
|
||||
|
||||
|
||||
""" If these keywords are found in the source file, treat them as a dependency.
|
||||
Must be lower-case strings.
|
||||
"""
|
||||
KEYWORDS = [
|
||||
'_',
|
||||
'$',
|
||||
'angular',
|
||||
'assert', # probably mocha
|
||||
'backbone',
|
||||
'batman',
|
||||
'c3',
|
||||
'can',
|
||||
'casper',
|
||||
'chai',
|
||||
'chaplin',
|
||||
'd3',
|
||||
'define', # probably require
|
||||
'describe', # mocha or jasmine
|
||||
'eco',
|
||||
'ember',
|
||||
'espresso',
|
||||
'expect', # probably jasmine
|
||||
'exports', # probably npm
|
||||
'express',
|
||||
'gulp',
|
||||
'handlebars',
|
||||
'highcharts',
|
||||
'jasmine',
|
||||
'jquery',
|
||||
'jstz',
|
||||
'ko', # probably knockout
|
||||
'm', # probably mithril
|
||||
'marionette',
|
||||
'meteor',
|
||||
'moment',
|
||||
'monitorio',
|
||||
'mustache',
|
||||
'phantom',
|
||||
'pickadate',
|
||||
'pikaday',
|
||||
'qunit',
|
||||
'react',
|
||||
'reactive',
|
||||
'require', # probably the commonjs spec
|
||||
'ripple',
|
||||
'rivets',
|
||||
'socketio',
|
||||
'spine',
|
||||
'thorax',
|
||||
'underscore',
|
||||
'vue',
|
||||
'way',
|
||||
'zombie',
|
||||
]
|
||||
|
||||
|
||||
class LassoJavascriptParser(TokenParser):
|
||||
|
||||
def parse(self, tokens=[]):
|
||||
if not tokens and not self.tokens:
|
||||
self.tokens = self._extract_tokens()
|
||||
for index, token, content in self.tokens:
|
||||
self._process_token(token, content)
|
||||
return self.dependencies
|
||||
|
||||
def _process_token(self, token, content):
|
||||
if u(token) == 'Token.Name.Other':
|
||||
self._process_name(token, content)
|
||||
elif u(token) == 'Token.Literal.String.Single' or u(token) == 'Token.Literal.String.Double':
|
||||
self._process_literal_string(token, content)
|
||||
|
||||
def _process_name(self, token, content):
|
||||
if content.lower() in KEYWORDS:
|
||||
self.append(content.lower())
|
||||
|
||||
def _process_literal_string(self, token, content):
|
||||
if 'famous/core/' in content.strip('"').strip("'"):
|
||||
self.append('famous')
|
34
wakatime/languages/unknown.py
Normal file
34
wakatime/languages/unknown.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
wakatime.languages.unknown
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Parse dependencies from files of unknown language.
|
||||
|
||||
:copyright: (c) 2014 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from . import TokenParser
|
||||
from ..compat import u
|
||||
|
||||
|
||||
FILES = {
|
||||
'bower': {'exact': False, 'dependency': 'bower'},
|
||||
'grunt': {'exact': False, 'dependency': 'grunt'},
|
||||
}
|
||||
|
||||
|
||||
class UnknownParser(TokenParser):
|
||||
|
||||
def parse(self, tokens=[]):
|
||||
self._process_file_name(os.path.basename(self.source_file))
|
||||
return self.dependencies
|
||||
|
||||
def _process_file_name(self, file_name):
|
||||
for key, value in FILES.items():
|
||||
found = (key == file_name) if value.get('exact') else (key.lower() in file_name.lower())
|
||||
if found:
|
||||
self.append(value['dependency'])
|
Loading…
Reference in a new issue