changeset 200:23ca347ab6a1

Style: use the Mercurial default name `fp' for file objects
author Franz Glasner <hg@dom66.de>
date Sat, 06 Oct 2018 16:08:09 +0200
parents 29cc67db9aa5
children 8e0a12929be9
files extensions/timestamps.py
diffstat 1 files changed, 39 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/extensions/timestamps.py	Sat Oct 06 16:01:49 2018 +0200
+++ b/extensions/timestamps.py	Sat Oct 06 16:08:09 2018 +0200
@@ -171,8 +171,8 @@
             if matcher(fn):
                 st = os.lstat(repo.wjoin(fn))
                 ts[fn] = to_isoformat(st.st_mtime)
-        with io.open(repo.wjoin(TIMESTAMPS_DATABASE), "wb") as f:
-            ts.write(f)
+        with io.open(repo.wjoin(TIMESTAMPS_DATABASE), "wb") as fp:
+            ts.write(fp)
 
 
 def restore_timestamps(ui, repo, ctx,
@@ -236,8 +236,8 @@
     configdata = configname = None
     if tsconfig:
         try:
-            with io.open(tsconfig, "rb") as f:
-                configdata = f.read()
+            with io.open(tsconfig, "rb") as fp:
+                configdata = fp.read()
         except IOError:
             pass
         else:
@@ -304,16 +304,16 @@
         return fn
 
 
-def db_reader(db):
-    """A generator that parses the file `db` yielding records consisting of fields
+def db_reader(fp):
+    """A generator that parses the file `fp` yielding records consisting of fields
 
-    :param db: the binary file to be read
+    :param fp: the binary file object to be read
 
     :return: a record which is a list of fields
 
     """
-    db = io.BufferedReader(db)
-    c = db.read(1)
+    fp = io.BufferedReader(fp)
+    c = fp.read(1)
     record = []
     field = []
     while c:
@@ -331,9 +331,9 @@
                 # handle CR-LF ('\r\n') combination: skip a '\n' that
                 # follows directly
                 #
-                c3 = db.peek(1)
+                c3 = fp.peek(1)
                 if c3 and c3[0] == '\n':
-                    db.read(1)
+                    fp.read(1)
         elif c == ',':
             # field separator
             record.append(''.join(field))
@@ -344,16 +344,16 @@
             # text field with special chars:
             # non-terminal '@' have been duplicated
             #
-            c2 = db.read(1)
+            c2 = fp.read(1)
             while c2:
                 if c2 == '@':
-                    c3 = db.peek(1)
+                    c3 = fp.peek(1)
                     if c3 and c3[0] == '@':
                         # a quoted (i.e. duplicated) `@'
                         sf.append('@')
                         # and consume it
-                        db.read(1)
-                        c2 = db.read(1)
+                        fp.read(1)
+                        c2 = fp.read(1)
                     else:
                         # end of quoted string
                         c2 = ''
@@ -363,13 +363,13 @@
                         #
                 else:
                     sf.append(c2)
-                    c2 = db.read(1)
+                    c2 = fp.read(1)
             field = sf
         elif not field and c == '#':
             # comment: `#' at the beginning of a line
             comment = ['#']
             while True:
-                c3 = db.peek(1)
+                c3 = fp.peek(1)
                 if c3 and c3[0] in '\r\n':
                     # end of comment
                     break
@@ -378,7 +378,7 @@
                     # just stay at the eol char
                     #
                 else:
-                    c2 = db.read(1)
+                    c2 = fp.read(1)
                     comment.append(c2)
             field = comment
         elif c in " \t":
@@ -386,7 +386,7 @@
                 field.append(c)
         else:
             field.append(c)
-        c = db.read(1)
+        c = fp.read(1)
     #
     # EOF
     # Check whether the last line contained data but no CR or LF
@@ -398,10 +398,10 @@
         yield record
 
 
-def db_writer(db):
-    """A coroutine to write records into the database `db`.
+def db_writer(fp):
+    """A coroutine to write records into the database `fp`.
 
-    :param db: the binary file to be written into
+    :param fp: the binary file object to write into
 
     """
     try:
@@ -411,18 +411,18 @@
             if len(fields) >= 2:
                 for field in fields:
                     if print_sep:
-                        db.write(',')
+                        fp.write(',')
                     else:
                         print_sep = True
-                    db.write(escape_str_field(field))
-                db.write('\n')
+                    fp.write(escape_str_field(field))
+                fp.write('\n')
             else:
                 # no escaping for comments and other special lines
                 for field in fields:
-                    db.write(field)
-                db.write('\n')
+                    fp.write(field)
+                fp.write('\n')
     finally:
-        db.flush()
+        fp.flush()
 
 
 def to_isoformat(t):
@@ -502,16 +502,16 @@
 
     @classmethod
     def from_filename(cls_, name=TIMESTAMPS_DATABASE):
-        with io.open(name, "rb") as f:
+        with io.open(name, "rb") as fp:
             ts = cls_()
-            ts.read(f)
+            ts.read(fp)
             return ts
 
     @classmethod
     def from_ctx(cls_, ctx, name=TIMESTAMPS_DATABASE):
-        with io.BytesIO(ctx[name].data()) as f:
+        with io.BytesIO(ctx[name].data()) as fp:
             ts = cls_()
-            ts.read(f)
+            ts.read(fp)
             return ts
 
     @property
@@ -530,15 +530,15 @@
     def encoding(self, v):
         self._encoding = v
 
-    def read(self, db):
-        """Initialize from external timestamps database `db`
+    def read(self, fp):
+        """Initialize from external timestamps database `fp`
 
-        :param db: an opened binary file ready for reading
+        :param fp: an opened binary file ready for reading
 
         """
         self._init()
         lineno = datano = 0
-        for record in db_reader(db):
+        for record in db_reader(fp):
             lineno += 1
             if len(record) == 2:
                 k, v = record
@@ -580,13 +580,13 @@
             else:
                 raise ValueError("unknown record type in line %d" % lineno)
 
-    def write(self, db):
-        """Write the internal representation into the file `db`"""
+    def write(self, fp):
+        """Write the internal representation into the file `fp`"""
 
         assert self._d is not None
         assert self._version == 1
 
-        dbwriter = db_writer(db)
+        dbwriter = db_writer(fp)
         dbwriter.send(None)  # prime the coroutine
 
         #