How to distribute .d.ts file in npm package - visual-studio

I am trying to include a .d.ts file in my npm package for my (non-TypeScript) library.
I have the following in my package.json:
"typings": "./src/iter.d.ts",
But no matter what I put in ./src/iter.d.ts, whenever I try to use it in Visual Studio, I always get a "Cannot find module 'iterjs'" message.
To reproduce, create a new TypeScript app in VS (I'm using 2015 with TS 1.8.4), do an npm install iterjs, and add the following to app.ts:
import iter from 'iterjs';
I've tried modifying the local copy of iter.d.ts (under node_modules) in a desperate attempt to get anything working at all; my local copy currently has only the following content:
export declare function bob(object: Object, property: string): boolean;
export default bob;
I can verify that Visual Studio is finding and reading the iter.d.ts file (I can see it being read in Process Monitor every time VS opens app.ts); however, there's apprently something in the .d.ts file it doesn't like. I have read about a dozen blog posts, gone through the Handbook, and a half-dozen TypeScript issues on the subject, and have tried about 20 different ways of exporting anything from the .d.ts file, but VS always just gives the same error (Cannot find module 'iterjs').

Node-like resolution of .d.ts files in NPM packages only works for type definitions in proper external module format. The .d.ts in your repository is in ambient module format, to convert it to the correct format remove declare module "iter" and prefix every symbol you wish to export with export declare, oh, and replace export = iter with export default iter. You can find additional info at https://github.com/Microsoft/TypeScript/wiki/Typings-for-npm-packages

Related

go-swagger restapi/configure_todo_list.go - api.TodoGetHandler undefined error

I am a newbie in go and go-swagger. I am following steps in Simple Server tutorial in goswagger.io.
I am using Ubuntu 18.04, swagger v0.25.0 and go 1.15.6.
Following the same steps, there are a few differences of the files generated. For instance, goswagger.io's has find_todos_okbody.go and get_okbody.go in models but mine does not. Why is that so?
Link to screenshot of my generated files vs
Link to screenshot of generated files by swagger.io
Starting the server as written in the tutorial go install ./cmd/todo-list-server/ gives me the following error. Can anyone please help with this?
# my_folder/swagger-todo-list/restapi
restapi/configure_todo_list.go:41:8: api.TodosGetHandler undefined (type *operations.TodoListAPI has no field or method TodosGetHandler)
restapi/configure_todo_list.go:42:6: api.TodosGetHandler undefined (type *operations.TodoListAPI has no field or method TodosGetHandler)
The first step in goswagger.io todo-list is swagger init spec .... Which directory should I run this command in? I ran it in a newly created folder in my home directory. However, from the page, it shows the path to be ~/go/src/github.com/go-swagger/go-swagger/examples/tutorials/todo-list. I am not sure whether I should use go get ..., git clone ... or create those folders. Can someone advise me?
Thanks.
This is likely the documentation lagging behind the version of the code that you are running. As long as it compiles, the specific files the tool generates isn't so crucial.
This is a compilation error. When you do go install foo it will try to build the foo package as an executable and then move that to your GOPATH/bin directory. It seems that the generated code in restapi/configure_todo_list.go isn't correct for the operations code generated.
All you need to run this tutorial yourself is an empty directory and the swagger tool (not its source code). You run the commands from the root of this empty project. In order not to run into GOPATH problems I would initialise a module with go mod init todo-list-example before doing anything else.
Note that while the todo-list example code exists inside the go-swagger source, it's there just for documenting example usage and output.
What I would advice for #2 is to make sure you're using a properly released version of go-swagger, rather than installing from the latest commit (which happens when you just do a go get), as I have found that to be occasionally unstable.
Next, re-generate the entire server, but make sure you also regenerate restapi/configure_todo_list.go by passing --regenerate-configureapi to your swagger generate call. This file isn't always refreshed because you're meant to modify it to configure your app, and if you changed versions of the tool it may be different and incompatible.
If after that you still get the compilation error, it may be worth submitting a bug report at https://github.com/go-swagger/go-swagger/issues.
Thanks #EzequielMuns. The errors in #2 went away after I ran go get - u -f ./... as stated in
...
For this generation to compile you need to have some packages in your GOPATH:
* github.com/go-openapi/runtime
* github.com/jessevdk/go-flags
You can get these now with: go get -u -f ./...
I think it's an error of swagger code generation. You can do as folloing to fix this:
delete file configure_todo_list.go;
regenerate code.
# swagger generate server -A todo-list -f ./swagger.yml
Then, you can run command go install ./cmd/todo-list-server/, it will succeed.

Writing .cargo/config.toml to allow rust code to be imported by python

I'm using rust-cpython to make a python module in rust. I've run my code on a linux os and it runs just fine but I get the familiar "linking with cc failed:exit code 1 error". I've gathered from this that I need to add the .cargo/config file to my project as suggested at the bottom of this:
https://github.com/dgrunwald/rust-cpython
I've copied and pasted their code into a file, config.toml, and place there in a directory, .cargo. I've tried nesting this in my src directory and my project directory with no success, what am I missing?
Solution found: Thought I'd post it as this gave me grief.
Everything with this setup is fine except the config file can't have the extension .toml despite being written in a toml format

Go install exclude file

I have created a go script that compiles, starts, checks the status, and ends a web service I created (that is also in go). However, I have come to a road block.
With the compile feature I run the following command:
go install .
Which gives the following error:
./script.go:55: main redeclared in this block
previous declaration at ./hello.go:8
Which makes sense as I have two different files, both with the main func and main package. I also tried moving the script to another folder and then changing the command ran to:
go install {path}
Where {path} is equal to the path I want installed/compiled. Which I then got the following error:
exit status 1: can't load package: package /var/www/test.com/go: import "/var/www/test.com/go": cannot import absolute path
So in conclusion I have thought of only one solution (and I am up to hear others if mine isn't the best approach). My idea is to exclude the script file from compiling with the rest of the files, but I am unsure how to.
I did some research and couldn't find an easy way to do it (such as an --exclude flag with the go install command). Does anybody know how to accomplish what I am trying to achieve?
Thank you.
you could give the hello.go a different package name, that should work. Or i am missing something?
Regards
Tim

How to package a Kivy app with Pyinstaller

I have a lot of troubles following the instructions form the Kivy website, many steps aren't explained like what should I answer to the warning.
WARNING: The output directory "..." and ALL ITS CONTENTS will be REMOVED! Continue? (y/n)
Even if I choose y, the folder isn't removed.
Also should I always add these lines:
from kivy.deps import sdl2, glew
Tree('C:\\Users\\<username>\\Desktop\\MyApp\\'),
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)]
in the .spec file? Why are they necessary?
Not many info is available for Kivy.
Because I spent a lot of time understanding how I should package my app, here are some instructions that would have really helped me.
Some info are available at http://pythonhosted.org/PyInstaller/
Python 3.6 as of march 2017
Because packaging my app gave me the error IndexError: tuple index out of range, I had to install the developement version of PyInstaller:
pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip
Step 1:
I moved all the files of MyApp in a folder "C:\Users\<username>\Desktop\MyApp": the .py, the .kv and the images and I created an icon.ico.
I created another folder C:\Users\<username>\Desktop\MyPackagedApp. In this folder I press Shift+right click and select open command window here.
Then I pasted this:
python -m PyInstaller --name MyApp --icon "C:\Users\<username>\Desktop\MyApp\icon.ico" "C:\Users\<username>\Desktop\MyApp\myapp.py"
This creates two folders, build and dist, and a .spec file. In dist/MyApp, I can find a .exe. Apparently, if my app is really simple (just one label), the packaged app can works without the Step 2.
Step 2:
The second step involves editing the .spec file. Here is an exemple of mine.
(cf Step 3, for the explanations about my_hidden_modules)
I go back to the cmd, and enter
python -m MyApp myapp.spec
I then got this warning:
WARNING: The output directory "..." and ALL ITS CONTENTS will be REMOVED! Continue? (y/n)
I enter y and then press enter.
Because I choosed y, I was surpised that the folder build was still there and that the dist/MyApp was still containing many files. But this is normal. PyInstaller can output a single file .exe or a single folder which contains all the script’s dependencies and an executable file. But the default output is a single folder with multiple files.
Step 3: adding hidden modules
When I click on the myapp.exe in dist/MyApp, the app crashed. In the log C:\Users\.kivy\logs\ I could find 2 errors: ModuleNotFoundError: No module named 'win32timezone' and SystemError: <class '_frozen_importlib._ModuleLockManager'>.
Because of this I had to edit the .spec file and add these lines:
my_hidden_modules = [
( 'C:\\Users\\<username>\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\win32\\lib\\win32timezone.py', '.' )
]
in a = Analysis I changed datas = [] to datas = my_hidden_modules,
Apparently this is because I used a FileChooser widget.
So, the line:
ALL ITS CONTENTS will be REMOVED!
yes, it will be removed AND replaced later with new files. Check the date. I think it prints permission denied if it can't do such a thin both for files and the whole folder, so you'd notice it. It's important though, because you need to add additional files into your folder.
Those additional files of two types:
kivy dependencies
application data
Dependencies are just binaries (+/- loaders, licenses, or so), you get them through the *[Tree(p) ...] piece of code, which is just a command for "get all files from that folder". Without them Kivy won't even start.
Similarly to that, the second Tree(<app folder>) does the same, but for your own files such as .py files, .kv files, images, music, databases, basically whatever you create.
Obviously if you remove the deps, app won't start and if you remove app data, you'll get some path errors and most likely crash. You don't want any of that :P
It also works if in the 'a = Analysis...' block in the spec file one substitutes
hiddenimports=[]
for
hiddenimports=['win32file', 'win32timezone']
for win32file, win32timezone or for whatever files are missing

How can I include ui and image files while using py2exe?

I am working on a project using Python 2.7 and PySide 1.1.2. My code is working without any problem on my GNU/Linux but I want to distribute for Windows ( 7 and 8 ) as well. I can't expect users to install Python and PySide, so I decided to use py2exe (I also tried cx_freeze and pyinstaller).
First of all, here is my file tree: My Project on GitHub
I created a setup.py, here it is:
# -*- coding: utf-8 -*-
from distutils.core import setup
import py2exe
setup(
console=['bin/metusuite.py'],
name='metusuite',
version='0.1',
author='H. Gökhan Sarı',
author_email='me#th0th.me',
packages=['metusuite_libs'],
package_dir={'metusuite_libs': 'metusuite_libs'},
package_data={'metusuite_libs': ['ui/*', 'images/*']},
scripts=['bin/metusuite.py'],
url='https://github.com/th0th/metusuite/',
license='LICENSE.txt',
description='METU Suite.',
long_description=open('README.md').read(),
)
When I run
setup.py py2exe
it successfully builds metusuite.exe in 'dist' folder, however, since application depends on external user interface files -created with Qt Designer- and it can't find them, I get an error:
Designer: An error has occurred while reading the UI file at line 1, column 0: Premature end of document.
Traceback (most recent call last):
File "metusuite.py", line 38, in <module>
File "metusuite_libs\msCafeteriaMenu.pyc", line 37, in __init__
File "metusuite_libs\msCafeteriaMenu.pyc", line 17, in __init__
RuntimeError: Unable to open/read ui device
And I couldn't figure out how am I supposed to add *.ui files (also there are some .png icons) into that structure. I was thinking of converting .ui files to Python code, then I would have encounter same issue when I need to add some icons.
Hence, how can I add my ui and png files in py2exe structure? Or is there any alternative method for what I am trying to accomplish?
Well, I think you could do one of two realistic things:
Compile your .ui files to .py files using pyside-uic and modify your code to do conditional loading of the py files for the user interface and place the png files in a Qt Resource file
Create a Qt Resource file with your ui files inside of it, compile that with pyside-rcc, and then load the ui files using QtUiTools or some similar process
pyside-uic
I greatly prefer using the pyside-uic method for loading ui files because it is the most straightforward way of loading ui files into a program that correlates with my knowledge of Qt in C++. pyside-uic is included with the PySide applications and for me it is found in the Scripts directory of my Python installation, e.g. C:\Python27\Scripts\pyside-uic.exe. Taking a note from how C++ compilation handles ui files, I typically compile my ui files to have a name like ui_[Name of the ui file].py:
C:\Python27\Scripts\pyside-uic mainwindow.ui > ui_mainwindow.py
Inside of that resulting .py file, pyside-uic creates a class named the same name as the base class of the ui file prepended with Ui_. So, for instance, if you created a mainwindow.ui that contained the definition for a class named MainWindow, the created class would be Ui_MainWindow. If the ui file defined a class named SourceWindow, the class within the .py file Ui_SourceWindow. In Qt Designer you set the class name by setting objectName in the root element of the object tree (in the upper right of the window).
With your files cafeteria_menu,ui and dialog_login.ui, you would get derived classes Ui_cafeteria_menu and Ui_dialog_login.
Once you have the .py file generated, it can be used by importing it into the definition file for your widget and used using the setupUi method of the class in the Ui file
from PySide import QtCore, QtGui
from ui_mainwindow import Ui_MainWindow
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
Once you have ui defined for the class, all of the connections and ui elements for the widget need to be accessed through self.ui
self.ui.lineEdit.textChanged[str].connect(self.processText)
Since you would have to put your .png files in a Qt Resource File, I'll talk about it in the next section.
pyside-rcc
Like pyside-uic, pyside-rcc is included with the PySide application, although mine is in the site-packages directory of Python instead of in Scripts (if it's in the same place for you, you can always copy it).
C:\Python27\lib\site-packages\PySide\pyside-rcc.exe
Before you can compile the Qt Resource File, you have to first create it using one of the Qt Tools. I use Qt Creator since it can perform almost all of the functions related to Qt in one application. The documentation for the Qt Resource System shows that the resource file is really just an XML file that defines file paths and internal paths for the resource system. You can set up and organize the files however you want but when it comes to compile, all of the files defined in the Resource File must be in the same directory or a sub-directory of the file. Once you have the Resource File defined, you need to use pyside-rcc.exe to compile it into a .py file. I typically name the resource file the same as the project and keep everything in one resource file to make dealing with the resources more concise.
C:\Python27\lib\site-packages\PySide\pyside-rcc.exe -py2 MyProject.qrc > MyProject_Resources.py
The -py2 switch defines that the output from the file should be formatted for Python 2.x. If you are using Python 3.x or plan to use it in the future, you can use the -py3 switch and the outcome will be compatible with Python 3.x.
Putting it all together
Since you are already loading the ui files directly QUiLoader, you just need to refactor your QUiLoader statements to load a QFile that opens the ui resource from the resource system. To use the files from the resource system, all you need to do is import your Resource .py file, the one generated from pyside-rcc, into the main script file of your program and the last line in the resource file is a call to qInitResources() which initializes the resources to be used in the entire program. To load a file using QFile, use a path that starts with ":" and then references the paths that were defined in the Resource File. You could create a file msResources.qrc that has ui and images that has your ui and png files defined as sub categories.
So, if your resource file looks something like this
/ui
cafeteria_menu.ui
dialog_login.ui
/images
cafeteria-menu.png
exit.png
logo.png
mail-fetch.ong
And, if you want to load those files, you just need to create a QIcon or QFile like so:
cafeteriaMenuIcon = QtGui.QIcon(":/images/cafeteria-menu.png")
cafeteriaMenuUi = QtCore.QFile(":/ui/cafeteria_menu.ui")
In use in your code for GUICafeteriaMenu in msCafeteriaMenu, I would just change the __init__ method for GuiCafeteriaMenu to load and use the ui file from the resources:
uiFile = QFile(":/ui/cafeteria_menu.ui")
uiFile.open(QFile.ReadOnly)
UiLoader.load(uiFile, self)
uiFile.close()
I would probably place the output from pyside-rcc into the metsuite_libs package into something like msResources.py and import the msResources file in the __init__.py file as part of your package. That way, once you have the .py files created and imported into your program, the extra file would be encapsulated in your package and you will not need to change your setup.py file. Before you do the py2exe conversion, running the refactored program should work just fine normally. Additionally, no matter how you handle the ui files, you will always need to use a Resource File to be able to package icons into the program. For portability reasons, using resource files for icons is probably a good habit to get in to.

Resources