本文整理汇总了Python中trac.mimeview.api.Mimeview类的典型用法代码示例。如果您正苦于以下问题:Python Mimeview类的具体用法?Python Mimeview怎么用?Python Mimeview使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Mimeview类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: expand_macro
def expand_macro(self, formatter, name, content):
from trac.mimeview.api import Mimeview
mime_map = Mimeview(self.env).mime_map
mime_type_filter = ''
args, kw = parse_args(content)
if args:
mime_type_filter = args.pop(0).strip().rstrip('*')
mime_types = {}
for key, mime_type in mime_map.iteritems():
if (not mime_type_filter or
mime_type.startswith(mime_type_filter)) and key != mime_type:
mime_types.setdefault(mime_type, []).append(key)
return tag.div(class_='mimetypes')(
tag.table(class_='wiki')(
tag.thead(tag.tr(
tag.th(_("MIME Types")), # always use plural
tag.th(tag.a("WikiProcessors",
href=formatter.context.href.wiki(
'WikiProcessors'))))),
tag.tbody(
tag.tr(tag.th(tag.tt(mime_type),
style="text-align: left"),
tag.td(tag.code(
' '.join(sorted(mime_types[mime_type])))))
for mime_type in sorted(mime_types.keys()))))
开发者ID:exocad,项目名称:exotrac,代码行数:27,代码来源:macros.py
示例2: get_search_results
def get_search_results(self, req, query, filters):
if 'repo' not in filters:
return
for filename, reponame in self.search_backend.find_words(query):
repo = self.env.get_repository(reponame=reponame, authname=req.authname)
node = repo.get_node(filename)
if node.kind == Node.DIRECTORY:
yield (self.env.href.browser(reponame, filename),
"%s (in %s)" % (filename, reponame), change.date, change.author,
'Directory')
else:
found = 0
mimeview = Mimeview(self.env)
content = mimeview.to_unicode(node.get_content().read(), node.get_content_type())
for n, line in enumerate(content.splitlines()):
line = line.lower()
for q in query:
idx = line.find(q)
if idx != -1:
found = n + 1
break
if found:
break
change = repo.get_changeset(node.rev)
yield (self.env.href.browser(reponame, filename
) + (found and '#L%i' % found or ''
),
"%s (in %s)" % (filename, reponame), change.date, change.author,
shorten_result(content, query))
开发者ID:pombredanne,项目名称:trac-MultiRepoSearchPlugin,代码行数:33,代码来源:search.py
示例3: _render_file
def _render_file(self, req, context, repos, node, rev=None):
""" trac.versioncontrol.web_ui.browser.BrowserModule._render_file()
copy with just the essentials needed for our purpose. """
req.perm(context.resource).require('FILE_VIEW')
mimeview = Mimeview(self.env)
# MIME type detection
CHUNK_SIZE = 4096
content = node.get_content()
chunk = content.read(CHUNK_SIZE)
mime_type = node.content_type
if not mime_type or mime_type == 'application/octet-stream':
mime_type = mimeview.get_mimetype(node.name, chunk) or \
mime_type or 'text/plain'
self.log.debug("Rendering ReposReadMe of node %[email protected]%s with mime-type %s"
% (node.name, str(rev), mime_type))
del content # the remainder of that content is not needed
add_stylesheet(req, 'common/css/code.css')
annotations = []
force_source = False
raw_href = ''
return mimeview.preview_data(context, node.get_content(),
node.get_content_length(),
mime_type, node.created_path,
raw_href,
annotations=annotations,
force_source=force_source)
开发者ID:nyuhuhuu,项目名称:trachacks,代码行数:30,代码来源:reposreadme.py
示例4: _test_convert_content
def _test_convert_content(self, expected, content, iterable):
mimeview = Mimeview(self.env)
output = mimeview.convert_content(self._make_req(), self.in_mimetype,
content, 'text', iterable=iterable)
if iterable:
self.assertNotIn(type(output[0]), (str, unicode))
self.assertEqual(expected, ''.join(output[0]))
else:
self.assertEqual(type(expected), type(output[0]))
self.assertEqual(expected, output[0])
self.assertEqual('text/plain', output[1])
self.assertEqual('txt', output[2])
开发者ID:exocad,项目名称:exotrac,代码行数:12,代码来源:api.py
示例5: test_extra_mimetypes
def test_extra_mimetypes(self):
"""
The text/x-ini mimetype is normally not known by Trac, but
Pygments supports it.
"""
mimeview = Mimeview(self.env)
self.assertEqual('text/x-ini; charset=utf-8',
mimeview.get_mimetype('file.ini'))
self.assertEqual('text/x-ini; charset=utf-8',
mimeview.get_mimetype('file.cfg'))
self.assertEqual('text/x-ini; charset=utf-8',
mimeview.get_mimetype('file.text/x-ini'))
开发者ID:exocad,项目名称:exotrac,代码行数:12,代码来源:pygments.py
示例6: __init__
def __init__(self, env, node, root, base):
super(FilePage, self).__init__(env, node, root, base)
self.mimeview = Mimeview(self.env)
self.exists = (node is not None)
self.mime_type = None
self.chunk = None
开发者ID:rcarmo,项目名称:tracdocs,代码行数:7,代码来源:web_ui.py
示例7: setUp
def setUp(self):
self.env = EnvironmentStub()
self.ticket_module = TicketModule(self.env)
self.mimeview = Mimeview(self.env)
self.req = Mock(hdf=HDFWrapper(['./templates']),
base_path='/trac.cgi', path_info='',
href=Href('/trac.cgi'))
开发者ID:cyphactor,项目名称:lifecyclemanager,代码行数:7,代码来源:conversion.py
示例8: ProductTicketConversionTestCase
class ProductTicketConversionTestCase(TicketConversionTestCase, \
MultiproductTestCase):
def setUp(self):
self._mp_setup()
self.global_env = self.env
self.env = ProductEnvironment(self.global_env, self.default_product)
# Product name inserted in RSS feed
self.env.product._data['name'] = 'My Project'
self.env.config.set('trac', 'templates_dir',
os.path.join(os.path.dirname(self.env.path),
'templates'))
self.ticket_module = ProductTicketModule(self.env)
self.mimeview = Mimeview(self.env)
self.req = Mock(base_path='/trac.cgi', path_info='',
href=Href('/trac.cgi'), chrome={'logo': {}},
abs_href=Href('http://example.org/trac.cgi'),
environ={}, perm=[], authname='-', args={}, tz=None,
locale='', session=None, form_token=None)
def test_csv_conversion(self):
ticket = self._create_a_ticket()
csv = self.mimeview.convert_content(self.req, 'trac.ticket.Ticket',
ticket, 'csv')
self.assertEqual(('\xef\xbb\xbf'
'id,summary,reporter,owner,description,status,'
'product,keywords,cc\r'
'\n1,Foo,santa,,Bar,,,,\r\n',
'text/csv;charset=utf-8', 'csv'), csv)
def test_tab_conversion(self):
ticket = self._create_a_ticket()
csv = self.mimeview.convert_content(self.req, 'trac.ticket.Ticket',
ticket, 'tab')
self.assertEqual(('\xef\xbb\xbf'
'id\tsummary\treporter\towner\tdescription\tstatus\t'
'product\tkeywords\tcc\r\n'
'1\tFoo\tsanta\t\tBar\t\t\t\t\r\n',
'text/tab-separated-values;charset=utf-8', 'tsv'),
csv)
def tearDown(self):
self.global_env.reset_db()
开发者ID:Stackato-Apps,项目名称:bloodhound,代码行数:45,代码来源:conversion.py
示例9: setUp
def setUp(self):
env = EnvironmentStub(enable=[Chrome, PatchRenderer])
req = Mock(base_path='', chrome={}, args={}, session={},
abs_href=Href('/'), href=Href('/'),
perm=MockPerm(), authname=None, tz=None)
self.context = Context.from_request(req)
self.patch = Mimeview(env).renderers[0]
patch_html = open(os.path.join(os.path.split(__file__)[0],
'patch.html'))
self.patch_html = Stream(list(HTMLParser(patch_html)))
开发者ID:gdgkyoto,项目名称:kyoto-gtug,代码行数:10,代码来源:patch.py
示例10: setUp
def setUp(self):
env = EnvironmentStub(enable=[Chrome, PatchRenderer])
req = Mock(base_path='', chrome={'static_hash': None}, args={},
session={}, abs_href=Href('/'), href=Href('/'), locale='',
perm=MockPerm(), authname=None, tz=None)
self.context = web_context(req)
self.patch = Mimeview(env).renderers[0]
patch_html = open(os.path.join(os.path.split(__file__)[0],
'patch.html'))
self.patch_html = Stream(list(HTMLParser(patch_html, encoding='utf-8')))
开发者ID:moreati,项目名称:trac-gitsvn,代码行数:10,代码来源:patch.py
示例11: setUp
def setUp(self):
self.env = EnvironmentStub(enable=[Chrome, PygmentsRenderer])
self.pygments = Mimeview(self.env).renderers[0]
self.req = Mock(base_path='', chrome={}, args={},
abs_href=Href('/'), href=Href('/'),
session={}, perm=None, authname=None, tz=None)
self.context = web_context(self.req)
pygments_html = open(os.path.join(os.path.split(__file__)[0],
'pygments.html'))
self.pygments_html = Stream(list(HTMLParser(pygments_html, encoding='utf-8')))
开发者ID:exocad,项目名称:exotrac,代码行数:10,代码来源:pygments.py
示例12: setUp
def setUp(self):
self.env = EnvironmentStub()
self.env.config.set('trac', 'templates_dir',
os.path.join(os.path.dirname(self.env.path),
'templates'))
self.ticket_module = TicketModule(self.env)
self.mimeview = Mimeview(self.env)
self.req = Mock(base_path='/trac.cgi', path_info='',
href=Href('/trac.cgi'), chrome={'logo': {}},
abs_href=Href('http://example.org/trac.cgi'),
environ={}, perm=[], authname='-', args={}, tz=None,
session=None, form_token=None)
开发者ID:gdgkyoto,项目名称:kyoto-gtug,代码行数:12,代码来源:conversion.py
示例13: test_get_supported_conversions
def test_get_supported_conversions(self):
class Converter0(Component):
implements(IContentConverter)
def get_supported_conversions(self):
yield 'key0', 'Format 0', 'c0', 'text/x-sample', 'text/html', 8
class Converter2(Component):
implements(IContentConverter)
def get_supported_conversions(self):
yield 'key2', 'Format 2', 'c2', 'text/x-sample', 'text/html', 2
class Converter1(Component):
implements(IContentConverter)
def get_supported_conversions(self):
yield 'key1', 'Format 1', 'c1', 'text/x-sample', 'text/html', 4
mimeview = Mimeview(self.env)
conversions = mimeview.get_supported_conversions('text/x-sample')
self.assertEqual(Converter0(self.env), conversions[0][-1])
self.assertEqual(Converter1(self.env), conversions[1][-1])
self.assertEqual(Converter2(self.env), conversions[2][-1])
开发者ID:wiraqutra,项目名称:photrackjp,代码行数:21,代码来源:api.py
示例14: process_combinewiki
def process_combinewiki(self, req, format, title, pages):
# Dump all pages to HTML files
files = [self._page_to_file(req, p) for p in pages]
titlefile = self._page_to_file(req, title, self.TITLE_HTML%title)
# File to write PDF to
pfile, pfilename = mkstemp('tracpdf')
os.close(pfile)
# Render
os.environ["HTMLDOC_NOCGI"] = 'yes'
codepage = Mimeview(self.env).default_charset
htmldoc_format = {'pdf': 'pdf14', 'ps':'ps3'}[format]
htmldoc_args = { 'book': None, 'format': htmldoc_format, 'left': '1.5cm',
'right': '1.5cm', 'top': '1.5cm', 'bottom': '1.5cm',
'charset': codepage.replace('iso-', ''), 'title': None,
'titlefile': titlefile}
htmldoc_args.update(dict(self.env.config.options('pagetopdf')))
htmldoc_args.update(dict(self.env.config.options('combinewiki')))
args_string = ' '.join(['--%s %s' % (arg, value or '') for arg, value
in htmldoc_args.iteritems()])
cmd_string = 'htmldoc %s %s -f %s'%(args_string, ' '.join(files), pfilename)
self.log.info('CombineWikiModule: Running %r', cmd_string)
os.system(cmd_string)
out = open(pfilename, 'rb').read()
# Clean up
os.unlink(pfilename)
for f in files:
os.unlink(f)
os.unlink(titlefile)
# Send the output
req.send_response(200)
req.send_header('Content-Type', {'pdf':'application/pdf', 'ps':'application/postscript'}[format])
req.send_header('Content-Length', len(out))
req.end_headers()
req.write(out)
raise RequestDone
开发者ID:nyuhuhuu,项目名称:trachacks,代码行数:40,代码来源:formats.py
示例15: expand_macro
def expand_macro(self, formatter, name, content):
add_stylesheet(formatter.req, 'lineno/css/lineno.css')
i = 1
self._anchor = 'a1'
while self._anchor in formatter._anchors:
self._anchor = 'a' + str(i)
i += 1
formatter._anchors[self._anchor] = True
mt = 'txt'
match = WikiParser._processor_re.match(content)
if match:
try: #Trac 0.12+
mt = match.group(2)
content = content[match.end(2)+1:]
except IndexError: #Trac 0.11
mt = match.group(1)
content = content[match.end(1)+1:]
mimeview = Mimeview(formatter.env)
mimetype = mimeview.get_mimetype(mt) or mimeview.get_mimetype('txt')
return mimeview.render(formatter.context, mimetype,
content, annotations=['codeblock-lineno'])
开发者ID:nyuhuhuu,项目名称:trachacks,代码行数:24,代码来源:LinenoMacro.py
示例16: setUp
def setUp(self):
self._mp_setup()
self.global_env = self.env
self.env = ProductEnvironment(self.global_env, self.default_product)
# Product name inserted in RSS feed
self.env.product._data['name'] = 'My Project'
self.env.config.set('trac', 'templates_dir',
os.path.join(os.path.dirname(self.env.path),
'templates'))
self.ticket_module = ProductTicketModule(self.env)
self.mimeview = Mimeview(self.env)
self.req = Mock(base_path='/trac.cgi', path_info='',
href=Href('/trac.cgi'), chrome={'logo': {}},
abs_href=Href('http://example.org/trac.cgi'),
environ={}, perm=[], authname='-', args={}, tz=None,
locale='', session=None, form_token=None)
开发者ID:Stackato-Apps,项目名称:bloodhound,代码行数:18,代码来源:conversion.py
示例17: setUp
def setUp(self):
self.env = EnvironmentStub()
self.env.config.set("trac", "templates_dir", os.path.join(os.path.dirname(self.env.path), "templates"))
self.ticket_module = TicketModule(self.env)
self.mimeview = Mimeview(self.env)
self.req = Mock(
base_path="/trac.cgi",
path_info="",
href=Href("/trac.cgi"),
chrome={"logo": {}},
abs_href=Href("http://example.org/trac.cgi"),
environ={},
perm=PermissionCache(self.env, "-"),
authname="-",
args={},
tz=None,
locale="",
session=None,
form_token=None,
)
开发者ID:spsoft-RockWang,项目名称:project-_trac,代码行数:20,代码来源:conversion.py
示例18: FilePage
class FilePage(Page):
def __init__(self, env, node, root, base):
super(FilePage, self).__init__(env, node, root, base)
self.mimeview = Mimeview(self.env)
self.exists = (node is not None)
self.mime_type = None
self.chunk = None
def get_html(self, req):
"""
Get the raw content from the repository and convert to html.
"""
mime_type, chunk = self.get_raw()
if not mime_type.startswith('text'):
raise TracError("Invalid mime-type: %s" % mime_type)
# Hack to support images, we change the path from relative
# the document being requested to absolute.
# 1: Ignore http and ftp urls to allow images to be fetched
# 2: Assume URLS beginning with "/" are relative to top-level
# 3: Assume URLS that do not include "http/ftp" are relative to
# current path.
def fixup(m):
text = m.group(1)
if text.startswith('http:') or text.startswith('ftp:'):
return m.group(0)
if text.startswith('/'):
text = text[1:]
dir = self.dir
if dir.endswith('/'):
dir = dir[:-1]
return '.. image:: %s/%s' % (req.href.docs(dir), text)
chunk = re.sub('\.\. image\:\:\s*(\S+)', fixup, chunk, re.MULTILINE)
# Assume all wiki pages are ReStructuredText documents
result = self.mimeview.render(req, mime_type, chunk)
if not isinstance(result, (str, unicode)):
result = unicode(result)
# Hack to pretty-print source code (assumes all literal-blocks
# contain source code).
result = result.replace('<pre class="literal-block">',
'<pre class="literal-block prettyprint">')
if 'prettyprint' in result:
# FIXME: Add as an event listener instead?
result += """
<script type="text/javascript">
var origOnLoad = window.onload;
function onLoad() {
if (origOnLoad) {
if(typeof(origOnLoad) == "string") {
eval(origOnLoad);
}
else {
origOnLoad();
}
}
prettyPrint();
}
window.onload = onLoad;
</script>
"""
return Markup(result)
def get_raw(self):
"""
Load the raw content from the repository.
"""
if self.mime_type is not None:
return self.mime_type, self.chunk
node = self.node
content_length = node.get_content_length()
if content_length > (1024 * 1024):
raise TracError("Docs is too large: %d" % content_length)
content = node.get_content()
chunk = content.read(content_length)
mime_type = node.content_type
# Guess the mime-type when possible to be text/plain.
if not mime_type or mime_type == 'application/octet-stream':
mime_type = self.mimeview.get_mimetype(node.name, chunk) or \
mime_type or 'text/plain'
if mime_type.startswith('text/plain'):
mime_type = 'text/x-rst; charset=utf8'
self.mime_type = mime_type
self.chunk = chunk
return mime_type, chunk
def save(self, req, text, comment):
"""
Save the specified text into this document.
#.........这里部分代码省略.........
开发者ID:rcarmo,项目名称:tracdocs,代码行数:101,代码来源:web_ui.py
示例19: _render_readme
def _render_readme(self, req, stream, data):
add_stylesheet(req, 'common/css/code.css')
repos = data.get('repos') or self.env.get_repository()
rev = req.args.get('rev', None)
# Rendering all READMEs in a directory preview
for entry in data['dir']['entries']:
try:
if not entry.isdir and entry.name.lower().startswith('readme'):
node = repos.get_node(entry.path, rev)
req.perm(data['context'].resource).require('FILE_VIEW')
mimeview = Mimeview(self.env)
content = node.get_content()
mimetype = node.content_type
divclass = 'searchable'
if entry.name.lower().endswith('.wiki'):
mimetype = 'text/x-trac-wiki'
divclass = 'searchable wiki'
elif entry.name.lower().endswith('.md'):
mimetype = 'text/x-markdown'
divclass = 'searchable markdown'
if not mimetype or mimetype == 'application/octet-stream':
mimetype = mimeview.get_mimetype(
node.name, content.read(4096)) or \
mimetype or 'text/plain'
del content
self.log.debug(
"ReadmeRenderer: rendering node %[email protected]%s as %s",
node.name, rev, mimetype)
output = mimeview.preview_data(
data['context'],
node.get_content(),
node.get_content_length(),
mimetype,
node.created_path,
'',
annotations=[],
force_source=False)
if output:
if isinstance(output['rendered'], Stream):
content = output['rendered'].select('.')
else:
content = output['rendered']
insert = tag.div(
tag.h1(entry.name,
tag.a(Markup(' ¶'),
class_="anchor",
href='#' + entry.name,
title='Link to file'),
id_=entry.name),
tag.div(content,
class_=divclass,
title=entry.name),
class_="readme",
style="padding-top: 1em;"
)
xpath = "//div[@id='content']/div[@id='help']"
stream |= Transformer(xpath).before(insert)
except Exception, e:
self.log.debug(to_unicode(e))
开发者ID:trac-hacks,项目名称:trac-readme-plugin,代码行数:62,代码来源:__init__.py
示例20: send_as_email
def send_as_email(self, req, sender, recipients, subject, text, mode, *resources):
"""
`authname` Trac username of sender
`sender` Tuple of (real name, email address)
`recipients` List of (real name, email address) recipient address tuples
`subject` The e-mail subject
`text` The text body of the e-mail
`files` List of paths to the files to send
"""
assert len(resources) > 0, 'Nothing to send!'
mailsys = self.distributor(self.env)
from_addr = sender[1]
root = MIMEMultipart('related')
root.set_charset(self._make_charset())
root.preamble = 'This is a multi-part message in MIME format.'
headers = {}
recp = [r[1] for r in recipients]
headers['Subject'] = subject
headers['To'] = ', '.join(recp)
headers['From'] = from_addr
headers['Date'] = formatdate()
authname = req.authname
files = []
links = []
attachments = []
mimeview = Mimeview(self.env)
for r in resources:
repo = RepositoryManager(self.env).get_repository(r.parent.id)
n = repo.get_node(r.id, rev=r.version)
files.append(n.path)
f = os.path.join(repo.repos.path, n.path)
if mode in (self.LINKS_ONLY, self.LINKS_ATTACHMENTS):
links.append((req.abs_href.browser(repo.reponame or None, n.path, format='raw'), os.path.basename(f)))
if mode in (self.ATTACHMENTS_ONLY, self.LINKS_ATTACHMENTS):
content = n.get_content().read()
mtype = n.get_content_type() or mimeview.get_mimetype(f, content)
if not mtype:
mtype = 'application/octet-stream'
if '; charset=' in mtype:
# What to use encoding for?
mtype, encoding = mtype.split('; charset=', 1)
attachments.append(os.path.basename(f))
maintype, subtype = mtype.split('/', 1)
part = MIMEBase(maintype, subtype)
part.set_payload(content)
part.add_header('content-disposition', 'attachment', filename=os.path.basename(f))
encode_base64(part)
root.attach(part)
body = self._format_email(authname, sender, recipients, subject, text, mode, links, attachments)
msg = MIMEText(body, 'html', 'utf-8')
root.attach(msg)
del root['Content-Transfer-Encoding']
for k, v in headers.items():
set_header(root, k, v)
email = (from_addr, recp, root.as_string())
# Write mail to /tmp
#import logging
#if self.log.isEnabledFor(logging.DEBUG):
# (fd, tmpname) = mkstemp()
# os.write(fd, email[2])
# os.close(fd)
# self.log.debug('Wrote mail from %s to %s to %s', email[0], email[1], tmpname)
self.log.info('Sending mail with items %s from %s to %s', resources, from_addr, recp)
try:
if using_announcer:
if mailsys.use_threaded_delivery:
mailsys.get_delivery_queue().put(email)
else:
mailsys.send(*email)
else:
mailsys.send_email(*email)
except Exception, e:
raise TracError(e.message)
开发者ID:CGI-define-and-primeportal,项目名称:trac-plugin-sourcesharing,代码行数:73,代码来源:sourcesharer.py
注:本文中的trac.mimeview.api.Mimeview类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论