File: pytools.py

package info (click to toggle)
vim-latexsuite 1%3A1.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 5,424 kB
  • sloc: xml: 5,159; python: 906; makefile: 92; perl: 59; sh: 14
file content (60 lines) | stat: -rw-r--r-- 1,554 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import glob
import os
import re
import string


import vim


def catFile(filename):
    """ assigns a local variable retval to the contents of a file """
    try:
        fp = open(filename)
        lines = fp.read()
        fp.close()
    except FileNotFoundError:
        lines = ''

    # escape double quotes and backslashes before quoting the string so
    # everything passes throught.
    vim.command("""let retval = "%s" """ % re.sub(r'"|\\', r'\\\g<0>', lines))
    return lines


def isPresentInFile(regexp, filename):
    """ check if regexp is present in the file """
    try:
        fp = open(filename)
        fcontents = fp.read()
        fp.close()
        if re.search(regexp, fcontents):
            vim.command('let retval = 1')
            return 1
        else:
            vim.command('let retval = 0')
            return None
    except FileNotFoundError:
        vim.command('let retval = 0')
        return None


def deleteFile(filepattern):
    """ deletes a file if present

       If the file does not exist, check if its a filepattern rather than a
       filename. If its a pattern, then deletes all files matching the pattern.
    """
    try:
        if os.path.isfile(filepattern):
            os.remove(filepattern)
        else:
            if glob.glob(filepattern):
                for filename in glob.glob(filepattern):
                    os.remove(filename)
            else:
                vim.command('let retval = -1')
    except:
        vim.command('let retval = -1')

# vim:ff=unix:noet:ts=4:sw=4:nowrap