Пример #1
0
def load_relation(filename: str, defname:Optional[str]=None) -> Optional[str]:
    '''
    Loads a relation into the set. Defname is the given name
    to the relation.

    Returns the name to the relation, or None if it was
    not loaded.
    '''
    if not os.path.isfile(filename):
        print(colorize(
            "%s is not a file" % filename, ERROR_COLOR), file=sys.stderr)
        return None

    if defname is None:
        f = filename.split('/')
        defname = f[-1].lower()
        if defname.endswith(".csv"):  # removes the extension
            defname = defname[:-4]

    if not rtypes.is_valid_relation_name(defname):
        print(colorize(
            "%s is not a valid relation name" % defname, ERROR_COLOR), file=sys.stderr)
        return None
    try:
        relations[defname] = relation.relation(filename)

        completer.add_completion(defname)
        printtty(colorize("Loaded relation %s" % defname, COLOR_GREEN))
        return defname
    except Exception as e:
        print(colorize(str(e), ERROR_COLOR), file=sys.stderr)
        return None
Пример #2
0
    def loadRelation(self, filenames=None):
        '''Loads a relation. Without parameters it will ask the user which relation to load,
        otherwise it will load filename, giving it name.
        It shouldn't be called giving filename but not giving name.'''
        # Asking for file to load
        if not filenames:
            f = QtWidgets.QFileDialog.getOpenFileNames(self, QtWidgets.QApplication.translate(
                "Form", "Load Relation"), "", QtWidgets.QApplication.translate("Form", "Relations (*.csv);;Text Files (*.txt);;All Files (*)"))
            filenames = f[0]

        for f in filenames:
            # Default relation's name
            name = os.path.basename(f).lower()

            if len(name) == 0:
                return

            if (name.endswith(".csv")):  # removes the extension
                name = name[:-4]

            if not rtypes.is_valid_relation_name(name):
                name = self.promptRelationName()

            if name is None:
                continue

            try:
                self.relations[name] = relation.relation(f)
            except Exception as e:
                print (e)
                QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate("Form", "Error"), "%s\n%s" %
                                              (QtWidgets.QApplication.translate("Form", "Check your query!"), e.__str__()))
                continue

        self.updateRelations()
Пример #3
0
def load_relation(filename, defname=None):
    if not os.path.isfile(filename):
        print (colorize(
            "%s is not a file" % filename, ERROR_COLOR), file=sys.stderr)
        return None

    f = filename.split('/')
    if defname == None:
        defname = f[len(f) - 1].lower()
        if defname.endswith(".csv"):  # removes the extension
            defname = defname[:-4]

    if not rtypes.is_valid_relation_name(defname):
        print (colorize(
            "%s is not a valid relation name" % defname, ERROR_COLOR), file=sys.stderr)
        return
    try:
        relations[defname] = relation.relation(filename)

        completer.add_completion(defname)
        printtty(colorize("Loaded relation %s" % defname, 0x00ff00))
        return defname
    except Exception as e:
        print (colorize(e, ERROR_COLOR), file=sys.stderr)
        return None
Пример #4
0
def load_relation(filename: str,
                  defname: Optional[str] = None) -> Optional[str]:
    '''
    Loads a relation into the set. Defname is the given name
    to the relation.

    Returns the name to the relation, or None if it was
    not loaded.
    '''
    if not os.path.isfile(filename):
        print(colorize("%s is not a file" % filename, ERROR_COLOR),
              file=sys.stderr)
        return None

    if defname is None:
        f = filename.split('/')
        defname = f[-1].lower()
        if defname.endswith(".csv"):  # removes the extension
            defname = defname[:-4]

    if not rtypes.is_valid_relation_name(defname):
        print(colorize("%s is not a valid relation name" % defname,
                       ERROR_COLOR),
              file=sys.stderr)
        return None
    try:
        relations[defname] = relation.relation(filename)

        completer.add_completion(defname)
        printtty(colorize("Loaded relation %s" % defname, COLOR_GREEN))
        return defname
    except Exception as e:
        print(colorize(str(e), ERROR_COLOR), file=sys.stderr)
        return None
