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
=== modified file 'bin/giraffe'
--- bin/giraffe 2011-03-23 18:20:15 +0000
+++ bin/giraffe 2011-03-24 17:20:19 +0000
@@ -4,8 +4,6 @@
44
5import os5import os
6import sys6import sys
7import shutil
8import codecs
9import optparse7import optparse
10import logging8import logging
119
@@ -18,46 +16,8 @@
18 sys.path.insert(0, PROJECT_ROOT_DIRECTORY)16 sys.path.insert(0, PROJECT_ROOT_DIRECTORY)
19 os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses17 os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses
2018
21LEVELS = ( logging.ERROR,
22 logging.WARNING,
23 logging.INFO,
24 logging.DEBUG,
25 )
26
27import giraffe19import giraffe
2820
29HTML_HEADER = \
30"""<?xml version="1.0" encoding="utf-8"?>
31<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
32<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
33 <head>
34 <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/>
35
36 <title>%(namespace)s-%(version)s %(lang)s API Documentation</title>
37 <link rel="stylesheet" href="../giraffe.css" type="text/css" />
38 </head>
39<body>
40<h1>%(namespace)s-%(version)s %(lang)s API Documentation</h1>
41"""
42
43HTML_FOOTER = \
44"""</body>
45</html>
46"""
47
48def get_css_path ():
49 giraffe_dir = os.path.dirname (giraffe.__file__)
50 return os.path.join (giraffe_dir, "data", "giraffe.css")
51
52def get_lang_name (lang):
53 return lang.__name__.rpartition(".")[2]
54
55def get_output_dir (lang, output_dir):
56 return "%s/%s" % (output_dir, get_lang_name (lang))
57
58def get_output_file (ns, lang, output_dir):
59 return get_output_dir (lang, output_dir) + "/%s-%s.html" % (ns.name, ns.version)
60
61if __name__ == "__main__":21if __name__ == "__main__":
6222
63 # Support for command line options.23 # Support for command line options.
@@ -72,38 +32,12 @@
72 parser.set_defaults(output_dir = 'giraffe-output', logging_level = 0)32 parser.set_defaults(output_dir = 'giraffe-output', logging_level = 0)
73 (options, args) = parser.parse_args()33 (options, args) = parser.parse_args()
7434
75 # Set the verbosity35 giraffe_app = giraffe.app.GiraffeApp(args, options.output_dir,
76 if options.debug_mode:36 options.debug_mode,
77 options.logging_level = 337 options.logging_level,
78 logging.basicConfig(level=LEVELS[options.logging_level],38 )
79 format='%(asctime)s %(levelname)s %(message)s')39
8040 giraffe_app.run()
81 output_dir = options.output_dir41
8242
83 # Open and parse input files
84 repo = giraffe.ast.Repository()
85 for f in args:
86 print "Reading %s" % f
87 repo.add_gir (file(f, "r"))
88 repo.link ()
89
90 for lang in giraffe.languages.modules:
91 try:
92 os.makedirs (get_output_dir (lang, output_dir))
93 except OSError:
94 pass
95
96 for ns in repo.namespaces:
97 output_path = get_output_file (ns, lang, output_dir)
98 output_file = codecs.open (output_path, "w", "utf-8")
99 print "Writing %s" % output_path
100 header = HTML_HEADER % { "namespace" : ns.name, "version" : ns.version, "lang" : get_lang_name(lang).title() }
101 print >> output_file, header
102 lang.write_namespace (ns, output_file)
103 print >> output_file, HTML_FOOTER
104
105 css_dir = output_dir
106 css_path = get_css_path ()
107 print "Installing %s into %s" % (css_path, css_dir)
108 shutil.copy(css_path, css_dir)
10943
11044
=== modified file 'giraffe/__init__.py'
--- giraffe/__init__.py 2011-02-22 13:14:07 +0000
+++ giraffe/__init__.py 2011-03-24 17:20:19 +0000
@@ -2,3 +2,4 @@
22
3import giraffe.ast3import giraffe.ast
4import giraffe.languages4import giraffe.languages
5import giraffe.app
56
=== added file 'giraffe/app.py'
--- giraffe/app.py 1970-01-01 00:00:00 +0000
+++ giraffe/app.py 2011-03-24 17:20:19 +0000
@@ -0,0 +1,97 @@
1#! /usr/bin/python
2# -*- coding: utf-8 -*-
3# vim:set shiftwidth=4 tabstop=4 noexpandtab:
4
5import os
6import sys
7import shutil
8import codecs
9import logging
10import giraffe.ast
11import giraffe.languages
12
13LEVELS = ( logging.ERROR,
14 logging.WARNING,
15 logging.INFO,
16 logging.DEBUG,
17 )
18
19HTML_HEADER = \
20"""<?xml version="1.0" encoding="utf-8"?>
21<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
22<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
23 <head>
24 <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/>
25
26 <title>%(namespace)s-%(version)s %(lang)s API Documentation</title>
27 <link rel="stylesheet" href="../giraffe.css" type="text/css" />
28 </head>
29<body>
30<h1>%(namespace)s-%(version)s %(lang)s API Documentation</h1>
31"""
32
33HTML_FOOTER = \
34"""</body>
35</html>
36"""
37
38def get_css_path ():
39 giraffe_dir = os.path.dirname (giraffe.__file__)
40 return os.path.join (giraffe_dir, "data", "giraffe.css")
41
42def get_lang_name (lang):
43 return lang.__name__.rpartition(".")[2]
44
45def get_output_dir (lang, output_dir):
46 return "%s/%s" % (output_dir, get_lang_name (lang))
47
48def get_output_file (ns, lang, output_dir):
49 return get_output_dir (lang, output_dir) + "/%s-%s.html" % (ns.name, ns.version)
50
51class GiraffeApp:
52 def __init__(self, args, output_dir, debug_mode = False, logging_level = None):
53 self.output_dir = output_dir
54 self.debug_mode = debug_mode
55 if not logging_level:
56 # To inherit the logging level from the importer, unless explicitly
57 # specified
58 self.logging_level = [i for i, lvl in enumerate(LEVELS) if lvl == logging.root.level][0]
59 else:
60 self.logging_level = logging_level
61 self.args = args
62
63 # Set the verbosity
64 if self.debug_mode:
65 self.logging_level = 3
66 logging.basicConfig(level=LEVELS[self.logging_level],
67 format='%(asctime)s %(levelname)s %(message)s')
68
69 def run(self):
70 # Open and parse input files
71 repo = giraffe.ast.Repository()
72 for f in self.args:
73 print "Reading %s" % f
74 repo.add_gir (file(f, "r"))
75 repo.link ()
76
77 for lang in giraffe.languages.modules:
78 try:
79 os.makedirs (get_output_dir (lang, self.output_dir))
80 except OSError:
81 pass
82
83 for ns in repo.namespaces:
84 output_path = get_output_file (ns, lang, self.output_dir)
85 output_file = codecs.open (output_path, "w", "utf-8")
86 print "Writing %s" % output_path
87 header = HTML_HEADER % { "namespace" : ns.name, "version" : ns.version, "lang" : get_lang_name(lang).title() }
88 print >> output_file, header
89 lang.write_namespace (ns, output_file)
90 print >> output_file, HTML_FOOTER
91
92 css_dir = self.output_dir
93 css_path = get_css_path ()
94 print "Installing %s into %s" % (css_path, css_dir)
95 shutil.copy(css_path, css_dir)
96
97

Subscribers

People subscribed via source and target branches