Skip to content

Commit

Permalink
Added support for querying pipeline structure by line number
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek RUTHS authored and Derek RUTHS committed Jan 24, 2017
1 parent 48c2934 commit 55b7b1c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
32 changes: 32 additions & 0 deletions xp/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,25 @@ def get_prefix(self):
def abs_path(self):
return os.path.dirname(self.abs_filename)

def get_stmt_from_lineno(self,lineno):
# adjust lineno to internal rep (internal linenos are indexed from zero)
lineno -= 1

# check the preamble
for stmt in self.preamble:
if stmt.lineno == lineno:
return stmt

# check the tasks
for task in self.tasks:
start_lineno = task.lineno
end_lineno = task.get_final_lineno()

if start_lineno <= lineno <= end_lineno:
return task

return None

def initialize(self):

# go through and deal with the extend and use statements
Expand Down Expand Up @@ -459,6 +478,9 @@ def __init__(self,name,dep_names,blocks,source_file,lineno):

self._dependencies = []

def get_final_lineno(self):
return max([b.get_final_lineno() for b in self.blocks])

def copy(self):
blocks = map(lambda x: x.copy(), self.blocks)
return Task(self.name,self.dep_names,blocks,self.source_file,self.lineno)
Expand Down Expand Up @@ -582,6 +604,12 @@ def update_context(self,context,cwd,pipelines):
for s in self.statements:
s.update_context(context,cwd,pipelines)

def get_final_lineno(self):
if len(self.statements) == 0:
return self.lineno
else:
return self.statements[-1].lineno

class CodeBlock:
def __init__(self,lang,arg_str,content,source_file,lineno):
self.lang = lang
Expand All @@ -590,6 +618,10 @@ def __init__(self,lang,arg_str,content,source_file,lineno):
self.source_file = source_file
self.lineno = lineno

def get_final_lineno(self):
# num lines
return self.lineno + len(self.content)

def copy(self):
return CodeBlock(self.lang,self.arg_str,self.content,self.source_file,self.lineno)

Expand Down
1 change: 1 addition & 0 deletions xp/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from tests.blocks import *
from tests.overload import *
from tests.dep_cases import *
from tests.linenos import *

if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
Expand Down
43 changes: 43 additions & 0 deletions xp/tests/linenos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Copyright 2016 Derek Ruths
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import unittest
from xp.pipeline import *
import xp.pipeline as pipeline
import os, os.path
import shutil
import time

BASE_PATH = os.path.dirname(__file__)

def get_complete_filename(fname):
return os.path.join(BASE_PATH,'pipelines',fname)

class LinenosTestCase(unittest.TestCase):

def test_preamble_linenos(self):
ep = get_pipeline(get_complete_filename('ddir_prefix'),
default_prefix=USE_FILE_PREFIX)

var_assign = ep.get_stmt_from_lineno(3)
no_stmt = ep.get_stmt_from_lineno(4)

task = ep.get_stmt_from_lineno(6)

self.assertTrue(isinstance(var_assign,VariableAssignment))
self.assertEquals(no_stmt,None)
self.assertEquals(task.name,'task1')

0 comments on commit 55b7b1c

Please sign in to comment.