author | Lennard de Rijk <ljvderijk@gmail.com> |
Mon, 01 Jun 2009 20:03:22 +0200 | |
changeset 2372 | f1cbafd21f75 |
parent 1981 | 8cfb054b73b2 |
permissions | -rw-r--r-- |
1981
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
2 |
# |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2009 the Melange authors. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
4 |
# |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
8 |
# |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
10 |
# |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
16 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
17 |
from __future__ import with_statement |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
18 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
19 |
"""User prompting and file access utilities.""" |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
20 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
21 |
__authors__ = [ |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
22 |
# alphabetical order by last name, please |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
23 |
'"David Anderson" <dave@natulte.net>', |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
24 |
] |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
25 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
26 |
import error |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
27 |
import log |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
28 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
29 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
30 |
class Error(error.Error): |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
31 |
pass |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
32 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
33 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
34 |
class FileAccessError(Error): |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
35 |
"""An error occured while accessing a file.""" |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
36 |
pass |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
37 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
38 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
39 |
def getString(prompt): |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
40 |
"""Prompt for and return a string.""" |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
41 |
prompt += ' ' |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
42 |
log.stdout.write(prompt) |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
43 |
log.stdout.flush() |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
44 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
45 |
response = sys.stdin.readline() |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
46 |
log.terminal_echo(prompt + response.strip()) |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
47 |
if not response: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
48 |
raise error.AbortedByUser('Aborted by ctrl+D') |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
49 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
50 |
return response.strip() |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
51 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
52 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
53 |
def confirm(prompt, default=False): |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
54 |
"""Ask a yes/no question and return the answer. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
55 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
56 |
Will reprompt the user until one of "yes", "no", "y" or "n" is |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
57 |
entered. The input is case insensitive. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
58 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
59 |
Args: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
60 |
prompt: The question to ask the user. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
61 |
default: The answer to return if the user just hits enter. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
62 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
63 |
Returns: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
64 |
True if the user answered affirmatively, False otherwise. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
65 |
""" |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
66 |
if default: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
67 |
question = prompt + ' [Yn]' |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
68 |
else: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
69 |
question = prompt + ' [yN]' |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
70 |
while True: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
71 |
answer = getString(question) |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
72 |
if not answer: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
73 |
return default |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
74 |
elif answer in ('y', 'yes'): |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
75 |
return True |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
76 |
elif answer in ('n', 'no'): |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
77 |
return False |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
78 |
else: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
79 |
log.error('Please answer yes or no.') |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
80 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
81 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
82 |
def getNumber(prompt): |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
83 |
"""Prompt for and return a number. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
84 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
85 |
Will reprompt the user until a number is entered. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
86 |
""" |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
87 |
while True: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
88 |
value_str = getString(prompt) |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
89 |
try: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
90 |
return int(value_str) |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
91 |
except ValueError: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
92 |
log.error('Please enter a number. You entered "%s".' % value_str) |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
93 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
94 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
95 |
def getChoice(intro, prompt, choices, done=None, suggest=None): |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
96 |
"""Prompt for and return a choice from a menu. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
97 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
98 |
Will reprompt the user until a valid menu entry is chosen. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
99 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
100 |
Args: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
101 |
intro: Text to print verbatim before the choice menu. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
102 |
prompt: The prompt to print right before accepting input. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
103 |
choices: The list of string choices to display. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
104 |
done: If not None, the list of indices of previously |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
105 |
selected/completed choices. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
106 |
suggest: If not None, the index of the choice to highlight as |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
107 |
the suggested choice. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
108 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
109 |
Returns: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
110 |
The index in the choices list of the selection the user made. |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
111 |
""" |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
112 |
done = set(done or []) |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
113 |
while True: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
114 |
print intro |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
115 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
116 |
for i, entry in enumerate(choices): |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
117 |
done_text = ' (done)' if i in done else '' |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
118 |
indent = '--> ' if i == suggest else ' ' |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
119 |
print '%s%2d. %s%s' % (indent, i+1, entry, done_text) |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
120 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
121 |
choice = getNumber(prompt) |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
122 |
if 0 < choice <= len(choices): |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
123 |
return choice-1 |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
124 |
log.error('%d is not a valid choice between %d and %d' % |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
125 |
(choice, 1, len(choices))) |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
126 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
127 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
128 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
129 |
def fileToLines(path): |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
130 |
"""Read a file and return it as a list of lines.""" |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
131 |
try: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
132 |
with file(path) as f: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
133 |
return f.read().split('\n') |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
134 |
except (IOError, OSError), e: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
135 |
raise FileAccessError(str(e)) |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
136 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
137 |
|
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
138 |
def linesToFile(path, lines): |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
139 |
"""Write a list of lines to a file.""" |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
140 |
try: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
141 |
with file(path, 'w') as f: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
142 |
f.write('\n'.join(lines)) |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
143 |
except (IOError, OSError), e: |
8cfb054b73b2
Factor out input and file utils into io.py.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
144 |
raise FileAccessError(str(e)) |