# <one line to give the library's name and an idea of what it does.>
# Copyright (C) 2014  <copyright holder> <email>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os, sys
import networkx
import argparse
try:
  import argcomplete
except ImportError:
  argcomplete = None

def make_parser(desc = ''):
  aparser = argparse.ArgumentParser(description = desc)
  aparser.add_argument(
    '-gd', '--graphs-dir', dest='graphs_dir', required=True, type=str,
    help=('Directory where the graphs are stored.'))
  aparser.add_argument(
    '-pd', '--pack-dir', dest='pack_dir', required=True, type=str,
    help=('Directory where the linguists packs are stored'))

  if argcomplete:
    argcomplete.autocomplete(aparser)
  return aparser

def read_graphs(graphs_dir):
  """
  Reads graphs from given directory

  @return dictionary where the key is filename and value is graph
  @rtype dict[str]->{networkx.MultiDiGraph}
  """
  gdict = {}
  if not os.path.exists(graphs_dir):
    raise Exception("Given directory does not exists!")

  for gf in os.listdir(graphs_dir):
    if not gf.endswith(".pickle"):
      continue
    print >> sys.stderr, "Reading graph {}".format(gf) 
    g = networkx.read_gpickle("{}/{}".format(graphs_dir, gf))
    if not gdict.has_key(gf):
      gdict[gf] = None
    gdict[gf] = g
  return gdict