mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
String method conversion. Added a trivial main to test it with.
This commit is contained in:
parent
ec3bbdef94
commit
51cc3bcd1c
1 changed files with 10 additions and 7 deletions
|
@ -23,7 +23,6 @@ read_mime_types(file) -- parse one file, return a dictionary or None
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import string
|
|
||||||
import posixpath
|
import posixpath
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
|
@ -64,11 +63,11 @@ def guess_type(url):
|
||||||
# data := *urlchar
|
# data := *urlchar
|
||||||
# parameter := attribute "=" value
|
# parameter := attribute "=" value
|
||||||
# type/subtype defaults to "text/plain"
|
# type/subtype defaults to "text/plain"
|
||||||
comma = string.find(url, ',')
|
comma = url.find(',')
|
||||||
if comma < 0:
|
if comma < 0:
|
||||||
# bad data URL
|
# bad data URL
|
||||||
return None, None
|
return None, None
|
||||||
semi = string.find(url, ';', 0, comma)
|
semi = url.find(';', 0, comma)
|
||||||
if semi >= 0:
|
if semi >= 0:
|
||||||
type = url[:semi]
|
type = url[:semi]
|
||||||
else:
|
else:
|
||||||
|
@ -86,8 +85,8 @@ def guess_type(url):
|
||||||
encoding = None
|
encoding = None
|
||||||
if types_map.has_key(ext):
|
if types_map.has_key(ext):
|
||||||
return types_map[ext], encoding
|
return types_map[ext], encoding
|
||||||
elif types_map.has_key(string.lower(ext)):
|
elif types_map.has_key(ext.lower()):
|
||||||
return types_map[string.lower(ext)], encoding
|
return types_map[ext.lower()], encoding
|
||||||
else:
|
else:
|
||||||
return None, encoding
|
return None, encoding
|
||||||
|
|
||||||
|
@ -103,7 +102,7 @@ def guess_extension(type):
|
||||||
global inited
|
global inited
|
||||||
if not inited:
|
if not inited:
|
||||||
init()
|
init()
|
||||||
type = string.lower(type)
|
type = type.lower()
|
||||||
for ext, stype in types_map.items():
|
for ext, stype in types_map.items():
|
||||||
if type == stype:
|
if type == stype:
|
||||||
return ext
|
return ext
|
||||||
|
@ -127,7 +126,7 @@ def read_mime_types(file):
|
||||||
while 1:
|
while 1:
|
||||||
line = f.readline()
|
line = f.readline()
|
||||||
if not line: break
|
if not line: break
|
||||||
words = string.split(line)
|
words = line.split()
|
||||||
for i in range(len(words)):
|
for i in range(len(words)):
|
||||||
if words[i][0] == '#':
|
if words[i][0] == '#':
|
||||||
del words[i:]
|
del words[i:]
|
||||||
|
@ -237,3 +236,7 @@ types_map = {
|
||||||
'.xwd': 'image/x-xwindowdump',
|
'.xwd': 'image/x-xwindowdump',
|
||||||
'.zip': 'application/zip',
|
'.zip': 'application/zip',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import sys
|
||||||
|
print guess_type(sys.argv[1])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue