Gimp python-fu , open a directory selection dialog - python-2.7

I want the user of a script to be able to select a directory to do "stuff". Not open a JPG or save a XCF. I just need a directory, because my script will do stuff including reading multiple files, and save multiple transformations.
I am supposed to be able to use
import Tkinter, tkFileDialog
root = Tkinter.Tk()
But gimp 2.10.24 has a... custom... install of the end-of-life python 2.7, and I get the error
This probably means that Tcl wasn't installed properly.
So is there something like "pdb.open_directory" built into gimp? What about other python libraries shipped with gimp, but actually work? I'd also be satisfied if I could get Tkinter working in gimp, but that seems even harder.

If you use the auto-generated parameter dialog for your Python-fu, then it's just a matter of using a parameter with a PF_DIRNAME type. For instance
register(
'export-tiles',
"Export tiles","Export tiles",author,author,year,"Export tiles...",
'*',
[
(PF_IMAGE, 'image', 'Input image', None),
(PF_DIRNAME, 'directory', 'Directory', '.'),
(PF_STRING, 'namePattern', 'Tile name', '{imageName}-{column1:02d}-{row1:02d}.png'),
(PF_SPINNER, 'rows', 'Rows', 10, (1,1000,1)),
(PF_SPINNER, 'columns', 'Columns', 10, (1,1000,1))
],
[],
exportTiles,
menu="<Image>/File/"
)
will elicit a dialog like this:
where the Directory button opens a directory selector and the function defined as:
def exportTiles(image,directory,namePattern,rows,columns):
will automatically receive a gimp.Image object in image (the active image when you call the script), a directory as a python string ('/tmp') in directory, the name pattern (as a python string) in namePattern, and rows and columns as python floats.

Related

how to add a new user to zkteco machine with its fingerprint by python lib "pyzk"

I have added a user to the machine with this cammande
conn.set_user(uid=1, name='John Doe', privilege=const.USER_DEFAULT, user_id='9999')
now I what to save it's fingerprints, I have tried to use this function
conn.save_user_template(user, fptmp)
but how can I get "fptmp" to pass it here

Text Encoding Between Python File, Maya Script Editor, and Maya UI (Python 2.7, Maya 2015, Windows 7)

