Example #1
0
def get_args(require=0):
    """
    >> get_args [<require>]

    Load the command line arguments after the last '--' into $arg1...$argN,
    optionally requiring at least 'require' such arguments.
    """
    from twill3 import commands, namespaces, shell, errors

    global_dict, local_dict = namespaces.get_twill_glocals()

    require = int(require)

    if len(shell.twillargs) < require:
        from twill3.errors import TwillAssertionError
        raise TwillAssertionError("too few arguments; %d rather than %d" % \
                                    (len(shell.twillargs), require,))

    if shell.twillargs:
        for i, arg in enumerate(shell.twillargs):
            global_dict["arg%d" % (i + 1,)] = arg

        print("get_args: loaded %d args as $arg1..$arg%d." % \
                             (i + 1, i + 1), file=commands.OUT)
    else:
        print("no arguments to parse!", file=commands.OUT)
Example #2
0
def getmatch(where, what):
    """
     >> getmatch into_var expression

     Evaluates an expression against __match__ and puts it into 'into_var'.
     """
    global_dict, local_dict = get_twill_glocals()
    match = local_dict['__match__']
    local_dict[where] = _do_eval(match, what)
Example #3
0
def popmatch(which):
    """
     >> popmatch index

     Pops __matchlist__[i] into __match__.
     """
    global_dict, local_dict = get_twill_glocals()

    matchlist = local_dict['__matchlist__']
    match = matchlist.pop(int(which))
    local_dict['__match__'] = match
Example #4
0
def showvar(which):
    """
     >> showvar var

     Shows the value of the variable 'var'.
     """
    global_dict, local_dict = get_twill_glocals()

    d = global_dict.copy()
    d.update(local_dict)

    print((d.get(str(which))))
Example #5
0
def split(what):
    """
     >> split <regexp>

     Sets __matchlist__ to re.split(regexp, page).
     """
    page = browser.get_html()

    m = re.split(what, page)

    global_dict, local_dict = get_twill_glocals()
    local_dict['__matchlist__'] = m
Example #6
0
def findall(what):
    """
     >> findall <regexp>

     Sets __matchlist__ to re.findall(regexp, page).
     """
    page = browser.get_html()

    regexp = re.compile(what, re.DOTALL)
    m = regexp.findall(page)

    global_dict, local_dict = get_twill_glocals()
    local_dict['__matchlist__'] = m
Example #7
0
def setmatch(what):
    """
     >> setmatch expression

     Sets each element __matchlist__ to eval(expression); 'm' is set
     to each element of __matchlist__ prior to processing.
     """
    global_dict, local_dict = get_twill_glocals()

    match = local_dict['__matchlist__']
    if isinstance(match, str):  # convert to list
        match = [
            match,
        ]

    new_match = [_do_eval(m, what) for m in match]
    local_dict['__matchlist__'] = new_match
Example #8
0
def csv_iterate(filename, scriptname):
    """
    >> csv_iterate <csv_file> <script>

    For each line in <csv_file>, read in a list of comma-separated values,
    put them in $col1...$colN, and execute <script>.
    """
    from twill3 import namespaces, execute_file, commands

    global_dict, local_dict = namespaces.get_twill_glocals()

    reader = csv.reader(open(filename, "rb"))
    for i, row in enumerate(reader):
        if DEBUG:
            print('csv_iterate: on row %d of %s' % (
                i,
                filename,
            ),
                  file=commands.OUT)
        for i, col in enumerate(row):
            global_dict["col%d" % (i + 1, )] = col

        execute_file(scriptname, no_reset=True)