This file is indexed.

/usr/share/pyshared/swap/n3p_tm.py is in python-swap 1.2.1-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""Why??

$$
"""



from n3p import n3p
import os
from uripath import join

class absolutizer(unicode):
    def __getitem__(self, other):
        return join(self, other)

Logic_NS = "http://www.w3.org/2000/10/swap/log#"
RDF_type_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
RDF_NS_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
# DAML_NS=DPO_NS = "http://www.daml.org/2001/03/daml+oil#"  # DAML plus oil
OWL_NS = "http://www.w3.org/2002/07/owl#"
DAML_sameAs_URI = OWL_NS+"sameAs"
parsesTo_URI = Logic_NS + "parsesTo"
RDF_spec = "http://www.w3.org/TR/REC-rdf-syntax/"

#List_NS = DPO_NS     # We have to pick just one all the time
List_NS = RDF_NS_URI     # From 20030808


LOG_implies_URI = "http://www.w3.org/2000/10/swap/log#implies"

INTEGER_DATATYPE = "http://www.w3.org/2001/XMLSchema#integer"
FLOAT_DATATYPE = "http://www.w3.org/2001/XMLSchema#double"
DECIMAL_DATATYPE = "http://www.w3.org/2001/XMLSchema#decimal"

NORMAL = 1
PREFIX = 2
FORSOME = 4
FORALL = 5
NEED_PERIOD = 8
LITERAL = 9
KEYWORDS = 10

class n3p_tm(n3p.N3Parser):
    def __init__(self, uri, tm, baseURI=False):
        n3p.N3Parser.__init__(self, 'nowhere', n3p.branches, n3p.regexps)
        self.tm = tm
        self.productions = []
        if baseURI:
            self._baseURI = baseURI + '#'
        else:
            self._baseURI = uri + '#'
        self.prefixes = {'':self._baseURI}
        self.abs = absolutizer(self._baseURI[:-1])

    def parse(self, start=n3p.start):
        n3p.N3Parser.parse(self, start)

    def startDoc(self):
        self.tm.start()

    def endDoc(self):
        return self.tm.end()

    def feed(self, data):
        self.data = data.decode('utf_8')
        self._mode = NORMAL
        self.parse()

    def onStart(self, prod):
        self.productions.append(prod)
        if prod == 'literal':
            self._mode = LITERAL
        #print 'begin' + prod

    def onFinish(self):
        prod = self.productions.pop()
        if prod == 'literal':
            self.tm.addLiteral(self.literal['string'], self.literal['type'], self.literal['lang'])
            self._mode = NORMAL
        #print 'end' + prod

    def onToken(self, prod, tok):
        tm = self.tm
        if 1:
            if self._mode == NORMAL:
                if prod == '.':
                    try:
                        tm.endStatement()
                    except ValueError:
                        tm.forewardPath()
                elif prod == ';':
                    tm.endStatement()
                    tm.addNode(None)
                elif prod == ',':
                    tm.endStatement()
                    tm.addNode(None)
                    tm.addNode(None)
                elif prod == '=>':
                    tm.addSymbol(LOG_implies_URI)

                elif prod == '<=':
                    tm.IsOf()
                    tm.addSymbol(LOG_implies_URI)
                elif prod == '=':
                    tm.addSymbol(DAML_sameAs_URI)
                elif prod == '@forAll':
                    self._mode = FORALL
                elif prod == '@forSome':
                    self._mode = FORSOME
                elif prod == '@a':
                    tm.addSymbol(RDF_type_URI)
                elif prod == '@is':
                    tm.IsOf()
                elif prod == '@of':
                    assert tm.checkIsOf()
                elif prod == '{':
                    tm.beginFormula()
                elif prod == '}':
                    tm.endFormula()
                elif prod == '[':
                    tm.beginAnonymous()
                elif prod == ']':
                    tm.endAnonymous()
                elif prod == '(':
                    tm.beginList()
                elif prod == ')':
                    tm.endList()
                elif prod == '@has':
                    pass
                elif prod == '@this':
                    tm.addNode('@this')
                elif prod == '^':
                    tm.backwardPath()
                elif prod == '!':
                    tm.forewardPath()
                elif prod == '@prefix':
                    self._mode = PREFIX
                elif prod == 'qname':
                    ret = self.decodeQName(tok, True)
                    if type(ret) is tuple:
                        tm.addAnonymous(ret[0])
                    else:
                        tm.addSymbol(ret)
                elif prod == 'explicituri':
                    tm.addSymbol(self.abs[tok[1:-1]])
                elif prod == 'numericliteral':
                    if '.' in tok or 'e' in tok:
                        tm.addLiteral(float(tok))
                    else:
                        tm.addLiteral(int(tok))
                elif prod == 'variable':
                    tm.addQuestionMarkedSymbol(self._baseURI+tok[1:])
                elif prod == '@keywords':
                    self._mode = KEYWORDS
                
            elif self._mode == LITERAL:
                if prod == 'string':
                    self.literal = {'string': unEscape(tok), 'type':None, 'lang':None}
                elif prod == 'qname':
                    self.literal['type'] = self.decodeQName(tok, False)
                elif prod == 'explicituri':
                    self.literal['type'] = self.abs[tok[1:-1]]
                elif prod == 'langcode':
                    self.literal['lang'] = tok
                else:
                    pass
            elif self._mode == PREFIX:
                if prod == 'qname':
                    n = tok.find(':')
                    self._newPrefix = tok[:n]
                elif prod == 'explicituri':
                    tm.bind(self._newPrefix, self.abs[tok[1:-1]])
                    self.prefixes[self._newPrefix] = self.abs[tok[1:-1]]
                elif prod == '.':
                    self._mode = NORMAL
            elif self._mode == FORALL:
                if prod == 'qname':
                    uri = self.decodeQName(tok, False)
                    tm.declareUniversal(uri)
                elif prod == 'explicituri':
                    tm.declareUniversal(self.abs[tok[1:-1]])
                elif prod == ',':
                    pass
                elif prod == '.':
                    self._mode = NORMAL
            elif self._mode == FORSOME:
                if prod == 'qname':
                    uri = self.decodeQName(tok, False)
                    tm.declareExistential(uri)
                elif prod == 'explicituri':
                    tm.declareExistential(self.abs[tok[1:-1]])
                elif prod == ',':
                    pass
                elif prod == '.':
                    self._mode = NORMAL
                    
        if prod == '.':
            self._mode = NORMAL
        parent = self.productions[-1]
        #print 'mode=', self._mode, '. prod=', prod, '. tok =', tok


    def decodeQName(self, qName, bNode):
        n = qName.find(':')
        if n < 0:
            pfx = ''
            frag = qName
        else:
            pfx = qName[:n]
            frag = qName[n+1:]
        try:
            self.prefixes[pfx]
        except KeyError:
            if pfx == '_':
                if bNode:
                    return (frag,)
                else:
                    return qName
            raise
        return self.prefixes[pfx]+frag

def unEscape(string):
    if string[:3] == '"""':
        real_str = string[3:-3]
        triple = True
    else:
        real_str = string[1:-1]
        triple = False
    ret = u''
    n = 0
    while n < len(real_str):
        ch = real_str[n]
        if ch == '\r':
            pass
        elif ch == '\\':
            a = real_str[n+1:n+2]
            if a == '':
                raise RuntimeError
            k = 'abfrtvn\\"'.find(a)
            if k >= 0:
                ret += '\a\b\f\r\t\v\n\\"'[k]
                n += 1
            elif a == 'u':
                m = real_str[n+2:n+6]
                assert len(m) == 4
                ret += unichr(int(m, 16))
                n += 5
            elif a == 'U':
                m = real_str[n+2:n+10]
                assert len(m) == 8
                ret += unichr(int(m, 16))
                n += 9
            else:
                raise ValueError('Bad Escape')
        else:
            ret += ch
                
        n += 1
        
        
    return ret



