Ajout SEP
authorCyril Robert <Cyril Robert crobert@inverse.ca>
Wed, 24 Feb 2010 22:19:04 +0000 (17:19 -0500)
committerCyril Robert <Cyril Robert crobert@inverse.ca>
Wed, 24 Feb 2010 22:19:04 +0000 (17:19 -0500)
12 files changed:
Apps/auf_sep/.conf.py.swp [new file with mode: 0644]
Apps/auf_sep/__init__.py [new file with mode: 0644]
Apps/auf_sep/harvest/__init__.py [new file with mode: 0644]
Apps/auf_sep/harvest/harvest.py [new file with mode: 0644]
Apps/auf_sep/harvest/harvesters/__init__.py [new file with mode: 0644]
Apps/auf_sep/sep/.io.py.swp [new file with mode: 0644]
Apps/auf_sep/sep/__init__.py [new file with mode: 0644]
Apps/auf_sep/sep/io.py [new file with mode: 0644]
Apps/auf_sep/setup.py [new file with mode: 0644]
Apps/auf_sep/storage/.json.py.swp [new file with mode: 0644]
Apps/auf_sep/storage/__init__.py [new file with mode: 0644]
Apps/auf_sep/storage/json.py [new file with mode: 0644]

diff --git a/Apps/auf_sep/.conf.py.swp b/Apps/auf_sep/.conf.py.swp
new file mode 100644 (file)
index 0000000..b680080
Binary files /dev/null and b/Apps/auf_sep/.conf.py.swp differ
diff --git a/Apps/auf_sep/__init__.py b/Apps/auf_sep/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/Apps/auf_sep/harvest/__init__.py b/Apps/auf_sep/harvest/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/Apps/auf_sep/harvest/harvest.py b/Apps/auf_sep/harvest/harvest.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/Apps/auf_sep/harvest/harvesters/__init__.py b/Apps/auf_sep/harvest/harvesters/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/Apps/auf_sep/sep/.io.py.swp b/Apps/auf_sep/sep/.io.py.swp
new file mode 100644 (file)
index 0000000..228a693
Binary files /dev/null and b/Apps/auf_sep/sep/.io.py.swp differ
diff --git a/Apps/auf_sep/sep/__init__.py b/Apps/auf_sep/sep/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/Apps/auf_sep/sep/io.py b/Apps/auf_sep/sep/io.py
new file mode 100644 (file)
index 0000000..3f41966
--- /dev/null
@@ -0,0 +1,58 @@
+# -*- encoding: utf-8 -*-
+from exceptions import Exception
+import sys
+sys.path.append ("../")
+import conf
+
+
+class SEP:
+    backend = None
+    required_methods = ['open', 'close', 'add', 'delete', 'search']
+
+    def __init__ (self):
+        # Charge le backend de stockage
+        module = 'storage.%s' % conf.BACKEND['plugin']
+        __import__ (module)
+        backend_module = sys.modules[module]
+        self.backend = backend_module.Backend (conf.BACKEND['options'])
+
+        # Vérifier qu'il répond à l'API
+        available = dir (self.backend)
+        for method in self.required_methods:
+            if not method in available:
+                msg = "Backend incomplet\nManque %s\nexiste %s" \
+                        % (method, available)
+                raise Exception(msg)
+
+        # Charge
+        self.backend.open ()
+
+
+    def __del__ (self):
+        self.backend.close ()
+
+#############
+# API privé
+
+
+#############
+# API public
+    def search (self, id = None, searchstring = None):
+        return self.backend.search (id, searchstring)
+
+    def add (self, id, metadata):
+        self.backend.add (id, metadata)
+
+    def delete (self, id):
+        self.backend.delete (id)
+
+    def update (self, id, metadata):
+        self.remove (id)
+        self.add (id, metadata)
+
+
+
+if __name__ == '__main__':
+    s = SEP()
+    print s.search (id=0)
+
diff --git a/Apps/auf_sep/setup.py b/Apps/auf_sep/setup.py
new file mode 100644 (file)
index 0000000..a78276e
--- /dev/null
@@ -0,0 +1,22 @@
+# -*- encoding: utf-8 -*-
+from setuptools import setup, find_packages
+import sys, os
+
+version = '0.1'
+
+setup(
+        name='auf_sep',
+        version=version,
+        description="Savoirs en partage, module de stockage/indexation",
+        classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
+        keywords='',
+        author='Cyril Robert',
+        author_email='cyril.robert@auf.org',
+        url='',
+        license='GPL',
+        packages=['auf_sep',],
+        package_dir={'auf_sep': '.',},
+        include_package_data=True,
+        zip_safe=False,
+        install_requires=[],
+        )
diff --git a/Apps/auf_sep/storage/.json.py.swp b/Apps/auf_sep/storage/.json.py.swp
new file mode 100644 (file)
index 0000000..567a0ba
Binary files /dev/null and b/Apps/auf_sep/storage/.json.py.swp differ
diff --git a/Apps/auf_sep/storage/__init__.py b/Apps/auf_sep/storage/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/Apps/auf_sep/storage/json.py b/Apps/auf_sep/storage/json.py
new file mode 100644 (file)
index 0000000..54c3813
--- /dev/null
@@ -0,0 +1,89 @@
+# -*- encoding: utf-8 -*-
+import os
+import simplejson as json
+
+####
+# Attention: pas efficace
+
+INDEX_NAME = 'index.json'
+DATA_FMT = '%s.json'
+
+
+class File:
+    def __init__ (self, path, mode):
+        self.fp = open (path, mode)
+
+    def __del__ (self):
+        if self.fp:
+            self.fp.close ()
+
+    def read (self):
+        return json.load (self.fp)
+
+    def write (self, object):
+        json.dump (object, self.fp)
+
+
+class Backend:
+    options = {}
+    data = {}
+    needs_write = False
+    remove = []
+
+    def __init__ (self, options):
+        self.options = options
+        # juste pour lancer une exception si jamais on a pas le path
+        p = self.options['path']
+
+    def open (self):
+        p = self.options['path']
+        ip = os.path.join (p, INDEX_NAME)
+
+        if not os.path.exists (p):
+            os.makedirs (p)
+
+        elif os.path.exists (ip):
+            index = File (ip, 'r').read ()
+            for id in index:
+                fp = os.path.join (p, DATA_FMT % id)
+                self.data[id] = File (fp, 'r').read ()
+                self.data[id]['needs_write'] = False
+            #print self.data
+
+    def close (self):
+        if self.needs_write:
+            p = self.options['path']
+            
+            # Enleve les vieux trucs
+            if len (self.remove) > 0:
+                for id in self.remove:
+                    fp = os.path.join (p, DATA_FMT % id)
+                    os.remove (fp)
+
+            # Ecrit l'index
+            File(os.path.join(p, INDEX_NAME), 'w').write(self.data.keys().sort())
+
+            # Ecrit les données
+            for id in self.data.keys ():
+                record = self.data[id]
+                if record['needs_write']:
+                    del (record['needs_write'])
+                    fp = os.path.join (p, DATA_FMT % id)
+                    File (fp, 'w').write (record)
+
+    def add (self, id, metadata):
+        self.data[id] = metadata
+        self.data[id]['needs_write'] = True
+
+    def delete (self, id):
+        del (self.data[id])
+        self.remove.append (id)
+
+    def search (self, id, searchstring):
+        rc = None
+
+        if id is not None and id in self.data.keys():
+            rc = self.data[id]
+
+        return rc
+