I've got a python file that I'm loading as a script into the maya script editor. The python file is currently encoded as UTF-8.
I have the need to use the ↑ and ↓ characters (or any other arrow substitutes within Unicode, such as ➘ or ➚, I just want to convey up and down). I'm using the characters as the label of a button. Here's the script:
import Maya.cmds as cmds
def initInterface():
cmds.window("mywin")
cmds.rowColumnLayout("my_rcl", nc=1)
cmds.button(label=u'\↑')
cmds.button(label=u'\↓')
cmds.showWindow("mywin")
initInterface()
The script is saved as myPythonScript.py and is then loaded into the Maya script editor using the load script button.
On execution, I get a UI window and buttons as expected, but the labels for the buttons are now "?" (question marks). I can't seem to get Maya to display the arrows.
To solve this, I've tried a couple of in-code things. Here are a few of my attempts:
# Attempt 1
upArrow = u'\↑'
upArrowEncoded = upArrow.encode("utf-8")
cmds.button(label=upArrowEncoded)
# Result: "?"
# Attempt 2
upArrow = u'\U+2B06'
cmds.button(label=upArrow)
# Result: "?B06"
# Attempt 3
upArrow = u'\U+2B06'
upArrowEncoded = upArrow.encode("utf-8")
cmds.button(label=upArrowEncoded)
# Result: "?"
To be honest (and is likely to be apparent from my code snippets) I've never experimented with text encoding and know next to nothing about it. I'm not sure if I need to change the encoding of my .py file, or encode the string with UTF-16 or something. This is way outside of my area of expertise and I'm having a hard time finding resources to help me understand text and string encoding.
I did check out this:
Unicode Within Maya
And this:
Convert a Unicode String to a String in Python Containing Extra Symbols
But I wasn't able to understand a lot of what I read, and I'm not sure if they relate to this issue or not.
I'm the type of person who doesn't enjoy using code I don't understand (how do people even document that?), so I'm here to ask for links to learning resources and for general advice on the subject, moreso than for a code snippet that does what I want. If it turns out this is not possible, I can use image buttons instead. But they are less efficient and time consuming to produce for each special character I may use.
Thank you for reading through this, and thank you in advance to anyone who can point me in the right direction here. Cheers!
As far as I can tell, the native MayaUi uses/has access to the Code Page 1252 Windows Latin 1 (ANSI) character set (at least on Windows...) as mentioned here, and after some noodling these *all appear to work as advertised.
I'd be curious to see an answer that explained how to change that and access what OP is looking for, but as an alternative to anyone that really truly wants more special characters, may I suggest learning PySide / Qt for building your UI.
Caveats
A lot more boilerplate and setup when it comes to making 'something simple'
Several mayaControls do not have direct Qt implementations (gradientControlNoAttr being a recent discovery, and case in point)
Example is written under the assumption that user has installed and uses Qt.py
Lets dive right in:
import maya.cmds as cmds
import maya.OpenMayaUI as omui
from Qt import QtCore, QtGui
from Qt.QtWidgets import *
from shiboken import wrapInstance
def maya_main_window():
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(long(main_window_ptr), QWidget)
class TestUi(QDialog):
def __init__(self, parent=maya_main_window()):
super(TestUi, self).__init__(parent)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
def create(self):
self.setWindowTitle("TestUi : Unicode")
self.setWindowFlags(QtCore.Qt.Tool)
self.create_controls()
self.create_layout()
self.create_connections()
def create_controls(self):
"""
Create the widgets for the dialog.
"""
# using "Python source code" unicode values
# ie: https://www.fileformat.info/info/unicode/char/2191/index.htm
self.up_button = QPushButton(u'\u2191')
self.down_button = QPushButton(u'\u2193')
self.left_button = QPushButton(u'\u2190')
self.right_button = QPushButton(u'\u2192')
def create_layout(self):
"""
Create the layouts & add widgets
"""
main_layout = QVBoxLayout()
main_layout.setContentsMargins(6, 6, 6, 6)
main_layout.addWidget(self.up_button)
main_layout.addWidget(self.down_button)
main_layout.addWidget(self.left_button)
main_layout.addWidget(self.right_button)
main_layout.addStretch()
self.setLayout(main_layout)
def create_connections(self):
"""
Create the signal/slot connections
"""
self.up_button.clicked.connect(self.on_button_pressed)
self.down_button.clicked.connect(self.on_button_pressed)
self.left_button.clicked.connect(self.on_button_pressed)
self.right_button.clicked.connect(self.on_button_pressed)
def on_button_pressed(self):
print "Button Pressed"
def LaunchUI():
if __name__ == "__main__":
# Development workaround for PySide winEvent error (Maya 2014)
# Make sure the UI is deleted before recreating
try:
test_ui.deleteLater()
test_ui.close()
except:
pass
# Create minimal UI object
test_ui = TestUi()
# Delete the UI if errors occur to avoid causing winEvent
# and event errors (in Maya 2014)
try:
test_ui.create()
test_ui.show()
except:
test_ui.deleteLater()
traceback.print_exc()
LaunchUI()
There's an awful lot to unpack there for not a terribly huge payoff, but the relevant piece of information is living under "create_controls".

get list of files in a sharepoint directory using python

I have a url for sharepoint directory(intranet) and need an api to return list of files in that directory given the url. how can I do that using python?
Posting in case anyone else comes across this issue of getting files from a SharePoint folder from just the folder path.
This link really helped me do this: https://github.com/vgrem/Office365-REST-Python-Client/issues/98. I found so much info about doing this for HTTP but not in Python so hopefully someone else needs more Python reference.
I am assuming you are all setup with client_id and client_secret with the Sharepoint API. If not you can use this for reference: https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs
I basically wanted to grab the names/relative urls of the files within a folder and then get the most recent file in the folder and put into a dataframe.
I'm sure this isn't the "Pythonic" way to do this but it works which is good enough for me.
!pip install Office365-REST-Python-Client
from office365.runtime.auth.client_credential import ClientCredential
from office365.runtime.client_request_exception import ClientRequestException
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import io
import datetime
import pandas as pd
sp_site = 'https://<org>.sharepoint.com/sites/<my_site>/'
relative_url = "/sites/<my_site/Shared Documents/<folder>/<sub_folder>"
client_credentials = ClientCredential(credentials['client_id'], credentials['client_secret'])
ctx = ClientContext(sp_site).with_credentials(client_credentials)
libraryRoot = ctx.web.get_folder_by_server_relative_path(relative_url)
ctx.load(libraryRoot)
ctx.execute_query()
#if you want to get the folders within <sub_folder>
folders = libraryRoot.folders
ctx.load(folders)
ctx.execute_query()
for myfolder in folders:
print("Folder name: {0}".format(myfolder.properties["ServerRelativeUrl"]))
#if you want to get the files in the folder
files = libraryRoot.files
ctx.load(files)
ctx.execute_query()
#create a dataframe of the important file properties for me for each file in the folder
df_files = pd.DataFrame(columns = ['Name', 'ServerRelativeUrl', 'TimeLastModified', 'ModTime'])
for myfile in files:
#use mod_time to get in better date format
mod_time = datetime.datetime.strptime(myfile.properties['TimeLastModified'], '%Y-%m-%dT%H:%M:%SZ')
#create a dict of all of the info to add into dataframe and then append to dataframe
dict = {'Name': myfile.properties['Name'], 'ServerRelativeUrl': myfile.properties['ServerRelativeUrl'], 'TimeLastModified': myfile.properties['TimeLastModified'], 'ModTime': mod_time}
df_files = df_files.append(dict, ignore_index= True )
#print statements if needed
# print("File name: {0}".format(myfile.properties["Name"]))
# print("File link: {0}".format(myfile.properties["ServerRelativeUrl"]))
# print("File last modified: {0}".format(myfile.properties["TimeLastModified"]))
#get index of the most recently modified file and the ServerRelativeUrl associated with that index
newest_index = df_files['ModTime'].idxmax()
newest_file_url = df_files.iloc[newest_index]['ServerRelativeUrl']
# Get Excel File by newest_file_url identified above
response= File.open_binary(ctx, newest_file_url)
# save data to BytesIO stream
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0) # set file object to start
# load Excel file from BytesIO stream
df = pd.read_excel(bytes_file_obj, sheet_name='Sheet1', header= 0)
Here is another helpful link of the file properties you can view: https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-rest-reference/dn450841(v=office.15). Scroll down to file properties section.
Hopefully this is helpful to someone. Again, I am not a pro and most of the time I need things to be a bit more explicit and written out. Maybe others feel that way too.
You need to do 2 things here.
Get a list of files (which can be directories or simple files) in
the directory of your interest.
Loop over each item in this list of files and check if
the item is a file or a directory. For each directory do the same as
step 1 and 2.
You can find more documentation at https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest#working-with-files-attached-to-list-items-by-using-rest
def getFilesList(directoryName):
...
return filesList
# This will tell you if the item is a file or a directory.
def isDirectory(item):
...
return true/false
Hope this helps.
I have a url for sharepoint directory
Assuming you asking about a library, you can use SharePoint's REST API and make a web service call to:
https://yourServer/sites/yourSite/_api/web/lists/getbytitle('Documents')/items?$select=Title
This will return a list of documents at: https://yourServer/sites/yourSite/Documents
See: https://msdn.microsoft.com/en-us/library/office/dn531433.aspx
You will of course need the appropriate permissions / credentials to access that library.
You can not use "server name/sites/Folder name/Subfolder name/_api/web/lists/getbytitle('Documents')/items?$select=Title" as URL in SharePoint REST API.
The URL structure should be like below considering WebSiteURL is the URL of site/subsite containing document library from which you are trying to get files and Documents is the Display name of document library:
WebSiteURL/_api/web/lists/getbytitle('Documents')/items?$select=Title
And if you want to list metadata field values you should add Field names separated by comma in $select.
Quick tip: If you are not sure about the REST API URL formation. Try pasting the URL in Chrome browser (you must be logged in to SharePoint site with appropriate permissions) and see if you get proper result as XML if you are successful then update the REST URL and run the code. This way you will save time of running your python code.

Unable to save persistence file using wxPython PersistenceManager

