Пример #1
0
def Clipout(diter, schema, *args, **kargs):
    import lib.pyperclip as clip
    a = []

    exportheader = False

    for i in args:
        if i.startswith('h'):
            exportheader = True

    for i in kargs:
        if i.startswith('h'):
            exportheader = True

    if exportheader == True:
        a.append(u'\t'.join([unicode(i[0]).replace('\t', '    ').replace('\n', ' ') for i in schema]).encode('utf_8',
                                                                                                             'replace'))
        exportheader = False

    for row in diter:
        a.append(
            u'\t'.join([unicode(i).replace('\t', '    ').replace('\n', ' ') for i in row]).encode('utf_8', 'replace'))

    if os.name == 'nt':
        clip.setcb(functions.mstr('\n'.join(a)))
    else:
        clip.setcb('\n'.join(a))
    def addtorow(self, xpath, data, elem=None):
        fullp='/'.join(xpath)
        path=None

        if elem!=None:
            s=self.schemagetall
            if fullp in s:
                path=fullp
            else:
                shortp=pathwithoutns(xpath)
                if shortp in s:
                    path=shortp

            if path == None:
                return

            try:
                data = cleansubtree.match(etree.tostring(elem)).groups()[0]
            except AttributeError:
                data = etree.tostring(elem)
        else:
            s=self.schema

        if fullp in s:
            path=fullp
        else:
            shortp=pathwithoutns(xpath)
            if shortp in s:
                path=shortp

        if path==None:
            if self.strict==2 and elem==None:
                path=xpath
                self.resetrow()
                msg='Undeclared path in XML-prototype was found in the input data. The path is:\n'
                shortp='/'+pathwithoutns(path)
                fullp='/'+'/'.join(path)
                if shortp!=fullp:
                    msg+=shortp+'\n'
                msg+=fullp+'\nThe data to insert into path was:\n'+functions.mstr(data)
                raise etree.ParseError(msg)
        else:
            if self.row[s[path][0]]=='':
                self.row[s[path][0]]=data.replace('\t', self.tabreplace)
                return

            i=1
            attribnum=path+'1'

            oldattribnum=path
            while attribnum in s:
                if self.row[s[attribnum][0]]=='':
                    self.row[s[attribnum][0]]=data.replace('\t', self.tabreplace)
                    return
                i+=1
                oldattribnum=attribnum
                attribnum=path+str(i)

            self.row[s[oldattribnum][0]]+='\t'+data.replace('\t', self.tabreplace)
Пример #3
0
    def addtorow(self, xpath, data, elem=None):
        fullp='/'.join(xpath)
        path=None

        if elem!=None:
            s=self.schemagetall
            if fullp in s:
                path=fullp
            else:
                shortp=pathwithoutns(xpath)
                if shortp in s:
                    path=shortp

            if path == None:
                return

            try:
                data = cleansubtree.match(etree.tostring(elem)).groups()[0]
            except AttributeError:
                data = etree.tostring(elem)
        else:
            s=self.schema

        if fullp in s:
            path=fullp
        else:
            shortp=pathwithoutns(xpath)
            if shortp in s:
                path=shortp

        if path==None:
            if self.strict==2 and elem==None:
                path=xpath
                self.resetrow()
                msg='Undeclared path in XML-prototype was found in the input data. The path is:\n'
                shortp='/'+pathwithoutns(path)
                fullp='/'+'/'.join(path)
                if shortp!=fullp:
                    msg+=shortp+'\n'
                msg+=fullp+'\nThe data to insert into path was:\n'+functions.mstr(data)
                raise etree.ParseError(msg)
        else:
            if self.row[s[path][0]]=='':
                self.row[s[path][0]]=data.replace('\t', self.tabreplace)
                return

            i=1
            attribnum=path+'1'

            oldattribnum=path
            while attribnum in s:
                if self.row[s[attribnum][0]]=='':
                    self.row[s[attribnum][0]]=data.replace('\t', self.tabreplace)
                    return
                i+=1
                oldattribnum=attribnum
                attribnum=path+str(i)

            self.row[s[oldattribnum][0]]+='\t'+data.replace('\t', self.tabreplace)
Пример #4
0
def strict1(tabiter, colcount):
    linenum = 0
    while True:
        row = tabiter.next()
        linenum += 1
        if len(row) != colcount:
            raise functions.OperatorError(
                __name__.rsplit('.')[-1],
                "Line " + str(linenum) + " is invalid. Found " +
                str(len(row)) + " of expected " + str(colcount) +
                " columns\n" + "The line's parsed contents are:\n" +
                u','.join([mstr(x) for x in row]))
        yield row
Пример #5
0
def pyfun(*args):
    """
    .. function:: pyfun(pyfunction, parameters)

    Calls a python function and returns the result. If an error occurs, it throws an
    exception.

    >>> sql("select pyfun('math.sqrt', 25)")
    pyfun('math.sqrt', 25)
    ----------------------
    5.0
    >>> sql("select pyfun('math.log10', 100)")
    pyfun('math.log10', 100)
    ------------------------
    2.0
    >>> sql("select pyfun('math.log10', -1)") # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
    ...
    OperatorError: Madis SQLError:
    Operator PYFUN: math.log10: math domain error
    """

    if len(args) == 0:
        return

    fsplit = args[0].split('.')
    try:
        f = __import__(fsplit[0])
        for i in fsplit[1:]:
            f = f.__dict__[i]
    except KeyboardInterrupt:
        raise
    except:
        try:
            f = __import__('libexternal' + '.' + fsplit[0])
            for i in fsplit:
                f = f.__dict__[i]
        except:
            raise functions.OperatorError("pyfun",
                                          "didn't find function: " + args[0])

    try:
        res = f(*args[1:])
    except Exception, e:
        raise functions.OperatorError("pyfun",
                                      args[0] + ": " + functions.mstr(e))
Пример #6
0
def pyfun(*args):
    """
    .. function:: pyfun(pyfunction, parameters)

    Calls a python function and returns the result. If an error occurs, it throws an
    exception.

    >>> sql("select pyfun('math.sqrt', 25)")
    pyfun('math.sqrt', 25)
    ----------------------
    5.0
    >>> sql("select pyfun('math.log10', 100)")
    pyfun('math.log10', 100)
    ------------------------
    2.0
    >>> sql("select pyfun('math.log10', -1)") # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
    ...
    OperatorError: Madis SQLError:
    Operator PYFUN: math.log10: math domain error
    """

    if len(args)==0:
        return

    fsplit=args[0].split('.')
    try:
        f=__import__(fsplit[0])
        for i in fsplit[1:]:
            f=f.__dict__[i]
    except KeyboardInterrupt:
        raise
    except:
        try:
            f=__import__('libexternal'+'.'+fsplit[0])
            for i in fsplit:
                f=f.__dict__[i]
        except:
            raise functions.OperatorError("pyfun","didn't find function: "+args[0])

    try:
        res=f(*args[1:])
    except Exception, e:
        raise functions.OperatorError("pyfun",args[0]+": "+functions.mstr(e))
