2014-12-23 05:13:11 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2018-03-11 20:29:30 +00:00
|
|
|
wakatime.dependencies.python
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2014-12-23 05:13:11 +00:00
|
|
|
|
|
|
|
Parse dependencies from Python code.
|
|
|
|
|
2014-12-25 06:58:56 +00:00
|
|
|
:copyright: (c) 2014 Alan Hamlett.
|
2014-12-23 05:13:11 +00:00
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from . import TokenParser
|
|
|
|
|
|
|
|
|
|
|
|
class PythonParser(TokenParser):
|
|
|
|
state = None
|
|
|
|
parens = 0
|
|
|
|
nonpackage = False
|
2015-09-26 20:04:35 +00:00
|
|
|
exclude = [
|
|
|
|
r'^os$',
|
2016-06-13 14:41:17 +00:00
|
|
|
r'^sys$',
|
2015-09-26 20:04:35 +00:00
|
|
|
r'^sys\.',
|
2016-08-31 15:30:49 +00:00
|
|
|
r'^__future__$',
|
2015-09-26 20:04:35 +00:00
|
|
|
]
|
2014-12-23 05:13:11 +00:00
|
|
|
|
2015-09-26 20:04:35 +00:00
|
|
|
def parse(self):
|
2014-12-23 05:13:11 +00:00
|
|
|
for index, token, content in self.tokens:
|
|
|
|
self._process_token(token, content)
|
|
|
|
return self.dependencies
|
|
|
|
|
|
|
|
def _process_token(self, token, content):
|
2015-09-16 21:59:30 +00:00
|
|
|
if self.partial(token) == 'Namespace':
|
2014-12-23 05:13:11 +00:00
|
|
|
self._process_namespace(token, content)
|
2015-09-16 21:59:30 +00:00
|
|
|
elif self.partial(token) == 'Operator':
|
2014-12-23 05:13:11 +00:00
|
|
|
self._process_operator(token, content)
|
2015-09-16 21:59:30 +00:00
|
|
|
elif self.partial(token) == 'Punctuation':
|
2014-12-23 05:13:11 +00:00
|
|
|
self._process_punctuation(token, content)
|
2015-09-16 21:59:30 +00:00
|
|
|
elif self.partial(token) == 'Text':
|
2014-12-23 05:13:11 +00:00
|
|
|
self._process_text(token, content)
|
|
|
|
else:
|
|
|
|
self._process_other(token, content)
|
|
|
|
|
|
|
|
def _process_namespace(self, token, content):
|
|
|
|
if self.state is None:
|
|
|
|
self.state = content
|
|
|
|
else:
|
2014-12-25 05:09:15 +00:00
|
|
|
if content == 'as':
|
|
|
|
self.nonpackage = True
|
|
|
|
else:
|
|
|
|
self._process_import(token, content)
|
2014-12-23 05:13:11 +00:00
|
|
|
|
|
|
|
def _process_operator(self, token, content):
|
2016-08-31 15:13:25 +00:00
|
|
|
pass
|
2014-12-23 05:13:11 +00:00
|
|
|
|
|
|
|
def _process_punctuation(self, token, content):
|
|
|
|
if content == '(':
|
|
|
|
self.parens += 1
|
|
|
|
elif content == ')':
|
|
|
|
self.parens -= 1
|
|
|
|
self.nonpackage = False
|
|
|
|
|
|
|
|
def _process_text(self, token, content):
|
|
|
|
if self.state is not None:
|
|
|
|
if content == "\n" and self.parens == 0:
|
|
|
|
self.state = None
|
|
|
|
self.nonpackage = False
|
|
|
|
|
|
|
|
def _process_other(self, token, content):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _process_import(self, token, content):
|
2014-12-25 05:09:15 +00:00
|
|
|
if not self.nonpackage:
|
|
|
|
if self.state == 'from':
|
2015-09-16 19:59:52 +00:00
|
|
|
self.append(content, truncate=True, truncate_to=1)
|
2014-12-25 05:09:15 +00:00
|
|
|
self.state = 'from-2'
|
|
|
|
elif self.state == 'import':
|
2015-09-16 19:59:52 +00:00
|
|
|
self.append(content, truncate=True, truncate_to=1)
|
2014-12-25 05:09:15 +00:00
|
|
|
self.state = 'import-2'
|
|
|
|
elif self.state == 'import-2':
|
2015-09-16 19:59:52 +00:00
|
|
|
self.append(content, truncate=True, truncate_to=1)
|
2014-12-25 05:09:15 +00:00
|
|
|
else:
|
|
|
|
self.state = None
|
2014-12-23 05:13:11 +00:00
|
|
|
self.nonpackage = False
|