I'm developing a GUI application using wxpython that has roughly 110 user-chosen parameters. Since I would like for users to be able to save these options to a project file, I decided to use the PersistenceManager module that's included with wxPython.
The persistence works great as long as I don't try to specify the filename in which to save the settings, i.e., I use the default value (C:\users\username\AppData\programName\Persistence_Options), and just have the program save the settings when it exits.
What I'm trying to do is allow the user to choose a file to save the settings (since they might have multiple projects with different options). But, when I use the SetPersistenceFile method with the user-specified filename, no file gets saved, and no error message is returned, even though it's definitely executing those lines of code, which are given below. (The OnSave function is a method of the main window of the program.)
def OnSave(self, e):
self.dirname = os.getcwd()
if self.ProjectFile == '':
dlg = wx.FileDialog(self, "Save project file", self.dirname, "", "Project configuration file (.prj)|*.prj", wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_CANCEL:
return
else:
self.ProjectFile = os.path.join(dlg.GetDirectory(), dlg.GetFilename())
#print self.ProjectFile
if self.ProjectFile != '':
print "Made it to here (Save)..."
#self.Register(self) # Also tried calling Register in __init__
self._persistMgr = PM.PersistenceManager.Get()
print self.ProjectFile # Gives correct filename
self._persistMgr.SetPersistenceFile(self.ProjectFile)
self._persistMgr.Save(self)
print "Finished saving."
I've tried using a local PersistenceManager object, rather than having it as a class member, and this made no difference. Interestingly enough, if I declare the self.__persistMgr object in the window's __init__ function and use the SetPersistenceFile method with a hard-coded filename there, it writes the file, however this is not helpful since the user needs to specify that at runtime.
Does anyone know why the file isn't saving and how I can fix this?
Not sure why your code is giving you grief, the following works on Linux, although that may be of no consolation to you.
It is cobbled together from a number of sources, not having come across the PersistenceManager myself.
#!/usr/bin/python
import wx , os
import wx.lib.agw.persist as PM
class persist(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "A window that maintains size and position after restart")
self.Bind(wx.EVT_CLOSE, self.OnClose)
# Very important step!!
if self.GetName() == "frame":
self.SetName("My Persist Frame") # Do not use the default name!!
dirname = os.getcwd()
dlg = wx.FileDialog(self, "Project file", dirname, "", "Project configuration file (.prj)|*.prj|All files (*.*)|*.*", wx.FD_SAVE)
if dlg.ShowModal() == wx.ID_CANCEL:
_configFile = os.path.join(os.getcwd(), "persist-saved.prj") # getname()
else:
_configFile = os.path.join(dlg.GetDirectory(), dlg.GetFilename())
print _configFile
self._persistMgr = PM.PersistenceManager.Get()
self._persistMgr.SetPersistenceFile(_configFile)
self._persistMgr.RegisterAndRestoreAll(self)
self._persistMgr.Save(self)
def OnClose(self, event):
self._persistMgr.SaveAndUnregister()
event.Skip()
if __name__ == '__main__':
my_app = wx.App()
p = persist(None)
p.Show()
my_app.MainLoop()
Result in my .prj file:
[Persistence_Options]
[Persistence_Options/Window]
[Persistence_Options/Window/My\ Persist\ Frame]
x=('int', '9')
y=('int', '134')
w=('int', '319')
h=('int', '78')
Maximized=('bool', 'False')
Iconized=('bool', 'False')
Note the setting of the name this would be true of whatever it is that you are saving for persistence.
Edit: With regard to your comment
I think that you might be hoping for more than the PersistenceManager can cope with currently.
wxWidgets has built-in support for a (constantly growing) number of controls. Currently the following classes are supported:
wxTopLevelWindow (and hence wxFrame and wxDialog)
wxBookCtrlBase (i.e. wxNotebook, wxListbook, wxToolbook and wxChoicebook)
wxTreebook
To automatically save and restore the properties of the windows of classes listed above you need to:
Set a unique name for the window using wxWindow::SetName(): this step is important as the name is used in the configuration file and so must be unique among all windows of the same class.
Call wxPersistenceManager::Register() at any moment after creating the window and then wxPersistenceManager::Restore() when the settings may be restored (which can't be always done immediately, e.g. often the window needs to be populated first). If settings can be restored immediately after the window creation, as is often the case for wxTopLevelWindow, for example, then wxPersistenceManager::RegisterAndRestore() can be used to do both at once.
If you do not want the settings for the window to be saved (for example the changes to the dialog size are usually not saved if the dialog was cancelled), you need to call wxPersistenceManager::Unregister() manually. Otherwise the settings will be automatically saved when the control itself is destroyed.
Source: http://www.ccp4.ac.uk/dist/checkout/wxPython-src-3.0.2.0/docs/doxygen/out/html/overview_persistence.html
Of course I could be hopelessly wrong, as I have already admitted, I haven't used it before or really investigated it properly.

Undetected Django app when installing from git

I'm having trouble installing django-admin_action_mail from git.
I tried to install it via:
pip install
git+https://github.com/mjbrownie/django-admin_action_mail.git
But Django did not pick it up when I added it to settings.INSTALLED_APPS.
Did I miss something?
The admin code for that app is commented out (see here: https://github.com/mjbrownie/django-admin_action_mail/blob/master/admin_action_mail/admin.py ) so nothing is going to show up on the admin page - even if it's working and enabled.
It looks as though you need to create your own models to handle the mailing functions. Take a look at the README where it tells you to add something like the following in your app's admin.py:
from admin_action_mail.actions import mail_action
class MyModelAdmin(admin.ModelAdmin):
#Note all args are optional
actions = [
mail_action(
'description' : "Send Email to Related Users",
'email_dot_path' : 'email', # dot path string to email field (eg 'user.email')
'email_template_html' : 'admin_action_email/email.html'
'reply_to' : 'noreply#example.com' # defaults to request.user.email
)
]
admin.site.register(MyModel, MyModelAdmin)
Have you added a model like that to your own app's admin.py?
EDIT: As the problem appears to be with installation, the following should help:
You can add arbitrary paths to your wsgi path spec, that means it will pick up Python app modules in other locations. Assuming your app is installed in /home/user2161049/myapp you can put your external modules under /home/user2161049/myapp/external. In this case copy the contents of that app into /home/user2161049/myapp/external/admin_action_mail/.
To add this to your settings.py:
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(SITE_ROOT, 'external'))
The first line defines SITE_ROOT based on the current running script (setup.py) at startup. The second adds the external folder into the search path. You can put anything you want in there, and even define a specific folder somewhere else if you want to keep your externals out of your app folder. Restart the server and it should find the app just fine.