Permalink
Browse files

Initial commit

git-svn-id: http://libmtag.googlecode.com/svn/python/trunk@13 195de3f0-f62e-0410-be82-1f248da840de
  • Loading branch information...
0 parents commit fe814350708d8013a7723d6b09a267fab9b9781c @felipec committed May 8, 2007
Showing with 232 additions and 0 deletions.
  1. +9 −0 Makefile
  2. +195 −0 libmtagmodule.c
  3. +16 −0 setup.py
  4. +12 −0 test.py
@@ -0,0 +1,9 @@
+libmtag.so:
+ python setup.py build
+ cp `find build -name "libmtag.so"` .
+
+install:
+ python setup.py install
+
+clean:
+ rm -rf libmtag.so build/
@@ -0,0 +1,195 @@
+#include <Python.h>
+#include "structmember.h"
+
+#include <mtag.h>
+
+static PyObject *MTagError;
+
+typedef struct {
+ PyObject_HEAD;
+ PyObject *tag;
+ MTag_File *c_file;
+} File;
+
+typedef struct {
+ PyObject_HEAD;
+ PyObject *file;
+ MTag_Tag *c_tag;
+} Tag;
+
+static PyTypeObject FileType =
+{
+ PyObject_HEAD_INIT (NULL)
+};
+
+static PyTypeObject TagType =
+{
+ PyObject_HEAD_INIT (NULL)
+};
+
+static void
+Tag_dealloc (Tag* self)
+{
+ self->ob_type->tp_free ((PyObject*)self);
+}
+
+static int
+Tag_init (PyObject *self,
+ PyObject *args,
+ PyObject *kwords)
+{
+ Tag *c_self;
+ PyObject *file;
+
+ c_self = (Tag *) self;
+
+ if (!PyArg_ParseTuple (args, "O", &file))
+ {
+ return -1;
+ }
+
+ c_self->file = file;
+
+ c_self->c_tag = mtag_file_tag (((File *)file)->c_file);
+
+ return 0;
+}
+
+static PyObject *
+Tag_get (Tag *self,
+ PyObject *args)
+{
+ char *key;
+
+ PyArg_ParseTuple (args, "s", &key);
+
+ return Py_BuildValue ("s", mtag_tag_get (self->c_tag, key));
+}
+
+static PyObject *
+Tag_set (Tag *self,
+ PyObject *args)
+{
+ char *key;
+ char *value;
+
+ PyArg_ParseTuple (args, "ss", &key, &value);
+
+ mtag_tag_set (self->c_tag, key, value);
+
+ Py_INCREF (Py_None);
+ return Py_None;
+}
+
+static void
+File_dealloc (File* self)
+{
+ mtag_file_free (self->c_file);
+
+ self->ob_type->tp_free ((PyObject*)self);
+}
+
+static int
+File_init (PyObject *self,
+ PyObject *args,
+ PyObject *kwords)
+{
+ File *c_self;
+ const char *c_file_name;
+
+ c_self = (File *) self;
+
+ if (!PyArg_ParseTuple (args, "s", &c_file_name))
+ {
+ return -1;
+ }
+
+ c_self->c_file = mtag_file_new (c_file_name);
+
+ if (!c_self->c_file)
+ {
+ /** @todo raise exception. */
+ }
+
+ return 0;
+}
+
+static PyObject *
+File_tag (File *self,
+ PyObject *args)
+{
+ if (!self->tag)
+ {
+ self->tag = (PyObject *) PyObject_New (Tag, &TagType);
+ PyObject_CallMethod (self->tag, "__init__", "O", self);
+ }
+
+ Py_INCREF (self->tag);
+ return self->tag;
+}
+
+static PyObject *
+File_save (File *self,
+ PyObject *args)
+{
+ mtag_file_save (self->c_file);
+
+ Py_INCREF (Py_None);
+ return Py_None;
+}
+
+static PyMethodDef File_methods[] =
+{
+ {"tag", (PyCFunction)File_tag, METH_NOARGS, "Tag."},
+ {"save", (PyCFunction)File_save, METH_NOARGS, "Save."},
+ {NULL}
+};
+
+static PyMethodDef Tag_methods[] =
+{
+ {"get", (PyCFunction)Tag_get, METH_VARARGS, "Get."},
+ {"set", (PyCFunction)Tag_set, METH_VARARGS, "Set."},
+ {NULL}
+};
+
+static PyMethodDef MTagMethods[] =
+{
+ {NULL, NULL, 0, NULL}
+};
+
+PyMODINIT_FUNC
+initlibmtag (void)
+{
+ PyObject *m;
+
+ m = Py_InitModule ("libmtag", MTagMethods);
+
+ TagType.tp_new = PyType_GenericNew;
+ TagType.tp_name = "Tag";
+ TagType.tp_basicsize = sizeof (Tag);
+ TagType.tp_dealloc = (destructor) Tag_dealloc;
+ TagType.tp_flags = Py_TPFLAGS_DEFAULT;
+ TagType.tp_methods = Tag_methods;
+ TagType.tp_init = Tag_init;
+
+ FileType.tp_new = PyType_GenericNew;
+ FileType.tp_name = "File";
+ FileType.tp_basicsize = sizeof (File);
+ FileType.tp_dealloc = (destructor) File_dealloc;
+ FileType.tp_flags = Py_TPFLAGS_DEFAULT;
+ FileType.tp_methods = File_methods;
+ FileType.tp_init = File_init;
+
+ if (PyType_Ready (&FileType) < 0)
+ return;
+
+ if (PyType_Ready (&TagType) < 0)
+ return;
+
+ PyModule_AddObject (m, "Tag", (PyObject *)&TagType);
+ PyModule_AddObject (m, "File", (PyObject *)&FileType);
+
+ MTagError = PyErr_NewException ("libmtag.error", NULL, NULL);
+ Py_INCREF (MTagError);
+ PyModule_AddObject (m, "error", MTagError);
+}
@@ -0,0 +1,16 @@
+from distutils.core import setup
+from distutils.extension import Extension
+import commands
+
+def pkgconfig(*packages, **kw):
+ flag_map = {'-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries'}
+ for token in commands.getoutput("pkg-config --libs --cflags %s" % ' '.join(packages)).split():
+ kw.setdefault(flag_map.get(token[:2]), []).append(token[2:])
+ return kw
+
+module1 = Extension('libmtag', ['libmtagmodule.c'], **pkgconfig('libmtag'))
+
+setup (name = 'MTag',
+ version = '0.0.0',
+ description = 'This is a demo package',
+ ext_modules = [module1])
12 test.py
@@ -0,0 +1,12 @@
+import libmtag
+import sys
+
+myfile = libmtag.File(sys.argv[1])
+
+print "%s - %s" % (myfile.tag().get("artist"), myfile.tag().get("title"))
+
+myfile.tag().set("artist", "foo")
+
+print "%s - %s" % (myfile.tag().get("artist"), myfile.tag().get("title"))
+
+# myfile.save()

0 comments on commit fe81435

Please sign in to comment.