How to check if a module exists in Lua? - lua

I'm using xdg-menu-to-awesome-wm to generate a Lua file containing the GNOME menu, for inclusion in Awesome WM. Since the generator script may not be installed, I need some way for Lua to only require the menu module if it exists.
I don't want to go looking through file names, since it could be anywhere in package.path. One option would be to ignore the exception created when the module does not exist, but I'd rather not ignore any other exceptions - I do want to know if the module contains any syntax or other errors. The reference unfortunately doesn't specify which exceptions can be generated, so I'm not sure how to do it.

If you need to distinguish between a missing module and a syntax error, you can directly access the searcher functions in package.searchers.
These functions will:
Return a loader function if successful
Return a string if the module is not found
Throw an error if there is a syntax error
So what you can do is mimic the way require searches for a module, calling each searcher in turn until one of them returns a function. Unlike require, we need not throw an error if the module is not found, i.e. if every searcher function returns a string.
function isModuleAvailable(name)
if package.loaded[name] then
return true
else
for _, searcher in ipairs(package.searchers or package.loaders) do
local loader = searcher(name)
if type(loader) == 'function' then
package.preload[name] = loader
return true
end
end
return false
end
end

Look, I had the same problem with 'luafilesystem' module, I worked it out like this,
local haslfs,lfs = pcall(require,"lfs")
if haslfs then
configLines["PROJECT_HOME"] = lfs.currentdir()
else
configLines["PROJECT_HOME"] = prompt("Project path > ")
end
'lfs' here is the module handle . And pcall is used to know if the module is really loaded without propagating the error.

What I do is wrap the require in a pcall so that the module is loaded and a fail to load can be caught. There is a fully worked function which I use to download and install the missing modules from our servers here:
http://www.fhug.org.uk/wiki/doku.php?id=plugins:code_snippets:module_require_with_load
function loadrequire(module)
local function requiref(module)
require(module)
end
res = pcall(requiref,module)
if not(res) then
-- Do Stuff when no module
end
end
loadrequire('menu')

Related

Service __len__ not found Unexpected error, recovered safely

python3.8
My code:
from googleads import adwords
def execute_request():
adwords_client = adwords.AdWordsClient.LoadFromStorage(path="google_general/googleads.yaml")
campaign_service = adwords_client.GetService('CampaignService', version='v201809')
pass
context["dict_list"] = execute_request()
Traceback:
Traceback (most recent call last):
File "/home/michael/pycharm-community-2019.3.2/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_xml.py", line 282, in frame_vars_to_xml
xml += var_to_xml(v, str(k), evaluate_full_value=eval_full_val)
File "/home/michael/pycharm-community-2019.3.2/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_xml.py", line 369, in var_to_xml
elif hasattr(v, "__len__") and not is_string(v):
File "/home/michael/PycharmProjects/ads3/venv/lib/python3.8/site-packages/googleads/common.py", line 694, in __getattr__
raise googleads.errors.GoogleAdsValueError('Service %s not found' % attr)
googleads.errors.GoogleAdsValueError: Service __len__ not found
Unexpected error, recovered safely.
googleads.yaml about logging
logging:
version: 1
disable_existing_loggers: False
formatters:
default_fmt:
format: ext://googleads.util.LOGGER_FORMAT
handlers:
default_handler:
class: logging.StreamHandler
formatter: default_fmt
level: DEBUG
loggers:
# Configure root logger
"":
handlers: [default_handler]
level: DEBUG
I've just started studying the API.
Namely, I'm trying to execute my first request (https://developers.google.com/adwords/api/docs/guides/first-api-call#make_your_first_api_call)
Could you help me with this problem? At least how to localize it more precisely.
This seems to be a problem which results from the way the PyCharm debugger inspects live objects during debugging.
Specifically, it checks if a given object has the __len__ attribute/method in the code of var_to_xml, most likely to determine an appropriate representation of the object for the debugger interface (which seems to require constructing an XML representation).
googleads service objects such as your campaign_service, however, use some magic to be able to call the defined SOAP methods on them without requiring to hard-code all of them. The code looks like this:
def __getattr__(self, attr):
"""Support service.method() syntax."""
if self._WsdlHasMethod(attr):
if attr not in self._method_proxies:
self._method_proxies[attr] = self._CreateMethod(attr)
return self._method_proxies[attr]
else:
raise googleads.errors.GoogleAdsValueError('Service %s not found' % attr)
This means that the debugger's check for a potential __len__ attribute is intercepted, and because the CampaignService does not have a SOAP operation called __len__, an exception is raised.
You can validate this by running your snippet in the regular way (i.e. not debugging it) and checking if that works.
An actual fix would seem to either require that PyCharm's debugger changes the way it inspects objects (not calling hasattr(v, "__len__")) or that googleads modifies the way it implements __getattr__, for example by actually implementing a __len__ method that just raises AttributeError.

lua lfs on flashair w04 seems not to work

