mercurial/scmutil.py
changeset 31553 56acc4250900
parent 31419 1fc3d1f02865
child 31679 0f8ba0bc1154
equal deleted inserted replaced
31552:d0b9e9803caf 31553:56acc4250900
   963 def gddeltaconfig(ui):
   963 def gddeltaconfig(ui):
   964     """helper function to know if incoming delta should be optimised
   964     """helper function to know if incoming delta should be optimised
   965     """
   965     """
   966     # experimental config: format.generaldelta
   966     # experimental config: format.generaldelta
   967     return ui.configbool('format', 'generaldelta', False)
   967     return ui.configbool('format', 'generaldelta', False)
       
   968 
       
   969 class simplekeyvaluefile(object):
       
   970     """A simple file with key=value lines
       
   971 
       
   972     Keys must be alphanumerics and start with a letter, values must not
       
   973     contain '\n' characters"""
       
   974 
       
   975     def __init__(self, vfs, path, keys=None):
       
   976         self.vfs = vfs
       
   977         self.path = path
       
   978 
       
   979     def read(self):
       
   980         lines = self.vfs.readlines(self.path)
       
   981         try:
       
   982             d = dict(line[:-1].split('=', 1) for line in lines if line)
       
   983         except ValueError as e:
       
   984             raise error.CorruptedState(str(e))
       
   985         return d
       
   986 
       
   987     def write(self, data):
       
   988         """Write key=>value mapping to a file
       
   989         data is a dict. Keys must be alphanumerical and start with a letter.
       
   990         Values must not contain newline characters."""
       
   991         lines = []
       
   992         for k, v in data.items():
       
   993             if not k[0].isalpha():
       
   994                 e = "keys must start with a letter in a key-value file"
       
   995                 raise error.ProgrammingError(e)
       
   996             if not k.isalnum():
       
   997                 e = "invalid key name in a simple key-value file"
       
   998                 raise error.ProgrammingError(e)
       
   999             if '\n' in v:
       
  1000                 e = "invalid value in a simple key-value file"
       
  1001                 raise error.ProgrammingError(e)
       
  1002             lines.append("%s=%s\n" % (k, v))
       
  1003         with self.vfs(self.path, mode='wb', atomictemp=True) as fp:
       
  1004             fp.write(''.join(lines))
       
  1005