/cite.py

https://gitlab.com/dylan-ashley/minerva-python
Python | 95 lines | 53 code | 10 blank | 32 comment | 22 complexity | f4a6c3143991130a81a6f12e0cf7452d MD5 | raw file
  1. #!/usr/bin/env python3 -O
  2. """
  3. This module provides an easy way to aggregate citations.
  4. A citation is considered to be a single line comment of two or three values
  5. seperated by semicolons. If only two values appear it is interpreted as a URL
  6. followed by a date. If three values appear it is interpreted as a name
  7. followed by a URL and then a date.
  8. Execute this program in the folder containing the files to search.
  9. """
  10. # The MIT License (MIT)
  11. #
  12. # Copyright (c) 2016 Dylan Robert Ashley
  13. #
  14. # Permission is hereby granted, free of charge, to any person obtaining a copy
  15. # of this software and associated documentation files (the "Software"), to deal
  16. # in the Software without restriction, including without limitation the rights
  17. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. # copies of the Software, and to permit persons to whom the Software is
  19. # furnished to do so, subject to the following conditions:
  20. #
  21. # The above copyright notice and this permission notice shall be included in
  22. # all copies or substantial portions of the Software.
  23. #
  24. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30. # THE SOFTWARE.
  31. import glob
  32. import os
  33. def find_lines_from_file(file):
  34. lines = list()
  35. try:
  36. with open(file, "r") as infile:
  37. data = [line.strip() for line in infile]
  38. for line in data:
  39. if len(line) == 0:
  40. continue
  41. elif line[0] == "#":
  42. line = line[1:]
  43. elif line[:2] == "//":
  44. line = line[2:]
  45. elif (line[:4] == "<!--") and (line[-3:] == "-->"):
  46. line = line[4:-3]
  47. else:
  48. continue
  49. if ((1 <= line.count(";") <= 2) and
  50. (len(line[line.rfind(";") + 1:].strip()) == 10)):
  51. lines.append(line)
  52. except UnicodeDecodeError:
  53. pass
  54. return lines
  55. def find_lines_from_path(path):
  56. lines = list()
  57. for item in glob.glob("{}/*".format(path)):
  58. if os.path.isdir(item):
  59. for line in find_lines_from_path(item):
  60. lines.append(line)
  61. elif os.path.isfile(item):
  62. for line in find_lines_from_file(item):
  63. lines.append(line)
  64. return lines
  65. def main():
  66. lines = list()
  67. print("<table>")
  68. print("\t<tr>")
  69. for item in ["Name", "URL", "Date Retrieved"]:
  70. print("\t\t<td nowrap=\"nowrap\">{}</td>".format(item))
  71. print("\t</tr>")
  72. for line in sorted(list(set(find_lines_from_path(
  73. "{}".format(os.getcwd()))))):
  74. line = line.strip().split(";")
  75. if len(line) == 2:
  76. line = ["N/A", line[0], line[1]]
  77. print("\t<tr>")
  78. for item in line:
  79. print("\t\t<td nowrap=\"nowrap\">{}</td>".format(item.strip()))
  80. print("\t</tr>")
  81. print("</table>")
  82. if __name__ == "__main__":
  83. main()