Пример #5
0
def load_relation(filename, defname=None):
    if not os.path.isfile(filename):
        print (colorize(
            "%s is not a file" % filename, ERROR_COLOR), file=sys.stderr)
        return None

    f = filename.split('/')
    if defname == None:
        defname = f[len(f) - 1].lower()
        if defname.endswith(".csv"):  # removes the extension
            defname = defname[:-4]

    if not rtypes.is_valid_relation_name(defname):
        print (colorize(
            "%s is not a valid relation name" % defname, ERROR_COLOR), file=sys.stderr)
        return
    try:
        relations[defname] = relation.relation(filename)

        completer.add_completion(defname)
        printtty(colorize("Loaded relation %s" % defname, 0x00ff00))
        return defname
    except Exception as e:
        print (colorize(e, ERROR_COLOR), file=sys.stderr)
        return None
Пример #6
0
def run_test(testname):
    '''Runs a specific test executing the file
    testname.query
    and comparing the result with
    testname.result
    The query will be executed both unoptimized and
    optimized'''
    print("Running test: " + colorize(testname, COLOR_MAGENTA))

    query = None
    expr = None
    o_query = None
    o_expr = None
    result_rel = None
    result = None
    o_result = None

    try:
        result_rel = relation.relation('%s%s.result' % (tests_path, testname))

        query = readfile('%s%s.query' % (tests_path, testname)).strip()
        o_query = optimizer.optimize_all(query, rels)

        expr = parser.parse(query)
        result = expr(rels)

        o_expr = parser.parse(o_query)
        o_result = o_expr(rels)

        c_expr = parser.tree(query).toCode()
        c_result = eval(c_expr, rels)

        if (o_result == result_rel) and (result
                                         == result_rel) and (c_result
                                                             == result_rel):
            print(colorize('Test passed', COLOR_GREEN))
            return True
    except Exception as inst:
        traceback.print_exc(file=sys.stdout)
        print(inst)
        pass
    print(colorize('ERROR', COLOR_RED))
    print("Query: %s -> %s" % (query, expr))
    print("Optimized query: %s -> %s" % (o_query, o_expr))
    print(colorize('=====================================', COLOR_RED))
    print(colorize("Expected result", COLOR_GREEN))
    print(result_rel)
    print(colorize("Result", COLOR_RED))
    print(result)
    print(colorize("Optimized result", COLOR_RED))
    print(o_result)
    print(
        colorize("optimized result match %s" % str(result_rel == o_result),
                 COLOR_MAGENTA))
    print(
        colorize("result match %s" % str(result == result_rel), COLOR_MAGENTA))
    print(colorize('=====================================', COLOR_RED))
    return False
Пример #7
0
def run_test(testname):
    '''Runs a specific test executing the file
    testname.query
    and comparing the result with
    testname.result
    The query will be executed both unoptimized and
    optimized'''
    print "Running test: " + colorize(testname, COLOR_MAGENTA)

    query = None
    expr = None
    o_query = None
    o_expr = None
    result_rel = None
    result = None
    o_result = None

    try:
        result_rel = relation.relation('%s%s.result' % (tests_path, testname))

        query = unicode(
            readfile('%s%s.query' % (tests_path, testname)).strip(), 'utf8')
        o_query = optimizer.optimize_all(query, rels)

        expr = parser.parse(query)  # Converting expression to python string
        result = eval(expr, rels)  # Evaluating the expression

        o_expr = parser.parse(
            o_query)  # Converting expression to python string
        o_result = eval(o_expr, rels)  # Evaluating the expression

        c_expr = parser.tree(query).toCode()  # Converting to python code
        c_result = eval(c_expr, rels)

        if (o_result == result_rel) and (result
                                         == result_rel) and (c_result
                                                             == result_rel):
            print colorize('Test passed', COLOR_GREEN)
            return True
    except Exception as inst:
        print inst
        pass
    print colorize('ERROR', COLOR_RED)
    print "Query: %s -> %s" % (query, expr)
    print "Optimized query: %s -> %s" % (o_query, o_expr)
    print colorize('=====================================', COLOR_RED)
    print colorize("Expected result", COLOR_GREEN)
    print result_rel
    print colorize("Result", COLOR_RED)
    print result
    print colorize("Optimized result", COLOR_RED)
    print o_result
    print colorize("optimized result match %s" % str(result_rel == o_result),
                   COLOR_MAGENTA)
    print colorize("result match %s" % str(result == result_rel),
                   COLOR_MAGENTA)
    print colorize('=====================================', COLOR_RED)
    return False
