Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. from ruamel.yaml import YAML
  2. from schema import Schema, And, Or
  3. import os
  4. import re
  5. import fire
  6.  
  7. CONTACTS = "contacts.yml"
  8. PEOPLE = "people.yml"
  9.  
  10.  
  11. # helper functions
  12. def load_yaml(yamlfile):
  13. """load yaml file"""
  14. yamlloader = YAML()
  15. with open(yamlfile, "r") as fileobj:
  16. db = yamlloader.load(fileobj)
  17. return db
  18.  
  19.  
  20. def is_there(ymlfile):
  21. """Check if a yaml file is in the current working directory."""
  22. curr_dir = os.getcwd()
  23. for item in os.listdir(curr_dir):
  24. if os.path.isfile(item) and item == ymlfile:
  25. return ymlfile
  26. else:
  27. print(f"There is no {ymlfile} in directory.")
  28. return None
  29.  
  30.  
  31. # class and functions for checking
  32. class PesronValidator:
  33. """A class to validate person."""
  34. def __init__(self, contactsfile=CONTACTS, peoplefile=PEOPLE):
  35. self.contactsfile = contactsfile
  36. self.peoplefile = peoplefile
  37. self.contacts = load_yaml(contactsfile).keys() if is_there(contactsfile) else None
  38. self.people = load_yaml(peoplefile).keys() if is_there(peoplefile) else None
  39.  
  40. def validate(self, person):
  41. in_contacts = person in self.contacts if self.contacts else False
  42. in_people = person in self.people if self.people else False
  43.  
  44. if in_contacts or in_people:
  45. return True
  46. else:
  47. print(f"There is no {person} in known contacts or people.")
  48. return False
  49.  
  50.  
  51. def is_proposal_id(some_id: str):
  52. """Check if proposal id is valid."""
  53. if re.match(r"\d\d\d\d\d\d", some_id):
  54. return True
  55. else:
  56. print(f"The proposal id should be 6 digits but this is {id}.")
  57. return False
  58.  
  59.  
  60. # Options for properties
  61. MONTHS = ["Jan", "Feb", "Mar", "Apr", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
  62. STATUS = ["submitted", "in progress", "closed"]
  63. TITLE_WORD_LIMIT = 500
  64. VALID_YEAR_RANGE = [1999, 2100]
  65.  
  66.  
  67. # schema for properties
  68. PROPERTIES = {"id": And(str, is_proposal_id),
  69. "authors": [And(str, PesronValidator().validate)],
  70. "day": And(int, lambda x: 0 < x < 32),
  71. "full": {"narrative": str},
  72. "instrument": Or(str, None),
  73. "month": And(Or(str, int), lambda x: x in MONTHS),
  74. "notes": [str],
  75. "project": str,
  76. "status": And(str, lambda x: x in STATUS),
  77. "title": str,
  78. "title_short": And(str, lambda x: len(x) < TITLE_WORD_LIMIT),
  79. "year": And(int, lambda x: VALID_YEAR_RANGE[0] < x < VALID_YEAR_RANGE[1])}
  80.  
  81.  
  82. # the schema template
  83. SCHEMA = {str: PROPERTIES}
  84.  
  85.  
  86. # main function
  87. def validate(yamlfile: str, schema=None):
  88. """Validate yamlfile using a schema. The schema is a python object acceptable for module schema."""
  89. db = load_yaml(yamlfile)
  90.  
  91. if schema:
  92. pass
  93. else:
  94. schema = SCHEMA
  95.  
  96. schemaobj = Schema(schema, ignore_extra_keys=True)
  97. schemaobj.validate(db)
  98.  
  99. print(f"{yamlfile} is validated.")
  100. return
  101.  
  102.  
  103. def main():
  104. """Make a CLI using fire"""
  105. fire.Fire(validate)
  106. return
  107.  
  108.  
  109. if __name__ == "__main__":
  110. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement