Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update to latest libmtag.

New features include strip and get_all tags.

git-svn-id: http://libmtag.googlecode.com/svn/python/trunk@45 195de3f0-f62e-0410-be82-1f248da840de
  • Loading branch information...
commit d7768dc730e2bba61f7787fc17f08960572f1e39 1 parent a6d32d8
@felipec authored
Showing with 100 additions and 12 deletions.
  1. +98 −12 libmtagmodule.c
  2. +2 −0  test.py
View
110 libmtagmodule.c
@@ -40,38 +40,75 @@ Tag_init (PyObject *self,
{
Tag *c_self;
PyObject *file;
+ const char *tag_id = NULL;
+ int create = false;
c_self = (Tag *) self;
- if (!PyArg_ParseTuple (args, "O", &file))
+ if (!PyArg_ParseTuple (args, "O|si", &file, &tag_id, &create))
{
return -1;
}
c_self->file = file;
- c_self->c_tag = mtag_file_tag (((File *)file)->c_file);
+ if (tag_id)
+ c_self->c_tag = mtag_file_get_tag (((File *)file)->c_file, tag_id, create);
+ else
+ c_self->c_tag = mtag_file_tag (((File *)file)->c_file);
- return 0;
+ return 0;
+}
+
+static void
+each_tag (const char *key,
+ const char *value,
+ void *user_data)
+{
+ PyObject *py_key;
+ PyObject *py_value;
+ PyObject *dict;
+
+ if (!key || !value)
+ return;
+
+ py_key = Py_BuildValue ("s", key);
+ py_value = Py_BuildValue ("s", value);
+ dict = user_data;
+
+ /** @todo some items will be overwritten */
+ PyDict_SetItem (dict, py_key, py_value);
+}
+
+static PyObject *
+Tag_get_all (Tag *self,
+ PyObject *args)
+{
+ PyObject *dict;
+ dict = PyDict_New ();
+
+ mtag_tag_for_each (self->c_tag, each_tag, dict);
+
+ return dict;
}
static PyObject *
Tag_get (Tag *self,
PyObject *args)
{
- char *key;
+ const char *key;
- PyArg_ParseTuple (args, "s", &key);
+ PyArg_ParseTuple (args, "s", &key);
- return Py_BuildValue ("s", mtag_tag_get (self->c_tag, key));
+ return Py_BuildValue ("s", mtag_tag_get (self->c_tag, key));
}
static PyObject *
Tag_set (Tag *self,
PyObject *args)
{
- char *key;
- char *value;
+ const char *key;
+ const char *value;
PyArg_ParseTuple (args, "ss", &key, &value);
@@ -118,10 +155,43 @@ static PyObject *
File_tag (File *self,
PyObject *args)
{
- if (!self->tag)
+ PyObject *tag_id = NULL;
+ PyObject *create = NULL;
+ PyArg_UnpackTuple (args, "name", 0, 2, &tag_id, &create);
+
+ if (tag_id)
+ {
+ PyObject *tag;
+ Tag *c_tag;
+ tag = (PyObject *) PyObject_New (Tag, &TagType);
+ c_tag = (Tag *) tag;
+ if (create)
+ PyObject_CallMethod (tag, "__init__", "OOO", self, tag_id, create);
+ else
+ PyObject_CallMethod (tag, "__init__", "OO", self, tag_id);
+ if (!c_tag->c_tag)
+ {
+ Py_DECREF (tag);
+ Py_INCREF (Py_None);
+ return Py_None;
+ }
+ return tag;
+ }
+
+ if (!self->tag)
{
- self->tag = (PyObject *) PyObject_New (Tag, &TagType);
- PyObject_CallMethod (self->tag, "__init__", "O", self);
+ PyObject *tag;
+ Tag *c_tag;
+ tag = (PyObject *) PyObject_New (Tag, &TagType);
+ c_tag = (Tag *) tag;
+ PyObject_CallMethod (tag, "__init__", "O", self);
+ if (!c_tag->c_tag)
+ {
+ Py_DECREF (tag);
+ Py_INCREF (Py_None);
+ return Py_None;
+ }
+ self->tag = tag;
}
Py_INCREF (self->tag);
@@ -138,10 +208,25 @@ File_save (File *self,
return Py_None;
}
+static PyObject *
+File_strip (File *self,
+ PyObject *args)
+{
+ const char *tag_id = NULL;
+
+ PyArg_ParseTuple (args, "s", &tag_id);
+
+ mtag_file_strip_tag (self->c_file, tag_id);
+
+ Py_INCREF (Py_None);
+ return Py_None;
+}
+
static PyMethodDef File_methods[] =
{
- {"tag", (PyCFunction)File_tag, METH_NOARGS, "Tag."},
+ {"tag", (PyCFunction)File_tag, METH_VARARGS, "Tag."},
{"save", (PyCFunction)File_save, METH_NOARGS, "Save."},
+ {"strip", (PyCFunction)File_strip, METH_VARARGS, "Strip Tag."},
{NULL}
};
@@ -149,6 +234,7 @@ static PyMethodDef Tag_methods[] =
{
{"get", (PyCFunction)Tag_get, METH_VARARGS, "Get."},
{"set", (PyCFunction)Tag_set, METH_VARARGS, "Set."},
+ {"get_all", (PyCFunction)Tag_get_all, METH_NOARGS, "Get All."},
{NULL}
};
View
2  test.py
@@ -9,4 +9,6 @@
print "%s - %s" % (myfile.tag().get("artist"), myfile.tag().get("title"))
+print myfile.tag("id3v2").get_all ()
+
# myfile.save()
Please sign in to comment.
Something went wrong with that request. Please try again.