Пример #8
0
def run_test(testname):
    '''Runs a specific test executing the file
    testname.query
    and comparing the result with
    testname.result
    The query will be executed both unoptimized and
    optimized'''
    print ("Running test: " + colorize(testname, COLOR_MAGENTA))

    query = None
    expr = None
    o_query = None
    o_expr = None
    result_rel = None
    result = None
    o_result = None

    try:
        result_rel = relation.relation('%s%s.result' % (tests_path, testname))

        query = readfile('%s%s.query' % (tests_path, testname)).strip()
        o_query = optimizer.optimize_all(query, rels)

        expr = parser.parse(query)  # Converting expression to python string
        result = eval(expr, rels)  # Evaluating the expression

        o_expr = parser.parse(
            o_query)  # Converting expression to python string
        o_result = eval(o_expr, rels)  # Evaluating the expression

        c_expr = parser.tree(query).toCode()  # Converting to python code
        c_result = eval(c_expr, rels)

        if (o_result == result_rel) and (result == result_rel) and (c_result == result_rel):
            print (colorize('Test passed', COLOR_GREEN))
            return True
    except Exception as inst:
        print (inst)
        pass
    print (colorize('ERROR', COLOR_RED))
    print ("Query: %s -> %s" % (query, expr))
    print ("Optimized query: %s -> %s" % (o_query, o_expr))
    print (colorize('=====================================', COLOR_RED))
    print (colorize("Expected result", COLOR_GREEN))
    print (result_rel)
    print (colorize("Result", COLOR_RED))
    print (result)
    print (colorize("Optimized result", COLOR_RED))
    print (o_result)
    print (colorize("optimized result match %s" %
           str(result_rel == o_result), COLOR_MAGENTA))
    print (colorize("result match %s" %
           str(result == result_rel), COLOR_MAGENTA))
    print (colorize('=====================================', COLOR_RED))
    return False
Пример #9
0
def run_test(testname):
    '''Runs a specific test executing the file
    testname.query
    and comparing the result with 
    testname.result
    The query will be executed both unoptimized and
    optimized'''
    print "Running test: " + colored(testname, 'magenta')

    query = None
    expr = None
    o_query = None
    o_expr = None
    result_rel = None
    result = None
    o_result = None

    try:
        result_rel = relation.relation('%s%s.result' % (tests_path, testname))

        query = readfile('%s%s.query' % (tests_path, testname)).strip()
        query = unicode(query, 'utf-8')

        expr = parser.parse(query)  #Converting expression to python code

        o_query = optimizer.optimize_all(query, rels)

        result = eval(expr, rels)  #Evaluating the expression

        o_expr = parser.parse(o_query)  #Converting expression to python code
        o_result = eval(o_expr, rels)  #Evaluating the expression

        if (o_result == result_rel) and (result == result_rel):
            print colored('Test passed', 'green')
            return True
    except Exception as inst:
        print inst, "------"
        pass
    print colored('ERROR', 'red')
    print "Query: %s -> %s" % (query, expr)
    print "Optimized query: %s -> %s" % (o_query, o_expr)
    print colored('=====================================', 'red')
    print "\033[33;1mExpected result\033[0m"
    print result_rel
    print "\033[33;1mResult\033[0m"
    print result
    print "\033[33;1mOptimized result\033[0m"
    print o_result
    print "\033[33;1moptimized result match\033[0m", result_rel == o_result
    print "\033[33;1mresult match          \033[0m", result == result_rel
    print colored('=====================================', 'red')
    return False
