File: tools.py

package info (click to toggle)
tinysparql 3.10.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,468 kB
  • sloc: ansic: 118,310; python: 6,139; javascript: 719; sh: 121; perl: 106; xml: 67; makefile: 31; sql: 1
file content (42 lines) | stat: -rw-r--r-- 1,090 bytes parent folder | download | duplicates (4)
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
# -*- coding: utf-8 -*-

import string
import random
import datetime
import os

import ontology_prefixes

output_filenames = {}
last_uris = {}
result = {}
now = datetime.datetime.today().strftime('%Y-%m-%dT%H:%M:%SZ')

####################################################################################
def addType(name, order):
  output   = '%03d-' % order + name.replace( '#', '_') + '.ttl'

  output_filenames[name] = output
  result[name] = []
  last_uris[name] = []

def addItem(itemtype, uri, content):
  last_uris[itemtype].append( uri )
  result[itemtype].append( content )

def getLastUri(type):
  return last_uris[type][-1]

def getRandomUri(type):
  return random.choice(last_uris[type])

def saveResult (output_dir=None):
  output_dir = output_dir or 'ttl'
  for ontology, content in list(result.items()):
    print('Saving', output_filenames[ontology], '...')
    path = os.path.join(output_dir, output_filenames[ontology])
    output = open(path, 'w')
    output.write( ontology_prefixes.ontology_prefixes )
    for it in content:
      output.write( it )
    output.close()