Translate SVN path to local file system path in Python
I'm writing a utility in Python that will attach changed files in Subversion to an email and send it when a subset of folders that are under source control in SVN have been changed. I am using the pysvn library to access the repository.
I have a copy of the files on my local file system and I do an update to check if the files have changed since the last time the utility was run.
I am at the point where I am translating the path names in SVN to the path names on my local copy.
Currently I have written the following to do the job:
def formatPaths(self, paths):
newPaths = list()
for path in paths:
path = path[len(self.basePath):]
path = path.replace("/", "\\")
newPaths.append(path)
return newPaths
self.basePath would be something like "/trunk/project1" and I'm looking to just get the relative path of a subset of folders (I.e. folder1 under "/trunk/project1").
Is this a good way to solve this problem or is there some magical function I missed?
Asked by: Walter136 | Posted: 27-01-2022
Answer 1
Stay with the slice operator, but do not change the loop variable inside the loop. for fun, try the generator expression (or keep the listcomp).
baselen = len(self.basePath)
return (path[baselen:].replace("/", "\\") for path in paths)
Edit: `lstrip()' is not relevant here. From the manual:
Answered by: Madaline525 | Posted: 28-02-2022str.lstrip([chars])
Return a copy of the string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on.
Answer 2
Your specific solution to the path name copy is reasonable, but your general solution to the entire problem could be improved.
I would easy_install anyvc
, a library developed for the PIDA IDE which is a uniform python interface into version control systems, and use it instead:
from anyvc import Subversion
vc = Subversion('/trunk')
modified = [f.relpath for f in vc.list() if f.state != 'clean']
for f in modified:
print f.relpath # the relative path of the file to the source root
Additionally, I would probably attach a diff to an email rather than the actual file. But I guess that's your choice.
Answered by: Wilson939 | Posted: 28-02-2022Answer 3
Hm... That would do it:
baselen = len(self.basePath)
for path in paths:
path = path[baselen:].replace("/", "\\")
newPaths.append(path)
return newPaths
If you like, you can do it like this:
baselen = len(self.basePath)
return (path[baselen:].replace("/", "\\") for path in paths)
Not calculating baselen
in every loop iteration is also good practice.
Similar questions
python - help me translate Java code making use of bytes into jython code
how do I translate this code into jython?
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file + ".zip"));
byte[] buf = new byte[1024];
int len;
//Create a new Zip entry with the file's name.
ZipEntry zipEntry = new ZipEntry(file.toString());
//Create a buffered input stream out of the file
//we're trying to add into the Zip archive.
FileInputStream fin...
ajax - Call Google Translate from Python
I want to execute this script (view source) that uses Google Translate AJAX API from python, and be able to pass arguments and get the answer back. I don't care about the HTML.
I understand I need to embed a Javascript interpreter of some sort. Does this mean I need to have a browser instance and manipula...
How to translate python tuple unpacking to Matlab?
I am translating some python code to Matlab, and want to figure out what the best way to translate the python tuple unpacking to Matlab is.
For the purposes of this example, a Body is a class whose constructor takes as input two functionals.
I have the following python code:
X1 = lambda t: cos(t)
Y1 = lambda t: sin(t)
X2 = lambda t: cos(t) + 1
Y2 = lambda t: sin(t) + 1
coords ...
django - Translate a python dict into a Solr query string
I'm just getting started with Python, and I'm stuck on the syntax that I need to convert a set of request.POST parameters to Solr's query syntax.
The use case is a form defined like this:
class SearchForm(forms.Form):
text = forms.CharField()
metadata = forms.CharField()
figures = forms.CharField()
Upon submission, the form needs to generate a url-encoded string to pas...
python - translate by replacing words inside existing text
What are common approaches for translating certain words (or expressions) inside a given text, when the text must be reconstructed (with punctuations and everythin.) ?
The translation comes from a lookup table, and covers words, collocations, and emoticons like L33t, CUL8R, :-), etc.
Simple string search-and-replace is not enough since it can replace part of longer words (cat > dog ≠> caterpillar > dogerp...
What is the best way to translate this recursive python method into Java?
In another question I was provided with a great answer involving generating certain sets for the Chinese Postman Problem.
The answer provided was:
def get_pairs(s):
if not s: yield []
else:
i = min(s)
for j in s - set([i]):
for r in g...
Help me translate Python code which replaces an extension in file name to C++
I apologize if you know nothing about Python, however, the following snippet should be very readable to anyone. The only trick to watch out for - indexing a list with [-1] gives you the last element if there is one, or raises an exception.
>>> fileName = 'TheFileName.Something.xMl'
>>> fileNameList = fileName.split('.')
>>> assert(len(fileNameList) > 1) # Must have...
python - Django: translate in a different language than current language
I would like to notify the user in case he's viewing the site in a language that doesn't correspond to his first preference in the ACCEPT_LANGUAGE header.
For this reason I would like to present the message to the user in his first prefered language rather than the one he's currently viewing the web-site.
Is it possible with django (views and templates) to translate a string in a specific language indipende...
python - How do you translate this to objective C?
Closed. This question needs to be more focused. It ...
Can somenone translate this from Ruby to Python. Its a basic map function
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python