Пример #10
0
    def loadRelation(self, filename=None, name=None):
        '''Loads a relation. Without parameters it will ask the user which relation to load,
        otherwise it will load filename, giving it name.
        It shouldn't be called giving filename but not giving name.'''
        #Asking for file to load
        if filename == None:
            filename = QtGui.QFileDialog.getOpenFileName(
                self, QtGui.QApplication.translate("Form", "Load Relation"),
                "",
                QtGui.QApplication.translate(
                    "Form",
                    "Relations (*.csv);;Text Files (*.txt);;All Files (*)"))
            filename = compatibility.get_filename(filename)

        #Default relation's name
        f = filename.split('/')  #Split the full path
        defname = f[len(f) - 1].lower()  #Takes only the lowercase filename

        if len(defname) == 0:
            return

        if (defname.endswith(".csv")):  #removes the extension
            defname = defname[:-4]

        if name == None:  #Prompt dialog to insert name for the relation
            res = QtGui.QInputDialog.getText(
                self, QtGui.QApplication.translate("Form", "New relation"),
                QtGui.QApplication.translate(
                    "Form", "Insert the name for the new relation"),
                QtGui.QLineEdit.Normal, defname)
            if res[1] == False or len(res[0]) == 0:
                return

            #Patch provided by Angelo 'Havoc' Puglisi
            name = compatibility.get_py_str(res[0])

        if not rtypes.is_valid_relation_name(name):
            r = QtGui.QApplication.translate(
                "Form", str("Wrong name for destination relation: %s." % name))
            QtGui.QMessageBox.information(
                self, QtGui.QApplication.translate("Form", "Error"), r)
            return

        try:
            self.relations[name] = relation.relation(filename)
        except Exception, e:
            print e
            QtGui.QMessageBox.information(
                None, QtGui.QApplication.translate("Form", "Error"),
                "%s\n%s" % (QtGui.QApplication.translate(
                    "Form", "Check your query!"), e.__str__()))
            return
Пример #11
0
def load_relations():
    '''Loads all the relations present in the directory indicated in the
    examples_path variable and stores them in the rels dictionary'''
    print("Loading relations")
    for i in os.listdir(examples_path):
        if i.endswith('.csv'):  # It's a relation, loading it

            # Naming the relation
            relname = i[:-4]

            print ("Loading relation %s with name %s..." % (i, relname))

            rels[relname] = relation.relation('%s%s' % (examples_path, i))
            print('done')
Пример #12
0
def load_relations():
    '''Loads all the relations present in the directory indicated in the
    examples_path variable and stores them in the rels dictionary'''
    print("Loading relations")
    for i in os.listdir(examples_path):
        if i.endswith('.csv'):  # It's a relation, loading it

            # Naming the relation
            relname = i[:-4]

            print("Loading relation %s with name %s..." % (i, relname))

            rels[relname] = relation.relation('%s%s' % (examples_path, i))
            print('done')
