Merge lp:~dpm/giraffe/giraffeapp into lp:giraffe

Proposed by David Planella
Status: Merged
Merged at revision: 53
Proposed branch: lp:~dpm/giraffe/giraffeapp
Merge into: lp:giraffe
Diff against target: 217 lines (+106/-74)
3 files modified
bin/giraffe (+8/-74)
giraffe/__init__.py (+1/-0)
giraffe/app.py (+97/-0)
To merge this branch: bzr merge lp:~dpm/giraffe/giraffeapp
Reviewer Review Type Date Requested Status
Mikkel Kamstrup Erlandsen (community) Approve
Review via email: mp+54662@code.launchpad.net

Description of the change

Allow the giraffe application to be imported and executed from a module.

This allows for easier integration with other programs that use it, such as the docs publisher on developer.ubuntu.com

To post a comment you must log in.
lp:~dpm/giraffe/giraffeapp updated
54. By David Planella

Allowed inheriting the log level, converted spaces to tabs to be consistent with the rest of the giraffe code

55. By David Planella

Removed a leftover print statement

Revision history for this message
Mikkel Kamstrup Erlandsen (kamstrup) wrote :

Looks good to me. Nice work!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/giraffe'
2--- bin/giraffe 2011-03-23 18:20:15 +0000
3+++ bin/giraffe 2011-03-24 17:20:19 +0000
4@@ -4,8 +4,6 @@
5
6 import os
7 import sys
8-import shutil
9-import codecs
10 import optparse
11 import logging
12
13@@ -18,46 +16,8 @@
14 sys.path.insert(0, PROJECT_ROOT_DIRECTORY)
15 os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses
16
17-LEVELS = ( logging.ERROR,
18- logging.WARNING,
19- logging.INFO,
20- logging.DEBUG,
21- )
22-
23 import giraffe
24
25-HTML_HEADER = \
26-"""<?xml version="1.0" encoding="utf-8"?>
27-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
28-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
29- <head>
30- <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/>
31-
32- <title>%(namespace)s-%(version)s %(lang)s API Documentation</title>
33- <link rel="stylesheet" href="../giraffe.css" type="text/css" />
34- </head>
35-<body>
36-<h1>%(namespace)s-%(version)s %(lang)s API Documentation</h1>
37-"""
38-
39-HTML_FOOTER = \
40-"""</body>
41-</html>
42-"""
43-
44-def get_css_path ():
45- giraffe_dir = os.path.dirname (giraffe.__file__)
46- return os.path.join (giraffe_dir, "data", "giraffe.css")
47-
48-def get_lang_name (lang):
49- return lang.__name__.rpartition(".")[2]
50-
51-def get_output_dir (lang, output_dir):
52- return "%s/%s" % (output_dir, get_lang_name (lang))
53-
54-def get_output_file (ns, lang, output_dir):
55- return get_output_dir (lang, output_dir) + "/%s-%s.html" % (ns.name, ns.version)
56-
57 if __name__ == "__main__":
58
59 # Support for command line options.
60@@ -72,38 +32,12 @@
61 parser.set_defaults(output_dir = 'giraffe-output', logging_level = 0)
62 (options, args) = parser.parse_args()
63
64- # Set the verbosity
65- if options.debug_mode:
66- options.logging_level = 3
67- logging.basicConfig(level=LEVELS[options.logging_level],
68- format='%(asctime)s %(levelname)s %(message)s')
69-
70- output_dir = options.output_dir
71-
72- # Open and parse input files
73- repo = giraffe.ast.Repository()
74- for f in args:
75- print "Reading %s" % f
76- repo.add_gir (file(f, "r"))
77- repo.link ()
78-
79- for lang in giraffe.languages.modules:
80- try:
81- os.makedirs (get_output_dir (lang, output_dir))
82- except OSError:
83- pass
84-
85- for ns in repo.namespaces:
86- output_path = get_output_file (ns, lang, output_dir)
87- output_file = codecs.open (output_path, "w", "utf-8")
88- print "Writing %s" % output_path
89- header = HTML_HEADER % { "namespace" : ns.name, "version" : ns.version, "lang" : get_lang_name(lang).title() }
90- print >> output_file, header
91- lang.write_namespace (ns, output_file)
92- print >> output_file, HTML_FOOTER
93-
94- css_dir = output_dir
95- css_path = get_css_path ()
96- print "Installing %s into %s" % (css_path, css_dir)
97- shutil.copy(css_path, css_dir)
98+ giraffe_app = giraffe.app.GiraffeApp(args, options.output_dir,
99+ options.debug_mode,
100+ options.logging_level,
101+ )
102+
103+ giraffe_app.run()
104+
105+
106
107
108=== modified file 'giraffe/__init__.py'
109--- giraffe/__init__.py 2011-02-22 13:14:07 +0000
110+++ giraffe/__init__.py 2011-03-24 17:20:19 +0000
111@@ -2,3 +2,4 @@
112
113 import giraffe.ast
114 import giraffe.languages
115+import giraffe.app
116
117=== added file 'giraffe/app.py'
118--- giraffe/app.py 1970-01-01 00:00:00 +0000
119+++ giraffe/app.py 2011-03-24 17:20:19 +0000
120@@ -0,0 +1,97 @@
121+#! /usr/bin/python
122+# -*- coding: utf-8 -*-
123+# vim:set shiftwidth=4 tabstop=4 noexpandtab:
124+
125+import os
126+import sys
127+import shutil
128+import codecs
129+import logging
130+import giraffe.ast
131+import giraffe.languages
132+
133+LEVELS = ( logging.ERROR,
134+ logging.WARNING,
135+ logging.INFO,
136+ logging.DEBUG,
137+ )
138+
139+HTML_HEADER = \
140+"""<?xml version="1.0" encoding="utf-8"?>
141+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
142+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
143+ <head>
144+ <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/>
145+
146+ <title>%(namespace)s-%(version)s %(lang)s API Documentation</title>
147+ <link rel="stylesheet" href="../giraffe.css" type="text/css" />
148+ </head>
149+<body>
150+<h1>%(namespace)s-%(version)s %(lang)s API Documentation</h1>
151+"""
152+
153+HTML_FOOTER = \
154+"""</body>
155+</html>
156+"""
157+
158+def get_css_path ():
159+ giraffe_dir = os.path.dirname (giraffe.__file__)
160+ return os.path.join (giraffe_dir, "data", "giraffe.css")
161+
162+def get_lang_name (lang):
163+ return lang.__name__.rpartition(".")[2]
164+
165+def get_output_dir (lang, output_dir):
166+ return "%s/%s" % (output_dir, get_lang_name (lang))
167+
168+def get_output_file (ns, lang, output_dir):
169+ return get_output_dir (lang, output_dir) + "/%s-%s.html" % (ns.name, ns.version)
170+
171+class GiraffeApp:
172+ def __init__(self, args, output_dir, debug_mode = False, logging_level = None):
173+ self.output_dir = output_dir
174+ self.debug_mode = debug_mode
175+ if not logging_level:
176+ # To inherit the logging level from the importer, unless explicitly
177+ # specified
178+ self.logging_level = [i for i, lvl in enumerate(LEVELS) if lvl == logging.root.level][0]
179+ else:
180+ self.logging_level = logging_level
181+ self.args = args
182+
183+ # Set the verbosity
184+ if self.debug_mode:
185+ self.logging_level = 3
186+ logging.basicConfig(level=LEVELS[self.logging_level],
187+ format='%(asctime)s %(levelname)s %(message)s')
188+
189+ def run(self):
190+ # Open and parse input files
191+ repo = giraffe.ast.Repository()
192+ for f in self.args:
193+ print "Reading %s" % f
194+ repo.add_gir (file(f, "r"))
195+ repo.link ()
196+
197+ for lang in giraffe.languages.modules:
198+ try:
199+ os.makedirs (get_output_dir (lang, self.output_dir))
200+ except OSError:
201+ pass
202+
203+ for ns in repo.namespaces:
204+ output_path = get_output_file (ns, lang, self.output_dir)
205+ output_file = codecs.open (output_path, "w", "utf-8")
206+ print "Writing %s" % output_path
207+ header = HTML_HEADER % { "namespace" : ns.name, "version" : ns.version, "lang" : get_lang_name(lang).title() }
208+ print >> output_file, header
209+ lang.write_namespace (ns, output_file)
210+ print >> output_file, HTML_FOOTER
211+
212+ css_dir = self.output_dir
213+ css_path = get_css_path ()
214+ print "Installing %s into %s" % (css_path, css_dir)
215+ shutil.copy(css_path, css_dir)
216+
217+

Subscribers

People subscribed via source and target branches