File: pylib_generic.py

package info (click to toggle)
eprover 2.6%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 21,288 kB
  • sloc: ansic: 331,111; csh: 12,026; python: 10,178; awk: 5,825; makefile: 461; sh: 389
file content (146 lines) | stat: -rwxr-xr-x 3,849 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# ----------------------------------
"""
Module pylib_generic

Generic stuff useful in many different contexts.

Copyright 2003,2004 Stephan Schulz, schulz@eprover.org

This code is part of the support structure for the equational
theorem prover E. Visit

 http://www.eprover.org

for more information.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program ; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA  02111-1307 USA 

The original copyright holder can be contacted as

Stephan Schulz (I4)
Technische Universitaet Muenchen
Institut fuer Informatik
Boltzmannstrasse 3
Garching bei Muenchen
Germany

or via email (address above).
"""


import sys
import os
import re
import time


whitespace_re = re.compile('\s+')
"""Regular expression representing white space."""

dstring_re     = re.compile('".*?"')
"""Regular expression representing a string in double quotes."""

dqstring_re     = re.compile('".*?[^\\\\]"')
"""Regular expression representing a string in double quotes with quoting."""

sstring_re     = re.compile("'.*?'")
"""Regular expression representing a string in single quotes."""

sqstring_re     = re.compile("'.*?[^\\\\]'")
"""Regular expression representing a string in single quotes with quoting."""

other_re       = re.compile("[^'\"\s]+")
"""
Regular expression representing anything not containing white space or
quotes.
""" 

sqquote_re = re.compile("[^\\\\]'")
"""Regular expression represening  an unquoted single quote."""

nonprint_ascii_re = re.compile("[^ -\177]")
"""
Regular expression representing all non-printable characters, where
only ASCII 32-127 are considered printable.
"""

def asciify_str(str, repl="_"):
    """
    Return str with all non-printable characters replaced according to
    repl (which can be a strig or a function as per re.sub()).
    """
    res = nonprint_ascii_re.sub(repl, str)
    return res


def break_shell_str(opts):
    """
    Break a string of arguments into an array of argments as the shell would.
    """
    res = []
    tmp = ""
    opts = opts.strip()+" "
    while opts != "":
        mo = whitespace_re.match(opts)
        if mo:
            res.append(tmp)
            tmp = ""
            opts = opts[mo.end():]
        else:
            mo = dstring_re.match(opts) or sstring_re.match(opts)
            if mo:
                tmp = tmp+mo.group()[1:-1]
                opts = opts[mo.end():]
            else:
                mo = other_re.match(opts)
                try:
                    tmp = tmp+mo.group()
                    opts = opts[mo.end():]
                except AttributeError:
                    raise Error("Impossible string in break_shell_str()");
    return res   


class timer(object):
    def __init__(self, settime):
        self.set(settime)

    def remaining(self):
        now = time.time()
        if now > self.expiry:
            return 0
        return self.expiry - now

    def expired(self):
        return self.remaining() == 0


    def set(self, settime):
        self.expiry = time.time()+settime

    def __str__(self):
        return "<timer: %f (%f remaining)>"%(self.expiry, self.remaining())


if __name__ == '__main__':

    tmp = """
    eprover -xAuto -tAuto -H"(1* Error, 3* Fridolin)"
    """
    print break_shell_str(tmp)