A semantic search engine for source code https://bitshift.benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from operator import concat
  2. from pygments import highlight
  3. from pygments.lexers import find_lexer_class, 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, highlight_code=False):
  68. """
  69. Convert the codelet into a dictionary that can be sent as JSON.
  70. :param highlight_code: Whether to return code as pygments-highlighted
  71. HTML or as plain source.
  72. :type highlight_code: bool
  73. :return: The codelet as a dictionary.
  74. :rtype: str
  75. """
  76. lang = LANGS[self.language]
  77. code = self.code
  78. if highlight_code:
  79. lexer = find_lexer_class(lang)() or get_lexer_by_name("text")
  80. symbols = reduce(concat, self.symbols.values(), [])
  81. lines = reduce(concat, [[loc[0] for loc in sym[1] + sym[2]]
  82. for sym in symbols], [])
  83. formatter = HtmlFormatter(linenos=True, hl_lines=lines)
  84. code = highlight(code, lexer, formatter)
  85. return {
  86. "name": self.name, "code": code, "lang": lang,
  87. "authors": self.authors, "url": self.url,
  88. "created": self.date_created.isoformat(),
  89. "modified": self.date_modified.isoformat(), "origin": self.origin
  90. }