Пример #7
0
def execprogram(*args):
    """
    .. function:: execprogram(stdin=null, program_name, parameters, [raise_error]) -> text or blob

    Function *execprogram* executes a shell command and returns its output. If the
    value of the first argument is not *null*, the arguments value will be pushed in program's Standard Input.

    If the program doesn't return a *0* return code, then a madIS error will be raised, containing
    the contents of the program's error stream.

    If the last argument of *execprogram* is set to *null*, then all program errors will be returned as *null*
    (see "cat non_existent_file" examples below).

    Every one of the program's parameters must be provided as different arguments of the *execprogram* call
    (see "cat -n" example below).

    .. note::
        Function *execprogram* tries by default to convert the program's output to UTF-8. If the conversion
        isn't succesfull, then it returns the output as a binary blob.

    Examples:

    >>> table1('''
    ... echo    test
    ... echo    1
    ... ''')
    >>> sql("select execprogram(null, a, b) from table1")
    execprogram(null, a, b)
    -----------------------
    test
    1

    >>> sql("select execprogram(null, null, '-l')") #doctest:+ELLIPSIS +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
    ...
    OperatorError: Madis SQLError:
    Operator EXECPROGRAM: Second parameter should be the name of the program to run

    >>> sql("select execprogram(null, null, '-l', null)") #doctest:+ELLIPSIS +NORMALIZE_WHITESPACE
    execprogram(null, null, '-l', null)
    -----------------------------------
    None

    >>> sql("select execprogram('test', 'cat')")
    execprogram('test', 'cat')
    --------------------------
    test

    >>> sql('''select execprogram('test', 'cat', '-n')''') #doctest:+ELLIPSIS +NORMALIZE_WHITESPACE
    execprogram('test', 'cat', '-n')
    --------------------------------
         1        test

    >>> sql("select execprogram(null, 'NON_EXISTENT_PROGRAM')") #doctest:+ELLIPSIS +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
    ...
    OperatorError: Madis SQLError:
    Operator EXECPROGRAM: [Errno 2] No such file or directory

    >>> sql("select execprogram(null, 'cat', 'non_existent_file')") #doctest:+ELLIPSIS +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
    ...
    OperatorError: Madis SQLError:
    Operator EXECPROGRAM: cat: non_existent_file: No such file or directory

    >>> sql("select execprogram(null, 'cat', 'non_existent_file', null)") #doctest:+ELLIPSIS +NORMALIZE_WHITESPACE
    execprogram(null, 'cat', 'non_existent_file', null)
    ---------------------------------------------------
    None
    """

    if len(args) < 2:
        raise functions.OperatorError('execprogram',
                                      "First parameter should be data to provide to program's STDIN, or null")

    raise_error = False
    if len(args) > 2 and args[-1] == None:
        raise_error = True

    if args[1] == None:
        if raise_error:
            return None
        else:
            raise functions.OperatorError('execprogram', "Second parameter should be the name of the program to run")

    outtext = errtext = ''
    try:
        p = subprocess.Popen([unicode(x) for x in args[1:] if x != None],
                             stdin=subprocess.PIPE if args[0] != None else None, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        if args[0] == None:
            outtext, errtext = p.communicate()
        else:
            val = args[0]
            valtype = type(val)
            if valtype == unicode:
                val = val.encode('utf-8')
            if valtype in (int, float):
                val = str(val)
            outtext, errtext = p.communicate(val)
    except Exception, e:
        raise functions.OperatorError('execprogram', functions.mstr(e))
Пример #8
0
        else:
            val = args[0]
            valtype = type(val)
            if valtype == unicode:
                val = val.encode('utf-8')
            if valtype in (int, float):
                val = str(val)
            outtext, errtext = p.communicate(val)
    except Exception, e:
        raise functions.OperatorError('execprogram', functions.mstr(e))

    if p.returncode != 0:
        if raise_error:
            return None
        else:
            raise functions.OperatorError('execprogram', functions.mstr(errtext).strip())

    try:
        outtext = unicode(outtext, 'utf-8')
    except KeyboardInterrupt:
        raise
    except:
        return buffer(outtext)

    return outtext


execprogram.registered = True


def sleep(*args):
            connection.close()
            exit(0)

        elif command=="functions":
            for ftype in functions.functions:
                for f in functions.functions[ftype]:
                    printterm(f+' :'+ftype)

        elif "help".startswith(command):
            if not argument:
                printterm(helpmessage)
            else:
                for i in functions.functions:
                    if argument in functions.functions[i]:
                        printterm("Function "+ argument + ":")
                        printterm(functions.mstr(functions.functions[i][argument].__doc__))

        elif command=="autoreload":
            automatic_reload=automatic_reload ^ True
            printterm("Automatic reload is now: " + str(automatic_reload))

        else:
            validcommand=False
            printterm("""unknown command. Enter ".help" for help""")

        if validcommand:
            histstatement='.'+command+' '+argument+rest
            try:
                readline.add_history(histstatement.encode('utf-8'))
            except:
                pass
Пример #10
0
def execprogram(*args):
    """
    .. function:: execprogram(stdin=null, program_name, parameters, [raise_error]) -> text or blob

    Function *execprogram* executes a shell command and returns its output. If the
    value of the first argument is not *null*, the arguments value will be pushed in program's Standard Input.

    If the program doesn't return a *0* return code, then a madIS error will be raised, containing
    the contents of the program's error stream.

    If the last argument of *execprogram* is set to *null*, then all program errors will be returned as *null*
    (see "cat non_existent_file" examples below).

    Every one of the program's parameters must be provided as different arguments of the *execprogram* call
    (see "cat -n" example below).

    .. note::
        Function *execprogram* tries by default to convert the program's output to UTF-8. If the conversion
        isn't succesfull, then it returns the output as a binary blob.

    Examples:

    >>> table1('''
    ... echo    test
    ... echo    1
    ... ''')
    >>> sql("select execprogram(null, a, b) from table1")
    execprogram(null, a, b)
    -----------------------
    test
    1

    >>> sql("select execprogram(null, null, '-l')") #doctest:+ELLIPSIS +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
    ...
    OperatorError: Madis SQLError:
    Operator EXECPROGRAM: Second parameter should be the name of the program to run

    >>> sql("select execprogram(null, null, '-l', null)") #doctest:+ELLIPSIS +NORMALIZE_WHITESPACE
    execprogram(null, null, '-l', null)
    -----------------------------------
    None

    >>> sql("select execprogram('test', 'cat')")
    execprogram('test', 'cat')
    --------------------------
    test

    >>> sql('''select execprogram('test', 'cat', '-n')''') #doctest:+ELLIPSIS +NORMALIZE_WHITESPACE
    execprogram('test', 'cat', '-n')
    --------------------------------
         1        test

    >>> sql("select execprogram(null, 'NON_EXISTENT_PROGRAM')") #doctest:+ELLIPSIS +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
    ...
    OperatorError: Madis SQLError:
    Operator EXECPROGRAM: [Errno 2] No such file or directory

    >>> sql("select execprogram(null, 'cat', 'non_existent_file')") #doctest:+ELLIPSIS +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
    ...
    OperatorError: Madis SQLError:
    Operator EXECPROGRAM: cat: non_existent_file: No such file or directory

    >>> sql("select execprogram(null, 'cat', 'non_existent_file', null)") #doctest:+ELLIPSIS +NORMALIZE_WHITESPACE
    execprogram(null, 'cat', 'non_existent_file', null)
    ---------------------------------------------------
    None
    """

    if len(args) < 2:
        raise functions.OperatorError(
            'execprogram',
            "First parameter should be data to provide to program's STDIN, or null"
        )

    raise_error = False
    if len(args) > 2 and args[-1] == None:
        raise_error = True

    if args[1] == None:
        if raise_error:
            return None
        else:
            raise functions.OperatorError(
                'execprogram',
                "Second parameter should be the name of the program to run")

    outtext = errtext = ''
    try:
        p = subprocess.Popen(
            [unicode(x) for x in args[1:] if x != None],
            stdin=subprocess.PIPE if args[0] != None else None,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        if args[0] == None:
            outtext, errtext = p.communicate()
        else:
            val = args[0]
            valtype = type(val)
            if valtype == unicode:
                val = val.encode('utf-8')
            if valtype in (int, float):
                val = str(val)
            outtext, errtext = p.communicate(val)
    except Exception, e:
        raise functions.OperatorError('execprogram', functions.mstr(e))
Пример #11
0
            connection.close()
            exit(0)

        elif command == "functions":
            for ftype in functions.functions:
                for f in functions.functions[ftype]:
                    printterm(f + ' :' + ftype)

        elif "help".startswith(command):
            if not argument:
                printterm(helpmessage)
            else:
                for i in functions.functions:
                    if argument in functions.functions[i]:
                        printterm("Function " + argument + ":")
                        printterm(functions.mstr(functions.functions[i][argument].__doc__))

        elif command == "autoreload":
            automatic_reload = automatic_reload ^ True
            printterm("Automatic reload is now: " + str(automatic_reload))

        else:
            validcommand = False
            printterm("""unknown command. Enter ".help" for help""")

        if validcommand:
            histstatement = '.' + command + ' ' + argument + rest
            try:
                readline.add_history(histstatement.encode('utf-8'))
            except:
                pass
Пример #12
0
            val = args[0]
            valtype = type(val)
            if valtype == unicode:
                val = val.encode('utf-8')
            if valtype in (int, float):
                val = str(val)
            outtext, errtext = p.communicate(val)
    except Exception, e:
        raise functions.OperatorError('execprogram', functions.mstr(e))

    if p.returncode != 0:
        if raise_error:
            return None
        else:
            raise functions.OperatorError('execprogram',
                                          functions.mstr(errtext).strip())

    try:
        outtext = unicode(outtext, 'utf-8')
    except KeyboardInterrupt:
        raise
    except:
        return buffer(outtext)

    return outtext


execprogram.registered = True


def sleep(*args):
Пример #13
0
        else:
            val = args[0]
            valtype = type(val)
            if valtype == unicode:
                val = val.encode('utf-8')
            if valtype in (int,float):
                val = str(val)
            outtext, errtext=p.communicate( val )
    except Exception,e:
        raise functions.OperatorError('execprogram', functions.mstr(e))

    if p.returncode!=0:
        if raise_error:
            return None
        else:
            raise functions.OperatorError('execprogram', functions.mstr(errtext).strip())

    try:
        outtext=unicode(outtext, 'utf-8')
    except KeyboardInterrupt:
        raise
    except:
        return buffer(outtext)

    return outtext

execprogram.registered=True


def sleep(*args):
    """
Пример #14
0
def strict1(tabiter, colcount):
    linenum = 0
    while True:
        row = tabiter.next()
        linenum += 1
        if len(row) != colcount:
            raise functions.OperatorError(__name__.rsplit('.')[-1],"Line " + str(linenum) + " is invalid. Found "+str(len(row))+" of expected "+str(colcount)+" columns\n"+"The line's parsed contents are:\n" + u','.join([mstr(x) for x in row]))
        yield row