<s> import glob <EOL> import logging <EOL> import os <EOL> from pathlib import Path <EOL> from subprocess import check_output <EOL> import webbrowser <EOL> from setuptools import Command <EOL> DOCS_PATH = "<STR_LIT>" <EOL> DOCS_BUILD_PATH = os . path . join ( DOCS_PATH , "<STR_LIT>" ) <EOL> ENCODING = '<STR_LIT:utf-8>' <EOL> LICENSE_PATH = "<STR_LIT>" <EOL> README_PATH = "<STR_LIT>" <EOL> PANDOC_FILTER_DIR = "<STR_LIT>" <EOL> PANDOC_HTML_FILTER = "<STR_LIT>" <EOL> PANDOC_RST_FILTER = "<STR_LIT>" <EOL> log = logging . getLogger ( __name__ ) <EOL> def ensure_dir ( path ) : <EOL> if not os . path . exists ( path ) : <EOL> log . info ( "<STR_LIT>" % path ) <EOL> os . makedirs ( path ) <EOL> def read ( path , encoding = None ) : <EOL> if encoding is None : <EOL> encoding = ENCODING <EOL> with open ( path , '<STR_LIT:r>' , encoding = ENCODING ) as f : <EOL> text = f . read ( ) <EOL> return text <EOL> def write ( text , path , description = None ) : <EOL> """<STR_LIT>""" <EOL> desc = ( '<STR_LIT>' % description ) if description else '<STR_LIT>' <EOL> log . info ( "<STR_LIT>" % ( desc , path ) ) <EOL> with open ( path , '<STR_LIT:w>' , encoding = ENCODING ) as f : <EOL> f . write ( text ) <EOL> def run_pandoc ( args ) : <EOL> args = [ '<STR_LIT>' ] + args <EOL> log . info ( "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( args ) ) <EOL> try : <EOL> stdout = check_output ( args ) <EOL> except FileNotFoundError as err : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % err ) <EOL> raise Exception ( msg ) <EOL> return stdout <EOL> def run_pandoc_filter ( filter_name , output_format , source_path , target_path ) : <EOL> """<STR_LIT>""" <EOL> filter_path = os . path . join ( PANDOC_FILTER_DIR , filter_name ) <EOL> return run_pandoc ( [ "<STR_LIT>" , filter_path , "<STR_LIT>" % output_format , <EOL> "<STR_LIT>" , target_path , source_path ] ) <EOL> def html_target_path ( rel_path ) : <EOL> return os . path . join ( DOCS_BUILD_PATH , rel_path ) <EOL> def md2html ( md_path ) : <EOL> opath = Path ( md_path ) <EOL> target_path = html_target_path ( str ( opath . with_suffix ( "<STR_LIT>" ) ) ) <EOL> run_pandoc_filter ( PANDOC_HTML_FILTER , "<STR_LIT:html>" , md_path , target_path ) <EOL> return target_path <EOL> def build_html ( ) : <EOL> ensure_dir ( DOCS_BUILD_PATH ) <EOL> target_readme_path = md2html ( README_PATH ) <EOL> ensure_dir ( html_target_path ( DOCS_PATH ) ) <EOL> md_paths = glob . glob ( os . path . join ( DOCS_PATH , "<STR_LIT>" ) ) <EOL> for md_path in md_paths : <EOL> md2html ( md_path ) <EOL> readme_opath = Path ( target_readme_path ) <EOL> uri = readme_opath . resolve ( ) . as_uri ( ) <EOL> log . info ( "<STR_LIT>" % ( target_readme_path , uri ) ) <EOL> webbrowser . open ( uri ) <EOL> def update_long_description ( ) : <EOL> return run_pandoc_filter ( PANDOC_RST_FILTER , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> class CommandBase ( Command ) : <EOL> description = None <EOL> user_options = [ ] <EOL> def initialize_options ( self ) : <EOL> pass <EOL> def finalize_options ( self ) : <EOL> pass <EOL> def run ( self ) : <EOL> try : <EOL> self . _run ( ) <EOL> except FileNotFoundError as err : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> class BuildHtmlCommand ( CommandBase ) : <EOL> description = "<STR_LIT>" <EOL> def _run ( self ) : <EOL> build_html ( ) <EOL> class LongDescriptionCommand ( CommandBase ) : <EOL> description = "<STR_LIT>" <EOL> def _run ( self ) : <EOL> update_long_description ( ) </s>
