blob: 57ba3034ce10843dd30659d6b2e49bd07239b51a [file] [log] [blame]
Joe Gregorio652898b2011-05-02 21:07:43 -04001#!/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
20Generates a command-line client sample application from a set of files
21that contain only the relevant portions that change between each API.
22This allows all the common code to go into a template.
23
24Usage:
25 python sample_generator.py
26
27Must be run from the root of the respository directory.
28"""
29
30__author__ = 'jcgregorio@google.com (Joe Gregorio)'
31
32import os.path
33import glob
34import sys
35import pprint
36import string
37import textwrap
38
39if not os.path.isdir('samples/src'):
40 sys.exit('Must be run from root of the respository directory.')
41
42f = open('samples/src/template.tmpl', 'r')
43template = string.Template(f.read())
44f.close()
45
46for 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 Gregorio7d791212011-05-16 21:58:52 -070057
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 Gregorio652898b2011-05-02 21:07:43 -040067 variables['description'] = textwrap.fill(variables['description'])
68 variables['content'] = content
69 variables['name'] = os.path.basename(filename).split('.', 1)[0]
Joe Gregorio7d791212011-05-16 21:58:52 -070070
Joe Gregorio652898b2011-05-02 21:07:43 -040071 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']