Skip to content

Instantly share code, notes, and snippets.

@LukeGary462
Last active November 20, 2023 16:10
Show Gist options
  • Select an option

  • Save LukeGary462/3c9b2e9eca9416686d8bfda3ae21b886 to your computer and use it in GitHub Desktop.

Select an option

Save LukeGary462/3c9b2e9eca9416686d8bfda3ae21b886 to your computer and use it in GitHub Desktop.
Comment Block
#!/usr/bin/env python
# python 3
## @file: GenerateDoxyFileBlock.py
# @name: Luke Gary
# @company: <company>
# @date: 2020/2/20
####################################################################################################
# @copyright
# Copyright <year> <company> as an unpublished work.
# All Rights Reserved.
#
# @license The information contained herein is confidential
# property of <company>. The user, copying, transfer or
# disclosure of such information is prohibited except
# by express written agreement with <company>.
####################################################################################################
"""
doc block template
"""
import datetime
import os
import time
from os.path import basename
#pylint: disable=import-error
import sublime_plugin
class GenerateCommentBlock(sublime_plugin.TextCommand):
"""text command autogenerate doxygen style file header block
adds shebang to supported scripting languages
Extends:
sublime_plugin.TextCommand
"""
def run(self, edit):
"""
run command
"""
pos = 0
# Get File Name #
file_name = self.view.file_name()
fname = basename(file_name)
ftype = os.path.splitext(file_name)[1][1:].strip().lower()
f_last_modify_time = time.ctime(os.path.getmtime(file_name))
f_create_time = time.ctime(os.path.getctime(file_name))
print("file_name{}", file_name)
print("fname {}", fname)
print("ftype {}", ftype)
print("f_last_modify_time {}", f_last_modify_time)
print("f_create_time {}", f_create_time)
# Get Date #
date = datetime.datetime.now()
dateyr = str(date.year)
datemo = str(date.month)
dateda = str(date.day)
dateymd = dateyr + "/" + datemo + "/" + dateda
doc = ''
# python style
if ftype in ['py']:
doc = self.make_python(
fname=fname,
author='Luke Gary',
company='<company>',
date=dateymd
)
elif ftype in ['h', 'hpp']:
doc = self.make_c_header_style(
fname=fname,
author='Luke Gary',
company='<company>',
date=dateymd,
needs_guard=True,
)
elif ftype in ['c', 'ino', 'cpp']:
doc = self.make_c_style(
fname=fname,
author='Luke Gary',
company='<company>',
date=dateymd,
needs_guard=True,
)
self.view.insert(edit, pos, doc)
@staticmethod
def insert_license(com_ch: str, company: str, line_width: int = 80) -> str:
"""
make license block
:param com_ch: The comment character
:type com_ch: str
:param company: The company
:type company: str
:param line_width: The line width
:type line_width: int
:returns: formatted string
:rtype: str
"""
date = datetime.datetime.now()
dateyr = str(date.year)
output = '{x}'.format(x=com_ch)
for _ in range(line_width-1):
output += '{x}'.format(x=com_ch)
output += '\n'
output += '{com_ch} @copyright\n'.format(com_ch=com_ch)
output += '{com_ch} Copyright {year} {company} as an unpublished work.\n'.format(
com_ch=com_ch, company=company, year=dateyr
)
output += '{com_ch} All Rights Reserved.\n'.format(
com_ch=com_ch
)
output += '{com_ch}\n'.format(com_ch=com_ch)
output += '{com_ch} @license The information contained herein is confidential\n'.format(
com_ch=com_ch
)
output += '{com_ch} property of {company}. The user, copying, transfer or\n'.format(
com_ch=com_ch, company=company
)
output += '{com_ch} disclosure of such information is prohibited except\n'.format(
com_ch=com_ch
)
output += '{com_ch} by express written agreement with {company}.\n'.format(
com_ch=com_ch, company=company
)
for _ in range(line_width):
output += str(com_ch)
output += '\n'
return output
def make_c_style(self, **kwargs) -> str:
"""
Makes a c style comment block.
:param kwargs: The keywords arguments
:type kwargs: dictionary
"""
output = '/**\n'
output += '* @file: {x}\n'.format(x=kwargs.get("fname"))
output += '* @name: {x}\n'.format(x=kwargs.get("author"))
output += '* @company: {x}\n'.format(x=kwargs.get("company"))
output += '* @date: {x}\n'.format(x=kwargs.get("date"))
output += self.insert_license(
com_ch='*',
company=kwargs.get('company')
)
output += '* @brief: \n'
output += '* \n'
output += '*/\n'
if kwargs.get('needs_guard'):
output += '#ifdef __cplusplus\n'
output += 'extern "C" {\n'
output += '#endif\n\n'
sections = [
'Includes', 'Defines', 'Local Types and Typedefs', 'Static Function Prototypes',
'Global Variables', 'Static Variables', 'Functions'
]
seperator = ''.join(['*' for _ in range(79)])
for section in sections:
output += '/{x}\n'.format(x=seperator)
output += '* {x}\n'.format(x=section)
output += '{x}/\n'.format(x=seperator)
if section == 'Includes':
output += '#include "{x}"'.format(
x=basename(self.view.file_name()).replace('.c', '.h')
)
output += '\n\n'
if kwargs.get('needs_guard'):
output += '#ifdef __cplusplus\n'
output += '} // extern "C"\n#endif\n'
return output
def make_c_header_style(self, **kwargs) -> str:
"""
Makes c header defs.
:param kwargs: The keywords arguments
:type kwargs: dictionary
:returns: formatted string
:rtype: str
"""
def_name = basename(self.view.file_name()).upper().replace('.', '_')
def_name = '_'+def_name+'_'
output = '/**\n'
output += '* @file: {x}\n'.format(x=kwargs.get("fname"))
output += '* @name: {x}\n'.format(x=kwargs.get("author"))
output += '* @company: {x}\n'.format(x=kwargs.get("company"))
output += '* @date: {x}\n'.format(x=kwargs.get("date"))
output += self.insert_license(
com_ch='*',
company=kwargs.get('company')
)
output += '* @brief: \n'
output += '* \n'
output += '*/\n'
output += '#pragma once\n'
output += '#ifndef {x}\n'.format(x=def_name.replace('-', '_'))
output += '#define {x}\n'.format(x=def_name.replace('-', '_'))
if kwargs.get('needs_guard'):
output += '#ifdef __cplusplus\n'
output += 'extern "C" {\n'
output += '#endif\n\n'
sections = [
'Includes', 'Defines', 'Local Types and Typedefs', 'Prototypes'
]
seperator = ''.join(['*' for _ in range(79)])
for section in sections:
output += '/{x}\n'.format(x=seperator)
output += '* {x}\n'.format(x=section)
output += '{x}/\n'.format(x=seperator)
output += '\n\n'
if kwargs.get('needs_guard'):
output += '#ifdef __cplusplus\n'
output += '} // extern "C"\n#endif\n'
output += '#endif // {x}\n'.format(x=def_name.replace('-', '_'))
return output
def make_python(self, **kwargs) -> str:
"""
Makes a python comment block.
"""
output = '#!/usr/bin/env python\n'
output += '# python 3\n'
output += '#pylint: disable=\n'
output += '## @file: {x}\n'.format(x=kwargs.get("fname"))
output += '# @name: {x}\n'.format(x=kwargs.get("author"))
output += '# @company: {x}\n'.format(x=kwargs.get("company"))
output += '# @date: {x}\n'.format(x=kwargs.get("date"))
output += self.insert_license(
com_ch='#',
company=kwargs.get('company')
)
# add python doc-string
output += '\n\"\"\"\n{ high-level module description }\n\"\"\"\n'
return output
def make_bash(self, **kwargs):
"""
Makes a bash comment block.
"""
output = '#!/usr/bin/bash\n'
output += '## @file: {x}\n'.format(x=kwargs.get("fname"))
output += '# @name: {x}\n'.format(x=kwargs.get("author"))
output += '# @company: {x}\n'.format(x=kwargs.get("company"))
output += '# @date: {x}\n'.format(x=kwargs.get("date"))
output += self.insert_license(
com_ch='#',
company=kwargs.get('company')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment