rana-cli/wakatime/dependencies/data.py

65 lines
2 KiB
Python
Raw Normal View History

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
from ..compat import u
2014-12-25 05:22:40 +00:00
FILES = {
'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):
state = None
level = 0
2014-12-25 05:22:40 +00:00
def parse(self):
2014-12-25 05:22:40 +00:00
self._process_file_name(os.path.basename(self.source_file))
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():
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'])
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