<s> from __future__ import unicode_literals <EOL> import os <EOL> from mkdocs import utils , legacy <EOL> from mkdocs . config . base import Config , ValidationError <EOL> class BaseConfigOption ( object ) : <EOL> def __init__ ( self ) : <EOL> self . warnings = [ ] <EOL> self . default = None <EOL> def is_required ( self ) : <EOL> return False <EOL> def validate ( self , value ) : <EOL> return self . run_validation ( value ) <EOL> def reset_warnings ( self ) : <EOL> self . warnings = [ ] <EOL> def pre_validation ( self , config , key_name ) : <EOL> """<STR_LIT>""" <EOL> def run_validation ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> def post_validation ( self , config , key_name ) : <EOL> """<STR_LIT>""" <EOL> class SubConfig ( BaseConfigOption , Config ) : <EOL> def __init__ ( self , * config_options ) : <EOL> BaseConfigOption . __init__ ( self ) <EOL> Config . __init__ ( self , config_options ) <EOL> self . default = { } <EOL> def validate ( self , value ) : <EOL> self . load_dict ( value ) <EOL> return self . run_validation ( value ) <EOL> def run_validation ( self , value ) : <EOL> Config . validate ( self ) <EOL> return self <EOL> class OptionallyRequired ( BaseConfigOption ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default = None , required = False ) : <EOL> super ( OptionallyRequired , self ) . __init__ ( ) <EOL> self . default = default <EOL> self . required = required <EOL> def is_required ( self ) : <EOL> return self . required <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> if self . default is not None : <EOL> value = self . default <EOL> elif not self . required : <EOL> return <EOL> elif self . required : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> return self . run_validation ( value ) <EOL> class Type ( OptionallyRequired ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , type_ , length = None , ** kwargs ) : <EOL> super ( Type , self ) . __init__ ( ** kwargs ) <EOL> self . _type = type_ <EOL> self . length = length <EOL> def run_validation ( self , value ) : <EOL> if not isinstance ( value , self . _type ) : <EOL> msg = ( "<STR_LIT>" <EOL> . format ( self . _type , type ( value ) ) ) <EOL> elif self . length is not None and len ( value ) != self . length : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( self . _type , value , self . length , <EOL> len ( value ) ) <EOL> else : <EOL> return value <EOL> raise ValidationError ( msg ) <EOL> class Deprecated ( BaseConfigOption ) : <EOL> def __init__ ( self , moved_to = None ) : <EOL> super ( Deprecated , self ) . __init__ ( ) <EOL> self . default = None <EOL> self . moved_to = moved_to <EOL> def pre_validation ( self , config , key_name ) : <EOL> if config . get ( key_name ) is None or self . moved_to is None : <EOL> return <EOL> warning = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . warnings . append ( warning ) <EOL> if '<STR_LIT:.>' not in self . moved_to : <EOL> target = config <EOL> target_key = self . moved_to <EOL> else : <EOL> move_to , target_key = self . moved_to . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> target = config <EOL> for key in move_to . split ( '<STR_LIT:.>' ) : <EOL> target = target . setdefault ( key , { } ) <EOL> if not isinstance ( target , dict ) : <EOL> return <EOL> target [ target_key ] = config . pop ( key_name ) <EOL> class URL ( OptionallyRequired ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default = '<STR_LIT>' , required = False ) : <EOL> super ( URL , self ) . __init__ ( default , required ) <EOL> def run_validation ( self , value ) : <EOL> if value == '<STR_LIT>' : <EOL> return value <EOL> try : <EOL> parsed_url = utils . urlparse ( value ) <EOL> except ( AttributeError , TypeError ) : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> if parsed_url . scheme : <EOL> return value <EOL> raise ValidationError ( <EOL> "<STR_LIT>" ) <EOL> class RepoURL ( URL ) : <EOL> """<STR_LIT>""" <EOL> def post_validation ( self , config , key_name ) : <EOL> if config [ '<STR_LIT>' ] is not None and config . get ( '<STR_LIT>' ) is None : <EOL> repo_host = utils . urlparse ( <EOL> config [ '<STR_LIT>' ] ) . netloc . lower ( ) <EOL> if repo_host == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif repo_host == '<STR_LIT>' : <EOL> config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> config [ '<STR_LIT>' ] = repo_host . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] . title ( ) <EOL> class Dir ( Type ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , exists = False , ** kwargs ) : <EOL> super ( Dir , self ) . __init__ ( type_ = utils . string_types , ** kwargs ) <EOL> self . exists = exists <EOL> def run_validation ( self , value ) : <EOL> value = super ( Dir , self ) . run_validation ( value ) <EOL> if self . exists and not os . path . isdir ( value ) : <EOL> raise ValidationError ( "<STR_LIT>" . format ( value ) ) <EOL> return os . path . abspath ( value ) <EOL> class SiteDir ( Dir ) : <EOL> """<STR_LIT>""" <EOL> def post_validation ( self , config , key_name ) : <EOL> if config [ '<STR_LIT>' ] . startswith ( config [ '<STR_LIT>' ] ) : <EOL> raise ValidationError ( <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) . format ( config [ '<STR_LIT>' ] , config [ '<STR_LIT>' ] ) ) <EOL> elif config [ '<STR_LIT>' ] . startswith ( config [ '<STR_LIT>' ] ) : <EOL> self . warnings . append ( <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) . format ( config [ '<STR_LIT>' ] , config [ '<STR_LIT>' ] ) ) <EOL> class ThemeDir ( Dir ) : <EOL> """<STR_LIT>""" <EOL> def post_validation ( self , config , key_name ) : <EOL> theme_in_config = any ( [ '<STR_LIT>' in c for c in config . user_configs ] ) <EOL> package_dir = os . path . abspath ( <EOL> os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) <EOL> theme_dir = [ utils . get_themes ( ) [ config [ '<STR_LIT>' ] ] , ] <EOL> config [ '<STR_LIT>' ] = os . path . join ( package_dir , '<STR_LIT>' ) <EOL> if config [ '<STR_LIT>' ] is not None : <EOL> if not theme_in_config : <EOL> theme_dir = [ ] <EOL> theme_dir . insert ( <NUM_LIT:0> , config [ '<STR_LIT>' ] ) <EOL> config [ '<STR_LIT>' ] = theme_dir <EOL> search_assets = os . path . join ( package_dir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> config [ '<STR_LIT>' ] . append ( search_assets ) <EOL> class Theme ( OptionallyRequired ) : <EOL> """<STR_LIT>""" <EOL> def run_validation ( self , value ) : <EOL> themes = utils . get_theme_names ( ) <EOL> moved_themes = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> if value in themes : <EOL> return value <EOL> elif value in moved_themes : <EOL> raise ValidationError ( <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( value ) <EOL> ) <EOL> raise ValidationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( value , '<STR_LIT:U+002CU+0020>' . join ( themes ) ) <EOL> ) <EOL> class Extras ( OptionallyRequired ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file_match = None , ** kwargs ) : <EOL> super ( Extras , self ) . __init__ ( ** kwargs ) <EOL> self . file_match = file_match <EOL> def run_validation ( self , value ) : <EOL> if isinstance ( value , list ) : <EOL> return value <EOL> else : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" . format ( type ( value ) ) ) <EOL> def walk_docs_dir ( self , docs_dir ) : <EOL> if self . file_match is None : <EOL> raise StopIteration <EOL> for ( dirpath , dirs , filenames ) in os . walk ( docs_dir ) : <EOL> dirs . sort ( ) <EOL> for filename in sorted ( filenames ) : <EOL> fullpath = os . path . join ( dirpath , filename ) <EOL> if os . path . islink ( fullpath ) : <EOL> fp = os . path . join ( dirpath , os . readlink ( fullpath ) ) <EOL> if not os . path . exists ( fp ) : <EOL> continue <EOL> relpath = os . path . normpath ( os . path . relpath ( fullpath , docs_dir ) ) <EOL> if self . file_match ( relpath ) : <EOL> yield relpath <EOL> def post_validation ( self , config , key_name ) : <EOL> if config [ key_name ] is not None : <EOL> return <EOL> extras = [ ] <EOL> for filename in self . walk_docs_dir ( config [ '<STR_LIT>' ] ) : <EOL> extras . append ( filename ) <EOL> config [ key_name ] = extras <EOL> class Pages ( Extras ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( Pages , self ) . __init__ ( utils . is_markdown_file , ** kwargs ) <EOL> def run_validation ( self , value ) : <EOL> if not isinstance ( value , list ) : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" . format ( type ( value ) ) ) <EOL> if len ( value ) == <NUM_LIT:0> : <EOL> return <EOL> config_types = set ( type ( l ) for l in value ) <EOL> if config_types . issubset ( set ( [ utils . text_type , dict , str ] ) ) : <EOL> return value <EOL> if config_types . issubset ( set ( [ utils . text_type , list , str ] ) ) : <EOL> return legacy . pages_compat_shim ( value ) <EOL> raise ValidationError ( "<STR_LIT>" . format ( <EOL> config_types , <EOL> set ( [ utils . text_type , dict , ] ) <EOL> ) ) <EOL> def post_validation ( self , config , key_name ) : <EOL> if config [ key_name ] is not None : <EOL> return <EOL> pages = [ ] <EOL> for filename in self . walk_docs_dir ( config [ '<STR_LIT>' ] ) : <EOL> if os . path . splitext ( filename ) [ <NUM_LIT:0> ] == '<STR_LIT:index>' : <EOL> pages . insert ( <NUM_LIT:0> , filename ) <EOL> else : <EOL> pages . append ( filename ) <EOL> config [ key_name ] = utils . nest_paths ( pages ) <EOL> class NumPages ( OptionallyRequired ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , at_lest = <NUM_LIT:1> , ** kwargs ) : <EOL> super ( NumPages , self ) . __init__ ( ** kwargs ) <EOL> self . at_lest = at_lest <EOL> def post_validation ( self , config , key_name ) : <EOL> if config [ key_name ] is not None : <EOL> return <EOL> try : <EOL> config [ key_name ] = len ( config [ '<STR_LIT>' ] ) > self . at_lest <EOL> except TypeError : <EOL> config [ key_name ] = False <EOL> class Private ( OptionallyRequired ) : <EOL> """<STR_LIT>""" <EOL> def run_validation ( self , value ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> class MarkdownExtensions ( OptionallyRequired ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , builtins = None , configkey = '<STR_LIT>' , ** kwargs ) : <EOL> super ( MarkdownExtensions , self ) . __init__ ( ** kwargs ) <EOL> self . builtins = builtins or [ ] <EOL> self . configkey = configkey <EOL> self . configdata = { } <EOL> def run_validation ( self , value ) : <EOL> if not isinstance ( value , ( list , tuple ) ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> extensions = [ ] <EOL> for item in value : <EOL> if isinstance ( item , dict ) : <EOL> if len ( item ) > <NUM_LIT:1> : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> ext , cfg = item . popitem ( ) <EOL> extensions . append ( ext ) <EOL> if cfg is None : <EOL> continue <EOL> if not isinstance ( cfg , dict ) : <EOL> raise ValidationError ( '<STR_LIT>' <EOL> "<STR_LIT>" . format ( ext ) ) <EOL> self . configdata [ ext ] = cfg <EOL> elif isinstance ( item , utils . string_types ) : <EOL> extensions . append ( item ) <EOL> else : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> return utils . reduce_list ( self . builtins + extensions ) <EOL> def post_validation ( self , config , key_name ) : <EOL> config [ self . configkey ] = self . configdata </s>
