Skip to content

Commit

Permalink
Merge pull request #799 from Izaron/something
Browse files Browse the repository at this point in the history
Temporarily wrapped the Python API
  • Loading branch information
Evgeny Shulgin committed Oct 18, 2017
2 parents 07f289d + a6f0a07 commit a170f55
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/ccextractor.c
Expand Up @@ -57,15 +57,18 @@ int api_start(struct ccx_s_options api_options)
return 0;
}
#endif

// Initialize CCExtractor libraries
ctx = init_libraries(&api_options);
#ifdef ENABLE_PYTHON
int i=0;

while(i<api_options.python_param_count)
{
free(api_options.python_params[i]);
i++;
}
#endif
if (!ctx && errno == ENOMEM)
fatal (EXIT_NOT_ENOUGH_MEMORY, "Not enough memory\n");
else if (!ctx && errno == EINVAL)
Expand Down Expand Up @@ -415,7 +418,9 @@ int api_start(struct ccx_s_options api_options)
curl_global_cleanup();
#endif
dinit_libraries(&ctx);
#ifdef PYTHON_API
free_python_global_vars();
#endif

if (!ret)
mprint("\nNo captions were found in input.\n");
Expand All @@ -430,6 +435,7 @@ int api_start(struct ccx_s_options api_options)
}
return ret ? EXIT_OK : EXIT_NO_CAPTIONS;
}
#ifdef ENABLE_PYTHON
void free_python_global_vars()
{
int i=0;
Expand All @@ -441,6 +447,7 @@ void free_python_global_vars()
}
free(array.subs);
}
#endif

struct ccx_s_options* api_init_options()
{
Expand All @@ -453,6 +460,7 @@ void check_configuration_file(struct ccx_s_options api_options)
parse_configuration(&api_options);
}

