OSDN Git Service

Fix toolbar always show.
[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 re
19 import os.path
20 import codecs
21 import fileinput
22
23 import misc
24
25 REG_EXPR_TITLE = re.compile(".*<>.*<>.*<>.*<>(.*)")
26 REG_EXPR_ELEM = re.compile( \
27     "(?P<name>.*)<>(?P<mail>.*)<>(?P<date>.*)<>(?P<msg>.*)<>")
28
29 def get_dat_file_size(bbs, board, thread):
30     """Returns size of dat file"""
31     dat_path = misc.get_thread_dat_path(bbs, board, thread)
32     if not os.path.exists(dat_path):
33         return 0
34
35     return os.path.getsize(dat_path)
36
37 def get_dat_line_count(bbs, board, thread):
38     """Returns the number of line of a dat file specified by bbs, board
39     and thread
40
41     bbs: bbs id
42
43     board: board id
44
45     thread: thread id
46     """
47     dat_path = misc.get_thread_dat_path(bbs, board, thread)
48     if not os.path.exists(dat_path):
49         return 0
50
51     f = fileinput.FileInput(dat_path)
52     for l in f: -1
53     return f.filelineno()
54
55 def do_get_title_from_dat(line):
56     if line:
57         m = REG_EXPR_TITLE.match(line.decode("cp932", "replace"))
58         if m:
59             return m.group(1)
60     return ""
61     
62 def get_title_from_dat(bbs, board, thread):
63     """Returns thread title in dat file
64
65     bbs: bbs id
66
67     board: board id
68
69     thread: thread id
70
71     If failed, return None
72     """
73     dat_path = misc.get_thread_dat_path(bbs, board, thread)
74     if not os.path.exists(dat_path):
75         return None
76
77     f = open(dat_path, "r")
78     try:
79         line = f.readline()
80         return do_get_title_from_dat(line)
81     finally:
82         f.close()
83
84 def split_line_to_elems(line, func):
85     """Splits a line to elements and invokes func
86
87     line: represents one res
88
89     func: is invoked after splitting
90     format of user functon is:
91     def some_func(name, mail, date, msg):
92     where parameters represent corresponding elements of the res
93     """
94     m = REG_EXPR_ELEM.match(line)
95     if m:
96         name = m.group("name")
97         mail = m.group("mail")
98         date = m.group("date")
99         msg = m.group("msg")
100         func(name, mail, date, msg)
101
102 def load_dat(bbs, board, thread, func):
103     """Loads entire dat and invokes func per one res
104
105     bbs: bbs id
106
107     board: board id
108
109     thread: thread id
110
111     func: is invoked per one res
112     format of user function is:
113     def some_func(line):
114     where line is raw body of the res
115     """
116     dat_path = misc.get_thread_dat_path(bbs, board, thread)
117     if not os.path.exists(dat_path):
118         return
119
120     f = fileinput.FileInput(dat_path)
121     for line in f:
122         func(line)
123     f.close()
124
125 def load_dat_partly(bbs, board, thread, func, resnum):
126     """Loads dat partly
127     similar to load_dat, but load_dat_partly does not load entire dat.
128
129     bbs: bbs id
130
131     board: board id
132
133     thread: thread id
134
135     func: invoked per one res
136
137     resnum: load downward resnum
138     """
139     dat_path = misc.get_thread_dat_path(bbs, board, thread)
140     if not os.path.exists(dat_path):
141         return
142
143     f = fileinput.FileInput(dat_path)
144     for line in f:
145         num = f.filelineno()
146         if num >= resnum:
147             func(line)
148     f.close()