[xiph-cvs] cvs commit: vorbis-python/src pyvorbisfile.h pyvorbisinfo.c vorbismodule.h
Andrew Catham Master of Python
andrew at xiph.org
Tue Jul 16 22:04:41 PDT 2002
andrew 02/07/16 22:04:40
Modified: . ChangeLog setup.py
src pyvorbisfile.h pyvorbisinfo.c vorbismodule.h
Log:
2002-07-16 Andrew H. Chatham <andrew at andrewchatham.com>
* pyvorbisinfo.c: Can now create comments from dictionaries
(a MUCH better interface)
* vorbismodule.h: Removed unneeded declarations
Revision Changes Path
1.14 +5 -0 vorbis-python/ChangeLog
Index: ChangeLog
===================================================================
RCS file: /usr/local/cvsroot/vorbis-python/ChangeLog,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- ChangeLog 2002/05/21 21:43:13 1.13
+++ ChangeLog 2002/07/17 05:04:39 1.14
@@ -1,3 +1,8 @@
+2002-07-16 Andrew H. Chatham <andrew at andrewchatham.com>
+ * pyvorbisinfo.c: Can now create comments from dictionaries
+ (a MUCH better interface)
+ * vorbismodule.h: Removed unneeded declarations
+
2002-05-21 Andrew H. Chatham <andrew.chatham at duke.edu>
* pyvorbisinfo.c: Don't crash if comments are not valid UTF-8
<p><p>1.6 +2 -1 vorbis-python/setup.py
Index: setup.py
===================================================================
RCS file: /usr/local/cvsroot/vorbis-python/setup.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- setup.py 2002/01/27 11:18:23 1.5
+++ setup.py 2002/07/17 05:04:39 1.6
@@ -55,7 +55,8 @@
'src/pyvorbisinfo.c',
'src/vcedit.c',
'src/general.c'],
- define_macros = [('VERSION', '"%s"' % pyvorbis_version)],
+ define_macros = [('VERSION', '"%s"' %
+ pyvorbis_version)],
include_dirs=[vorbis_include_dir,
ogg_include_dir],
library_dirs=[vorbis_lib_dir,
<p><p>1.5 +2 -0 vorbis-python/src/pyvorbisfile.h
Index: pyvorbisfile.h
===================================================================
RCS file: /usr/local/cvsroot/vorbis-python/src/pyvorbisfile.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- pyvorbisfile.h 2002/02/18 01:16:34 1.4
+++ pyvorbisfile.h 2002/07/17 05:04:40 1.5
@@ -14,6 +14,8 @@
#define PY_VORBISFILEOBJECT(x) (((py_vorbisfile *)x)->py_file)
extern PyTypeObject py_vorbisfile_type;
+PyObject *py_file_new(PyObject *, PyObject *);
+
#endif /* __PYVORBIS_FILE_H__ */
<p><p>1.10 +128 -5 vorbis-python/src/pyvorbisinfo.c
Index: pyvorbisinfo.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-python/src/pyvorbisinfo.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- pyvorbisinfo.c 2002/05/21 21:43:14 1.9
+++ pyvorbisinfo.c 2002/07/17 05:04:40 1.10
@@ -334,13 +334,9 @@
}
PyObject *
-py_comment_new(PyObject *self, PyObject *args)
+py_comment_new_empty()
{
py_vcomment *newobj;
-
- if (!PyArg_ParseTuple(args, ""))
- return NULL;
-
newobj = (py_vcomment *) PyObject_NEW(py_vcomment,
&py_vcomment_type);
if (!newobj)
@@ -688,6 +684,133 @@
}
in[pos] = '\0';
return 0;
+}
+
+/* Assign a tag in a vorbis_comment, special-casing the VENDOR tag. */
+static int
+assign_tag(vorbis_comment *vcomment, const char *key, PyObject *tag)
+{
+ char *tag_str;
+ char tag_buff[1024];
+ if (PyString_Check(tag)) {
+ tag_str = PyString_AsString(tag);
+ } else {
+ /* TODO - Unicode */
+ }
+ if (!strcasecmp(key, "vendor")) {
+ vcomment->vendor = strdup(tag_str);
+ } else {
+ snprintf(tag_buff, sizeof(tag_buff), "%s=%s", key, tag_str);
+ printf("Add tag %s\n", tag_buff);
+ vorbis_comment_add(vcomment, tag_buff);
+ }
+ return 1;
+}
+
+/*
+ NOTE:
+ Something like this should be wrong but will, I guess, 'work':
+ { 'Vendor' : 'me', 'VENDOR': 'someone else'}
+ */
+static int
+create_comment_from_items(vorbis_comment *vcomment,
+ const char *key, PyObject *item_vals)
+{
+
+ if (PyString_Check(item_vals)) {
+ return assign_tag(vcomment, key, item_vals);
+ } else if (PySequence_Check(item_vals)) {
+ int j, val_length = PySequence_Length(item_vals);
+ if (!strcasecmp(key, "vendor") && val_length > 1) {
+ PyErr_SetString(PyExc_ValueError, "Cannot have multiple vendor tags");
+ }
+ for (j = 0; j < val_length; j++) {
+ PyObject *tag_value = PySequence_GetItem(item_vals, j);
+ if (!tag_value)
+ return 0;
+ if (!assign_tag(vcomment, key, tag_value))
+ return 0;
+ }
+ } else {
+ PyErr_SetString(PyExc_ValueError, "Value not a string or sequence.");
+ return 0;
+ }
+ return 1;
+}
+
+static vorbis_comment *
+create_comment_from_dict(PyObject *dict)
+{
+ vorbis_comment *vcomment = NULL;
+ int initted = 0;
+ PyObject *items = NULL;
+ int k, length;
+
+ vcomment = (vorbis_comment *) malloc(sizeof(vorbis_comment));
+ if (!vcomment) {
+ PyErr_SetString(PyExc_MemoryError, "error allocating vcomment");
+ goto error;
+ }
+
+ vorbis_comment_init(vcomment);
+ initted = 1;
+ items = PyDict_Items(dict);
+ if (!items)
+ goto error;
+ length = PyList_Size(items);
+ for (k = 0; k < length; k++) {
+ PyObject *pair = PyList_GetItem(items, k);
+ PyObject *key, *val;
+ if (!pair)
+ goto error;
+ assert(PyTuple_Check(pair));
+ key = PyTuple_GetItem(pair, 0);
+ val = PyTuple_GetItem(pair, 1);
+ if (!PyString_Check(key)) {
+ PyErr_SetString(PyExc_ValueError, "Key not a string");
+ goto error;
+ }
+ if (!create_comment_from_items(vcomment, PyString_AsString(key), val))
+ goto error;
+ }
+
+ return vcomment;
+
+ error:
+ /* Note: I hate dealing with memory */
+ Py_XDECREF(items);
+ if (vcomment) {
+ if (initted)
+ vorbis_comment_clear(vcomment);
+ free(vcomment);
+ }
+ return NULL;
+}
+
+PyObject *
+py_comment_new(PyObject *self, PyObject *args)
+{
+ py_vcomment *pvc;
+ PyObject *dict;
+ vorbis_comment *vcomment;
+ if (PyArg_ParseTuple(args, "")) {
+ return py_comment_new_empty();
+ } else if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict))
+ return NULL;
+ vcomment = create_comment_from_dict(dict);
+ if (!vcomment)
+ return NULL;
+ pvc = (py_vcomment *) PyObject_NEW(py_vcomment,
+ &py_vcomment_type);
+ if (!pvc) {
+ vorbis_comment_clear(vcomment);
+ free(vcomment);
+ return NULL;
+ }
+ pvc->vc = vcomment;
+ pvc->parent = NULL;
+ pvc->malloced = 1;
+ return (PyObject *) pvc;
}
static PyObject *
<p><p>1.3 +0 -6 vorbis-python/src/vorbismodule.h
Index: vorbismodule.h
===================================================================
RCS file: /usr/local/cvsroot/vorbis-python/src/vorbismodule.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- vorbismodule.h 2001/06/01 20:54:46 1.2
+++ vorbismodule.h 2002/07/17 05:04:40 1.3
@@ -17,12 +17,6 @@
extern char py_vcomment_doc[];
extern char py_dsp_doc[];
-/* Module-accessible functions */
-
-PyObject *py_file_new(PyObject *, PyObject *);
-PyObject *py_info_new(PyObject *, PyObject *, PyObject *);
-PyObject *py_comment_new(PyObject *, PyObject *);
-
/* Utility functions/macros */
PyObject *v_error_from_code(int, char*);
<p><p><p>--- >8 ----
List archives: http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body. No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.
More information about the commits
mailing list