watcher

Mattepuffo's logo
Implementare un directory watcher in Python

Implementare un directory watcher in Python

In altri implementare un dir-watcher non richiede nessuna libreria esterna.

Se vogliamo fare la stessa coas in Python, invece, dobbiamo usare una libreria esterna.

Nel caso specifico useremo Watchdog, che è multi piattaforma, e quindi usabile sia su Linux, che su Mac che su Windows.

Per installarla possiamo usare pip:

pip install watchdog

Sul sito ci sono anche altre alternative di installazione, ma a me pip ha funzionato senza problemi.

Mattepuffo's logo
Monitorare il file system con Java

Monitorare il file system con Java

Finalmente vacanza, e finalmente posso dedicarmi di più al blog e alla programmazione per conto mio!

Oggi vediamo come monitorare il file system usando Java e il packaje NIO (incluso nel JDK ovviamente).

Qui potete leggere le differenze rispetto al più classico IO.

Partiamo dal main:

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchService;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;

public class Main {

    public static void main(String[] args) throws IOException, InterruptedException {
        Path folder = Paths.get("/path/to/dir");
        if (folder == null) {
            throw new UnsupportedOperationException("Directory not found");
        }
        WatchService ws = folder.getFileSystem().newWatchService();
        Watcher w = new Watcher(ws);
        Thread th = new Thread(w, "Watcher");
        th.start();
        folder.register(ws, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
        th.join();
    }

}

Come vedete usiamo praticamente solo classi e oggetti di NIO.

Mattepuffo's logo
Filtrare una ListView in Android

Filtrare una ListView in Android

Oggi vediamo come impostare dei filtri in una ListView in Android.

In sostanza, avendo una ListView caricata (do per scontato che sappiate come caricarla), filtreremo la lista in base a quello che scriviamo in una EditText (una casella di testo).

Partendo dal layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/txt_search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/cerca"
        android:maxLines="1" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Così facendo abbiamo impostato una EditText a inizio layout, che di default non è selezionata.

Mattepuffo's logo
Monitorare una directory in C#

Monitorare una directory in C#

Avevo la necesità di controllare le modifiche effettuate dentro una particolare directory.

Grazie al forum.html.it sono arrivato presto alla soluzione: usare la classe FileSystemWatcher di C#.

Vediamo come usare questa classe.

Questa è la classe che mi sono creato io:

namespace DirectoryMonitor
{
    class DirMonitor
    {
        private string path;
        private DateTime dt = new DateTime();

        public DirMonitor(string path)
        {
            this.path = path;
        }

        public void Watcher()
        {
            FileSystemWatcher fw = new FileSystemWatcher();
            fw.Path = path;
            fw.IncludeSubdirectories = false;
            fw.NotifyFilter =
                NotifyFilters.LastAccess |
                NotifyFilters.LastWrite |
                NotifyFilters.FileName |
                NotifyFilters.DirectoryName;
            fw.Changed += new FileSystemEventHandler(OnChanged);
            fw.Created += new FileSystemEventHandler(OnChanged);
            fw.Deleted += new FileSystemEventHandler(OnChanged);
            fw.Renamed += new RenamedEventHandler(OnRenamed);
            fw.EnableRaisingEvents = true;

            Console.WriteLine("Digita q per uscire.");
            Console.WriteLine();
            while (Console.Read() != 'q') ;
        }

        private void OnChanged(object source, FileSystemEventArgs e)
        {
            dt = DateTime.UtcNow;
            Console.WriteLine("PATH " + e.FullPath + " " + e.ChangeType + " AT " + dt.ToLocalTime());
        }

        private void OnRenamed(object source, RenamedEventArgs e)
        {
            dt = DateTime.UtcNow;
            Console.WriteLine("OLD PATH " + e.OldFullPath + " NEW PATH " + e.FullPath + " " + e.ChangeType + " AT " + dt.ToLocalTime());
        }
    }
}