本文整理汇总了Python中trac.attachment.Attachment.open方法的典型用法代码示例。如果您正苦于以下问题:Python Attachment.open方法的具体用法?Python Attachment.open怎么用?Python Attachment.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trac.attachment.Attachment
的用法示例。
在下文中一共展示了Attachment.open方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: source
# 需要导入模块: from trac.attachment import Attachment [as 别名]
# 或者: from trac.attachment.Attachment import open [as 别名]
def source(self, req, args):
arg = re.compile(":").split(args)
if (len(arg) != 2):
raise TracError('Usage: BibAdd(attachment:[path/to/]file)')
realm = 'wiki'
page = None
file = arg[1]
path_info = arg[1].split('/', 1) # greedy! split wikipath and filename
if len(path_info) > 2:
raise TracError('Usage: BibAdd(attachment:[path/to/]file)')
elif len(path_info) == 1:
file = path_info[0]
page = req.args.get('page')
if page is None: # TODO: clean solution
page = 'WikiStart'
bib = Attachment(self.env, realm, page, file)
elif len(path_info) == 2:
page = path_info[0]
file = path_info[1]
bib = Attachment(self.env, realm, page, file)
file = bib.open()
text = file.read()
file.close()
return _extract(text)
示例2: process_request
# 需要导入模块: from trac.attachment import Attachment [as 别名]
# 或者: from trac.attachment.Attachment import open [as 别名]
def process_request(self, req):
"""Process the request. For ClearSilver, return a (template_name,
content_type) tuple, where `template` is the ClearSilver template to use
(either a `neo_cs.CS` object, or the file name of the template), and
`content_type` is the MIME type of the content. For Genshi, return a
(template_name, data, content_type) tuple, where `data` is a dictionary
of substitutions for the template.
For both templating systems, "text/html" is assumed if `content_type` is
`None`.
Note that if template processing should not occur, this method can
simply send the response itself and not return anything.
"""
# handle image setting
if req.method == 'POST':
self.set_default_image(req)
req.redirect(req.get_header('referer') or req.href(req.path_info))
# GET default image
ticket_id, size = self.ticket_id_and_size(req.path_info)
image = DefaultTicketImage(self.env).default_image(ticket_id, size)
assert image is not None # TODO better
images = ImageTrac(self.env).images(ticket_id)
attachment = Attachment(self.env, 'ticket', ticket_id, images[image][size])
mimeview = Mimeview(self.env)
mimetype = mimeview.get_mimetype(attachment.filename)
req.send(attachment.open().read(), mimetype)
示例3: getAttachment
# 需要导入模块: from trac.attachment import Attachment [as 别名]
# 或者: from trac.attachment.Attachment import open [as 别名]
def getAttachment(self, req, path):
""" returns the content of an attachment. """
pagename, filename = os.path.split(path)
attachment = Attachment(self.env, "wiki", pagename, filename)
req.perm(attachment.resource).require("ATTACHMENT_VIEW")
return Binary(attachment.open().read())
示例4: getAttachment
# 需要导入模块: from trac.attachment import Attachment [as 别名]
# 或者: from trac.attachment.Attachment import open [as 别名]
def getAttachment(self, req, ticket, filename):
""" returns the content of an attachment. """
attachment = Attachment(self.env, 'ticket', ticket, filename)
req.perm(attachment.resource).require('ATTACHMENT_VIEW')
return Binary(attachment.open().read())
示例5: expand_macro
# 需要导入模块: from trac.attachment import Attachment [as 别名]
# 或者: from trac.attachment.Attachment import open [as 别名]
def expand_macro(self,formatter,name,content):
args, kwargs = parse_args(content, strict=False)
if len(args) != 1:
raise TracError('[[Usage: BibAdd(source:[email protected]) or BibAdd(attachment:wikipage/file) or BibAdd(attachment:file)]] ')
whom = re.compile(":|@").split(args[0])
file = None
rev = None
pos = None
path = None
entry = None
entries = None
# load the file from the repository
if whom[0] == 'source':
if len(whom) < 2:
raise TracError('[[Missing argument(s) for citing from source; Usage: BibAdd(source:[email protected])]]')
elif len(whom) == 2:
rev = 'latest'
else:
rev = whom[2]
file = whom[1]
repos = self.env.get_repository()
bib = repos.get_node(file, rev)
file = bib.get_content()
string = file.read()
# load the file from the wiki attachments
elif whom[0] == 'attachment':
if (len(whom) != 2):
raise TracError('Wrong syntax for environment \'attachment\'; Usage: BibAdd(attachment:file)')
pos = 'wiki'
page = None
file = whom[1]
path_info = whom[1].split('/',1)
if len(path_info) == 2:
page = path_info[0]
file = path_info[1]
else:
page = formatter.req.args.get('page')
if (page == None):
page = 'WikiStart'
bib = Attachment(self.env,pos,page,file)
file = bib.open()
string = file.read()
# use wiki page itself
elif whom[0] == 'wiki':
if (len(whom) != 2):
raise TracError('Wrong syntax for environment \'wiki\'; Usage BibAdd(wiki:page)')
page = WikiPage(self.env,whom[1])
if page.exists:
if '{{{' in page.text and '}}}' in page.text:
tmp = re.compile('{{{|}}}',2).split(page.text)
string = tmp[1]
else:
raise TracError('No code block on page \'' + whom[1] + '\' found.')
else:
raise TracError('No wiki page named \'' + whom[1] + '\' found.')
else:
raise TracError('Unknown location \''+ whom[0] +'\'')
try:
# handle all data as unicode objects
try:
u = unicode(string,"utf-8")
except TypeError:
u = string
entries = extract_entries(u)
except UnicodeDecodeError:
raise TracError("A UnicodeDecodeError occured while loading the data. Try to save the file in UTF-8 encoding.")
if entries == None:
raise TracError('No entries from file \''+ args[0] +'\' loaded.')
bibdb = getattr(formatter, BIBDB,{})
bibdb.update(entries)
setattr(formatter,BIBDB,bibdb)
示例6: getAttachment
# 需要导入模块: from trac.attachment import Attachment [as 别名]
# 或者: from trac.attachment.Attachment import open [as 别名]
def getAttachment(self, req, path):
""" returns the content of an attachment. """
pagename, filename = posixpath.split(path)
attachment = Attachment(self.env, 'wiki', pagename, filename)
return xmlrpclib.Binary(attachment.open().read())
示例7: render_macro
# 需要导入模块: from trac.attachment import Attachment [as 别名]
# 或者: from trac.attachment.Attachment import open [as 别名]
def render_macro(self, req, name, content):
# args will be null if the macro is called without parenthesis.
if not content:
return ''
args = content.split(',')
filespec = args[0]
# parse filespec argument to get module and id if contained.
parts = filespec.split(':')
url = None
if len(parts) == 3: # module:id:attachment
if parts[0] in ['wiki', 'ticket']:
module, id, file = parts
else:
raise Exception("%s module can't have attachments" % parts[0])
elif len(parts) == 2:
from trac.versioncontrol.web_ui import BrowserModule
try:
browser_links = [link for link,_ in
BrowserModule(self.env).get_link_resolvers()]
except Exception:
browser_links = []
if parts[0] in browser_links: # source:path
module, file = parts
rev = None
if '@' in file:
file, rev = file.split('@')
url = req.href.browser(file, rev=rev)
raw_url = req.href.browser(file, rev=rev, format='raw')
desc = filespec
else: # #ticket:attachment or WikiPage:attachment
# FIXME: do something generic about shorthand forms...
id, file = parts
if id and id[0] == '#':
module = 'ticket'
id = id[1:]
elif id == 'htdocs':
raw_url = url = req.href.chrome('site', file)
desc = os.path.basename(file)
elif id in ('http', 'https', 'ftp'): # external URLs
raw_url = url = desc = id+':'+file
else:
module = 'wiki'
elif len(parts) == 1: # attachment
# determine current object
# FIXME: should be retrieved from the formatter...
# ...and the formatter should be provided to the macro
file = filespec
module, id = 'wiki', 'WikiStart'
path_info = req.path_info.split('/',2)
if len(path_info) > 1:
module = path_info[1]
if len(path_info) > 2:
id = path_info[2]
if module not in ['wiki', 'ticket']:
raise Exception('Cannot reference local attachment from here')
else:
raise Exception('No filespec given')
if not url: # this is an attachment
from trac.attachment import Attachment
attachment = Attachment(self.env, module, id, file)
url = attachment.href(req)
raw_url = attachment.href(req, format='raw')
desc = attachment.description
width, height = swfsize(attachment.open())
if len(args) == 3:
if args[1][0] == '*':
width = int(width * float(args[1][1:]))
else:
width = args[1]
if args[2][0] == '*':
height = int(height * float(args[2][1:]))
else:
height = args[2]
elif len(args) != 1:
raise Exception('Too few arguments. (filespec, width, height)')
else:
if len(args) < 3:
raise Exception('Too few arguments. (filespec, width, height)')
else:
width = args[1]
height = args[2]
vars = {
'width': width,
'height': height,
'raw_url': raw_url
}
return """
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"
width="%(width)s" height="%(height)s">
<param name="movie" value="%(raw_url)s">
<param name="quality" value="low">
<param name="play" value="true">
<embed src="%(raw_url)s" quality="low" width="%(width)s" height="%(height)s"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer">
</embed>
#.........这里部分代码省略.........
示例8: getAttachment
# 需要导入模块: from trac.attachment import Attachment [as 别名]
# 或者: from trac.attachment.Attachment import open [as 别名]
def getAttachment(self, req, ticket, filename):
""" returns the content of an attachment. """
attachment = Attachment(self.env, 'ticket', ticket, filename)
return xmlrpclib.Binary(attachment.open().read())