def parse(uri, options): 
   baseURI = options.baseURI
   sink = object()
   if options.root: 
      sink.quantify = lambda *args: True
      sink.flatten = lambda *args: True
   if ':' not in uri: 
      uri = 'file://' + os.path.join(os.getcwd(), uri)
   if baseURI and (':' not in baseURI): 
      baseURI = 'file://' + os.path.join(os.getcwd(), baseURI)
   p = n3p_tm(uri, sink, baseURI=baseURI)
   p.parse()

def main(argv=None): 
   import optparse

   class MyHelpFormatter(optparse.HelpFormatter): 
      def __init__(self): 
         kargs = {'indent_increment': 2, 'short_first': 1, 
                  'max_help_position': 25, 'width': None}
         optparse.HelpFormatter.__init__(self, **kargs)
      def format_usage(self, usage): 
         return optparse._("%s") % usage.lstrip()
      def format_heading(self, heading): 
         return "%*s%s:\n" % (self.current_indent, "", heading)
   formatter = MyHelpFormatter()

   parser = optparse.OptionParser(usage=__doc__, formatter=formatter)
   parser.add_option("-b", "--baseURI", dest="baseURI", default=False, 
                     help="set the baseURI", metavar="URI")
   parser.add_option("-r", "--root", dest="root", 
                     action="store_true", default=False, 
                     help="print triples in the root formula only")
   options, args = parser.parse_args(argv)

   if len(args) == 1: 
      parse(args[0], options)
   else: parser.print_help()

if __name__=="__main__": 
   main()