示例#1
0
文件: base.py 项目: nbashev/noc
 def get_current_state(self) -> TextIOWrapper:
     """
     Returns file object of current state or None
     """
     self.load_mappings()
     if not os.path.isdir(self.archive_dir):
         self.logger.info("Creating archive directory: %s",
                          self.archive_dir)
         try:
             os.mkdir(self.archive_dir)
         except OSError as e:
             self.logger.error("Failed to create directory: %s (%s)",
                               self.archive_dir, e)
             # @todo: Die
     if os.path.isdir(self.archive_dir):
         fn = list(
             sorted(f for f in os.listdir(self.archive_dir)
                    if self.rx_archive.match(f)))
     else:
         fn = []
     if not fn:
         return StringIO("")
     path = os.path.join(self.archive_dir, fn[-1])
     logger.info("Current state from %s", path)
     return compressor(path, "r").open()
示例#2
0
文件: base.py 项目: nbashev/noc
 def save_state(self):
     """
     Save current state
     """
     if not self.new_state_path:
         return
     self.logger.info("Summary: %d new, %d changed, %d removed", self.c_add,
                      self.c_change, self.c_delete)
     self.logger.info("Error delete by referred: %s",
                      "\n".join(b.json() for _, b in self.referred_errors))
     t = time.localtime()
     archive_path = os.path.join(
         self.archive_dir,
         compressor.get_path("import-%04d-%02d-%02d-%02d-%02d-%02d.jsonl" %
                             tuple(t[:6])),
     )
     self.logger.info("Moving %s to %s", self.new_state_path, archive_path)
     if self.new_state_path.endswith(compressor.ext):
         # Simply move the file
         shutil.move(self.new_state_path, archive_path)
     else:
         # Compress the file
         self.logger.info("Compressing")
         with open(self.new_state_path,
                   "r") as s, compressor(archive_path, "w") as d:
             d.write(s.read())
         os.unlink(self.new_state_path)
     self.logger.info("Saving mappings to %s", self.mappings_path)
     mdata = "\n".join("%s,%s" % (k, self.mappings[k])
                       for k in sorted(self.mappings))
     safe_rewrite(self.mappings_path, mdata)
示例#3
0
文件: base.py 项目: nbashev/noc
 def get_new_state(self) -> Optional[TextIOWrapper]:
     """
     Returns file object of new state, or None when not present
     """
     # Try import.csv
     path = compressor.get_path(
         os.path.join(self.import_dir, "import.jsonl"))
     if not os.path.isfile(path):
         return None
     logger.info("Loading from %s", path)
     self.new_state_path = path
     return compressor(path, "r").open()
示例#4
0
文件: base.py 项目: nbashev/noc
 def get_new_state(self) -> io.TextIOWrapper:
     self.ensure_import_dir()
     path = compressor.get_path(os.path.join(self.import_dir, "import.jsonl"))
     self.logger.info("Writing to %s", path)
     return compressor(path, "w").open()
示例#5
0
文件: base.py 项目: nbashev/noc
 def get_problem_file(self) -> io.TextIOWrapper:
     self.ensure_import_dir()
     path = compressor.get_path(os.path.join(self.import_dir, "import.csv.rej"))
     self.logger.info("Writing to %s", path)
     return compressor(path, "w").open()