Пример #13
0
    def loadRelation(self, filename=None, name=None):
        '''Loads a relation. Without parameters it will ask the user which relation to load,
        otherwise it will load filename, giving it name.
        It shouldn't be called giving filename but not giving name.'''
        # Asking for file to load
        if filename == None:
            filename = QtGui.QFileDialog.getOpenFileName(self, QtGui.QApplication.translate(
                "Form", "Load Relation"), "", QtGui.QApplication.translate("Form", "Relations (*.csv);;Text Files (*.txt);;All Files (*)"))
            filename = compatibility.get_filename(filename)

        # Default relation's name
        f = filename.split('/')  # Split the full path
        defname = f[len(f) - 1].lower()  # Takes only the lowercase filename

        if len(defname) == 0:
            return

        if (defname.endswith(".csv")):  # removes the extension
            defname = defname[:-4]

        if name == None:  # Prompt dialog to insert name for the relation
            res = QtGui.QInputDialog.getText(
                self, QtGui.QApplication.translate("Form", "New relation"), QtGui.QApplication.translate(
                    "Form", "Insert the name for the new relation"),
                QtGui.QLineEdit.Normal, defname)
            if res[1] == False or len(res[0]) == 0:
                return

            # Patch provided by Angelo 'Havoc' Puglisi
            name = compatibility.get_py_str(res[0])

        if not rtypes.is_valid_relation_name(name):
            r = QtGui.QApplication.translate(
                "Form", str("Wrong name for destination relation: %s." % name))
            QtGui.QMessageBox.information(
                self, QtGui.QApplication.translate("Form", "Error"), r)
            return

        try:
            self.relations[name] = relation.relation(filename)
        except Exception, e:
            print e
            QtGui.QMessageBox.information(None, QtGui.QApplication.translate("Form", "Error"), "%s\n%s" %
                                          (QtGui.QApplication.translate("Form", "Check your query!"), e.__str__()))
            return
Пример #14
0
    def multi_execute(self, query):
        '''Executes multiple queries, separated by \n

        They can have a syntax of
        [varname =] query
        to assign the result to a new relation
        '''
        r = relation()
        queries = query.split('\n')
        for query in queries:
            if query.strip() == '':
                continue

            relname, query = self.split_query(query)

            try:
                r = self.execute(query, relname)
            except Exception as e:
                raise Exception('Error in query: %s\n%s' % (query, str(e)))
        return r
Пример #15
0
def load_relation(filename, defname=None):
    if not os.path.isfile(filename):
        return colored("%s is not a file" % filename, 'red')

    f = filename.split('/')
    if defname == None:
        defname = f[len(f) - 1].lower()
        if defname.endswith(".csv"):  # removes the extension
            defname = defname[:-4]

    if not rtypes.is_valid_relation_name(defname):
        return colored("%s is not a valid relation name" % defname, 'red')

    try:
        relations[defname] = relation.relation(filename)

        completer.add_completion(defname)
        out = colored("Loaded relation %s" % defname, 'green', attrs=['bold'])
        return out + " " + defname
    except Exception, e:
        return colored(e, 'red')
Пример #16
0
    def multi_execute(self, query):
        '''Executes multiple queries, separated by \n

        They can have a syntax of
        [varname =] query
        to assign the result to a new relation
        '''
        r = relation()
        queries = query.split('\n')
        for query in queries:
            if query.strip() == '':
                continue

            relname, query = self.split_query(query)

            try:
                r = self.execute(query, relname)
            except Exception as e:
                raise Exception('Error in query: %s\n%s' % (
                    query,
                    str(e)
                ))
        return r
Пример #17
0
    def create_relation(self):
        h = (self.table.item(0, i).text()
             for i in range(self.table.columnCount()))

        try:
            header = relation.header(h)
        except Exception as e:
            QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate("Form", "Error"), "%s\n%s" % (
                QtWidgets.QApplication.translate("Form", "Header error!"), e.__str__()))
            return None
        r = relation.relation()
        r.header = header

        for i in range(1, self.table.rowCount()):
            hlist = []
            for j in range(self.table.columnCount()):
                try:
                    hlist.append(self.table.item(i, j).text())
                except:
                    QtWidgets.QMessageBox.information(None, QtWidgets.QApplication.translate(
                        "Form", "Error"), QtWidgets.QApplication.translate("Form", "Unset value in %d,%d!" % (i + 1, j + 1)))
                    return None
            r.insert(hlist)
        return r
Пример #18
0
    def deleteColumn(self):
        if self.table.columnCount() > 1:
            self.table.removeColumn(self.table.currentColumn())

    def deleteRow(self):
        if self.table.rowCount() > 2:
            self.table.removeRow(self.table.currentRow())


def edit_relation(rel=None):
    '''Opens the editor for the given relation and returns a _new_ relation
    containing the new relation.
    If the user cancels, it returns None'''

    ui = rel_edit.Ui_Dialog()
    Form = creatorForm(rel)

    ui.setupUi(Form)
    Form.setUi(ui)

    Form.exec_()
    return Form.result_relation


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    r = relation.relation(
        "/home/salvo/dev/relational/trunk/samples/people.csv")
    print (edit_relation(r))
Пример #19
0
class creatorForm(QtGui.QDialog):
    def __init__(self, rel=None):
        QtGui.QDialog.__init__(self)

        self.setSizeGripEnabled(True)
        self.result_relation = None
        self.rel = rel

    def setUi(self, ui):
        self.ui = ui
        self.table = self.ui.table

        if self.rel == None:
            self.setup_empty()
        else:
            self.setup_relation(self.rel)

    def setup_relation(self, rel):

        self.table.insertRow(0)

        for i in rel.header.attributes:
            item = QtGui.QTableWidgetItem()
            item.setText(i)
            self.table.insertColumn(self.table.columnCount())
            self.table.setItem(0, self.table.columnCount() - 1, item)

        for i in rel.content:
            self.table.insertRow(self.table.rowCount())
            for j in range(len(i)):
                item = QtGui.QTableWidgetItem()
                item.setText(i[j])
                self.table.setItem(self.table.rowCount() - 1, j, item)

        pass

    def setup_empty(self):
        self.table.insertColumn(0)
        self.table.insertColumn(0)
        self.table.insertRow(0)
        self.table.insertRow(0)

        i00 = QtGui.QTableWidgetItem()
        i01 = QtGui.QTableWidgetItem()
        i10 = QtGui.QTableWidgetItem()
        i11 = QtGui.QTableWidgetItem()
        i00.setText('Field name 1')
        i01.setText('Field name 2')
        i10.setText('Value 1')
        i11.setText('Value 2')

        self.table.setItem(0, 0, i00)
        self.table.setItem(0, 1, i01)
        self.table.setItem(1, 0, i10)
        self.table.setItem(1, 1, i11)

    def create_relation(self):
        hlist = []

        for i in range(self.table.columnCount()):
            hlist.append(compatibility.get_py_str(
                self.table.item(0, i).text()))
        try:
            header = relation.header(hlist)
        except Exception, e:
            QtGui.QMessageBox.information(
                None, QtGui.QApplication.translate("Form", "Error"),
                "%s\n%s" % (QtGui.QApplication.translate(
                    "Form", "Header error!"), e.__str__()))
            return None
        r = relation.relation()
        r.header = header

        for i in range(1, self.table.rowCount()):
            hlist = []
            for j in range(self.table.columnCount()):
                try:
                    hlist.append(
                        compatibility.get_py_str(self.table.item(i, j).text()))
                except:
                    QtGui.QMessageBox.information(
                        None, QtGui.QApplication.translate("Form", "Error"),
                        QtGui.QApplication.translate(
                            "Form", "Unset value in %d,%d!" % (i + 1, j + 1)))
                    return None
            r.content.add(tuple(hlist))
        return r
Пример #20
0
 def load(self, filename, name):
     '''Loads a relation from file, and gives it a name to
     be used in subsequent queries.'''
     rel = relation(filename)
     self.set_relation(name, rel)
Пример #21
0
 def load(self, filename, name):
     '''Loads a relation from file, and gives it a name to
     be used in subsequent queries.'''
     rel = relation(filename)
     self.set_relation(name, rel)