Package redlink :: Module format
[frames] | no frames]

Source Code for Module redlink.format

 1  # -*- coding: utf8 -*- 
 2   
 3  # Licensed under the Apache License, Version 2.0 (the "License"); 
 4  # you may not use this file except in compliance with the License. 
 5  # You may obtain a copy of the License at 
 6  # 
 7  #     http://www.apache.org/licenses/LICENSE-2.0 
 8  # 
 9  # Unless required by applicable law or agreed to in writing, software 
10  # distributed under the License is distributed on an "AS IS" BASIS, 
11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
12  # See the License for the specific language governing permissions and 
13  # limitations under the License. 
14   
15   
16 -class FormatDef:
17 """ 18 Format (internal) definition 19 """ 20
21 - def __init__(self, name, mimetype, rdflibMapping=None):
22 self.name = name 23 self.mimetype = mimetype 24 self.rdflibMapping = rdflibMapping
25
26 - def __str__(self):
27 return "%s[%s]" % (self.name, self.mimetype)
28
29 - def __cmp__(self, other):
30 if other is None: 31 return -1 32 elif isinstance(other, FormatDef): 33 return -1 * int(not self.mimetype == other.mimetype) 34 elif "/" in other: 35 if ";" in other: 36 return -1 * int(not self.mimetype == other.split(";")[0]) 37 else: 38 return -1 * int(not self.mimetype == other) 39 else: 40 return -1 * int(not self.name == other)
41
42 - def __eq__(self, other):
43 if other is None: 44 return -1 45 elif isinstance(other, FormatDef): 46 return self.mimetype == other.mimetype 47 elif "/" in other: 48 if ";" in other: 49 return self.mimetype == other.split(";")[0] 50 else: 51 return self.mimetype == other 52 else: 53 return self.name == other
54 55
56 -class Format:
57 """ 58 Redlink formats 59 """ 60 61 TEXT = FormatDef("text", "text/plain") 62 PDF = FormatDef("pdf", "application/pdf") 63 HTML = FormatDef("html", "text/html") 64 OFFICE = FormatDef("office", "application/doc") 65 OCTETSTREAM = FormatDef("octetstream", "application/octet-stream") 66 67 JSON = FormatDef("json", "application/json") 68 XML = FormatDef("xml", "application/xml") 69 REDLINKJSON = FormatDef("redlinkjson", "application/redlink-analysis+json") 70 REDLINKXML = FormatDef("redlinkxml", "application/redlink-analysis+xml") 71 72 JSONLD = FormatDef("jsonld", "application/ld+json", "json-ld") 73 RDFXML = FormatDef("rdfxml", "application/rdf+xml", "xml") 74 RDFJSON = FormatDef("rdfjson", "application/rdf+json") 75 TURTLE = FormatDef("turtle", "text/turtle", "turtle") 76 NT = FormatDef("nt", "text/rdf+n3", "n3")
77 78
79 -def from_mimetype(mimetype):
80 """ 81 Returns a C{FormatDef} representing the passed mimetype 82 83 @type mimetype: str 84 @param mimetype: format mimetype 85 @return: format 86 """ 87 for name, format in Format.__dict__.items(): 88 if isinstance(format, FormatDef): 89 if format == mimetype: 90 return format 91 return None
92