close

  
<p> function GooglePythonCodeGenerator(code_generator){
  code_generator.reset();
  this.cg = code_generator;
  this.cg.json_options = PythonCodeGenerator.defaultJsonOptions;
  this.cg.addPreGenerator('Python', 'google', 'generateImports');
  this.cg.addPreGenerator('Python', 'google', 'generateClient');
  this.cg.addServiceGenerator('Python', 'google', 'mqlread');
  this.cg.addServiceGenerator('Python', 'google', 'search');
  this.cg.addServiceGenerator('Python', 'google', 'topic');
}

CodeGenerator.generatorsByLanguageAndLibrary['Python']['google'] = GooglePythonCodeGenerator;

GooglePythonCodeGenerator.prototype.getLibraries = function(params, options) {
  return [{
    'name': 'Google APIs Client Library for Python',
    'url': 'http://code.google.com/p/google-api-python-client/'
  }];
}

GooglePythonCodeGenerator.prototype.generateImports = function(params, options) {
  return PythonCodeGenerator.generateImports(this.cg,[
    ['apiclient','discovery'],
    ['apiclient','model']
  ]);
}
  
GooglePythonCodeGenerator.prototype.generateClient = function(params, options) {
  var service = options['service'];
  var output  = "";
  output += PythonCodeGenerator.const("developer_key") + " = 'YOUR-API-KEY-GOES-HERE'\n"
  output += 'model.JsonModel.alt_param = ""\n';
  output += PythonCodeGenerator.var("freebase_client") + " = discovery.build('freebase', '" + options['version'] + "', developerKey=" + PythonCodeGenerator.const("developer_key") + ")\n"
  return output;
}
  
GooglePythonCodeGenerator.prototype.generateMqlread = function(params, options) {
  var query = JSON.parse(params['query']);
  var multiresult_query = ((query instanceof Array) &amp;&amp; query.length == 1) || !params['cursor'];
  var output  = "";
  output += PythonCodeGenerator.var("query") + " = " + PythonCodeGenerator.formatMqlQuery(this.cg, query) + "\n";
  
  // mqlreaditer
  if (params['cursor'] == '') {
    var filtered_params = this.cg.filterParams(params, ["query","cursor"]);
    output += PythonCodeGenerator.var("results") + " = " + PythonCodeGenerator.var("freebase_client") + ".mqlreaditer(" + PythonCodeGenerator.var("query");
    if (filtered_params.size() &gt; 0) {
      output += "," + this.formatParams(filtered_params, options) + ")\n\n";
    } else {
      output += ")\n";
    }
    this.cg.increaseIndent();
    output += this.generateMqlReadResults(query, params, options);
  // mqlread
  } else {
    var filtered_params = this.cg.filterParams(params, ["query"]);
    if (multiresult_query) {
      output += PythonCodeGenerator.var("results") + " = " + PythonCodeGenerator.var("freebase_client") + ".mqlread(" + PythonCodeGenerator.var("query") + "['result']";
    }
    if (filtered_params.size() &gt; 0) {
      output += "," + this.formatParams(filtered_params, options) + ")\n\n";
    } else {
      output += ")\n";
    }
    output += this.generateMqlReadResults(query, params, options);
  }

  var path = this.cg.getQuerySamplePath(query);
  output += this.cg.getIndent() + "print " + PythonCodeGenerator.var("topic") + PythonCodeGenerator.formatPath(path) + "\n";
  if (params['cursor'] == '') {
    this.cg.decreaseIndent();
  }
  return output;
}
  
GooglePythonCodeGenerator.prototype.generateMqlReadResults = function(query, params, options) {;
  var multiresult_query = ((query instanceof Array) &amp;&amp; query.length == 1) || !params['cursor'];
  var output  = "";
    
  output += this.cg.getIndent() + PythonCodeGenerator.var("url") + " = " + PythonCodeGenerator.var("service_url") + " + '?'";
  if (params.size() &gt; 0) {
    output += " + " + "urllib.urlencode(" + PythonCodeGenerator.var("params") + ")" + "\n";
  } else {
    output += "\n";
  }
  output += this.cg.getIndent() + PythonCodeGenerator.var("response") + " = json.loads(urllib.urlopen(" + PythonCodeGenerator.var("url") + ").read())\n";
  if (multiresult_query) {
    output += this.cg.getIndent() + "for " + PythonCodeGenerator.var("topic") + " in " + PythonCodeGenerator.var("response") + "['result']:\n";
    this.cg.increaseIndent();
  }
  output += this.cg.getIndent() + PythonCodeGenerator.var("topic") + " = " + PythonCodeGenerator.var("response") + "['result']\n";
  return output;
}

GooglePythonCodeGenerator.prototype.generateSearch = function(params, options) {
  var query = params['query'];
  var output  = "";

  output += "for " + PythonCodeGenerator.var("result") + " in " + PythonCodeGenerator.var("response") + "['result']:\n";
  var path = this.cg.getSearchQuerySamplePath(query);
  output += CodeGenerator.tab + "print " + PythonCodeGenerator.var("result") + PythonCodeGenerator.formatPath(path) + "\n";
  return output;
}

GooglePythonCodeGenerator.prototype.generateTopic = function(params, options) {
  var id = params['id'];
  var output  = "";
  var filtered_params = this.cg.filterParams(params, ["id"]);
  
  var path = this.cg.getTopicSamplePath(id);
  output += "print " + PythonCodeGenerator.var("topic") + PythonCodeGenerator.formatPath(path) + "\n";
  return output;
}

GooglePythonCodeGenerator.prototype.formatParams = function(params, options) {
  var output  = "";
  
    var json_options = CodeGenerator.defaultJsonOptions;
    if (params.size() &gt; 1) {
      json_options.object_key_seperator = ': ';
    }
    output += PythonCodeGenerator.var("params") + " = ";
    output += this.cg.formatJSON(params.params, params.size() &gt; 1, json_options);
    output += "\n";

  return output;
} </p>

Comments

Hide