how to delete an entire directory with Pyside2? - pyqt5

I have a QFileSystemModel and can delete empty directory with QFileSystemModel.rmdir(index).
Is there a way to delete non-empty directory with Pyside2?
I can shutil.rmtree() delete non-empty directory, but since I use Qt, I tend to use the Qt method. if it existed.

my solution is outside of QFileSystemModel. use
QDir.removeRecursively()
def del_folder(self, checked, index):
if not index.isValid():
return
model = index.model()
path = model.fileInfo(index).absoluteFilePath()
dir1 = QtCore.QDir(path)
dir1.removeRecursively() # remove all.

Related

Variable inside a variable in Jenkinsfile

What is the correct Jenkinsfile syntax in order to use a variable value when executing command with another variable?
For example:
def lastItemIndex = "${json.items.size()-1}"
def path = "${json.items[${lastItemIndex}].assets.downloadUrl}"
echo "${path}"
First variable is lastItemIndex and second one is json.
The second row is not running properly when I tried different options.
The syntax in your second row is mostly fine. Your problem is that you are storing the return of lastItemIndex as a String and then attempting to use it as an Integer in your second row of code.
You can fix your first row with:
lastItemIndex = json.items.size() - 1
and then it will be an Integer type and def path = "${json.items[lastItemIndex].assets.downloadUrl}" will succeed.
Alternatively, you could just have the second line of code with:
def path = "${json.items[-1].assets.downloadUrl}"
to access the last element of the array.
Note that in general if you need to convert a String to an Integer within a Jenkins Pipeline via Groovy you can utilize the to_Integer method.
Thanks to Matt, eventually that what works for me:
def lastItemIndex = json.items.size()-1
def path = json.items[lastItemIndex].assets.downloadUrl

How to create a deep copy in Karate

How can I create a totally independent copy of a variable in karate. So that changing one doesn't affect the other.
I tried simply assigning it to a new variable but it didn't work.
* def copyJson = originalJson
Changing copyJson changes the originalJson too.
This is possible, and explained in the documentation: https://github.com/intuit/karate#copy
* def original = { key: 'value' }
# this will create a "deep copy"
* copy foo = original

Python: Accessing indices of iterable passed into `Pool.map(function(), iterable)`

I have code that looks something like this:
def downloadImages(self, path):
for link in self.images: #self.images=list of links opened via requests & written to file
if stuff in link:
#parse link a certain way to get `name`
else:
#parse link a different way to get `name`
r = requests.get(link)
with open(path+name,'wb') as f:
f.write(r.content)
pool = Pool(2)
scraper.prepPage(url)
scraper.downloadImages('path/to/directory')
I want to change downloadImages to parallelize the function. Here's my attempt:
def downloadImage(self, path, index of self.images currently being processed):
if stuff in link: #because of multiprocessing, there's no longer a loop providing a reference to each index...the index is necessary to parse and complete the function.
#parse link a certain way to get `name`
else:
#parse link a different way to get `name`
r = requests.get(link)
with open(path+name,'wb') as f:
f.write(r.content)
pool = Pool(2)
scraper.prepPage(url)
pool.map(scraper.downloadImages('path/to/directory', ***some way to grab index of self.images****), self.images)
How can I refer to the index currently being worked on of the iterable passed into pool.map()?
I'm completely new to multiprocessing & couldn't find what I was looking for in the documentation...I also couldn't find a similar question via google or stackoverflow.

Pass the value of a variable to a function in Python3

I have created a function that opens a file and reads the first line of it into a variable named 'farmer_id'. I would now like to use this 'farmer_id' variable in a second function in my script, and I'm not sure of the best way to do this. Global variables are allowed, so any solution is welcome.
I have read everything I can find on Stackoverflow on this topic, but I cannot find anything which works for my specific problem.
def get_id_from_file():
file = open('C:/Users/my.name/Documents/test.txt', 'r')
farmer_id = file.readline()
You can do like this
def any_func(former_id):
pass
def get_id_from_file():
file = open(r'C:/Users/my.name/Documents/test.txt', 'r')
farmer_id = file.readline()
return farmer_id
farmerid = get_id_from_file()
any_func(farmerid)

Find the tfs path of merged branch

Using TFS, I have trunk $/project/trunk and a branch $/project/dev/feature/new_one.
I have merged my branch back to trunk as follows:
C33($/project/trunk)
| \
| \
| C32($/project/dev/feature/new_one)
| |
| |
| |
...
I use the TFS API and can find the merge changeset C33. With the method QueryMerges(), I'm able to find the parent changeset C32 with all the changes on the files, but not the information I need :(
Is there a way, using the TFS API, to find the repository path of the branch merged $/project/dev/feature/new_one?
With the changeset C32, I'm only able to get paths of modified files, like $/project/dev/feature/new_one/path/to/file.txt but I'm unable to extract the path of the branch from the full path of the file :(
PS : A solution working since TFS2008 will be the best, but if it works only since 2010, it should be good...
PS2 : solving this problem will help to manage merge changesets in git-tfs which I develop...
Unfortunately there is no API method to get a branch for a given item path, which you would think is a fairly common use case.
TFS 2010 onwards you can use VersionControlServer.QueryRootBranchObjects to query all branches in version control. Using RecursionType.Full as the parameter to this method you will get a BranchObject array of all branches with no parent and all of their descendents. You can then determine a branch for a given file path as follows:
var collection = new TfsTeamProjectCollection(new Uri("http://tfsuri"));
var versionControl = collection.GetService<VersionControlServer>();
var branchObjects = versionControl.QueryRootBranchObjects(RecursionType.Full);
var mergeFilePath = "$/project/dev/feature/new_one/path/to/file.txt";
var branch = branchObjects.SingleOrDefault(b => {
var branchPath = b.Properties.RootItem.Item;
return mergeFilePath.StartsWith(branchPath.EndsWith("/") ? branchPath : branchPath + "/");
});
Console.WriteLine(branch.Properties.RootItem.Item);
As shown, the path to the branch is at BranchObject.Properties.RootItem.Item. I believe it is safe to find the relevant BranchObject in the array simply by checking which branch's path is contained in the merge file's path (given it is only possible match at most one branch as TFS enforces that only one branch can exist in a given folder hierarchy).
Just to be aware, I have been burned by this Connect issue when using QueryRootBranchObjects in TFS 2012. The cause were some spurious branches that had apostrophes in the branch name.
The workaround to this is to use VersionControlServer.QueryBranchObjects, however this takes an item identifier which is the exact path to the branch. Clearly you don't know the branch path at this point as all you have is a file path, so you have to recurse up the directories of the file path calling QueryBranchObjects each time until you get a match.