Skip to content
This repository has been archived by the owner on Oct 20, 2022. It is now read-only.

Commit

Permalink
Rename exporter to indexer. Add dummy SQLite target writer.
Browse files Browse the repository at this point in the history
  • Loading branch information
efritz committed Jul 29, 2019
1 parent 86cfd8c commit 55b469b
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 52 deletions.
2 changes: 1 addition & 1 deletion lsif-py
@@ -1,6 +1,6 @@
#!/usr/bin/env python

from lsif_exporter.script import main
from lsif_indexer.script import main

if __name__ == '__main__':
main()
33 changes: 0 additions & 33 deletions lsif_exporter/export.py

This file was deleted.

File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lsif_exporter/consts.py → lsif_indexer/consts.py
@@ -1,4 +1,4 @@
EXPORTER_VERSION = '0.1.0'
INDEXER_VERSION = '0.1.0'
POSITION_ENCODING = 'utf-16'
PROTOCOL_VERSION = '0.4.0'

Expand Down
36 changes: 30 additions & 6 deletions lsif_exporter/emitter.py → lsif_indexer/emitter.py
Expand Up @@ -3,12 +3,12 @@

class Emitter:
"""
Emitter writes LSIF-dump data to the given file writer. There are
Emitter writes LSIF-dump data to the given writer. The location to
which dump data is written depends on the given writer. There are
convenience methods to generate unique vertex and edge identifiers
and map positional arguments ot the correct names for the label type.
The majority of the methods in this class definition are added
dynamically via setattr (below).
and map positional arguments ot the correct names for the label
type. The majority of the methods in this class definition are
added dynamically via setattr (below).
"""
def __init__(self, writer):
self.writer = writer
Expand All @@ -22,10 +22,33 @@ def emit(self, **kwargs):
"""
node_id = str(self._lines + 1)
self._lines += 1
self.writer.write(json.dumps({'id': node_id, **kwargs}) + '\n')
self.writer.write({'id': node_id, **kwargs})
return node_id


class FileWriter:
"""
FileWriter writes LSIF-dump data to the given file.
"""
def __init__(self, file):
self.file = file

def write(self, data):
self.file.write(json.dumps(data) + '\n')


class DBWriter:
"""
DBWriter writes LSIF-dump data into a SQLite database.
"""
def __init__(self):
pass

def write(self, data):
# TODO(efritz) - implement
raise RuntimeError('Unimplemented')


# A map from vertex labels to the fields they support. Fields
# are ordered based on their positional argument construction.
VERTEX_FIELDS = {
Expand Down Expand Up @@ -75,5 +98,6 @@ def emitter(self, *args):
make_emitter(type_name, name, fields),
)


# Meta-construct the Emitter class
add_emitters()
41 changes: 36 additions & 5 deletions lsif_exporter/file_export.py → lsif_indexer/index.py
Expand Up @@ -2,7 +2,11 @@
import os

from .analysis import get_names
from .consts import INDENT, MAX_HIGHLIGHT_RANGE
from .emitter import Emitter, FileWriter
from .consts import (
INDENT, MAX_HIGHLIGHT_RANGE,
POSITION_ENCODING, PROTOCOL_VERSION,
)


class DefinitionMeta:
Expand All @@ -18,12 +22,12 @@ def __init__(self, range_id, result_set_id, contents):
self.reference_range_ids = set()


class FileExporter:
class FileIndexer:
"""
Analysis the definitions and uses in the given file and
add an LSIF document to the emitter. As analysis is done
on a per-file basis, this class holds the majority of the
exporter logic.
indexer logic.
"""
def __init__(self, filename, emitter, project_id, verbose):
self.filename = filename
Expand All @@ -32,8 +36,8 @@ def __init__(self, filename, emitter, project_id, verbose):
self.verbose = verbose
self.definition_metas = {}

def export(self):
print('Analyzing file {}'.format(self.filename))
def index(self):
print('Indexing file {}'.format(self.filename))

with open(self.filename) as f:
source = f.read()
Expand Down Expand Up @@ -239,6 +243,33 @@ def _debug_use(self, name, definition):
))


def index(workspace, writer, verbose):
"""
Read each python file (recursively) in the given path and
write the analysis of each source file as an LSIF-dump to
the given file writer.
"""
uri = 'file://{}'.format(os.path.abspath(workspace))

emitter = Emitter(FileWriter(writer))
emitter.emit_metadata(PROTOCOL_VERSION, POSITION_ENCODING, uri)
project_id = emitter.emit_project('py')

file_count = 0
for root, dirs, files in os.walk(workspace):
for file in files:
_, ext = os.path.splitext(file)
if ext != '.py':
continue

file_count += 1
path = os.path.join(root, file)
FileIndexer(path, emitter, project_id, verbose).index()

if file_count == 0:
print('No files found to index')


def make_ranges(name):
"""
Return a start and end range values for a range vertex.
Expand Down
12 changes: 6 additions & 6 deletions lsif_exporter/script.py → lsif_indexer/script.py
@@ -1,27 +1,27 @@
import argparse
import time

from .consts import (EXPORTER_VERSION, PROTOCOL_VERSION)
from .export import export
from .consts import (INDEXER_VERSION, PROTOCOL_VERSION)
from .index import index


def main():
args = parse_args()
start = time.time()

with open(args.o, 'w+') as f:
export(args.workspace, f, args.verbose)
index(args.workspace, f, args.verbose)

print('\nProcessed in {0:.2f}ms'.format((time.time() - start) * 1000))


def parse_args():
parser = argparse.ArgumentParser(description='lsif-py is an LSIF exporter for Python.')
parser = argparse.ArgumentParser(description='lsif-py is an LSIF indexer for Python.')
parser.add_argument('workspace', help='set the path to the code, current directory by default')
parser.add_argument('-o', help='change the output file, "data.lsif" by default', default='data.lsif')
parser.add_argument('-v', '--verbose', action='store_true', help='Output verbose logs', default=False)
parser.add_argument('--version', action='version', version='Go LSIF exporter: {}, Protocol version: {}'.format(
EXPORTER_VERSION,
parser.add_argument('--version', action='version', version='Go LSIF indexer: {}, Protocol version: {}'.format(
INDEXER_VERSION,
PROTOCOL_VERSION,
))

Expand Down

0 comments on commit 55b469b

Please sign in to comment.