How do I import the white project into an IronPython program? - wpf

I am working on some UI automation software and just recently moved the project from Python to IronPython since the requirements for this project state it will only be used in a Windows environment. However, I need to automated the UI of a program that uses Windows Presentation Foundation (WPF). I found this one library that looks like it might be useful called White.
http://white.codeplex.com/
So I would like to use this in my IronPython program but all the example code I have been seeing so far for importing modules written in C# or with a C# interface has been for the Microsoft/Windows built-ins. I figured I should be able to reference it since you can do it with IronRuby according to this article.
http://www.natontesting.com/2010/02/17/how-to-test-a-wpf-app-using-ironruby-and-white/
However, I have to imagine the means/syntax by which IronRuby would import/reference White is very different than how IronPython would do it. I have also found posts by other developers saying they are using IronPython and White, but can't find a post that includes the code to actually make the reference to White. How would I go about this?

import clr
clr.AddReference("White.Core")
clr.AddReference("White.NUnit")
from White.NUnit import *
from White import *
from White.Core import *
from White.Core.Configuration import *
from White.Core.UIItems import *
from White.Core.UIItems.WindowItems import *
from White.Core.UIItems.ListBoxItems import *
from White.Core.UIItems.Container import *
from White.Core.UIItems.Finders import *
from White.Core.Factory import *
from White.Core.Finder import *
from White.Core.AutomationElementSearch import *
from White.Core.WindowsAPI import *
Then use the white api as normal.
app = Application.Attach(proc)
win = app.GetWindow('Window Caption')
print win.Name
box = win.Get[MultilineTextBox]('textBoxId')
print box.Text

IronPython is capable of addressing any CLR assembly using the following:
import clr
clr.AddReference("AssemblyName")
Because the white project is .NET based this will work. To use objects from the assembly:
from AssemblyName import *
(of course you can use a subset here)
Then simply instantiate and use your objects:
from System.Collections import BitArray
ba = BitArray(5)
ba.Set(0, True) # call the Set method
ba[0]
This documentation should help.

Related

Dynamically import images in ReactJS with variables

I'm creating a game with sprays but I can't put all the sprays in the same folder because at least 50/ 100 and more are comming and I want to import all the images from a folder, the problem is that the name is variable.
Example :
import * as JhonImgs from "../images/Jhon/"
import * as JhonImgs from "../images/Lucas/"
Or something like that. All the images have the same extension, .png.
The problem is require.context() doesn't allow me use variables. And I can't call one by one, or use a cdn server.
Thanks.

rollup package splitting / code splitting

I am working on a components library (such as material-ui). I am using rollup for compiling.
For the moment I can generate a single package, and to import a component such as a button, I can do it like that :
import { Button } from "package-name";
Can I modify the rollup configuration to generate a separate package or chunk for each component.
In other word, can I split the big-size package in separate small-size packages (one for each component)
and use them in a way similar to this:
import { Button } from "package-name/Button";
I start looking to the Code Splitting...
But I am wandring if there is a known best practice to have this result?
Is there a specific way to write the rollup.config file ?

Add column heading (and corresponding property) to Wagtail index view

My team and I have created a section to allow our company to add landing pages. We'd like to include some additional columns to the index view of the associated model, as illustrated in this image.
I've found a few old posts (2014-ish) that indicate this wasn't possible, but I can't find anything newer that makes that claim invalid. Is it possible to do this, and if so can someone point me in the right direction?
If you are willing to patch the view and template for the Page Explorer, you should be able to do this. My group isn't patching the Page Explorer so I don't have example code for that but our general approach is as follows:
We have a django app called wagtail_patches which is listed in our INSTALLED_APPS before the wagtail apps.
In wagtail_patches/apps.py we have:
from django.apps import AppConfig
class WagtailPatchesConfig(AppConfig):
name = 'wagtail_patches'
verbose_name = 'Wagtail Patches'
ready_is_done = False
def ready(self):
"""
This function runs as soon as the app is loaded. It executes our monkey patches to various parts of Wagtail
that change it to support our architecture of fully separated tenants.
"""
# As suggested by the Django docs, we need to make absolutely certain that this code runs only once.
if not self.ready_is_done:
# The act of performing this import executes all the code in monkey_patches.
from . import monkey_patches
# Unlike monkey_patches, the code of wagtail_hook_patches is in the function patch_hooks().
from .wagtail_hook_patches import patch_hooks
patch_hooks()
self.ready_is_done = True
else:
print("{}.ready() executed more than once! This method's code is skipped on subsequent runs.".format(
self.__class__.__name__
))
Then in wagtail_patches/monkey_patches.py we import the module to be patched, then write a new method, and then replace the stock version with the new method. For example:
from wagtail.admin.forms.collections import CollectionForm
def collection_form_clean_name(self):
if <weird custom condition>:
raise ValidationError('some error message')
CollectionForm.clean_name = collection_form_clean_name
Overriding templates is just like normal django template overriding, place your customized version of some file in a folder hierarchy matching it's usual position within Wagtail.
You can do this with ModelAdmin for a specific model, but it won't appear in the Pages Explorer view as in your screenshot. Instead, it will appear in a new menu item on the left sidebar. I also find that Hooks is a great place to store this logic. Just be aware of how ModelAdmin differs from modeladmin. The Bakery Demo has some good examples of how this all works.

