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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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:
  25. astr += "A1 - {}\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 "CY - {}/{}/{}/{}\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 toString(self):
  57. rstr = "Current infile: {}.{}".format(self.fn, self.ext)
  58. rstr += "\n"
  59. rstr += "Current outfile: {}".format(self.outfile)
  60. return rstr
  61. def print(self):
  62. rstr = "Current infile: {}.{}".format(self.fn, self.ext)
  63. rstr += "\n"
  64. rstr += "Current outfile: {}".format(self.outfile)
  65. print(rstr)
  66. def main(filename):
  67. cList = CiteList(filename)
  68. if __name__ == "__main__":
  69. args = sys.argv()
  70. if len(args) < 2:
  71. print("Input file required.")
  72. os.exit(0)
  73. main(args[1])