Hi for the last week i have been trying to get my Flashair to upload its files over ftp.
I can make a ftp connection without any problems, but when i try to iterate over the files in its folder its gives me a error on the lua lfs, the only thing its returning is nil.
its goes about this code sample:
for file in lfs.dir(localDir) do
attr = lfs.attributes(localDir .. file)
print( "Found "..attr.mode..": " .. file )
if attr.mode == "file" then
response = fa.ftp("put", ftpString..file, localDir .. file)
if response ~= nil then
print("Success!")
else
print("Fail!")
end
end
end
the error i get is:
lua: ftp.lua:17: attempt to index a nil value (global 'lfs')
stack traceback:
ftp.lua:17: in main chunk
[C]: in ?
where line 17 is the first line in the provided code sample.
Am I missing something, seems that i cannot find a lot information about this problem in combination with the Flashair.
Any help would be appreciated.
I am new to Lua so don't flame me if my response is not beneficial to you. I have been developing an app for W-03 and when testing the Lua app on my MACBOOK linux virtual env I was seeing the same error, and discovered I had to use require "lfs" statement in my test code. require "lfs" is not necessary when running on the actual W-03 though.

LM_TranslateLayer:DrawMe(moho, view) error

this is old code (Lua 5.1)
if (self.wiggleMode == self.LAYER_MODE) then
if (self.wiggleChannel == self.TRANS_MODE) then
LM_TranslateLayer:DrawMe(moho, view)
i have try change to lua 5.3 but error on:
LM_TranslateLayer:DrawMe(moho, view)
error: attempt to index global 'LM_TranslateLayer' (a nil value)
My guess is that LM_TranslateLayer comes from an external library that is loaded via require. Make sure that this library does define global symbols.
In Lua 5.1, require automatically defined a global table to hold the module's content. So, require"foo" definedfoo`.
In Lua 5.2 this changed and the idiom is now local foo = require"foo".
Check how the library that defines LM_TranslateLayer is loaded.

Defining Modules VS.NET vs F# Interactive

I have written this code which compiles and works perfectly in VS.NET 2010
module ConfigHandler
open System
open System.Xml
open System.Configuration
let GetConnectionString (key : string) =
ConfigurationManager.ConnectionStrings.Item(key).ConnectionString
however when I do a control + A and Alt + Enter to send this to FSI I get an error
ConfigHandler.fs(2,1): error FS0010: Unexpected start of structured construct in definition. Expected '=' or other token.
OK.
So I change my code to
module ConfigHandler =
open System
open System.Xml
open System.Configuration
let GetConnectionString (key : string) =
ConfigurationManager.ConnectionStrings.Item(key).ConnectionString
Now Control + A, Alt + Enter is successful and I FSI nicely tells me
module ConfigHandler = begin
val GetConnectionString : string -> string
end
However now If I try to compile my code in VS.NET 2010, I get an error message
Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'
How can I have both? Ability to compile in VS.NET and the ability to send modules to FSI?
There is a tiny -- but crucial -- difference between your two snippets of code which is to blame here.
F# has two ways to declare a module. The first, a "top-level module", is declared like this:
module MyModule
// ... code goes here
The other way to declare a module is as a "local module", like so:
module MyModule =
// ... code goes here
The main differences between the "top-level" and "local" declarations are that the local declaration is followed by an = sign and the code within a "local" module must be indented.
The reason you get the ConfigHandler.fs(2,1): error FS0010: Unexpected start of structured construct in definition. Expected '=' or other token. message for the first snippet is that you can't declare top-level modules in fsi.
When you added the = sign to your module definition, it changed from a top-level module to a local module. From there, you got the error Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule' because local modules must be nested within a top-level module or a namespace. fsi doesn't allow you to define namespaces (or top-level modules), so if you want to copy-paste the entire file into fsi the only way it'll work is if you use the compilation directives as #pad mentioned. Otherwise, you can simply copy-paste the local module definitions (without the containing namespace) into fsi and they should work as expected.
Reference:
Modules (F#) on MSDN
The common solution is to keep the first example and create a fsx file which references the module:
#load "ConfigHandler.fs"
You have advantage of loading multiple modules and writing plumbing code for experiment.
If you really want to load ConfigHandler.fs directly to F# Interactive, you can use INTERACTIVE symbol and compiler directives:
#if INTERACTIVE
#else
module ConfigHandler
#endif
which works for both fsi and fsc.

Is there any way to force load a module when it is opened through FSI?

If I compile the following module into a dll
namespace MyNs
module SomeModule =
do printfn "module loading"
let x = 23
then reference the dll in FSI and execute the command open MyNs.SomeModule "module loading" does not print immediately. It only prints when I access x which causes all the top level let and do bindings to execute (normal behavior I know in the .NET world). Is there any way, perhaps via an attribute on the module, I can indicate that module should load immediately upon opening in FSI?
Opening a module never does anything at runtime. It just puts all the symbols in the opened namespace in scope for unqualified access below the open statement.
Section 12.5 of the language spec is what you want to read - this details when the static initialization of a module will run.
Given that, the only time when this initialization is run automatically, as far as I know, is for last module in an exe.
I.e. I don't think there is a direct way to accomplish what you want.
If you have reflective access to the module:
ModuleType.TypeInitializer.Invoke(null, null)
will invoke the static initialization.
You can add the AutoOpen attribute to the module
[<AutoOpen>]
module SomeModule =
do printfn "module loading"
let x = 23
However this will only print the module loading message when you reference x.
Not sure if you found the solution to your problem but in my case I wanted to start an agent when my website was starting and it was indeed starting twice like you mentioned.
What I did was set a method let start() = inside the module and invoke the method using a static do xxx.start() from my main Site type.
Found that by reading the language spec Kurt linked.

Resources