2014-09-01 17:01:13 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-09-19 11:05:14 +02:00
|
|
|
from sqlalchemy import create_engine, MetaData
|
2014-09-16 11:34:53 +02:00
|
|
|
from sqlalchemy.ext.automap import automap_base
|
|
|
|
from sqlalchemy.orm import Session
|
2014-09-02 16:31:02 +02:00
|
|
|
|
|
|
|
|
2014-09-16 13:51:51 +02:00
|
|
|
class Project():
|
2014-09-16 11:34:53 +02:00
|
|
|
""" A helper class representing a Citavi project. """
|
|
|
|
def __init__(self, sqlite_file):
|
|
|
|
""" Constructor to get absolute path to sqlite. """
|
|
|
|
self.sqlite_file = sqlite_file
|
|
|
|
self._is_open = False
|
|
|
|
self._is_error = False
|
2014-09-19 11:05:14 +02:00
|
|
|
self._sa = {} # Object namespace for sqlalchemy related objects
|
2014-09-18 11:46:53 +02:00
|
|
|
|
2014-09-16 11:34:53 +02:00
|
|
|
def __del__(self):
|
|
|
|
""" Destructor to close the citavi file. """
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def is_valid(self):
|
|
|
|
""" This method returns False in case the given sqlite file is not a valid Citavi project. """
|
|
|
|
return self._is_open and not self._is_error
|
|
|
|
|
|
|
|
def get_persons(self):
|
2014-09-16 13:51:51 +02:00
|
|
|
""" Returns all persons from the Citavi project. """
|
2014-09-16 11:34:53 +02:00
|
|
|
try:
|
2014-09-16 13:51:51 +02:00
|
|
|
person_class = self._sa_sqlite_autobase.classes.Person
|
|
|
|
citavi_persons = self._sa_sqlite_session.query(person_class).all()
|
|
|
|
return citavi_persons
|
2014-09-16 11:34:53 +02:00
|
|
|
except:
|
2014-09-16 13:51:51 +02:00
|
|
|
self._is_error = True
|
|
|
|
# TODO: better error handling!
|
2014-09-29 15:01:35 +02:00
|
|
|
print u"An error occured within a get_persons call!"
|
2014-09-18 11:46:53 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def get_person_by_uuid(self, uuid):
|
|
|
|
""" Returns a person from the Citavi project by their uuid. """
|
|
|
|
try:
|
|
|
|
person_class = self._sa_sqlite_autobase.classes.Person
|
2014-09-29 15:01:35 +02:00
|
|
|
citavi_person = self._sa_sqlite_session.query(person_class).filter(u'ID=\'' + unicode(uuid) + u'\'').all()
|
2014-09-18 11:46:53 +02:00
|
|
|
return citavi_person[0]
|
|
|
|
except:
|
|
|
|
self._is_error = True
|
|
|
|
# TODO: better error handling!
|
2014-09-29 15:01:35 +02:00
|
|
|
print u"An error occured within a get_person_by_uuid call!"
|
2014-09-16 11:34:53 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
""" Public method to open a citavi project file. """
|
|
|
|
if not self._is_open:
|
|
|
|
self._open()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
""" Public method to close a citavi project file. """
|
|
|
|
if self._is_open:
|
|
|
|
self._close()
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
""" Internal method to open a citavi project file. """
|
|
|
|
try:
|
2014-09-29 15:01:35 +02:00
|
|
|
self._sa_sqlite_engine = create_engine(u'sqlite+pysqlite:///' + self.sqlite_file)
|
2014-09-16 11:34:53 +02:00
|
|
|
self._sa_sqlite_session = Session(self._sa_sqlite_engine)
|
|
|
|
self._sa_sqlite_meta = MetaData(bind=self._sa_sqlite_engine)
|
|
|
|
self._sa_sqlite_meta.reflect()
|
|
|
|
self._sa_sqlite_autobase = automap_base()
|
|
|
|
self._sa_sqlite_autobase.prepare(self._sa_sqlite_engine, reflect=True)
|
|
|
|
self._is_open = True
|
|
|
|
except:
|
|
|
|
self._is_error = True
|
|
|
|
self._is_open = False
|
|
|
|
|
|
|
|
def _close(self):
|
|
|
|
""" Internal method to actually close a citavi project file. """
|
|
|
|
try:
|
|
|
|
del self._sa.sqlite_engine
|
|
|
|
del self._sa.sqlite_session
|
|
|
|
del self._sa.sqlite_meta
|
|
|
|
del self._sa.sqlite_autobase
|
|
|
|
del self._sa.sqlite_engine
|
|
|
|
self._is_open = False
|
|
|
|
except:
|
|
|
|
self._is_error = True
|
|
|
|
self._is_open = True
|
|
|
|
|
|
|
|
|
2014-09-18 11:46:53 +02:00
|
|
|
class ProjectManager():
|
|
|
|
""" A backend to provide fast access to Citavi identity data. """
|
2014-09-16 11:34:53 +02:00
|
|
|
|
2014-09-16 13:51:51 +02:00
|
|
|
def __init__(self):
|
2014-09-18 11:46:53 +02:00
|
|
|
""" Constructor initializing dictionary for instances of Project. """
|
|
|
|
self._projects = {}
|
|
|
|
|
2014-09-16 13:51:51 +02:00
|
|
|
def __del__(self):
|
2014-09-18 11:46:53 +02:00
|
|
|
""" Destructor making sure all Project instances are properly deconstructed. """
|
|
|
|
self._projects.clear()
|
2014-09-16 11:34:53 +02:00
|
|
|
|
2014-09-18 11:46:53 +02:00
|
|
|
def get_path_for_project_id(self, project_id):
|
2014-09-29 15:01:35 +02:00
|
|
|
return u'media/citavi/project_' + unicode(project_id) + u'.ctt4'
|
2014-09-16 11:34:53 +02:00
|
|
|
|
2014-09-18 11:46:53 +02:00
|
|
|
def _add_project(self, project_id):
|
|
|
|
""" Internal method to add a Project instance if not existing. """
|
|
|
|
if project_id not in self._projects:
|
|
|
|
self._projects[project_id] = Project(self.get_path_for_project_id(project_id))
|
|
|
|
self._projects[project_id].open()
|
2014-09-16 11:34:53 +02:00
|
|
|
|
2014-09-18 11:46:53 +02:00
|
|
|
def get_person_by_uuid(self, project_id, uuid):
|
2014-09-29 13:40:07 +02:00
|
|
|
""" Returns the person matching the given uuid. """
|
2014-09-18 11:46:53 +02:00
|
|
|
self._add_project(project_id)
|
|
|
|
return self._projects[project_id].get_person_by_uuid(uuid)
|
|
|
|
|
|
|
|
def get_persons_from_project(self, project_id):
|
2014-09-29 13:40:07 +02:00
|
|
|
""" Returns all person from a projects. """
|
2014-09-18 11:46:53 +02:00
|
|
|
self._add_project(project_id)
|
|
|
|
return self._projects[project_id].get_persons()
|