Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

pyris.py 2.9KB

před 5 roky
před 5 roky
před 5 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """ Takes an input citation file and formats it as a Refman (RIS) file.
  2. """
  3. __author__ = "Noëlle Anthony"
  4. __version__ = "0.1.0"
  5. import sys, os
  6. class Citation:
  7. def __init__(self):
  8. self.type = ""
  9. self.authors = {}
  10. self.authorcount = 0
  11. self.title = ""
  12. self.date = {
  13. year: "",
  14. month: "",
  15. day: "",
  16. info: ""
  17. }
  18. def addAuthor(self, author):
  19. fmtAuthor = ",".join(list(map(lambda x: x.strip(), author.split(','))))
  20. self.authors[self.authorcount] = fmtAuthor
  21. self.authorcount += 1
  22. def getAuthors(self):
  23. astr = ""
  24. for author in self.authors.values():
  25. astr += "AU - {}\n".format(author)
  26. return astr
  27. def addType(self, ctype):
  28. self.type = ctype
  29. def getType(self):
  30. return "TY - {}\n".format(self.type)
  31. def addTitle(self, ctitle):
  32. self.title = ctitle
  33. def getTitle(self):
  34. return "TI - {}\n".format(self.title)
  35. def addDate(self, yr=None, mn=None, dy=None, info=None):
  36. self.date.year = "" if yr == None else yr
  37. self.date.month = "" if mn == None else mn
  38. self.date.day = "" if dy == None else dy
  39. self.date.info = "" if info == None else info
  40. def getDate(self):
  41. return "PY - {}/{}/{}/{}\n".format(self.date.year, self.date.month, self.date.day, self.date.info)
  42. class CiteList:
  43. def __init__(self, filename):
  44. self.citations = {}
  45. self.idx = 0
  46. chunks = filename.split(".")
  47. if len(chunks) == 1: # has no extension
  48. self.fn, self.ext = chunks[0], ""
  49. elif len(chunks) >= 3: # has multiple periods in filename, last one indicates extension
  50. self.fn, self.ext = ".".join(chunks[:-1]), chunks[-1]
  51. else: # has one period in filename
  52. self.fn, self.ext = chunks[0], chunks[1]
  53. with open(filename, 'r') as f:
  54. self.contents = f.read()
  55. self.outfile = self.fn + ".ris"
  56. def loadCitations(self):
  57. """ Create citations from self.content.
  58. Input: each citation in EndNote format begins with ^PT
  59. and ends with ^ER
  60. There's a blank line between records that can be discarded
  61. """
  62. lines = self.contents.split("\n")
  63. cite = Citation()
  64. authors = False
  65. title = False
  66. sTitle = ""
  67. for line in lines:
  68. if line == "PT J":
  69. cite.addType("JOUR")
  70. if line[:2] == "AF": # we're on authors
  71. authors = True
  72. cite.addAuthor(line[3:])
  73. elif authors and line[:2] == " ": # another author
  74. cite.addAuthor(line[3:])
  75. elif authors:
  76. authors = False
  77. if line[:2] == "TI": # we're on the title
  78. title = True
  79. cite.addTitle(line[])
  80. def toString(self):
  81. rstr = "Current infile: {}.{}".format(self.fn, self.ext)
  82. rstr += "\n"
  83. rstr += "Current outfile: {}".format(self.outfile)
  84. return rstr
  85. def print(self):
  86. rstr = "Current infile: {}.{}".format(self.fn, self.ext)
  87. rstr += "\n"
  88. rstr += "Current outfile: {}".format(self.outfile)
  89. print(rstr)
  90. def main(filename):
  91. cList = CiteList(filename)
  92. if __name__ == "__main__":
  93. args = sys.argv()
  94. if len(args) < 2:
  95. print("Input file required.")
  96. os.exit(0)
  97. main(args[1])