dbmanager.py

This module contains operations to manipulate the database

Warning: since it involves manipulating source code, some operations may not be very reliable.

Additionaly, we currently do not have tests for this file. Thus, my suggestion is to use only the insert() and set_attribute() functions. These functions were heavily used during my snowballing.

For other operations, such as removing citations/work, I recommend editting files manually.

Relevant functions

insert

insert(text, citations=None, ratio=0.9, dry_run=False)[source]

Insert text that might contain works and citations.

Arguments:

  • text – code with work and citations

Keyword arguments:

  • citations – citations filename
  • ratio – comparison threshold for existing work
  • dry_run – do not apply changes to the database

Example:

insert('''
pimentel2016a = DB(WorkSnowball(
    2016, "Tracking and analyzing the evolution of provenance from scripts",
    display="noworkflow a",
    authors="Pimentel, João Felipe and Freire, Juliana and Braganholo, Vanessa and Murta, Leonardo",
    place=IPAW,
    pp="16--28",
    entrytype="inproceedings",
))

DB(Citation(
    pimentel2016a, murta2014a, ref="[14]",
    contexts=[
    ],
))
''', citations="murta2014a");

set_attribute

set_attribute(varname, field, value, year=None, dry_run=False, old=None)[source]

Set attribute for work

Arguments:

  • varname – work variable name
  • field – work attribute name
  • value – new value for attribute

Keyword arguments:

  • year – limit work search for specific year
  • dry_run – do not apply changes to the database

Example:

set_attribute('murta2014a', "display", "now");

rename_work

rename_work(original_name, new_name, year=None, new_year=None, citations=True, dry_run=False)[source]

Rename work

insert_work

insert_work(varname, name, text, year=None, ratio=0.9, dry_run=False)[source]

Insert work by :text in file :name

insert_citation

insert_citation(name, text, dry_run=False)[source]

Insert citation by :text in file :name

remove_target_citation

remove_target_citation(name, varname, year=None, dry_run=False)[source]

Remove citation where the cited is :varname

remove_source_citation

remove_source_citation(name, varname, year=None, dry_run=False)[source]

Remove citation where the citer is :varname

Other Functions

rename_lines

rename_lines(filename, lines, sep, original_name, new_name, year=None, new_year=None, citations=True, dry_run=False)[source]

Rename work in year and citation files

rename_citation

rename_citation(name, original_varname, new_varname, year=None, new_year=None, dry_run=False)[source]

Rename citation varname

citation_operation

citation_operation(filename, lines, varname, year, operation, value='')[source]

Apply :operation for :varname in a citation file :filename

work_operation

work_operation(filename, lines, varname, operation, value='')[source]

Apply :operation for :varname in a year file :filename

save_lines

save_lines(filename, lines, sep='\n')[source]

Write python file with utf-8

read_file

read_file(filename)[source]

Read python file with the right codec

is_assign_to_name

is_assign_to_name(stmt)[source]

Check if stmt is an assignment to name

is_call_statement

is_call_statement(stmt)[source]

Check if stmt is a call expr

Operation classes

ReplaceOperation

class ReplaceOperation(target)[source]

Bases: object

Operation for replacing .target lines by :value

apply(lines, value)[source]

Replace lines of :target by :value

DelOperation

class DelOperation(keyword, delete_lines=False)[source]

Bases: object

Operation for removing .keyword

apply(lines, value)[source]

Remove .keyword from :lines

AddKeywordOperation

class AddKeywordOperation(attribute, last_line)[source]

Bases: object

Operation for adding :attribute

apply(lines, value)[source]

Add .attribute = :value into :lines

InsertOperation

class InsertOperation(last_line, last=False, add_line=True)[source]

Bases: object

Operation for inserting values in a given line

apply(lines, value)[source]

Insert :value at line .last_line

DetectOperation

class DetectOperation[source]

Bases: object

Operation for detecting elements

apply(lines, value)[source]

Detect elements

Visitor classes

EditVisitor

class EditVisitor(lines, varname, operation='rename')[source]

Bases: ast.NodeVisitor

Visitor for editing attributes of a Work identified by .varname

add_keyword(attribute, last_line)[source]

Instantiate an operation to add keyword

remove_keyword(keyword, remove_lines)[source]

Instantiate an operation to remove a keyword

replace(node)[source]

Instantiate a replace operation

visit_Assign(node)[source]

Visits assign and check if it represents the desired .varname

Then, instantiate and apply the desired .operation

BodyVisitor

class BodyVisitor(lines, varname, operation='delete')[source]

Bases: ast.NodeVisitor

Visit body of year file to find .varname and apply .operation

insert(line, last)[source]

Instantiate an operation to insert code

process_body(body)[source]

Finds desired varnames in body and instantiate operation

remove_stmt(stmt)[source]

Instantiate an operation to remove the stmt

visit_Interactive(node)[source]

Process body of Interactive node

visit_Module(node)[source]

Process body of Module node

visit_Suite(node)[source]

Processes body of Suite node

CitationVisitor

class CitationVisitor(lines, varname, year, operation='remove import')[source]

Bases: ast.NodeVisitor

Visit body of citation file to apply operation

process_body(body)[source]

Find citation/import and applies the desired .operation

visit_Interactive(node)[source]

Process body of Interactive node

visit_Module(node)[source]

Process body of Module node

visit_Suite(node)[source]

Processes body of Suite node