Skip to content

Commit

Permalink
Update directdocs to run under python3
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfletch committed Nov 26, 2019
1 parent 5d75fcb commit 3d9887d
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 60 deletions.
3 changes: 2 additions & 1 deletion directdocs/acquireoriginal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#! /usr/bin/env python
#! /usr/bin/env python3
"""Builds links from the OpenGL.org man pages to our "original" directory"""
from __future__ import absolute_import
import os, sys, glob, shutil, re, subprocess

MAN_TARGET = 'original'
Expand Down
4 changes: 3 additions & 1 deletion directdocs/dumbmarkup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Simplistic wiki-like format parsing support"""
from __future__ import absolute_import
import re,os,sys,textwrap, datetime
import logging
import six
log = logging.getLogger( __name__ )

text_re = re.compile(
Expand Down Expand Up @@ -84,7 +86,7 @@ def child_function( match ):
return markup( text, child_function )
def append( self, value ):
"""Append given value to our content"""
if isinstance( value, (str,unicode)):
if isinstance( value, (str,six.text_type)):
if self.children:
last_child = self.children[-1]
if last_child.tail:
Expand Down
21 changes: 13 additions & 8 deletions directdocs/dumbpydoc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#! /usr/bin/env python
"""Extremely dumb replacement for pydoc"""
from __future__ import absolute_import
from __future__ import print_function
from directdocs import model,references
import OpenGL
import six
from six.moves import range
from six.moves import zip
OpenGL.USE_ACCELERATE = False # document the Python versions
OpenGL.MODULE_ANNOTATIONS = True # tell us where the constants/alternates are defined...
from OpenGL.extensions import _Alternate as Alternate
Expand All @@ -13,7 +18,7 @@
from OpenGL.GL import glGetString
from OpenGL.platform.baseplatform import _NullFunctionPointer as NullFunc
from OpenGL import platform
import dumbmarkup
from . import dumbmarkup

CythonMethod = type( platform.PLATFORM.GL.glGetString )

Expand Down Expand Up @@ -111,7 +116,7 @@ def inspect( self ):
if self._inspected:
return
self._inspected = True
for key,value in sorted( self.mod.__dict__.items(), key=lambda x: x[0].lower()):
for key,value in sorted( list(self.mod.__dict__.items()), key=lambda x: x[0].lower()):
if self.interesting( value ):
for attr,types in self.FT_MAP:
if isinstance( value, types ):
Expand All @@ -129,8 +134,8 @@ def inspect( self ):
value = inspector( value, key )
collection.append( (key,value))
else:
if not isinstance( value, (str,unicode,int,long,dict)):
print 'not interesting', key, type(value)
if not isinstance( value, (str,six.text_type,int,int,dict)):
print('not interesting', key, type(value))
if self.is_package:
dirname = os.path.dirname( self.mod.__file__ )
for name in sorted(os.listdir( dirname )):
Expand All @@ -149,7 +154,7 @@ def sub_module( self, name ):
mod = PyModule( ".".join( [self.name,name] ))
try:
mod.inspect()
except Exception, err:
except Exception as err:
log.warn( 'Unable to import %s: %s'%( mod.name, err ))
return None
else:
Expand Down Expand Up @@ -218,14 +223,14 @@ def docstring( self ):
if self.cls.__doc__:
try:
return textwrap.dedent( self.cls.__doc__ or '' )
except TypeError, err:
except TypeError as err:
return None
else:
return None

def inspect( self ):
"""Introspect to find methods, properties, etceteras"""
for key,value in sorted(self.cls.__dict__.items(), key=lambda x: x[0].lower()):
for key,value in sorted(list(self.cls.__dict__.items()), key=lambda x: x[0].lower()):
if isinstance( value, (
types.FunctionType,
types.MethodType,
Expand All @@ -249,7 +254,7 @@ def docstring( self ):
if self.target.__doc__:
try:
return textwrap.dedent( self.target.__doc__ or '' )
except TypeError, err:
except TypeError as err:
return None
else:
return None
Expand Down
40 changes: 22 additions & 18 deletions directdocs/generate.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#! /usr/bin/env python
#! /usr/bin/env python3
"""Generates the PyOpenGL reference documentation"""
from __future__ import absolute_import
from __future__ import print_function
import glob, os, datetime, subprocess, re, sys
#import elementtree.ElementTree as ET
import lxml.etree as ET
from genshi.template import TemplateLoader
import logging
import six
log = logging.getLogger( 'generate' )

from directdocs.model import Function, Parameter, ParameterReference
from directdocs import model,references
from OpenGL import __version__
from OpenGL._bytes import as_8_bit
from OpenGL import GL, GLU, GLUT, GLE,GLX

loader = TemplateLoader([os.path.join(os.path.dirname( __file__ ), 'templates')])
Expand All @@ -24,7 +28,7 @@
XML_NS = "http://www.w3.org/XML/1998/namespace"
LINK_NS = "http://www.w3.org/1999/xlink"

WRAPPER = """<?xml version="1.0" encoding="UTF-8"?>
WRAPPER = b"""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book SYSTEM "test" [ <!ENTITY nbsp " "> ]>
<book
Expand All @@ -33,7 +37,7 @@
xmlns:xlink="%s"
>
%%s
</book>"""%( DOCBOOK_NS, MML_NS, LINK_NS )
</book>"""%tuple( as_8_bit(x) for x in [DOCBOOK_NS, MML_NS, LINK_NS ] )

IMPLEMENTATION_MODULES = [
# modules which contain external API entry points...
Expand All @@ -54,14 +58,14 @@ def get_crossref( self, title, volume=None,section=None ):
key = '%s.%s'%(title,volume)
if '(' in title:
title = title.split('(')[0]
if self.sections.has_key( key ):
if key in self.sections:
return self.sections[key]
elif self.section_titles.has_key( title ):
elif title in self.section_titles:
return self.section_titles[ title ]
elif self.functions.has_key( title ):
elif title in self.functions:
return self.functions[title]
elif title.startswith( 'glX') or title.startswith( 'wgl' ):
print 'Reference to', title, 'in', getattr(section,'title','Unknown')
print('Reference to', title, 'in', getattr(section,'title','Unknown'))
return None
else:
# try a linear scan for suffixed version...
Expand Down Expand Up @@ -133,7 +137,7 @@ def process( self, tree ):
elif id:
self.discussions.append(section)
elif not id:
log.warn( 'Found reference section without id: %s', section.items() )
log.warn( 'Found reference section without id: %s', list(section.items()) )
self.discussions.append( section )
continue
processed_sections[ id ] = True
Expand Down Expand Up @@ -176,11 +180,11 @@ def process_funcprototype( self, node ):
data_type = typ,
name = paramname
))
except Exception, err:
print 'Failure retrieving parameter:', str(param)
except Exception as err:
print('Failure retrieving parameter:', str(param))
try:
function = self.functions[ funcname ]
except KeyError, err:
except KeyError as err:
function = Function(funcname,self)
self.functions[ funcname ] = function
# err.args += (self.functions.keys(),)
Expand Down Expand Up @@ -209,7 +213,7 @@ def process_variablelist( self, node ):

self.varrefs.extend( set )

HEADER_KILLER = re.compile( '[<][!]DOCTYPE.*?[>]', re.MULTILINE|re.DOTALL )
HEADER_KILLER = re.compile( b'[<][!]DOCTYPE.*?[>]', re.MULTILINE|re.DOTALL )
def strip_bad_header( data ):
"""Header in the xml files declared from opengl.org doesn't declare namespaces but files use them"""
match = HEADER_KILLER.search( data )
Expand All @@ -219,26 +223,26 @@ def strip_bad_header( data ):


