Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

classifile

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

classifile

  • 0.2.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Classifile

status

Classifile is a tool for classifying files by Ruby DSL.

Installation

Ruby 3.0.0 or higher is required.

$ gem install classifile

Usage

  1. Write a DSL.

    # dsl.rb 
    
    dir "Images" do
      image?
      dir "Dogs" do
        include? "dog"
      end
    
      dir "Cats" do
        dir "Kittens" do
          include? "kitten"
        end
        include? "cat"
      end
    end
    
    dir "Movies" do
      movie?
    end
    
  2. Prepare the files.

temp/
├from/
│ ├─wild_dog.jpg
│ ├─beautiful_cat.png
│ ├─cute_kitten.gif
│ └─dancing_cat.mpg
└to/
  1. Run the command
$ classifile --dsl dsl.rb --from "/temp/from/*"  --to "/temp/to" 
  1. Done!
temp/
├from/
└to/
 ├Images/
 │ ├Dogs/
 │ │ └─wild_dog.jpg
 │ └Cats/
 │  │└─beautiful_cat.png
 │  └Kittens/
 │   └─cute_kitten.gif
 └Movies/
  └─dancing_cat.mpg

Options

Command line options are here.

Sort optionLong optionDescription
-d PATH--dsl PATHDSL path
-f PATH--from PATHSource directory path
-t PATH--to PATHOutput directory path
-p--previewPreview Mode(don't move,don't copy)
-c--copyCopy Mode(don't move)
-h--helpShow this help

Syntax

Classifile rules.

  1. The hierarchy of the dir block represents the directory hierarchy.
  2. If the check fails, exit the block.
  3. When It complete a block to the end, it will be saved in that directory.

Block methods

dir block

dir block represents a directory. A dir block can also be nested.

dir "Images" do
   image?
   dir "Dogs" do
      include? "dog"
   end
end

dog.png -> /Images/Dogs/dog.png

group block

group block is a group that does not have a directory.

group "Animals" do
   dir "Dogs" do
      include? "dog"
   end
end

dog.png -> /Dogs/dog.png

del block

del block deletes the file.

del do
   end_with? ".tmp"
end

temp.tmp will be deleted.

Check methods

The check method checks if the file should be stored in that directory. If it does not pass the check, it will leave the block immediately.

include?

include? method checks if the file name contains one of the strings.

dir "Dogs" do
   include? "dog"
end

dir "Cats" do
   include? "cat" , "kitten"
end

dog.png -> /Dogs/dog.png cat.png -> /Cats/cat.png kitten.png -> /Cats/kitten.png

end_with?

end_with? method checks if the file name ends with one of the string.

dir "Archives" do
   end_with? ".zip" , ".gz"
end

doc.zip -> /Archives/doc.zip

image? / sound? / movie?

image? method checks if a file is an image or not.

sound? method checks if a file is an sound or not.

movie? method checks if a file is an movie or not.

dir "Images" do
   image?
end

dir "Movies" do
   image?
end

cat.png -> /Images/cat.png dog.avi -> /Movies/dog.avi

assert / assert_nil / assert_includes ...

You can also use the minitest methods.

dir "Archives" do |file|
   assert_includes %w[.zip .gz], file.extname
end

doc.zip -> /Archives/doc.zip

Other methods

empty_dir!

If empty_dir! is executed, the file will not be saved directly under that directory.

In this example, dog.png will be saved in Images/Dogs. In this example, dog.png will be saved in Images/Dogs, but cat.png will not be saved anywhere.

dir "Images" do
   empty_dir!
   image?
   dir "Dogs" do
      include? "dog"
   end
end

doc.png -> /Images/Dogs/dog.png cat.png -> (not saved)

after_save

Execute the method after the save is complete.

dir "Markdown" do |_file|
  end_with? ".md"
  after_save :hello
  
  def hello
    puts "hello"
  end
end

hello.md -> /Markdown/hello.md

and

The output is "hello".

Block argument

File information can be obtained by block argument.

dir "Archives" do |file|
   assert_includes %w[.zip .gz], file.extname
end
PropertyDescription
dirnameThe directory before the move
basenameFile name
pure_basenameFile name without extension
extnameExtension
atimeDate and time of made
ctimeDate and time of change
sizeFile size
to_pathPaths that will be saved

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kurehajime/classifile. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Classifile project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

FAQs

Package last updated on 16 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc