Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # Copyright (C) 2010 Google Inc. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | """Generate command-line samples from stubs. |
| 19 | |
| 20 | Generates a command-line client sample application from a set of files |
| 21 | that contain only the relevant portions that change between each API. |
| 22 | This allows all the common code to go into a template. |
| 23 | |
| 24 | Usage: |
| 25 | python sample_generator.py |
| 26 | |
| 27 | Must be run from the root of the respository directory. |
| 28 | """ |
| 29 | |
| 30 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 31 | |
| 32 | import os.path |
| 33 | import glob |
| 34 | import sys |
| 35 | import pprint |
| 36 | import string |
| 37 | import textwrap |
| 38 | |
| 39 | if not os.path.isdir('samples/src'): |
| 40 | sys.exit('Must be run from root of the respository directory.') |
| 41 | |
| 42 | f = open('samples/src/template.tmpl', 'r') |
| 43 | template = string.Template(f.read()) |
| 44 | f.close() |
| 45 | |
| 46 | for filename in glob.glob('samples/src/*.py'): |
| 47 | # Create a dictionary from the config file to later use in filling in the |
| 48 | # templates. |
| 49 | f = open(filename, 'r') |
| 50 | contents = f.read() |
| 51 | f.close() |
| 52 | config, content = contents.split('\n\n', 1) |
| 53 | variables = {} |
| 54 | for line in config.split('\n'): |
| 55 | key, value = line[1:].split(':', 1) |
| 56 | variables[key.strip()] = value.strip() |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 57 | |
| 58 | lines = content.split('\n') |
| 59 | outlines = [] |
| 60 | for l in lines: |
| 61 | if l: |
| 62 | outlines.append(' ' + l) |
| 63 | else: |
| 64 | outlines.append('') |
| 65 | content = '\n'.join(outlines) |
| 66 | |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 67 | variables['description'] = textwrap.fill(variables['description']) |
| 68 | variables['content'] = content |
| 69 | variables['name'] = os.path.basename(filename).split('.', 1)[0] |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 70 | |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 71 | f = open(os.path.join('samples', variables['name'], variables['name'] + '.py'), 'w') |
| 72 | f.write(template.substitute(variables)) |
| 73 | f.close() |
| 74 | print 'Processed: %s' % variables['name'] |