def load_file( filename ):
data = WRAPPER%(strip_bad_header(open(filename).read()))
data = WRAPPER%(strip_bad_header(open(filename,'rb').read()))
# data = open(filename).read()
parser = ET.ETCompatXMLParser(resolve_entities=False)
try:
return filter_comments( ET.XML( data, parser ) )
except Exception, err:
except Exception as err:
log.error( "Failure loading file: %r", filename )
raise

def filter_comments( tree ):
for element in tree:
if isinstance(element.tag, (str,unicode)):
if isinstance(element.tag, (str,six.text_type)):
filter_comments( element )
else:
tree.remove( element )
return tree

def init_output( ):
if not os.path.isdir( OUTPUT_DIRECTORY ):
print 'Creating new manual directory: %s'%(OUTPUT_DIRECTORY )
print('Creating new manual directory: %s'%(OUTPUT_DIRECTORY ))
os.mkdir( OUTPUT_DIRECTORY )
for file in os.listdir( 'output' ):
src = os.path.join( 'output', file )
Expand Down Expand Up @@ -271,7 +275,7 @@ def main():
log.info( 'Loading references' )
if os.path.isfile( references.CACHE_FILE ):
import pickle
samples = pickle.loads( open(references.CACHE_FILE).read())
samples = pickle.loads( open(references.CACHE_FILE,'rb').read())
else:
log.warn( """Loading references directly, run ./references.py to pre-generate""" )
samples = references.loadData()
Expand Down Expand Up @@ -345,7 +349,7 @@ def main():
data = stream.render('html')
data = data.encode('utf-8')
open(
output_file, 'w'
output_file, 'wb'
).write( data )
old_file = os.path.splitext( output_file )[0] + '.xhtml'
base = ref.url( section )
Expand Down
15 changes: 9 additions & 6 deletions directdocs/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Modelling objects for the documentation generator"""
from __future__ import absolute_import
import logging, types, inspect
from six.moves import zip
log = logging.getLogger( 'directdocs.model' )

from OpenGL import __version__
Expand Down Expand Up @@ -95,8 +97,8 @@ def set_deprecation( self, value ):
def has_function( self, name ):
"""Ask whether we have this name defined"""
return (
self.functions.has_key( name ) or
self.py_functions.has_key( name )
name in self.functions or
name in self.py_functions
)
def get_module( self ):
"""Retrieve our module"""
Expand Down Expand Up @@ -135,7 +137,7 @@ def find_python_functions( self ):
alias = name,
)
self.py_functions[name] = function
if not self.reference.functions.has_key( name ):
if name not in self.reference.functions:
self.reference.functions[ name ] = function
def get_samples( self, sample_set ):
"""Populate our sample-set for all of the available samples"""
Expand All @@ -146,11 +148,12 @@ def get_samples( self, sample_set ):
filtered = []
filter_set = {}
for name in names:
if not filter_set.has_key( name ):
if name not in filter_set:
filtered.append( name )
for name in filtered:
if sample_set.has_key( name ):
if name in sample_set:
self.samples.append( (name,Sample.joined(sample_set[name])))
self.samples.sort()

class Function( object ):
"""Function description produced from docs
Expand Down Expand Up @@ -260,7 +263,7 @@ def get_parameters( self, target ):
name, default=default_dict.get( name, NOT_DEFINED ),
function = self,
))
except Exception, err:
except Exception as err:
import pdb
pdb.set_trace()
if varargs:
Expand Down
9 changes: 6 additions & 3 deletions directdocs/oglctutorials.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#! /usr/bin/env python
"""Generate the OpenGLContext shader tutorials from '''-string marked-up source code"""
from __future__ import absolute_import
from __future__ import print_function
import re,os,sys,textwrap, datetime
from genshi.template import TemplateLoader
import logging
from six.moves import range
log = logging.getLogger( 'tutorials' )
from dumbmarkup import *
from .dumbmarkup import *

loader = TemplateLoader([os.path.join(os.path.dirname( __file__ ), 'templates')])

Expand Down Expand Up @@ -101,7 +104,7 @@ def generate_index( paths ):
)
data = stream.render( 'html' )
html_file = os.path.join( OUTPUT_DIRECTORY, 'index.html' )
print 'writing', html_file
print('writing', html_file)
open( html_file, 'w').write( data )
redirect( html_file )

Expand All @@ -116,7 +119,7 @@ def generate( tutorial, prev=None, next=None, path=None ):
next=next,
)
data = stream.render( 'html' )
print 'writing', tutorial.html_file
print('writing', tutorial.html_file)
open( tutorial.html_file, 'w').write( data )
redirect( tutorial.html_file )

Expand Down
Loading

0 comments on commit 3d9887d

Please sign in to comment.