A semantic search engine for source code https://bitshift.benkurtovic.com/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

63 lignes
1.5 KiB

  1. import os, ast
  2. import pygments.lexers as pgl
  3. from ..languages import LANGS
  4. from .python import parse_py
  5. _all__ = ["parse"]
  6. def _lang(codelet):
  7. """
  8. Private function to identify the language of a codelet.
  9. :param codelet: The codelet object to identified.
  10. :type code: Codelet
  11. .. todo::
  12. Modify function to incorporate tags from stackoverflow.
  13. """
  14. if codelet.filename is not None:
  15. return pgl.guess_lexer_for_filename(codelet.filename).name
  16. return LANGS.index(pgl.guess_lexer(codelet.code))
  17. def parse(codelet, pid):
  18. """
  19. Sends codelet code to the Java parsing process via a named pipe. Reads the
  20. resulting symbols from the pipe and updates the codelet.
  21. :param codelet: The codelet object to parsed.
  22. :param pid: The id of the current python process.
  23. :type code: Codelet
  24. :param pid: str.
  25. .. todo::
  26. Identify languages using pygments and change the write file based on
  27. that.
  28. """
  29. codelet.language = _lang(codelet)
  30. if codelet.language == LANGS.index("Python"):
  31. parse_py(codelet)
  32. else:
  33. write_f = "../../tmp/%d_parser.proc" % codelet.language
  34. with open(write_f, 'a') as wf:
  35. wf.write('pid:' + str(pid) + '\n')
  36. wf.write('body:\n' + codelet.code)
  37. read_f = '../../tmp/%s_py.data' % str(pid)
  38. data = ''
  39. while data == '':
  40. with open(read_f) as rf:
  41. data = rf.read()
  42. os.remove(read_f)
  43. codelet.symbols = ast.literal_eval(data.split(',')[1])