OSDN Git Service

Add one function for thread and modified some function for thread. datfile.load_dat...
[fukui-no-namari/fukui-no-namari.git] / src / Hage1 / 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 get_title_from_dat(bbs, board, thread):
56     """Returns thread title in dat file
57
58     bbs: bbs id
59
60     board: board id
61
62     thread: thread id
63
64     If failed, return None
65     """
66     dat_path = misc.get_thread_dat_path(bbs, board, thread)
67     if not os.path.exists(dat_path):
68         return None
69
70     f = open(dat_path, "r")
71     try:
72         line = f.readline()
73         if line:
74             m = REG_EXPR_TITLE.match(line.decode("cp932", "replace"))
75             if m:
76                 return m.group(1)
77     finally:
78         f.close()
79
80 def split_line_to_elems(line, func):
81     """Splits a line to elements and invokes func
82
83     line: represents one res
84
85     func: is invoked after splitting
86     format of user functon is:
87     def some_func(name, mail, date, msg):
88     where parameters represent corresponding elements of the res
89     """
90     m = REG_EXPR_ELEM.match(line)
91     if m:
92         name = m.group("name")
93         mail = m.group("mail")
94         date = m.group("date")
95         msg = m.group("msg")
96         func(name, mail, date, msg)
97
98 def load_dat(bbs, board, thread, func):
99     """Loads entire dat and invokes func per one res
100
101     bbs: bbs id
102
103     board: board id
104
105     thread: thread id
106
107     func: is invoked per one res
108     format of user function is:
109     def some_func(line):
110     where line is raw body of the res
111     """
112     dat_path = misc.get_thread_dat_path(bbs, board, thread)
113     if not os.path.exists(dat_path):
114         return
115
116     f = fileinput.FileInput(dat_path)
117     for line in f:
118         func(line)
119     f.close()
120
121 def load_dat_partly(bbs, board, thread, func, resnum):
122     """Loads dat partly
123     similar to load_dat, but load_dat_partly does not load entire dat.
124
125     bbs: bbs id
126
127     board: board id
128
129     thread: thread id
130
131     func: invoked per one res
132
133     resnum: load downward resnum
134     """
135     dat_path = misc.get_thread_dat_path(bbs, board, thread)
136     if not os.path.exists(dat_path):
137         return
138
139     f = fileinput.FileInput(dat_path)
140     for line in f:
141         num = f.filelineno()
142         if num >= resnum:
143             func(line)
144     f.close()