parser_script.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*!
  2. \file lib/gis/parser_script.c
  3. \brief GIS Library - Argument parsing functions (script)
  4. (C) 2001-2009, 2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. \author Soeren Gebbert added Dec. 2009 WPS process_description document
  9. */
  10. #include <stdio.h>
  11. #include <grass/gis.h>
  12. #include "parser_local_proto.h"
  13. /*!
  14. \brief Generate Python script-like output
  15. */
  16. void G__script(void)
  17. {
  18. FILE *fp = stdout;
  19. char *type;
  20. fprintf(fp,
  21. "#!/usr/bin/env python\n");
  22. fprintf(fp,
  23. "############################################################################\n");
  24. fprintf(fp, "#\n");
  25. fprintf(fp, "# MODULE: %s_wrapper\n", G_program_name());
  26. fprintf(fp, "# AUTHOR(S): %s\n", G_whoami());
  27. fprintf(fp, "# PURPOSE: Wrapper for %s\n", G_program_name());
  28. fprintf(fp, "# COPYRIGHT: (C) %s by %s, and the GRASS Development Team\n",
  29. GRASS_VERSION_DATE, G_whoami());
  30. fprintf(fp, "#\n");
  31. fprintf(fp,
  32. "# This program is free software; you can redistribute it and/or modify\n");
  33. fprintf(fp,
  34. "# it under the terms of the GNU General Public License as published by\n");
  35. fprintf(fp,
  36. "# the Free Software Foundation; either version 2 of the License, or\n");
  37. fprintf(fp, "# (at your option) any later version.\n");
  38. fprintf(fp, "#\n");
  39. fprintf(fp,
  40. "# This program is distributed in the hope that it will be useful,\n");
  41. fprintf(fp,
  42. "# but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  43. fprintf(fp,
  44. "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
  45. fprintf(fp, "# GNU General Public License for more details.\n");
  46. fprintf(fp, "#\n");
  47. fprintf(fp,
  48. "############################################################################\n\n");
  49. fprintf(fp, "#%%module\n");
  50. if (st->module_info.label)
  51. fprintf(fp, "#%% label: %s\n", st->module_info.label);
  52. if (st->module_info.description)
  53. fprintf(fp, "#%% description: %s\n", st->module_info.description);
  54. if (st->module_info.keywords) {
  55. fprintf(fp, "#%% keyword: ");
  56. G__print_keywords(fp, NULL);
  57. fprintf(fp, "\n");
  58. }
  59. fprintf(fp, "#%%end\n");
  60. if (st->n_flags) {
  61. struct Flag *flag;
  62. for (flag = &st->first_flag; flag; flag = flag->next_flag) {
  63. fprintf(fp, "#%%flag\n");
  64. fprintf(fp, "#%% key: %c\n", flag->key);
  65. if (flag->suppress_required)
  66. fprintf(fp, "#%% suppress_required: yes\n");
  67. if (flag->label)
  68. fprintf(fp, "#%% label: %s\n", flag->label);
  69. if (flag->description)
  70. fprintf(fp, "#%% description: %s\n", flag->description);
  71. if (flag->guisection)
  72. fprintf(fp, "#%% guisection: %s\n", flag->guisection);
  73. fprintf(fp, "#%%end\n");
  74. }
  75. }
  76. if (st->n_opts) {
  77. struct Option *opt;
  78. for (opt = &st->first_option; opt; opt = opt->next_opt) {
  79. switch (opt->type) {
  80. case TYPE_INTEGER:
  81. type = "integer";
  82. break;
  83. case TYPE_DOUBLE:
  84. type = "double";
  85. break;
  86. case TYPE_STRING:
  87. type = "string";
  88. break;
  89. default:
  90. type = "string";
  91. break;
  92. }
  93. fprintf(fp, "#%%option\n");
  94. fprintf(fp, "#%% key: %s\n", opt->key);
  95. fprintf(fp, "#%% type: %s\n", type);
  96. fprintf(fp, "#%% required: %s\n", opt->required ? "yes" : "no");
  97. fprintf(fp, "#%% multiple: %s\n", opt->multiple ? "yes" : "no");
  98. if (opt->options)
  99. fprintf(fp, "#%% options: %s\n", opt->options);
  100. if (opt->key_desc)
  101. fprintf(fp, "#%% key_desc: %s\n", opt->key_desc);
  102. if (opt->label)
  103. fprintf(fp, "#%% label: %s\n", opt->label);
  104. if (opt->description)
  105. fprintf(fp, "#%% description: %s\n", opt->description);
  106. if (opt->descriptions)
  107. fprintf(fp, "#%% descriptions: %s\n", opt->descriptions);
  108. if (opt->answer)
  109. fprintf(fp, "#%% answer: %s\n", opt->answer);
  110. if (opt->gisprompt)
  111. fprintf(fp, "#%% gisprompt: %s\n", opt->gisprompt);
  112. if (opt->guisection)
  113. fprintf(fp, "#%% guisection: %s\n", opt->guisection);
  114. if (opt->guidependency)
  115. fprintf(fp, "#%% guidependency: %s\n", opt->guidependency);
  116. fprintf(fp, "#%%end\n");
  117. }
  118. }
  119. fprintf(fp, "\nimport sys\n");
  120. fprintf(fp, "\nimport grass.script as grass\n");
  121. fprintf(fp, "\ndef main():");
  122. fprintf(fp, "\n # put code here\n");
  123. fprintf(fp, "\n return 0\n");
  124. fprintf(fp, "\nif __name__ == \"__main__\":");
  125. fprintf(fp, "\n options, flags = grass.parser()");
  126. fprintf(fp, "\n sys.exit(main())\n");
  127. }