#ifdef ENABLE_PYTHON
int compile_params(struct ccx_s_options *api_options,int argc)
{
//adding the parameter ./ccextractor to the list of python_params for further parsing
Expand Down Expand Up @@ -497,6 +505,7 @@ int api_param_count(struct ccx_s_options* api_options)
{
return api_options->python_param_count;
}
#endif

/*
* asprintf alternative
Expand Down Expand Up @@ -541,6 +550,7 @@ char* time_wrapper(char* fmt, unsigned h, unsigned m, unsigned s, unsigned ms)
return time;
}

#ifdef PYTHON_API
void call_from_python_api(struct ccx_s_options *api_options)
{
int indicator = api_options->signal_python_api;
Expand All @@ -549,6 +559,7 @@ void call_from_python_api(struct ccx_s_options *api_options)
else
signal_python_api=0;
}
#endif

#if defined(PYTHONAPI)
void run(PyObject * reporter, char * line, int encoding)
Expand All @@ -563,10 +574,16 @@ int main(int argc, char* argv[])
{
struct ccx_s_options* api_options = api_init_options();
check_configuration_file(*api_options);
#ifdef ENABLE_PYTHON
for(int i = 1; i < argc; i++)
api_add_param(api_options,argv[i]);
#endif

#ifdef ENABLE_PYTHON
int compile_ret = compile_params(api_options,argc);
#else
int compile_ret = parse_parameters (api_options, argc, argv);
#endif

if (compile_ret == EXIT_NO_INPUT_FILES)
{
Expand All @@ -582,7 +599,9 @@ int main(int argc, char* argv[])
exit(compile_ret);
}

#ifdef PYTHON_API
call_from_python_api(api_options);
#endif
int start_ret = api_start(*api_options);
return start_ret;
}
7 changes: 7 additions & 0 deletions src/ccextractor.h
Expand Up @@ -27,6 +27,7 @@ CURLcode res;
#include "funcobject.h"
#endif

#ifdef ENABLE_PYTHON
struct python_subs_modified{
char *start_time;
char* end_time;
Expand All @@ -40,12 +41,15 @@ struct python_subs_array{
struct python_subs_modified* subs;
};
void free_python_global_vars();
#endif


struct ccx_s_options ccx_options;
struct lib_ccx_ctx *signal_ctx;
#ifdef ENABLE_PYTHON
struct python_subs_array array;
int signal_python_api; // 1 symbolises that python wrapper made the call.
#endif
//volatile int terminate_asap = 0;

struct ccx_s_options* api_init_options();
Expand All @@ -63,8 +67,11 @@ void print_end_msg(void);

int main(int argc, char *argv[]);

#ifdef ENABLE_PYTHON
void call_from_python_api(struct ccx_s_options *api_options);
void free_python_global_vars();
#endif

#if defined(PYTHONAPI)
void run(PyObject * reporter, char * line, int encoding);
#endif
Expand Down
4 changes: 3 additions & 1 deletion src/extractors/extractor.c
@@ -1,5 +1,6 @@
#include "extractor.h"


#ifdef ENABLE_PYTHON
void python_extract_g608_grid(unsigned h1, unsigned m1, unsigned s1, unsigned ms1, unsigned h2, unsigned m2, unsigned s2, unsigned ms2, char* buffer, int identifier, int srt_counter, int encoding){
/*
* identifier = 0 ---> adding start and end time
Expand Down Expand Up @@ -94,3 +95,4 @@ void python_extract_g608_grid(unsigned h1, unsigned m1, unsigned s1, unsigned ms
free(temp);
#endif
}
#endif
3 changes: 2 additions & 1 deletion src/extractors/extractor.h
Expand Up @@ -3,6 +3,7 @@
#include "ccextractor.h"
#include "ccx_common_common.h"

#ifdef ENABLE_PYTHON
void python_extract_g608_grid(unsigned h1, unsigned m1, unsigned s1, unsigned ms1, unsigned h2, unsigned m2, unsigned s2, unsigned ms2, char* buffer, int identifier, int srt_counter, int encoding);

#endif

2 changes: 2 additions & 0 deletions src/lib_ccx/ccx_common_option.c
Expand Up @@ -172,6 +172,8 @@ void init_options (struct ccx_s_options *options)
stringztoms (DEF_VAL_ENDCREDITSFORATLEAST, &options->enc_cfg.endcreditsforatleast);
stringztoms (DEF_VAL_ENDCREDITSFORATMOST, &options->enc_cfg.endcreditsforatmost);

#ifdef ENABLE_PYTHON
options->python_param_count = 0;
options->python_params = malloc(options->python_param_count * sizeof *options->python_params);
#endif
}
3 changes: 3 additions & 0 deletions src/lib_ccx/ccx_common_option.h
Expand Up @@ -195,9 +195,12 @@ struct ccx_s_options // Options from user parameters
char *translate_langs;
char *translate_key;
#endif

#ifdef ENABLE_PYTHON
char** python_params; // An array of strings to store the params supplied in the python processing
int python_param_count; // Count of the total number of params passed via python bindings
int signal_python_api; // 1 symbolises that python wrapper made the call.
#endif
};

extern struct ccx_s_options ccx_options;
Expand Down
4 changes: 3 additions & 1 deletion src/lib_ccx/ccx_encoders_common.c
Expand Up @@ -1130,10 +1130,12 @@ int encode_sub(struct encoder_ctx *context, struct cc_subtitle *sub)
data->end_time += utc_refvalue * 1000;
}

#ifdef ENABLE_PYTHON
//making a call to python_encoder so that if the call is from the api, no output is generated.
if (signal_python_api)
wrote_something = pass_cc_buffer_to_python(data, context);
else
else
#endif
{
switch (context->write_format)
{
Expand Down
4 changes: 4 additions & 0 deletions src/lib_ccx/output.c
Expand Up @@ -9,6 +9,7 @@

void dinit_write(struct ccx_s_write *wb)
{
#ifdef ENABLE_PYTHON
if(!signal_python_api){
if (wb->fh > 0)
close(wb->fh);
Expand All @@ -17,6 +18,7 @@ void dinit_write(struct ccx_s_write *wb)
unlink(wb->semaphore_filename);
freep(&wb->semaphore_filename);
}
#endif
}

int temporarily_close_output(struct ccx_s_write *wb)
Expand Down Expand Up @@ -50,6 +52,7 @@ int temporarily_open_output(struct ccx_s_write *wb)

int init_write (struct ccx_s_write *wb, char *filename, int with_semaphore)
{
#ifdef ENABLE_PYTHON
if (signal_python_api){
char* output;
//writing to memory which would be then tailed by python.
Expand All @@ -60,6 +63,7 @@ int init_write (struct ccx_s_write *wb, char *filename, int with_semaphore)
free(output);
return EXIT_OK;
}
#endif
memset(wb, 0, sizeof(struct ccx_s_write));
wb->fh=-1;
wb->temporarily_closed = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/lib_ccx/params.c
Expand Up @@ -1137,13 +1137,15 @@ int parse_parameters (struct ccx_s_options *opt, int argc, char *argv[])
}
continue;
}
#ifdef ENABLE_PYTHON
//adding the support for -pythonapi param to indicate that python wrappers have been used
if (strcmp(argv[i], "-pythonapi")==0)
{
opt->signal_python_api =1;
opt->messages_target=0;
continue;
}
#endif

#ifdef ENABLE_HARDSUBX
// Parse -hardsubx and related parameters
Expand Down
3 changes: 2 additions & 1 deletion src/wrappers/wrapper.c
@@ -1,6 +1,7 @@
#include "wrapper.h"
#include "ccextractor.h"

#ifdef PYTHON_API
void set_pythonapi(struct ccx_s_options *api_options){
api_add_param(api_options,"-pythonapi");
}
Expand Down Expand Up @@ -38,4 +39,4 @@ debugdvbsub wrapper
void setdebugdvbsub(struct ccx_s_options *api_options){
api_add_param(api_options,"-debugdvbsub");
}

#endif
4 changes: 3 additions & 1 deletion src/wrappers/wrapper.h
Expand Up @@ -7,9 +7,11 @@ void set_pythonapi_via_python(struct ccx_s_options *api_options, PyObject * func
#else
#define my_pythonapi(args, func) set_pythonapi(args)
#endif

#ifdef PYTHON_API
void set_pythonapi(struct ccx_s_options *api_options);
void setautoprogram(struct ccx_s_options *api_options);
void setstdout(struct ccx_s_options *api_options);
void setpesheader(struct ccx_s_options *api_options);
void setdebugdvbsub(struct ccx_s_options *api_options);

#endif

0 comments on commit a170f55

Please sign in to comment.