Ironpython with pycharm: Drawing and Forms

This is probably a very noob question. I am trying to get some basic code from sharpdevelop into Pycharm. The code basically just draws a window and place some fields and buttons using Windows forms. I've set the interprter in Pycharm to be Ironpython. There seems to be something wrong with the import statements. When I run the following (Which is just a copy paste from code that is perfectly working in sharpdevelop):
import System.Drawing
import System.Windows.Forms
class MainForm(Form):
def __init__(self):
self.InitializeComponent()
def InitializeComponent(self):
self._checkBox1 = System.Windows.Forms.CheckBox()
self._comboBox1 = System.Windows.Forms.ComboBox()
self._label1 = System.Windows.Forms.Label()
#rest of code ...
I get the following message
ImportError: No module named Drawing
You will need to add a reference to the DLLs. It's three lines:
import clr
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
SharpDevelop takes care of adding the references in Program.py (the file that runs). It imports the MainForm class, instantiates it, and displays the form.
Since I am assuming this is the file you run, you will need to add the references within the same file.

ActiveX control in ironpython windows form

I'm having difficulties with a set of activeX controls used for automation of lab instruments. I've got them all to work in VB, in forms applications - as they won't work in console applications. My company is working mainly with python and there would thus be a great advantage in writting our drivers in python, mainly for lims integration.
As such, I have tried to embed my activeX controls within ironpython windows forms. I've tried two different ways of introducing my controls in the forms:
1) with clr referencing the Ax dll after using AxImp on the original ocx. Then I add my instance of the control as a form control and try to run it from there. It throws System.Windows.Forms.AxHost+InvalidActiveXStateException.
import clr
clr.AddReferenceToFileAndPath(r"C:\dlls\AxVCode3Lib.dll")
clr.AddReferenceByPartialName("System.Windows.Forms")
import System.Windows.Forms as Forms
import AxVCode3Lib
class activeXform(Forms.Form):
def __init__(self):
a = AxVCode3Lib.AxVCode3()
self.Controls.Add(a)
for i in self.Controls:
r = i.Initialize("serial")
print r
form = activeXform()
raw_input(">exit")
2) Using SystemReflection and the ProgID, which throws a generic "Exception has been thrown by the target of an invocation". The following code was introduced in a basic ironpython windows form, however I was not able to add those objects as form controls.
import System.Type
import System.Reflection
import System.Array
oType = System.Type.GetTypeFromProgID("VCODE3.VCode3Ctrl.1")
o = System.Activator.CreateInstance(oType)
args = System.Array[str](['serial'])
try:
r = oType.InvokeMember("Initialize", System.Reflection.BindingFlags.InvokeMethod, None, o, args)
print r
except Exception as e:
print e
Now I suspect both those examples fail due to some properties missing in my forms. However I can't figure out what it is, in particular in case 1 when the instance is actually added as a control, it seems I am very close.
You cannot use the methods of the ActiveX control until its native handle is created. In other words, not until after you call the form's Show() method.
Make sure you get the basic outline of a Winforms application correct, peek in the Program.cs source code file of a sample C# Winforms app. The Application.Run() call is essential. Use the form's Load event (or override OnLoad) as the first event where you can actually start using the control.

Resources