def clone_with_thread(self, thread):
if not thread:
raise ValueError, "parameter must not be empty"
- return Type2ch(self.host, self.board, thread)
+ another = Type2ch(self.host, self.board, thread)
+ another.bbs_type = self.bbs_type
+ another.uri = another.get_thread_uri()
+ return another
def get_uri_base(self):
return "http://" + self.host + "/" + self.board + "/"
raise BbsTypeError, "not specified thread"
return "http://" + self.host + "/test/read.cgi/" + \
self.board + "/" + self.thread + "/"
+
+ def get_board_dir_path(self):
+ """Returns board dir path from logs dir downward, not full path"""
+
+ return self.bbs_type + "/" + self.board
datalist = {}
def on_load_record(id, metadata_dic):
- idxfile_path = misc.get_thread_idx_path(
- self.bbs_type.bbs_type, self.bbs_type.board, id)
+ bbs_type_for_thread = self.bbs_type.clone_with_thread(id)
+ idxfile_path = misc.get_thread_idx_path(bbs_type_for_thread)
if os.path.exists(idxfile_path):
self._add_idx(datalist, id, metadata_dic)
print "load_cache"
- cachefile.load_cache(
- self.bbs_type.bbs_type, self.bbs_type.board, on_load_record)
+ cachefile.load_cache(self.bbs_type, on_load_record)
print "load_idx"
self._load_modified_idxfiles(datalist)
print "save_cache"
- cachefile.save_cache(
- self.bbs_type.bbs_type, self.bbs_type.board, datalist)
+ cachefile.save_cache(self.bbs_type, datalist)
return datalist
def _load_modified_idxfiles(self, datalist):
- basedir = misc.get_thread_idx_dir_path(
- self.bbs_type.bbs_type, self.bbs_type.board)
+ basedir = misc.get_thread_idx_dir_path(self.bbs_type)
if os.path.isdir(basedir):
for idxfile_path in glob.glob(os.path.join(basedir, "*.idx")):
thread_id, ext = os.path.splitext(
os.path.basename(idxfile_path))
idxlastModified = os.path.getmtime(idxfile_path)
+ bbs_type_for_thread = self.bbs_type.clone_with_thread(
+ thread_id)
if thread_id not in datalist:
print "new"
- dic = idxfile.load_idx(
- self.bbs_type.bbs_type, self.bbs_type.board, thread_id)
+ dic = idxfile.load_idx(bbs_type_for_thread)
#dic.pop("etag")
dic["idxlastModified"] = idxlastModified
self._add_idx(datalist, thread_id, dic)
elif idxlastModified > datalist[thread_id]["idxlastModified"]:
print "modified"
datalist[thread_id]["idxlastModified"] = idxlastModified
- dic = idxfile.load_idx(
- self.bbs_type.bbs_type, self.bbs_type.board, thread_id)
+ dic = idxfile.load_idx(bbs_type_for_thread)
for name in idxfile.metadata_namelist:
datalist[thread_id][name] = dic[name]
except ValueError:
lastmod = 0
- subjecttxt_path = misc.get_board_subjecttxt_path(
- self.bbs_type.bbs_type, self.bbs_type.board)
+ subjecttxt_path = misc.get_board_subjecttxt_path(self.bbs_type)
try:
for num, line_encoded \
in itertools.izip(itertools.count(1),
except ValueError:
lastmod = 0
- subjecttxt_path = misc.get_board_subjecttxt_path(
- self.bbs_type.bbs_type, self.bbs_type.board)
+ subjecttxt_path = misc.get_board_subjecttxt_path(self.bbs_type)
basedir = os.path.dirname(subjecttxt_path)
if not os.path.isdir(basedir):
os.makedirs(basedir)
def load_board_idx(self):
lastmod = ""
- boardidxfile = misc.get_board_idx_path(
- self.bbs_type.bbs_type, self.bbs_type.board)
+ boardidxfile = misc.get_board_idx_path(self.bbs_type)
try:
for line in file(boardidxfile):
if line.startswith("lastModified="):
if not lastmod:
return
- boardidx_path = misc.get_board_idx_path(
- self.bbs_type.bbs_type, self.bbs_type.board)
+ boardidx_path = misc.get_board_idx_path(self.bbs_type)
basedir = os.path.dirname(boardidx_path)
if not os.path.isdir(basedir):
os.makedirs(basedir)
self.merge_remote_subjecttxt(datalist)
gobject.idle_add(self.update_datastore, datalist)
- sbj_path = misc.get_board_subjecttxt_path(
- self.bbs_type.bbs_type, self.bbs_type.board)
+ sbj_path = misc.get_board_subjecttxt_path(self.bbs_type)
sbj_exists = os.path.exists(sbj_path)
if update or not sbj_exists:
def save(self):
try:
- states_path = misc.get_board_states_path(
- self.bbs_type.bbs_type, self.bbs_type.board)
+ states_path = misc.get_board_states_path(self.bbs_type)
dirname = os.path.dirname(states_path)
# save only if board dir exists.
except:
traceback.print_exc()
- states_path = misc.get_board_states_path(
- self.bbs_type.bbs_type, self.bbs_type.board)
+ states_path = misc.get_board_states_path(self.bbs_type)
if os.path.exists(states_path):
sort_column_name = "num"
sort_reverse = False
metadata_namelist = ["title", "lineCount", "lastModified", "idxlastModified"]
-def load_cache(bbs, board, func):
+def load_cache(bbs_type, func):
""" Loads metadata from .cache file
loads metadata and invokes handler per one thread
"""
# nothing to do if .cache file does not exist
- cachefile_path = misc.get_board_cache_path(bbs, board)
+ cachefile_path = misc.get_board_cache_path(bbs_type)
if not os.path.exists(cachefile_path):
return
finally:
f.close()
-def save_cache(bbs, board, dic):
+def save_cache(bbs_type, dic):
""" Saves metadata list to .cache file
bbs: bbs id
if not dic:
return
- cachefile_path = misc.get_board_cache_path(bbs, board)
+ cachefile_path = misc.get_board_cache_path(bbs_type)
# create a directroy where .cache file is if not exists
basedir = os.path.dirname(cachefile_path)
REG_EXPR_ELEM = re.compile( \
"(?P<name>.*)<>(?P<mail>.*)<>(?P<date>.*)<>(?P<msg>.*)<>")
-def get_dat_file_size(bbs, board, thread):
+def get_dat_file_size(bbs_type):
"""Returns size of dat file"""
- dat_path = misc.get_thread_dat_path(bbs, board, thread)
+ dat_path = misc.get_thread_dat_path(bbs_type)
if not os.path.exists(dat_path):
return 0
return os.path.getsize(dat_path)
-def get_dat_line_count(bbs, board, thread):
+def get_dat_line_count(bbs_type):
"""Returns the number of line of a dat file specified by bbs, board
and thread
thread: thread id
"""
- dat_path = misc.get_thread_dat_path(bbs, board, thread)
+ dat_path = misc.get_thread_dat_path(bbs_type)
if not os.path.exists(dat_path):
return 0
return m.group(1)
return ""
-def get_title_from_dat(bbs, board, thread):
+def get_title_from_dat(bbs_type):
"""Returns thread title in dat file
bbs: bbs id
If failed, return None
"""
- dat_path = misc.get_thread_dat_path(bbs, board, thread)
+ dat_path = misc.get_thread_dat_path(bbs_type)
if not os.path.exists(dat_path):
return None
msg = m.group("msg")
func(name, mail, date, msg)
-def load_dat(bbs, board, thread, func):
+def load_dat(bbs_type, func):
"""Loads entire dat and invokes func per one res
bbs: bbs id
def some_func(line):
where line is raw body of the res
"""
- dat_path = misc.get_thread_dat_path(bbs, board, thread)
+ dat_path = misc.get_thread_dat_path(bbs_type)
if not os.path.exists(dat_path):
return
func(line)
f.close()
-def load_dat_partly(bbs, board, thread, func, resnum):
+def load_dat_partly(bbs_type, func, resnum):
"""Loads dat partly
similar to load_dat, but load_dat_partly does not load entire dat.
resnum: load downward resnum
"""
- dat_path = misc.get_thread_dat_path(bbs, board, thread)
+ dat_path = misc.get_thread_dat_path(bbs_type)
if not os.path.exists(dat_path):
return
import os
import os.path
from fileinput import FileInput
-from misc import get_thread_idx_path
+import misc
metadata_namelist = ["title", "lineCount", "lastModified", "etag"]
-def load_idx(bbs, board, thread):
+def load_idx(bbs_type):
"""Loads index file of thread
bbs: bbs id
return dictionary which key is in metadata_namelist if idx file exist,
otherwise return empty dic
"""
- idxfile_path = get_thread_idx_path(bbs, board, thread)
+ idxfile_path = misc.get_thread_idx_path(bbs_type)
if not os.path.exists(idxfile_path):
return {"title":None,"lineCount":0,"lastModified":None,"etag":None}
datadic[name] = ""
return datadic
-def save_idx(bbs, board, thread, datadic):
+def save_idx(bbs_type, datadic):
"""Saves thread metadatas to a index file
bbs: bbs id
"""
# create a directory where idx file should belong if does not exist
- idxfile_path = get_thread_idx_path(bbs, board, thread)
+ idxfile_path = misc.get_thread_idx_path(bbs_type)
basedir = os.path.dirname(idxfile_path)
if not os.path.isdir(basedir):
os.makedirs(basedir)
import threading
import config
+from BbsType import bbs_type_exception
REG_EXPR_HTTPDATE = re.compile("((?:Mon)|(?:Tue)|(?:Wed)|(?:Thu)|(?:Fri)|(?:Sat)|(?:Sun)), (\d{2}) ((?:Jan)|(?:Feb)|(?:Mar)|(?:Apr)|(?:May)|(?:Jun)|(?:Jul)|(?:Aug)|(?:Sep)|(?:Oct)|(?:Nov)|(?:Dec)) (\d{4}) (\d{2}):(\d{2}):(\d{2}) GMT")
WDAY_DICT = {"Mon":0, "Tue":1, "Wed":2, "Thu":3, "Fri":4, "Sat":5, "Sun":6}
MON_DICT = {"Jan":1, "Feb":2, "Mar":3, "Apr":4, "May":5, "Jun":6, "Jul":7,
"Aug":8, "Sep":9, "Oct":10, "Nov":11, "Dec":12}
+def _check_thread(bbs_type):
+ """if bbs_type is not thread, raise BbsTypeError"""
+
+ if not bbs_type.is_thread():
+ raise bbs_type_exception.BbsTypeError, \
+ "the bbs_type does not represent thread: " + bbs_type.uri
+
def get_logs_dir_path():
return os.path.join(config.get_config_dir_path(), "logs")
-def get_thread_dat_dir_path(bbs, board):
+def get_thread_dat_dir_path(bbs_type):
"""Returns dir path for saving thread dat file"""
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
+ return os.path.join(get_board_dir_path(bbs_type), "dat")
- return os.path.join(get_board_dir_path(bbs, board), "dat")
-
-def get_thread_idx_dir_path(bbs, board):
+def get_thread_idx_dir_path(bbs_type):
"""Returns dir path for saving thread index file"""
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
-
- return os.path.join(get_board_dir_path(bbs, board), "idx")
-
-def get_thread_states_dir_path(bbs, board):
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
-
- return os.path.join(get_board_dir_path(bbs, board), "states")
-
-def get_thread_dat_path(bbs, board, thread):
- """Returns thread dat file path
-
- bbs: bbs id
-
- board: board id
-
- thread: thread id
- """
-
- # if parameter is empty, raise ValueError
- if not bbs or not board or not thread:
- raise ValueError, "parameter must not be empty"
-
- return os.path.join(get_thread_dat_dir_path(bbs, board), thread + ".dat")
-
-def get_board_subjecttxt_url(bbs, board):
- """Returns subject.txt file url
-
- bbs: bbs id
-
- board: board id
- """
-
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
-
- return get_board_base_url(bbs, board) + "subject.txt"
-
-def get_board_subjecttxt_path(bbs, board):
- """Returns subject.txt file path
-
- bbs: bbs id
-
- board: board id
- """
-
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
-
- return os.path.join(get_logs_dir_path(), bbs, board, "subject.txt")
-
-def get_thread_states_path(bbs, board, thread):
- # if parameter is empty, raise ValueError
- if not bbs or not board or not thread:
- raise ValueError, "parameter must not be empty"
-
- return os.path.join(get_thread_states_dir_path(bbs, board),
- thread + ".states")
-
-def get_board_states_path(bbs, board):
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
-
- return os.path.join(get_board_dir_path(bbs, board), "board.states")
-
-def get_board_idx_path(bbs, board):
- """Returns board idx file path
-
- bbs: bbs id
+ return os.path.join(get_board_dir_path(bbs_type), "idx")
- board: board id
- """
+def get_thread_states_dir_path(bbs_type):
+ """Returns dir path for saving thread states file"""
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
+ return os.path.join(get_board_dir_path(bbs_type), "states")
- return os.path.join(get_logs_dir_path(), bbs, board, "subject.idx")
+def get_thread_dat_path(bbs_type):
+ """Returns thread dat file path"""
-def get_board_dir_path(bbs, board):
- """Returns board dir path
+ _check_thread(bbs_type)
- bbs: bbs ID
+ return os.path.join(get_thread_dat_dir_path(bbs_type),
+ bbs_type.thread + ".dat")
- board: board ID
- """
+def get_board_subjecttxt_path(bbs_type):
+ """Returns subject.txt file path"""
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
+ return os.path.join(get_board_dir_path(bbs_type), "subject.txt")
- return os.path.join(get_logs_dir_path(), bbs, board)
+def get_thread_states_path(bbs_type):
+ """Returns thread states file path"""
-def get_thread_idx_path(bbs, board, thread):
- """Returns idx file path of thread
+ _check_thread(bbs_type)
- bbs: bbs ID
+ return os.path.join(get_thread_states_dir_path(bbs_type),
+ bbs_type.thread + ".states")
- board: board ID
+def get_board_states_path(bbs_type):
+ """Returns board states file path"""
- thread: thread ID
+ return os.path.join(get_board_dir_path(bbs_type), "board.states")
- Note: if parameter is empty, raise ValueError
- """
+def get_board_idx_path(bbs_type):
+ """Returns board idx file path"""
- # if parameter is empty, raise ValueError
- if not bbs or not board or not thread:
- raise ValueError, "parameter must not be empty"
+ return os.path.join(get_board_dir_path(bbs_type), "subject.idx")
- return os.path.join(get_thread_idx_dir_path(bbs, board), thread + ".idx")
+def get_board_dir_path(bbs_type):
+ """Returns board dir path"""
-def get_board_cache_path(bbs, board):
- """ Returns .cache file path of board
+ return os.path.join(get_logs_dir_path(), bbs_type.get_board_dir_path())
- bbs: bbs ID
+def get_thread_idx_path(bbs_type):
+ """Returns idx file path of thread"""
- board: board ID
+ _check_thread(bbs_type)
- Note: if parameter is empty, raise ValueError
- """
+ return os.path.join(get_thread_idx_dir_path(bbs_type),
+ bbs_type.thread + ".idx")
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
+def get_board_cache_path(bbs_type):
+ """Returns .cache file path of board"""
- return os.path.join(get_thread_idx_dir_path(bbs, board), ".cache")
+ return os.path.join(get_thread_idx_dir_path(bbs_type), ".cache")
def httpdate_to_secs(httpdate):
"""Returns the seconds since the epoch"""
self.widget_tree.signal_autoconnect(sigdic)
- title = datfile.get_title_from_dat(
- self.bbs_type.bbs_type, self.bbs_type.board, self.bbs_type.thread)
+ title = datfile.get_title_from_dat(self.bbs_type)
if title:
self.window.set_title(title)
def http_get_dat(self, on_get_res):
datfile_url = self.bbs_type.get_dat_uri()
- idx_dic = idxfile.load_idx(self.bbs, self.board, self.thread)
+ idx_dic = idxfile.load_idx(self.bbs_type)
lastmod = idx_dic["lastModified"]
etag = idx_dic["etag"]
if self.num > 0:
if not self.title:
- title = datfile.get_title_from_dat(
- self.bbs, self.board, self.thread)
+ title = datfile.get_title_from_dat(self.bbs_type)
if title:
self.title = title
gobject.idle_add(self.window.set_title, title)
# save idx
idx_dic = {"title": self.title, "lineCount": self.num,
"lastModified": lastmod, "etag": etag}
- idxfile.save_idx(self.bbs, self.board, self.thread, idx_dic)
+ idxfile.save_idx(self.bbs_type, idx_dic)
gobject.idle_add(session.thread_idx_updated,
self.bbs_type.get_thread_uri(), idx_dic)
self.textbuffer.create_mark("1", self.enditer, True)
gobject.idle_add(create_mark)
- line_count = datfile.get_dat_line_count(
- self.bbs, self.board, self.thread)
+ line_count = datfile.get_dat_line_count(self.bbs_type)
if line_count > self.num:
datfile.load_dat_partly(
- self.bbs, self.board, self.thread,
- self.append_rawres_to_buffer, self.num+1)
+ self.bbs_type, self.append_rawres_to_buffer, self.num+1)
def do_jump(num):
if self.jump_request_num:
gobject.idle_add(do_jump, self.num)
def get():
- dat_path = misc.get_thread_dat_path(
- self.bbs, self.board, self.thread)
+ dat_path = misc.get_thread_dat_path(self.bbs_type)
dat_file = FileWrap(dat_path)
def save_line_and_append_to_buffer(line):
self.textbuffer.create_mark("1", self.enditer, True)
gobject.idle_add(create_mark)
- datfile.load_dat(self.bbs, self.board, self.thread,
- self.append_rawres_to_buffer)
+ datfile.load_dat(self.bbs_type, self.append_rawres_to_buffer)
def jump():
def do_jump(num):
self.jump_request_num = int(resnum)
def load(self, update=False):
- dat_path = misc.get_thread_dat_path(
- self.bbs_type.bbs_type, self.bbs_type.board, self.bbs_type.thread)
+ dat_path = misc.get_thread_dat_path(self.bbs_type)
dat_exists = os.path.exists(dat_path)
if update or not dat_exists:
self.update()
def save(self):
try:
- states_path = misc.get_thread_states_path(
- self.bbs_type.bbs_type, self.bbs_type.board,
- self.bbs_type.thread)
- dat_path = misc.get_thread_dat_path(
- self.bbs_type.bbs_type, self.bbs_type.board,
- self.bbs_type.thread)
+ states_path = misc.get_thread_states_path(self.bbs_type)
+ dat_path = misc.get_thread_dat_path(self.bbs_type)
# save only if dat file exists.
if os.path.exists(dat_path):
except:
traceback.print_exc()
- states_path = misc.get_thread_states_path(
- self.bbs_type.bbs_type, self.bbs_type.board,
- self.bbs_type.thread)
+ states_path = misc.get_thread_states_path(self.bbs_type)
if os.path.exists(states_path):
for line in file(states_path):
if line.startswith("window_height="):