Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 60 additions & 19 deletions rcsfile.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!python
# RCSFile class and supporting functions / parsers
# Copyright (C) 2011 Ryan Kavanagh <[email protected]>
#
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
Expand Down Expand Up @@ -129,25 +129,25 @@ class RCSFile:

"""

def __init__(self, lines=None, file=None):
def __init__(self, lines=None, rcsfile=None):
""" Initialise the object with the lines of an RCS file. """
if lines and not file:
if lines and not rcsfile:
parsed = RCStext.parseString(''.join(lines))
self._filename = '?'
elif file and not lines:
parsed = RCStext.parseFile(file)
if type(file) is str:
self._filename = file
elif type(file) is file:
self._filename = file
self._filename = ('?', None)
elif rcsfile and not lines:
parsed = RCStext.parseFile(rcsfile)
if isinstance(rcsfile, basestring):
self._filename = (rcsfile, None)
elif isinstance(rcsfile, file):
self._filename = (rcsfile.filename, rcsfile)
else:
self._filename = '?'
self._filename = ('?', None)
else:
raise ValueError('Either lines or file must be provided')
# self._lines will contain all lines, newline characters removed
self._lines = []
# self._parsed will contain the full parsed result
self._parsed = parsed
# self._admin will contain the admin block's values
self._admin = parsed.admin
self._admin = self._parsed.admin
# self._symbols will contain tags / symbols
self._symbols = dict(map(lambda s: s.split(':'),
self._admin.symbols))
Expand All @@ -158,16 +158,57 @@ def __init__(self, lines=None, file=None):
self._deltatexts = dict((deltatext.deltanum, deltatext)
for deltatext in parsed.deltatexts)

@property
def deltas(self):
"""
A read-only property exposing the deltas.
Note: this is not strictly read-only. Only the underlying class variable
cannot be set.
"""
return self._deltas

@property
def deltatexts(self):
"""
A read-only property exposing the deltatexts.
Note: this is not strictly read-only. Only the underlying class variable
cannot be set.
"""
return self._deltatexts

@property
def filename(self):
""" A read-only property exposing the file name in string form. """
return self._filename[0]

@property
def parsed(self):
"""
A read-only property exposing the whole parsed results.
Note: this is not strictly read-only. Only the underlying class variable
cannot be set.
"""
return self._parsed

@property
def symbols(self):
"""
A read-only property exposing the symbols (all of them!).
Note: this is not strictly read-only. Only the underlying class variable
cannot be set.
"""
return self._symbols

def get_deltanums(self):
""" Returns a list of all delta numbers. """
return [d.deltanum for d in self._deltas]
return [d for d in self._deltas.keys()]

def get_delta(self, deltanum):
""" Return metadata about delta delta. """
try:
return self._deltas[deltanum]
except KeyError:
raise ValueError('Delta %s does not exist.' % delta)
raise ValueError('Delta %s does not exist.' % deltanum)

def get_deltatext(self, deltanum):
""" Return the deltatext for delta deltanum. """
Expand Down Expand Up @@ -205,7 +246,7 @@ def get_ancestor_tuples(self, deltanum):
ancestors = []
for i, next in enumerate(nexts):
if next[0] == deltanum:
# Probably more effecient that using 'nexts.remove'
# Probably more effecient than using 'nexts.remove'
# since we don't have to search the whole list
ancestors.append(nexts.pop(i))
break
Expand Down Expand Up @@ -465,7 +506,7 @@ def grep(self, pattern, format="rlL", wraplines=False):
elif attr == 't':
formatted_match.append(self.get_tags(match[0]))
elif attr == 'f':
formatted_match.append(self._filename)
formatted_match.append(self._filename[0])
elif attr == 'm':
formatted_match.append(self.get_message(match[0]))
else:
Expand Down