summaryrefslogtreecommitdiff
path: root/utils/data-generators/internals/tools.py
blob: 30f89f78803d8bf8190d75786ee07ecb872ae281 (plain)
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
import datetime, sys
import random

NAMESPACES = [
    	("rdf", "<http://www.w3.org/2000/01/rdf-schema#>"),
	("nrl", "<http://www.semanticdesktop.org/ontologies/2007/08/15/nrl#>"),
	("nid3","<http://www.semanticdesktop.org/ontologies/2007/05/10/nid3#>"),
	("nao", "<http://www.semanticdesktop.org/ontologies/2007/08/15/nao#>"),
	("nco", "<http://www.semanticdesktop.org/ontologies/2007/03/22/nco#>"),
	("nmo", "<http://www.semanticdesktop.org/ontologies/2007/03/22/nmo#>"),
	("nfo", "<http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#>"),
	("nie", "<http://www.semanticdesktop.org/ontologies/2007/01/19/nie#>"),
	("ncal", "<http://www.semanticdesktop.org/ontologies/2007/04/02/ncal#>"),
	("xsd", "<http://www.w3.org/2001/XMLSchema#>")
    ]

def print_namespaces ():
    for prefix, uri in NAMESPACES:
        print "@prefix %s: %s." % (prefix, uri)
    print ""


def print_property (property_name, value, t="str", final=False):

    if (value):
        if (final):
            end_line = ".\n"
        else:
            end_line = ";"
        try:
            
            if (t == "str"):
                print '\t%s "%s"%s' % (property_name.encode ('utf8'),
                                       str(value).encode ('utf8').replace('"','\\"').replace('\n','\\n'), end_line)
            elif (t == "uri"):
                print '\t%s <%s>%s' % (property_name.encode ('utf8'),
                                       value.encode ('utf8'), end_line)
            elif (t == "int"):
                print '\t%s %s%s' % (property_name.encode ('utf8'),
                                     str(value), end_line)
        except (UnicodeDecodeError, UnicodeEncodeError):
            print >> sys.stderr, "Encoding error in %s %s %s" % (property_name,
                                                                 value,
                                                                 end_line)

def print_instance (uri, klass, final=False):
    if (final):
        append = "."
    else:
        append = ";"
    print "<%s> a %s%s" % (uri, klass, append)

def print_anon_node (prop, objtype, objprop, objpropvalue, t="str", final=False):

    delimiter_before = "\""
    delimiter_after  = "\""
    if (t == "uri"):
        delimiter_before = "<"
        delimiter_after = ">"

    if (final):
        end = ".\n"
    else:
        end = ";"
    
    print "\t%s [a %s; %s %s%s%s]%s" % (prop, objtype, objprop,
                                        delimiter_before, objpropvalue, delimiter_after,
                                        end)


def getPseudoRandomDate ():
    moment = datetime.datetime.now() - datetime.timedelta(days=random.randint(0, 400), minutes=random.randint(0, 59), seconds=random.randint(0, 59))
    return moment.isoformat ().split('.')[0]

def get_random_uuid_uri ():
    return "urn:uuid:" + str(random.randint(0, sys.maxint))

def get_random_in_list (l):
    if len(l) == 0:
        return None

    return l[random.randint (0, len(l) - 1)]