#!/usr/bin/env python

from rdflib.Graph import ConjunctiveGraph
from rdflib import Namespace, Literal
from rdflib.URIRef import URIRef
from rdflib import RDF
from optparse import OptionParser
from sys import stdout
from lcsh.namespaces import *

opt_parser = OptionParser()
opt_parser.set_usage("usage: concept [options] prefLabel [prefLabel]*")
opt_parser.add_option('--depth', type="int", default=1, dest='depth', 
  help='amount of broader/narrower relationships to traverse')
opt_parser.add_option('--format', default='n3', dest='format',
  help='format to use in output: n3, rdf/xml, nt')
opts, args = opt_parser.parse_args()

g = ConjunctiveGraph('Sleepycat')
g.open('store')

subgraph = ConjunctiveGraph()
subgraph.bind('lcsh' , LCSH)
subgraph.bind('skos', SKOS)
subgraph.bind('dcterms', DCTERMS)

def get_uri_for_label(label):
  q = "SELECT ?lccn ?p ?o WHERE { ?lccn skos:prefLabel ?prefLabel . }"
  triples = list(g.query(q, {'?prefLabel' : Literal(label, "en")}, {'skos' : SKOS}))
  if len(triples) > 0:
    return triples[0][0]
  return None

def get_concepts(s, depth=1, seen=None):
  if depth != 0 and s not in seen:
    for p, o in g.predicate_objects(s):
      yield(s,p,o)
      seen.add(s)
      if (o, RDF.type, SKOS['Concept']) in g:
        for tuple in get_concepts(o, depth=depth-1, seen=seen):
          yield tuple

try:
  if args[0].startswith(LCSH):
    uri = URIRef(args[0])
  else:
    uri = get_uri_for_label(args[0])
  if uri:
    for triple in get_concepts(uri, depth=opts.depth, seen=set()):
      if type(triple) != tuple: continue
      subgraph.add(triple)

  subgraph.serialize(stdout, opts.format)
  print

finally:
  g.close()
