Search

Creating IOCTL linux kernel 5.3


Code for creating IOCTL command in linux kernel 5.3 . The details of creation and usage of kerenl is given in the post here

/* The code has been tested on kerenl 5.3
The steps to use the icotl in user space is given in
http://tuxthink.blogspot.com/2011/01/creating-ioctl-command.html
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h> // required for various structures related to files liked fops.
#include <linux/semaphore.h>
#include <linux/cdev.h>
#include "ioctl_basic.h"
#include <linux/version.h>
static int Major;
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,36)
#define UNLOCKED 1
#endif
int open(struct inode *inode, struct file *filp)
{
printk(KERN_INFO "Inside open \n");
return 0;
}
int release(struct inode *inode, struct file *filp) {
printk (KERN_INFO "Inside close \n");
return 0;
}
#ifdef UNLOCKED
long int ioctl_funcs(struct file *filp,unsigned int cmd, unsigned long arg)
{
int ret=0;
switch(cmd) {
case IOCTL_HELLO:
printk(KERN_INFO "Hello ioctl world");
break;
}
return ret;
}
struct file_operations fops = {
open: open,
unlocked_ioctl: ioctl_funcs,
release: release
};
#else
int ioctl_funcs(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
int data=10,ret;
switch(cmd) {
case IOCTL_HELLO:
printk(KERN_INFO "Hello ioctl world");
break;
}
return ret;
}
struct file_operations fops = {
open: open,
ioctl: ioctl_funcs,
release: release
};
#endif
struct cdev *kernel_cdev;
int char_arr_init (void) {
int ret;
dev_t dev_no,dev;
kernel_cdev = cdev_alloc();
kernel_cdev->ops = &fops;
kernel_cdev->owner = THIS_MODULE;
printk (" Inside init module\n");
ret = alloc_chrdev_region( &dev_no , 0, 1,"char_arr_dev");
if (ret < 0) {
printk("Major number allocation is failed\n");
return ret;
}
Major = MAJOR(dev_no);
dev = MKDEV(Major,0);
printk (" The major number for your device is %d\n", Major);
ret = cdev_add( kernel_cdev,dev,1);
if(ret < 0 )
{
printk(KERN_INFO "Unable to allocate cdev");
return ret;
}
return 0;
}
void char_arr_cleanup(void) {
printk(KERN_INFO " Inside cleanup_module\n");
cdev_del(kernel_cdev);
unregister_chrdev_region(Major, 1);
}
MODULE_LICENSE("GPL");
module_init(char_arr_init);
module_exit(char_arr_cleanup);
view raw create_ioctl.c hosted with ❤ by GitHub

Creating a copy of a spreadsheet having only unique rows from a given sheet


Let us say we have the following sheet which has the repeated values highlighted in bold. The following script can be used if we want to create a new sheet having rows with the repeated values of the column ID removed.



The value of file1 points to the original file and copy is the path to the new file to be created.

#!/usr/bin/env python3
"""
Tested on python 3.6
file1 : Path to the original file
copy: Path to the file to be created having unique rows from file1
"""
import pyoo
file1="/media/satish/5229e5b6-0b7b-44ef-a609-5aaf186c6bc4/data/pro_g/python/pyoo/test.ods"
copy="/media/satish/5229e5b6-0b7b-44ef-a609-5aaf186c6bc4/data/pro_g/python/pyoo/copy1.ods"
try:
desktop = pyoo.Desktop('localhost', 2002)
doc1 = desktop.open_spreadsheet(file1)
doc2 = desktop.create_spreadsheet()
except Exception as e :
print(e)
print("\nInvalid path for the files")
quit()
sheet1 = doc1.sheets[0]
sheet2 = doc2.sheets[0]
col=0
try:
column=input('Enter the column header of which unique elements are needed \n')
while (sheet1[0,col].value != column):
col=col+1
except IndexError:
print("Invalid column name")
quit()
i=0
scol=0
while(sheet1[0,scol].value != ''):
sheet2[0,scol].value=sheet1[0,scol].value
sheet2[0,scol].background_color= sheet1[0,scol].background_color
sheet2[0,scol].text_color=sheet1[0,scol].text_color
sheet2[0,scol].font_weight=sheet1[0,scol].font_weight
sheet2[0,scol].border_width=sheet1[0,scol].border_width
sheet2[0,scol].border_color=sheet1[0,scol].border_color
scol=scol+1
while(sheet1[i,col].value != ''):
i=i+1
last=i
x=sheet1[1:last,col].values
unique=list(set(x))
size=len(unique)
j=1
for row in range(last) :
for item in unique :
if(sheet1[row,col].value == item):
print(item)
sheet2[j,:].values = sheet1[row,:].values
j=j+1
unique.remove(item)
break
print("There are " + str(size) +" unique values")
doc2.save(copy)
doc1.close()
doc2.close()


Save the file as create_sheet_unique_rows.py and execute it.



The new file created should look as below.


Copy rows from one spreadsheet to new one based on columns in a spreadsheet

If we want to copy  the rows from one spreadsheet to a new file based on values in another file, for example we have the files listed below.


                   
                                                File1                                                                                File2



    If we want to create a new file which has the rows of File1 only for those ids which are present in file2 we can use the following pyoo script.

Steps for installation of pyoo has been given here.

Before executing the script, we have to ensure that libreoffice is listening to the pyoo by running the following command


In the script below, file1points to the file with source data ,file2 points to the file which has the reference values based on which we want to copy and copy points to the file which will be created. Update the paths respectively.

#!/usr/bin/env python3
"""
Tested on python 3.6
file1: File from where rows need to be copied
file2: File having the reference values
copy : The new file to be created with the required rows.
Update the above three paths before executing the script.
"""
import pyoo
file1=""
file2=""
copy=""
desktop = pyoo.Desktop('localhost', 2002)
try:
doc1 = desktop.open_spreadsheet(file1)
doc2 = desktop.open_spreadsheet(file2)
doc3 = desktop.create_spreadsheet()
except Exception as e :
print(e)
print("\nInvalid path for the files")
quit()
srcSheet = doc1.sheets[0] #Assuming the sheet0 has the data
fromSheet= doc2.sheets[0]
toSheet=doc3.sheets[0]
fr=tr=1
fcol=0
scol=0
while(srcSheet[0,scol].value != ''):
toSheet[0,scol].value=srcSheet[0,scol].value
toSheet[0,scol].background_color= srcSheet[0,scol].background_color
toSheet[0,scol].text_color=srcSheet[0,scol].text_color
toSheet[0,scol].font_weight=srcSheet[0,scol].font_weight
toSheet[0,scol].border_width=srcSheet[0,scol].border_width
toSheet[0,scol].border_color=srcSheet[0,scol].border_color
scol=scol+1
scol=1
i=0
while(fromSheet[fr,fcol].value != ''):
sr=1
while(srcSheet[sr,scol].value != ''):
if(srcSheet[sr,scol].value == fromSheet[fr,fcol].value):
toSheet[tr,:].values=srcSheet[sr,:].values
tr=tr+1
toSheet[tr,:].background_color= \
fromSheet[fr,fcol].background_color
toSheet[tr,:].text_color=srcSheet[sr,:].text_color
toSheet[tr,:].font_weight=srcSheet[sr,:].font_weight
toSheet[tr,:].border_width=srcSheet[sr,:].border_width
toSheet[tr,:].border_color=srcSheet[sr,:].border_color
i=i+1
sr=sr+1
fr=fr+1
print("Copied " + str(i) + " rows")
doc3.save(copy)
doc1.close()
doc2.close()
doc3.close()
view raw create_copy.py hosted with ❤ by GitHub


Save the script as create_copy.py and execute it.



We should see the following file by the name given in the copy path.



Librecalc automatic grading of columns

We can use the following Pyoo based python script to automatically assign grades based on CGPA values in a column.
For example, consider the following file with CGPAs listed in the column B



If we want to assign grades based on  the CGPA values, we can use the script attached.

#!/usr/bin/env python3
"""
Tested on python 3.6
Modify the path variable to point to the location of the librecalc file.
Modify the grade dict to suit the grades that you want to assign.
"""
import pyoo
path=""
desktop = pyoo.Desktop('localhost', 2002)
doc = desktop.open_spreadsheet(path)
sheet = doc.sheets[0]
column=input('Enter the column header of which grading is needed \n')
sheet = doc.sheets[0]
col=0
try:
while (sheet[0,col].value != column):
col=col+1
except IndexError:
print("Invalid column name")
quit()
"""
Please modify the following dictionary to
meet your grading standard
"""
grade_dict={
"Ten": "S+",
"Between 9 and 10": "A",
"Between 8 and 9" : "B",
"Between 7 and 8" : "C",
"Between 6 and 7" : "D",
"Between 5 and 6" : "E",
"Less than 5" : "F"
}
i=0
sheet[i,col+1].value = "Grades"
sheet[i,col+1].background_color=sheet[i,col].background_color
sheet[i,col+1].text_color=sheet[i,col].text_color
sheet[i,col+1].font_weight=sheet[i,col].font_weight
sheet[i,col+1].border_width=sheet[i,col].border_width
sheet[i,col+1].border_color=sheet[i,col].border_color
i=i+1
while(sheet[i,col].value != ''):
if(int(sheet[i,col].value) == 10):
sheet[i,col+1].value = grade_dict["Ten"]
elif(int(sheet[i,col].value) < 10 and int(sheet[i,col].value) >= 9):
sheet[i,col+1].value = grade_dict["Between 9 and 10"]
elif(int(sheet[i,col].value) < 9 and int(sheet[i,col].value) >= 8):
sheet[i,col+1].value = grade_dict["Between 8 and 9"]
elif(int(sheet[i,col].value) < 8 and int(sheet[i,col].value) >= 7):
sheet[i,col+1].value = grade_dict["Between 7 and 8"]
elif(int(sheet[i,col].value) < 7 and int(sheet[i,col].value) >= 6):
sheet[i,col+1].value = grade_dict["Between 6 and 7"]
elif(int(sheet[i,col].value) < 6 and int(sheet[i,col].value) >= 5):
sheet[i,col+1].value = grade_dict["Between 5 and 6"]
elif(int(sheet[i,col].value) < 5):
sheet[i,col+1].value = grade_dict["Less than 5"]
else:
sheet[i,col+1].value = "Invalid grade"
print("Assigned " + str(i) +"th grade")
sheet[i,col+1].background_color=sheet[i,col].background_color
sheet[i,col+1].text_color=sheet[i,col].text_color
sheet[i,col+1].font_weight=sheet[i,col].font_weight
sheet[i,col+1].border_width=sheet[i,col].border_width
sheet[i,col+1].border_color=sheet[i,col].border_color
i=i+1
doc.save()
doc.close()


The script needs Pyoo to be installed, the steps for the same can be seen here .

Please note that to be able to run the script the following command needs to be run in the terminal,

  
 






If the script is saved as column_grade.py run the script from the terminal.



The file should get updated and look as below.


Python Pyoo based script to find the unique elements in a librecalc column

The following is pyoo based python script will be able to list all the unique values in a given column of a librecalc. For example if we have the following file


If we want to find all the unique terms in the column num, just run the following script. Ensure that the path variable is initialized to the full path pointing to the librecalc file.  The script requires pyoo installed, the steps for which are listed here.

Before running the script, make sure to run the following command.





#!/usr/bin/env python3
"""
Tested on python 3.6
"""
import pyoo
path="/media/satish/5229e5b6-0b7b-44ef-a609-5aaf186c6bc4/data/pro_g/python/pyoo/test.ods"
desktop = pyoo.Desktop('localhost', 2002)
doc = desktop.open_spreadsheet(path)
sheet = doc.sheets[0]
column=input('Enter the column header of which unique elements are needed \n')
sheet = doc.sheets[0]
col=0
while (sheet[0,col].value != column):
col=col+1
i=0
addr1=str(sheet[1,col].address)
while(sheet[i,col].value != ''):
i=i+1
last=i
x=sheet[1:last,col].values
unique=list(set(x))
size=len(unique)
print("There are " + str(size) +" unique values")
for i in unique:
print(int(i))
doc.close()


Saved the script as unique_items.py, and execute it. For the above example file shown, we get the following output.

Python based Pyoo script to add all elements of a column in librecalc


The following is a script in pyoo, to add all the numbers in a column by taking the column header as an input from the user.
For example, let us say we have a file a shown below.


The scripts calculates the sum of numbers in the column whose header is mentioned by the user, for example "num" in this case, and updates the sheet with the sum in the row after the numbers and also puts the formula for the same in the cell.

To be able to run pyoo, the package has to be installed. The steps for installation are given here

 

#!/usr/bin/env python3
"""
Tested on python 3.6
"""
import pyoo
"""
Set the path to the file.
"""
path=""
desktop = pyoo.Desktop('localhost', 2002)
doc = desktop.open_spreadsheet(path)
column=input('Enter the column header for which the sum is needed \n')
sheet = doc.sheets[0]
col=0
while (sheet[0,col].value != column):
col=col+1
i=0
addr1=str(sheet[1,col].address)
while(sheet[i,col].value != ''):
i=i+1
last=i
addr2=str(sheet[last-1,col].address)
sheet[last,1].formula='=sum('+str(addr1)+':' + str(addr2)
print(int(sheet[last,1].value))
doc.save()
doc.close()
view raw sum_pyoo.py hosted with ❤ by GitHub

Save the script as pyoo_column_sum.py. Make sure that the variable "path" has been intialized to point to the file where the librecalc file is located.
Run the following command before executing the script.



Note that because of the headless being added at the end of the above command, the window of librecalc will not be launched, the headless option can be removed if the window is required.

Now we can run the script (Note that the script has been tested on python3.6).




 
We can note that hte sheet has got updated with the sum of the numbers in the column and the required formula also has been updated in the sheet.

from base import Element, Css, Payload, UnoBaseFeature, UnoBaseField ImportError: cannot import name 'Element'

While trying to use pyoo if the importing pyoo



throws the following error



Two possible workarounds for this are

install python3-uno if its already not installed



If after installing this the error still persist, it could be because another package by the name uno, not related to libreoffice has been installed which is causing a conflict. So uninstall the package and try it.




Create a 300DPI image using gimp

To be able to print high resolution graphic images its preferred to have the pictures of 300 dpi. We can create the 300dpi images using GIMP .

Click on



The following image will pop.



Click on the advance options. To open the following window.



Enter 300 in the X and Y resolution with pixels/in chosen and click on "OK".

The image created will be of 300X300 dpi.