A semantic search engine for source code https://bitshift.benkurtovic.com/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

85 lines
3.3 KiB

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