view read_revinfo.py @ 146:fb82e116a7bf

>>>>> Added tag v3.0 for changeset eb2e3ab2a6e4
author Franz Glasner <hg@dom66.de>
date Fri, 24 Aug 2018 02:10:56 +0200
parents bfb7cad85467
children
line wrap: on
line source

# -*- coding: utf-8 -*-
r"""Read the output of the revinfo extension.

The module handles the contents of a ".hg_archival.txt" file also.
"""

__version__ = "0"

__author__ = "Franz Glasner"


def read_revinfo_simple(filename):
    """Read file file `filename` and return it's contents as dict.

    If `filename` does not exist return `None`.

    This is a simple-minded implementation: squashes many path items into
    one (the last encountered).
    """
    try:
        f =  open(filename, "rt")
        try:
            return dict([[i.strip() for i in l.split(":", 1)] for l in f])
        finally:
            f.close()
    except EnvironmentError:
        return None


def read_revinfo_all(filename):
    """Read file file `filename` and return it's contents as dict.

    If `filename` does not exist return `None`.

    All the "path" items are collected into a list.
    """
    try:
        f =  open(filename, "rt")
        try:
            rv = {}
            for k, v in [[i.strip() for i in l.split(":", 1)] for l in f]:
                if k == "path":
                    try:
                        rv[k].append(v)
                    except KeyError:
                        rv[k] = [v]
                else:
                    rv[k] = v
            return rv
        finally:
            f.close()
    except EnvironmentError:
        return None


import sys
print read_revinfo_simple(sys.argv[1])
print read_revinfo_all(sys.argv[1])