A semantic search engine for source code https://bitshift.benkurtovic.com/
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

96 строки
3.7 KiB

  1. from operator import concat
  2. from pygments import highlight
  3. from pygments.lexers import get_lexer_by_name
  4. from pygments.formatters.html import HtmlFormatter
  5. from .languages import LANGS
  6. __all__ = ["Codelet"]
  7. class Codelet(object):
  8. """
  9. A source-code object with code metadata and composition analysis.
  10. :ivar name: (str) A suitable name for the codelet.
  11. :ivar code: (str) A containing the raw source code.
  12. :ivar filename: (str, or None) The filename of the snippet.
  13. :ivar language: (int, or None) The inferred language of `code`.
  14. :ivar authors: (array of tuples (str, str or None)) An array of tuples
  15. containing an author's name and profile URL (on the service the code
  16. was pulled from).
  17. :ivar url: (str) The url of the (page containing the) source code.
  18. :ivar date_created: (:class:`datetime.datetime`, or None) The date the code
  19. was published.
  20. :ivar date_modified: (:class:`datetime.datetime`, or None) The date the
  21. code was last modified.
  22. :ivar rank: (float) A quanitification of the source code's quality, as
  23. per available ratings (stars, forks, upvotes, etc.).
  24. :ivar symbols: (dict) Dictionary containing dictionaries of functions,
  25. classes, variable definitions, etc.
  26. :ivar origin: (tuple) 2-tuple of (site_name, site_url), as added by the
  27. database.
  28. """
  29. def __init__(self, name, code, filename, language, authors, url,
  30. date_created, date_modified, rank, symbols=None, origin=None):
  31. """
  32. Create a Codelet instance.
  33. :param name: see :attr:`self.name`
  34. :param code: see :attr:`self.code`
  35. :param filename: see :attr:`self.filename`
  36. :param language: see :attr:`self.language`
  37. :param authors: see :attr:`self.authors`
  38. :param url: see :attr:`self.url`
  39. :param date_created: see :attr:`self.date_created`
  40. :param date_modified: see :attr:`self.date_modified`
  41. :param rank: see :attr:`self.rank`
  42. :param symbols: see :attr:`self.symbols`
  43. :param origin: see :attr:`self.origin`
  44. :type name: see :attr:`self.name`
  45. :type code: see :attr:`self.code`
  46. :type filename: see :attr:`self.filename`
  47. :type language: see :attr:`self.language`
  48. :type authors: see :attr:`self.authors`
  49. :type url: see :attr:`self.url`
  50. :type date_created: see :attr:`self.date_created`
  51. :type date_modified: see :attr:`self.date_modified`
  52. :type rank: see :attr:`self.rank`
  53. :type symbols: see :attr:`self.symbols`
  54. :type origin: see :attr:`self.origin`
  55. """
  56. self.name = name
  57. self.code = code
  58. self.filename = filename
  59. self.language = language
  60. self.authors = authors
  61. self.url = url
  62. self.date_created = date_created
  63. self.date_modified = date_modified
  64. self.rank = rank
  65. self.symbols = symbols or {}
  66. self.origin = origin or (None, None)
  67. def serialize(self):
  68. """
  69. Convert the codelet into a dictionary that can be sent as JSON.
  70. :return: The codelet as a dictionary.
  71. :rtype: str
  72. """
  73. lang = LANGS[self.language]
  74. lines = reduce(concat, [[loc[0] for loc in sym[1] + sym[2]] for sym in
  75. reduce(concat, self.symbols.values(), [])], [])
  76. formatter = HtmlFormatter(linenos=True, hl_lines=lines)
  77. code = highlight(self.code, get_lexer_by_name(lang.lower()), formatter)
  78. return {
  79. "name": self.name, "code": code, "lang": lang,
  80. "authors": self.authors, "url": self.url,
  81. "created": self.date_created.isoformat(),
  82. "modified": self.date_modified.isoformat(), "origin": self.origin
  83. }