示例#1
0
def str_methods():

    # Related
    assert "what the".split(" ") == ["what", "the"]
    assert "what the" == " ".join(["what", "the"])

    # You'Re | You're
    assert "You're HIRED!!".title() == "You'Re Hired!!"
    assert string_capwords("You're HIRED!!") == "You're Hired!!"
示例#2
0
def _func_exceptions(parent, source):
    NAME = 'exceptions'
    try:
        exceptions = source['exceptions']
        # If renaming exceptions
        if isinstance(exceptions, dict):
            NAME = exceptions['CALL']
            exceptions = source.get(NAME, exceptions)
        # Get values
        for label, (message, description) in enumerate(exceptions):
            if not label:
                LABEL = string_capwords(NAME)
                new(parent, 'p', class_='info label',
                                 string=LABEL + ':' if LABEL else LABEL)
            new(parent, 'code', class_='exception_message', string=message)
            _str(new(parent, 'p', class_='exception_note'), description)
            new(parent, 'br')
    except (KeyError, ValueError):
        return
示例#3
0
def _func_exceptions(parent, source):
    NAME = 'exceptions'
    try:
        exceptions = source['exceptions']
        # If renaming exceptions
        if isinstance(exceptions, dict):
            NAME = exceptions['CALL']
            exceptions = source.get(NAME, exceptions)
        # Get values
        for label, (message, description) in enumerate(exceptions):
            if not label:
                LABEL = string_capwords(NAME)
                new(parent,
                    'p',
                    class_='info label',
                    string=LABEL + ':' if LABEL else LABEL)
            new(parent, 'code', class_='exception_message', string=message)
            _str(new(parent, 'p', class_='exception_note'), description)
            new(parent, 'br')
    except (KeyError, ValueError):
        return
示例#4
0
def _func_examples(parent, source):
    NAME = 'examples'
    try:
        examples = source['examples']
        # If renaming examples
        if isinstance(examples, dict):
            NAME = examples['CALL']
            examples = source.get(NAME, examples)
        for label, (note, example) in enumerate(examples):
            if not label:
                LABEL = string_capwords(NAME)
                new(parent, 'p', class_='info label',
                                 string=LABEL + ':' if LABEL else LABEL)
            n = new(parent, 'p', class_='info note')
            _str(n, str(note) + ':')
            new(parent, 'br')
            pre = new(parent, 'pre', class_='info')
            c = new(pre, 'code', class_='snippet')
            _code(c, example)
            new(parent, 'br')
    except (KeyError, ValueError):
        return
示例#5
0
def _func_examples(parent, source):
    NAME = 'examples'
    try:
        examples = source['examples']
        # If renaming examples
        if isinstance(examples, dict):
            NAME = examples['CALL']
            examples = source.get(NAME, examples)
        for label, (note, example) in enumerate(examples):
            if not label:
                LABEL = string_capwords(NAME)
                new(parent,
                    'p',
                    class_='info label',
                    string=LABEL + ':' if LABEL else LABEL)
            n = new(parent, 'p', class_='info note')
            _str(n, str(note) + ':')
            new(parent, 'br')
            pre = new(parent, 'pre', class_='info')
            c = new(pre, 'code', class_='snippet')
            _code(c, example)
            new(parent, 'br')
    except (KeyError, ValueError):
        return
示例#6
0
def _build(sources, outfolder, gentoc, toc, external_css):
    for pagename, source in sources.items():
        # If there is no valid source
        if source is None:
            continue
        # Clear soup and add en empty, new body
        SOUP.body.decompose()
        new(SOUP.html, 'body')

        # Set title
        SOUP.html.head.title.string = pagename
        # Get essentail values
        filename, depends = toc[pagename]
        # Constants
        SECTIONS = 7
        # Build basic structure
        column1 = new(SOUP.body, 'div', id='column1')
        column2 = new(SOUP.body, 'div', id='column2')

        generic = new(column1, 'div', id='generic')
        sidebar = new(column1, 'div', id='sidebar')
        content = new(column2, 'div', id='content')

        # OPTIONAL: custom header section
        try:
            _html_format(generic, source['HEAD'], outfolder)
            new(generic, 'br')
        except KeyError:
            SECTIONS -= 1
        # OPTIONAL: custom menu in sidebar
        try:
            _html_format(sidebar, source['MENU'], outfolder)
            new(sidebar, 'br')
        except KeyError:
            SECTIONS -= 1
        # OPTIONAL: custom abstract and introduction
        try:
            _html_format(content, source['INFO'], outfolder)
            new(content, 'br')
        except KeyError:
            SECTIONS -= 1
        # OPTIONAL: index
        if gentoc:
            sidebar_type = new(sidebar, 'div')
            new(sidebar_type, 'p', class_='label', string='Modules:')
            new(sidebar, 'br')
            _indx_format(sidebar, pagename, toc)
            new(sidebar, 'br')

        # TODO: Implement a Schema validator for better user-feedback

        # TODO: add FOOT key

        # TODO: add EXEC to cdoc to add "interactive" python snippets to code
        # EXEC: |
        #   with open('VERSION') as file:
        #       # Insert to USER:About
        #       DOC[USER][0].insert(0, {'name': 'Version', 'info': file.read()})

        # OPTIONAL: text and code
        try:
            blocks = source['TEXT']
            for block in blocks:
                # Get the first element of the list as the section name
                try:
                    section = string_capwords(block[0])
                except IndexError:
                    continue
                sidebar_text = new(sidebar, 'div')
                new(sidebar_text, 'p', class_='label',
                                       string='{}:'.format(section))
                new(sidebar_text, 'br')
                content_text = new(content, 'div')
                new(content_text, 'h2', class_='title', string=section)
                for user in block[1:]:
                    _text_format(sidebar_text, content_text, user)
                new(sidebar, 'br')
        except KeyError:
            SECTIONS -= 1
        # OPTIONAL: user defined
        try:
            userdefs = source['USER']
            for userdef in userdefs:
                # Get the first element of the list as the section name
                try:
                    section = string_capwords(userdef[0])
                except IndexError:
                    continue
                sidebar_user = new(sidebar, 'div')
                new(sidebar_user, 'p', class_='label',
                                       string='{}:'.format(section))
                new(sidebar_user, 'br')
                content_user = new(content, 'div')
                new(content_user, 'h2', class_='title', string=section)
                for user in userdef[1:]:
                    _user_format(sidebar_user, content_user, user)
                new(sidebar, 'br')
        except KeyError:
            SECTIONS -= 1
        # OPTIONAL: type definitions
        try:
            types = source['TYPE']
            sidebar_type = new(sidebar, 'div')
            new(sidebar_type, 'p', class_='label', string='Types:')
            new(sidebar_type, 'br')
            content_type = new(content, 'div')
            new(content_type, 'h2', class_='title', string='Types')
            for type in types:
                _type_format(sidebar_type, content_type, type)
            new(sidebar, 'br')
        except KeyError:
            SECTIONS -= 1
        # OPTIONAL: function definitions
        try:
            funcs = source['FUNC']
            sidebar_func = new(sidebar, 'div')
            new(sidebar_func, 'p', class_='label', string='Functions:')
            new(sidebar_func, 'br')
            content_func = new(content, 'div')
            new(content_func, 'h2', class_='title', string='Functions')
            for func in funcs:
                _func_format(sidebar_func, content_func, func)
        except KeyError:
            SECTIONS -= 1

        # Create HTML file
        if SECTIONS:
            output = os_path_join(outfolder, filename)
            with open(output, 'w', encoding='utf-8') as file:
                file.write(SOUP.decode(formatter='html'))
                print('CDOC: {!r} processed'.format(output))
            continue
        print('CDOC: !!! WARNING !!! in {!r} no data provided'.format(pagename))
    # Create folder if not exists to css
    stylepath = os_path_join(outfolder, 'css')
    try:
        os_makedirs(stylepath)
    except OSError as e:
        if not (e.errno == errno_EEXIST and os_path_isdir(stylepath)):
            raise
    # Create CSS path
    stylesheet = os_path_join(stylepath, 'cdoc.css')
    # If using the user created custom CSS
    if external_css:
        copyfile(external_css, stylesheet)
    # If using the default CSS
    else:
        with open(stylesheet, 'w', encoding='utf-8') as file:
            file.write(STYLE)
            print('CDOC: {!r} processed'.format(stylesheet))
示例#7
0
def _build(sources, outfolder, gentoc, toc, external_css):
    for pagename, source in sources.items():
        # If there is no valid source
        if source is None:
            continue
        # Clear soup and add en empty, new body
        SOUP.body.decompose()
        new(SOUP.html, 'body')

        # Set title
        SOUP.html.head.title.string = pagename
        # Get essentail values
        filename, depends = toc[pagename]
        # Constants
        SECTIONS = 7
        # Build basic structure
        column1 = new(SOUP.body, 'div', id='column1')
        column2 = new(SOUP.body, 'div', id='column2')

        generic = new(column1, 'div', id='generic')
        sidebar = new(column1, 'div', id='sidebar')
        content = new(column2, 'div', id='content')

        # OPTIONAL: custom header section
        try:
            _html_format(generic, source['HEAD'], outfolder)
            new(generic, 'br')
        except KeyError:
            SECTIONS -= 1
        # OPTIONAL: custom menu in sidebar
        try:
            _html_format(sidebar, source['MENU'], outfolder)
            new(sidebar, 'br')
        except KeyError:
            SECTIONS -= 1
        # OPTIONAL: custom abstract and introduction
        try:
            _html_format(content, source['INFO'], outfolder)
            new(content, 'br')
        except KeyError:
            SECTIONS -= 1
        # OPTIONAL: index
        if gentoc:
            sidebar_type = new(sidebar, 'div')
            new(sidebar_type, 'p', class_='label', string='Modules:')
            new(sidebar, 'br')
            _indx_format(sidebar, pagename, toc)
            new(sidebar, 'br')

        # TODO: Implement a Schema validator for better user-feedback

        # TODO: add FOOT key

        # TODO: add EXEC to cdoc to add "interactive" python snippets to code
        # EXEC: |
        #   with open('VERSION') as file:
        #       # Insert to USER:About
        #       DOC[USER][0].insert(0, {'name': 'Version', 'info': file.read()})

        # OPTIONAL: text and code
        try:
            blocks = source['TEXT']
            for block in blocks:
                # Get the first element of the list as the section name
                try:
                    section = string_capwords(block[0])
                except IndexError:
                    continue
                sidebar_text = new(sidebar, 'div')
                new(sidebar_text,
                    'p',
                    class_='label',
                    string='{}:'.format(section))
                new(sidebar_text, 'br')
                content_text = new(content, 'div')
                new(content_text, 'h2', class_='title', string=section)
                for user in block[1:]:
                    _text_format(sidebar_text, content_text, user)
                new(sidebar, 'br')
        except KeyError:
            SECTIONS -= 1
        # OPTIONAL: user defined
        try:
            userdefs = source['USER']
            for userdef in userdefs:
                # Get the first element of the list as the section name
                try:
                    section = string_capwords(userdef[0])
                except IndexError:
                    continue
                sidebar_user = new(sidebar, 'div')
                new(sidebar_user,
                    'p',
                    class_='label',
                    string='{}:'.format(section))
                new(sidebar_user, 'br')
                content_user = new(content, 'div')
                new(content_user, 'h2', class_='title', string=section)
                for user in userdef[1:]:
                    _user_format(sidebar_user, content_user, user)
                new(sidebar, 'br')
        except KeyError:
            SECTIONS -= 1
        # OPTIONAL: type definitions
        try:
            types = source['TYPE']
            sidebar_type = new(sidebar, 'div')
            new(sidebar_type, 'p', class_='label', string='Types:')
            new(sidebar_type, 'br')
            content_type = new(content, 'div')
            new(content_type, 'h2', class_='title', string='Types')
            for type in types:
                _type_format(sidebar_type, content_type, type)
            new(sidebar, 'br')
        except KeyError:
            SECTIONS -= 1
        # OPTIONAL: function definitions
        try:
            funcs = source['FUNC']
            sidebar_func = new(sidebar, 'div')
            new(sidebar_func, 'p', class_='label', string='Functions:')
            new(sidebar_func, 'br')
            content_func = new(content, 'div')
            new(content_func, 'h2', class_='title', string='Functions')
            for func in funcs:
                _func_format(sidebar_func, content_func, func)
        except KeyError:
            SECTIONS -= 1

        # Create HTML file
        if SECTIONS:
            output = os_path_join(outfolder, filename)
            with open(output, 'w', encoding='utf-8') as file:
                file.write(SOUP.decode(formatter='html'))
                print('CDOC: {!r} processed'.format(output))
            continue
        print(
            'CDOC: !!! WARNING !!! in {!r} no data provided'.format(pagename))
    # Create folder if not exists to css
    stylepath = os_path_join(outfolder, 'css')
    try:
        os_makedirs(stylepath)
    except OSError as e:
        if not (e.errno == errno_EEXIST and os_path_isdir(stylepath)):
            raise
    # Create CSS path
    stylesheet = os_path_join(stylepath, 'cdoc.css')
    # If using the user created custom CSS
    if external_css:
        copyfile(external_css, stylesheet)
    # If using the default CSS
    else:
        with open(stylesheet, 'w', encoding='utf-8') as file:
            file.write(STYLE)
            print('CDOC: {!r} processed'.format(stylesheet))