parser_script.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 python3\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. int i;
  56. for(i = 0; i < st->n_keys; i++) {
  57. fprintf(fp, "# %% keyword: %s\n", st->module_info.keywords[i]);
  58. }
  59. }
  60. fprintf(fp, "# %%end\n");
  61. if (st->n_flags) {
  62. struct Flag *flag;
  63. for (flag = &st->first_flag; flag; flag = flag->next_flag) {
  64. fprintf(fp, "# %%flag\n");
  65. fprintf(fp, "# %% key: %c\n", flag->key);
  66. if (flag->suppress_required)
  67. fprintf(fp, "# %% suppress_required: yes\n");
  68. if (flag->label)
  69. fprintf(fp, "# %% label: %s\n", flag->label);
  70. if (flag->description)
  71. fprintf(fp, "# %% description: %s\n", flag->description);
  72. if (flag->guisection)
  73. fprintf(fp, "# %% guisection: %s\n", flag->guisection);
  74. fprintf(fp, "# %%end\n");
  75. }
  76. }
  77. if (st->n_opts) {
  78. struct Option *opt;
  79. for (opt = &st->first_option; opt; opt = opt->next_opt) {
  80. switch (opt->type) {
  81. case TYPE_INTEGER:
  82. type = "integer";
  83. break;
  84. case TYPE_DOUBLE:
  85. type = "double";
  86. break;
  87. case TYPE_STRING:
  88. type = "string";
  89. break;
  90. default:
  91. type = "string";
  92. break;
  93. }
  94. fprintf(fp, "# %%option\n");
  95. fprintf(fp, "# %% key: %s\n", opt->key);
  96. fprintf(fp, "# %% type: %s\n", type);
  97. fprintf(fp, "# %% required: %s\n", opt->required ? "yes" : "no");
  98. fprintf(fp, "# %% multiple: %s\n", opt->multiple ? "yes" : "no");
  99. if (opt->options)
  100. fprintf(fp, "# %% options: %s\n", opt->options);
  101. if (opt->key_desc)
  102. fprintf(fp, "# %% key_desc: %s\n", opt->key_desc);
  103. if (opt->label)
  104. fprintf(fp, "# %% label: %s\n", opt->label);
  105. if (opt->description)
  106. fprintf(fp, "# %% description: %s\n", opt->description);
  107. if (opt->descriptions)
  108. fprintf(fp, "# %% descriptions: %s\n", opt->descriptions);
  109. if (opt->answer)
  110. fprintf(fp, "# %% answer: %s\n", opt->answer);
  111. if (opt->gisprompt)
  112. fprintf(fp, "# %% gisprompt: %s\n", opt->gisprompt);
  113. if (opt->guisection)
  114. fprintf(fp, "# %% guisection: %s\n", opt->guisection);
  115. if (opt->guidependency)
  116. fprintf(fp, "# %% guidependency: %s\n", opt->guidependency);
  117. fprintf(fp, "# %%end\n");
  118. }
  119. }
  120. fprintf(fp, "\nimport sys\n");
  121. fprintf(fp, "\nimport grass.script as gs\n");
  122. fprintf(fp, "\ndef main():");
  123. fprintf(fp, "\n # put code here\n");
  124. fprintf(fp, "\n return 0\n");
  125. fprintf(fp, "\nif __name__ == \"__main__\":");
  126. fprintf(fp, "\n options, flags = gs.parser()");
  127. fprintf(fp, "\n sys.exit(main())\n");
  128. }