Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/schemes/filelink.py
blob: 0c0cb7c385bcb0aeff5369bdb059db6a06aa8e03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Copyright (C) 1998,1999  marduk <marduk@python.net>
# Copyright (C) 2002 Mike Meyer <mwm@mired.org>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

"""This module defines the functions needed for creating Link objects for urls
using the file scheme"""

import urlparse
import os
import time
import mimetypes
import myUrlLib
import re

mimetypes.types_map['.shtml']='text/html'

def init(self, url, parent):
    self.URL = myUrlLib.basejoin(parent,url)
    parsed = urlparse.urlparse(self.URL,'file',0)
    filename = parsed[2]
    if os.name != 'posix':
	filename = re.sub("^/\(//\)?\([a-zA-Z]\)[|:]","\\2:",filename)
    try:
	stats = os.stat(filename)
    except os.error:
	self.set_bad_link(self.URL, "No such file or directory")
	return

    self.size = stats[6]
    
    lastMod = stats[8]
    self.age = int((time.time()-lastMod)/myUrlLib.SECS_PER_DAY)
    
    self.type = mimetypes.guess_type(url)[0]
    if self.type is None: self.type = 'application/octet-stream' # good enough?

def get_document(url):
    parsed = urlparse.urlparse(url,'file',0)
    filename = parsed[2]
    if os.name != 'posix':
	filename = re.sub("^/\(//\)?\([a-zA-Z]\)[|:]","\\2:",filename)
    
    return open(filename,'r').read()