2014-12-25 05:22:40 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2018-03-11 20:29:30 +00:00
|
|
|
wakatime.dependencies.data
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2014-12-25 05:22:40 +00:00
|
|
|
|
|
|
|
Parse dependencies from data files.
|
|
|
|
|
|
|
|
:copyright: (c) 2014 Alan Hamlett.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from . import TokenParser
|
2014-12-25 06:58:56 +00:00
|
|
|
from ..compat import u
|
2014-12-25 05:22:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
FILES = {
|
2014-12-25 06:58:56 +00:00
|
|
|
'bower.json': {'exact': True, 'dependency': 'bower'},
|
|
|
|
'component.json': {'exact': True, 'dependency': 'bower'},
|
|
|
|
'package.json': {'exact': True, 'dependency': 'npm'},
|
2014-12-25 05:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class JsonParser(TokenParser):
|
2014-12-25 06:58:56 +00:00
|
|
|
state = None
|
|
|
|
level = 0
|
2014-12-25 05:22:40 +00:00
|
|
|
|
2015-09-26 20:04:35 +00:00
|
|
|
def parse(self):
|
2014-12-25 05:22:40 +00:00
|
|
|
self._process_file_name(os.path.basename(self.source_file))
|
2014-12-25 06:58:56 +00:00
|
|
|
for index, token, content in self.tokens:
|
|
|
|
self._process_token(token, content)
|
2014-12-25 05:22:40 +00:00
|
|
|
return self.dependencies
|
|
|
|
|
|
|
|
def _process_file_name(self, file_name):
|
|
|
|
for key, value in FILES.items():
|
2014-12-25 06:58:56 +00:00
|
|
|
found = (key == file_name) if value.get('exact') else (key.lower() in file_name.lower())
|
2014-12-25 05:22:40 +00:00
|
|
|
if found:
|
|
|
|
self.append(value['dependency'])
|
2014-12-25 06:58:56 +00:00
|
|
|
|
|
|
|
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
|