OSDN Git Service

Replace PangoLayout with ResLayout.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / datfile.py
1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 import os.path
19 import codecs
20 import fileinput
21
22 import misc
23
24 def get_dat_file_size(bbs_type):
25     """Returns size of dat file"""
26     dat_path = misc.get_thread_dat_path(bbs_type)
27     if not os.path.exists(dat_path):
28         return 0
29
30     return os.path.getsize(dat_path)
31
32 def get_dat_line_count(bbs_type):
33     """Returns the number of line of a dat file specified by bbs, board
34     and thread
35
36     bbs: bbs id
37
38     board: board id
39
40     thread: thread id
41     """
42     dat_path = misc.get_thread_dat_path(bbs_type)
43     if not os.path.exists(dat_path):
44         return 0
45
46     f = fileinput.FileInput(dat_path)
47     for l in f: -1
48     return f.filelineno()
49
50 def get_title_from_dat(bbs_type):
51     """Returns thread title in dat file
52
53     bbs: bbs id
54
55     board: board id
56
57     thread: thread id
58
59     If failed, return None
60     """
61     dat_path = misc.get_thread_dat_path(bbs_type)
62     if not os.path.exists(dat_path):
63         return None
64
65     f = open(dat_path, "r")
66     try:
67         line = f.readline()
68         return bbs_type.get_title_from_dat(line)
69     finally:
70         f.close()
71
72 def load_dat(bbs_type, func):
73     """Loads entire dat and invokes func per one res
74
75     bbs: bbs id
76
77     board: board id
78
79     thread: thread id
80
81     func: is invoked per one res
82     format of user function is:
83     def some_func(line):
84     where line is raw body of the res
85     """
86     dat_path = misc.get_thread_dat_path(bbs_type)
87     if not os.path.exists(dat_path):
88         return
89
90     f = fileinput.FileInput(dat_path)
91     for line in f:
92         func(line)
93     f.close()
94
95 def load_dat_partly(bbs_type, func, resnum):
96     """Loads dat partly
97     similar to load_dat, but load_dat_partly does not load entire dat.
98
99     bbs: bbs id
100
101     board: board id
102
103     thread: thread id
104
105     func: invoked per one res
106
107     resnum: load downward resnum
108     """
109     dat_path = misc.get_thread_dat_path(bbs_type)
110     if not os.path.exists(dat_path):
111         return
112
113     f = fileinput.FileInput(dat_path)
114     for line in f:
115         num = f.filelineno()
116         if num >= resnum:
117             func(line)
118     f.close()