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.

pyris.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.strip() == "": # it's a blank line and can be discarded
  69. continue
  70. if line == "PT J":
  71. cite.addType("JOUR")
  72. if line[:2] == "AF": # we're on authors
  73. authors = True
  74. cite.addAuthor(line[3:])
  75. elif authors and line[:2] == " ": # another author
  76. cite.addAuthor(line[3:])
  77. elif authors:
  78. authors = False
  79. if line[:2] == "TI": # we're on the title
  80. title = True
  81. cite.addTitle(line[])
  82. def toString(self):
  83. rstr = "Current infile: {}.{}".format(self.fn, self.ext)
  84. rstr += "\n"
  85. rstr += "Current outfile: {}".format(self.outfile)
  86. return rstr
  87. def print(self):
  88. rstr = "Current infile: {}.{}".format(self.fn, self.ext)
  89. rstr += "\n"
  90. rstr += "Current outfile: {}".format(self.outfile)
  91. print(rstr)
  92. def main(filename):
  93. cList = CiteList(filename)
  94. if __name__ == "__main__":
  95. args = sys.argv()
  96. if len(args) < 2:
  97. print("Input file required.")
  98. os.exit(0)
  99. main(args[1])