Skip to content

Instantly share code, notes, and snippets.

@Andrei-Stepanov
Created September 27, 2016 16:45
Show Gist options
  • Save Andrei-Stepanov/ff19ff38ea1aeec39c8e1e7519a8e36a to your computer and use it in GitHub Desktop.
Save Andrei-Stepanov/ff19ff38ea1aeec39c8e1e7519a8e36a to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from zope import interface
from zope.interface.interface import adapter_hooks
class IT(interface.Interface):
def foo():
pass
@interface.implementer(IT)
class T(object):
def foo(self):
print "Hello"
def __conform__(self, ICommand):
return SeleniumCommandLinux(Test("xxx"))
class Test(object):
def __init__(self, os):
self.os = os
class ICommand(interface.Interface):
def make():
"""Make a list of program and its parameters.
"""
class SeleniumCommand(object):
def __init__(self, test):
self.test = test
def common(self):
return self.test.os
@interface.implementer(ICommand)
class SeleniumCommandLinux(SeleniumCommand):
def make(self):
return ["Linux commands"] + [self.common()]
@interface.implementer(ICommand)
class SeleniumCommandWin(SeleniumCommand):
def make(self):
return ["Windows commands"] + [self.common()]
t = T()
c = ICommand(t)
c.make()
test1 = Test("linux")
test2 = Test("win")
# def adapt_test_cmd(iface, smth):
# print "Interface class to check: %r" % iface
# print "Something to check: %r" % smth
# if iface == ICommand:
# return SeleniumCommandLinux(test1)
#
# adapter_hooks.append(adapt_test_cmd) # run through all available hooks
# print "CommandProvider: %r" % ICommand("yes")
from zope.interface.adapter import AdapterRegistry
registry = AdapterRegistry()
def unvirsal_adapter2commands(smth): # FABRIC
return SeleniumCommandWin(test1)
registry.register([IT], ICommand, 'name', unvirsal_adapter2commands)
# registry.lookup1(IT, ICommand, 'name')(t) # Will return SeleniumCommandWin(test1). 'name' - optional
adapter_hooks.append(registry.adapter_hook)
# 1 - list of original interfaces. - adapter constructor will require an argument for each specified interface.
# 2 - The second argument is the interface the adapter provides.
# 3 - name of adapter
# 4 - callable object, fabric, to make ICommand
#ICommand(test1)
#ICommand(test2)
#scmd = ICommand(test, "Selenium") # two realization win + linux
#sel_cmd = scmd.make()
#
#
#
#@interface.implementer(ICommand)
#class CmdLineLinux(object):
#
# def __init__(self, cmdline):
# self.cmdline = cmdline
#
# def mk_cmd(self):
# return subprocess.list2cmdline(self.cmdline)
#
#
#def cmdline_invariant(cmdline):
# if type(cmdline) not list:
# raise CmdlineError(cmdline)
#
#
#class CmdLineError(zope.Interface.Invalid):
# """A bogous cmdline."""
# def __repr__(self):
# return "Bad cmdline: %r" % self.args
#
#
# defs = interface.Attribute("-Dxxx definitions.")
# opts = interface.Attribute("Selenium server options.")
# jar_rpath = interface.Attribute("Path to Selenium .jar file.")
# java = interface.Attribute("Path to Java interpreter.")
#
#
#class ICmdLine(interface.Interface):
#
# cmdline = interface.Attribute("Command and its options list.")
#
# def mk_cmd():
# """Make a string escaping it for passing to os.system() like calls.
# """
#
#
#@interface.implementer(IExternalAPIPort)
#class SeleniumLinux(object, Commands):
#
# def __init__(self):
# self.defs = []
# if self.cfg.selenium_driver == 'Firefox':
# profile = self.cfg.firefox_profile
# if profile:
# cmd = "firefox -CreateProfile %s" % profile
# output = self.ssn.cmd(cmd)
# output = re.findall(r'\'[^\']*\'', output)[1]
# output = output.replace("'",'')
# output = os.path.dirname(dirname)
# self.firefox_profile_dir = output
# self.vm.info("Created a new FF profile at: %s", output)
# self.defs.append("-Dwebdriver.firefox.profile=%s" % profile)
# self.opts = []
# self.opts.append("-port %s" % cfg.selenium_port)
# self.opts.append("-trustAllSSLcertificates")
# self.java = "java"
# selenium = download_asset("selenium", section=self.cfg_vm.selenium_ver)
# fname = os.path.basename(selenium)
# jar_rpath = os.path.join(self.workdir(), fname)
# self.vm.copy_files_to(selenium, jar_rpath)
#
# def mk_cmd(self):
# cmd = "{java} {defs} -jar {dst_fname} {opts}".format(self.java,
# self.defs,
# self.dst_fname,
# self.opts)
# self.vm.info("Selenium cmd: %s", cmd)
#
# def run(self, session):
# ssn.sendline(cmd)
#
#
#
#
# def run(self, ssn):
# """Some general info.
# There are ways to define Firefox options globaly:
#
# * /usr/lib64/firefox/defaults/preferences/<anyname>.js
# * /etc/firefox/pref/<anyname>.js
#
# Format is:
#
# user_pref("some.key", "somevalue");
# pref("some.key", "somevalue");
#
# All values are defined at:
#
# about:config?filter=color
#
# Also user can define values for specific profile:
#
# http://kb.mozillazine.org/Profile_folder_-_Firefox#Linux
#
# For curent profile go to: about:support and press "Open Directory"
#
# Selenium understands next options:
#
# -Dwebdriver.firefox.profile=my-profile
# -Dwebdriver.firefox.bin=/path/to/firefox
# -trustAllSSLcertificates
#
# Also it is possible to specify Firefox profile in selenium python
# bindings:
#
# FirefoxProfile p = new FirefoxProfile(new File("D:\\profile2"));
# p.updateUserPrefs(new File("D:\\t.js"));
#
# To create a new profile call:
#
# firefox -CreateProfile <profile name>
#
# """
# selenium = download_asset("selenium", section=self.cfg_vm.selenium_ver)
# fname = os.path.basename(selenium)
# dst_fname = os.path.join(self.workdir(), fname)
# self.vm.copy_files_to(selenium, dst_fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment