Advertisement
stuppid_bot

Generate License Comments on Python 3

Jan 17th, 2015
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.72 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from textwrap import wrap
  3.  
  4. comment_styles = [
  5.     {'name': 'Python',
  6.      'style': {
  7.         'indent': '# '
  8.         }
  9.      },
  10.     {'name': 'C-Style 1',
  11.      'style': {
  12.         'indent': '// '
  13.         }
  14.      },
  15.     {'name': 'C-Style 2',
  16.      'style': {
  17.         'before': '/**',
  18.         'indent': ' * ',
  19.         'after': ' **/'
  20.         }
  21.      },
  22.     ]
  23.  
  24. def main(fileobj, new_line='\n'):
  25.     txt = fileobj.read()
  26.     paras = txt.split('\n')
  27.  
  28.     lines = []
  29.     for item in comment_styles:
  30.         name = item.get('name')
  31.         style = item.get('style')
  32.         lines.append(name + ':' + new_line)
  33.         line_length = style['length'] if 'length' in style else 79
  34.        
  35.         if 'before' in style:
  36.             lines.append(style['before'])
  37.            
  38.         for para in paras:
  39.             lines += wrap(style['indent'] + para,
  40.                           line_length,
  41.                           subsequent_indent=style['indent'])
  42.        
  43.         if 'after' in style:
  44.             lines.append(style['after'])
  45.  
  46.         lines.append('')
  47.  
  48.     with open(fileobj.name + '.tpl', 'w', encoding=fileobj.encoding) as f:
  49.         f.write(new_line.join(lines))
  50.        
  51. if __name__ == '__main__':
  52.     main(open('MIT-LICENSE', encoding='utf-8'))
  53.  
  54. """
  55. MIT-LICENSE.tpl
  56. Python:
  57.  
  58. # Copyright (c) 2015 Sergei Snegirev <tz4678@gmail.com>
  59. #
  60. # Permission is hereby granted, free of charge, to any person obtaining a copy
  61. # of this software and associated documentation files (the "Software"), to deal
  62. # in the Software without restriction, including without limitation the rights
  63. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  64. # copies of the Software, and to permit persons to whom the Software is
  65. # furnished to do so, subject to the following conditions:
  66. #
  67. # The above copyright notice and this permission notice shall be included in
  68. # all copies or substantial portions of the Software.
  69. #
  70. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  71. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  72. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  73. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  74. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  75. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  76. # SOFTWARE.
  77.  
  78. C-Style 1:
  79.  
  80. // Copyright (c) 2015 Sergei Snegirev <tz4678@gmail.com>
  81. //
  82. // Permission is hereby granted, free of charge, to any person obtaining a copy
  83. // of this software and associated documentation files (the "Software"), to
  84. // deal in the Software without restriction, including without limitation the
  85. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  86. // sell copies of the Software, and to permit persons to whom the Software is
  87. // furnished to do so, subject to the following conditions:
  88. //
  89. // The above copyright notice and this permission notice shall be included in
  90. // all copies or substantial portions of the Software.
  91. //
  92. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  93. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  94. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  95. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  96. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  97. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  98. // IN THE SOFTWARE.
  99.  
  100. C-Style 2:
  101.  
  102. /**
  103. * Copyright (c) 2015 Sergei Snegirev <tz4678@gmail.com>
  104. *
  105. * Permission is hereby granted, free of charge, to any person obtaining a copy
  106. * of this software and associated documentation files (the "Software"), to
  107. * deal in the Software without restriction, including without limitation the
  108. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  109. * sell copies of the Software, and to permit persons to whom the Software is
  110. * furnished to do so, subject to the following conditions:
  111. *
  112. * The above copyright notice and this permission notice shall be included in
  113. * all copies or substantial portions of the Software.
  114. *
  115. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  116. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  117. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  118. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  119. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  120. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  121. * IN THE SOFTWARE.
  122. **/
  123.  
  124. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement