Commit | Line | Data |
---|---|---|
41a6d859 CM |
1 | """Common utility functions |
2 | """ | |
3 | ||
4 | __copyright__ = """ | |
5 | Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com> | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License version 2 as | |
9 | published by the Free Software Foundation. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | """ | |
20 | ||
21 | def read_string(filename, multiline = False): | |
22 | """Reads the first line from a file | |
23 | """ | |
24 | f = file(filename, 'r') | |
25 | if multiline: | |
26 | string = f.read() | |
27 | else: | |
28 | string = f.readline().strip() | |
29 | f.close() | |
30 | return string | |
31 | ||
32 | def write_string(filename, string, multiline = False): | |
33 | """Writes string to file and truncates it | |
34 | """ | |
35 | f = file(filename, 'w+') | |
36 | if multiline: | |
37 | f.write(string) | |
38 | else: | |
39 | print >> f, string | |
40 | f.close() | |
41 | ||
42 | def append_string(filename, string): | |
43 | """Appends string to file | |
44 | """ | |
45 | f = file(filename, 'a+') | |
46 | print >> f, string | |
47 | f.close() | |
48 | ||
49 | def insert_string(filename, string): | |
50 | """Inserts a string at the beginning of the file | |
51 | """ | |
52 | f = file(filename, 'r+') | |
53 | lines = f.readlines() | |
54 | f.seek(0); f.truncate() | |
55 | print >> f, string | |
56 | f.writelines(lines) | |
57 | f.close() | |
58 | ||
59 | def create_empty_file(name): | |
60 | """Creates an empty file | |
61 | """ | |
62 | file(name, 'w+').close() |