Is there any way for me to set up sublime to automatically create a folder of the same name when I create certain files.
I create landing pages that all have a lp_ prefix to the filename, I would like to watch for when a file with this name is created and then automatically create a folder of the same name in another directory (for css and images).
Would this be possible with a plugin or something like Grunt?
Example:
Create file: lp_test.php
Automate Folder Creation: /lp/lp_test/
You can make a plugin that extends EventListener and overrides (for example) on_post_save_async. You can use this simple example as base:
import sublime, sublime_plugin, os
# We extend event listener
class ExampleCommand(sublime_plugin.EventListener):
# This method is called every time a file is saved (not only the first time is saved)
def on_post_save_async(self, view):
variables = view.window().extract_variables()
fileBaseName = variables['file_base_name'] # File name without extension
path = 'C:/desiredPath/css/' + fileBaseName
if fileBaseName.startswith('lp_') and not os.path.exists(path):
os.mkdir(path)
EDIT: changed on_post_save to on_post_save_async as it runs in a different thread and does not block the application. Thanks MattDMo for commenting it and for adding python highlighting.
Related
for reference this is all using Pyqt5 and Python 3.6:
I've got a QStandardItemModel that is built from QStandardItems that are strings of the items in a zip (the model displays all the contents of a zipfile). I went with this choice as I can not cache the files locally, and my research shows that QFileSystemModel can not work on archives unless I unpack at least temporarily.
All items in the QStandardItemModel end in the correct extension for the file (.csv,.txt,ect), and I need to display the icon a user would see if they were looking at the file in windows explorer, however show it in the qtreeview (a user seeing content.csv should also see the icon for excel). On that note, this application is only running on windows.
How can I pull the extensions default system file icon, and set it during my setting of these items? Would I have to manually download the icons for my known file types and do this, or does the system store it somewhere I can access?
Here's some basic code of how I build and display the model and treeview:
self.zip_model = QtGui.QStandardItemModel()
# My Computer directory explorer
self.tree_zip = QTreeView()
self.tree_zip.setModel(self.zip_model)
def build_zip_model(self,current_directory):
self.zip_model.clear()
with zipfile.ZipFile(current_directory) as zip_file:
for item in zip_file.namelist():
model_item = QtGui.QStandardItem(item)
self.zip_model.appendRow(model_item)
You can use QFileIconProvider:
def build_zip_model(self, current_directory):
iconProvider = QtWidgets.QFileIconProvider()
self.zip_model.clear()
with zipfile.ZipFile(current_directory) as zip_file:
for item in zip_file.namelist():
icon = iconProvider.icon(QtCore.QFileInfo(item))
model_item = QtGui.QStandardItem(icon, item)
self.zip_model.appendRow(model_item)
I have a Selenium(Chrome) script that goes to a URL and downloads a bunch of files sequentially. But the names are all gibberish and as such neither Selenium nor Chrome has control over the name of the downloaded file. So what I want to do is watch the download directory for any new files that are created and then rename them on creation with a name of my own choosing.
How do I go about this? I've heard that watchdog is a good package to create and log an EventListener. But then how do I dynamically pass the handler a particular name when the on_created event is triggered? Is watchdog the right way to go or is there some other solution that can work?
NOTE: I did try grabbing all the in the directory using glob and then update the name of the latest file by comparing the creation time but that results in a logical error as it mixes up the filenames in case the new file hasn't downloaded by the time this method is executed. I've attached the code below for this method.
def __rename_downloaded_file(self, filename: str):
"""Rename the latest download file to the given name"""
# TODO create a listener instead of the while loop
while True:
# keep looping in case there are no file in directory.
list_of_files = glob.glob(f"{self.download_path}\\*.pdf")
if len(list_of_files) > 0:
break
latest_file = max(list_of_files, key=os.path.getctime)
print(latest_file)
head, _ = os.path.split(latest_file)
new_filename = os.path.join(head, filename+'.pdf')
print(new_filename)
os.rename(latest_file, new_filename)
did u tried to wait for file to download. If u did that way, u can just get latest file by max(list_of_files, key=os.path.getctime) and changed the latest file name. After the file changed you could have started downloading other files
The answer is here.
#Raghavendra Phayde thank you for putting me on this thread.
The list of downloads you get is sorted by latest download timestamp. You can then rename each file with the below code.
for old_filename, new_filename in zip(downloaded_file_names, rename_list):
head, _ = os.path.split(old_filename)
new_file_name_path = os.path.join(head, new_filename + '.pdf')
# print(old_filename)
# print(new_file_name_path)
# print('\n')
os.rename(old_filename, new_file_name_path)
Just want to know is there any proper way to load multiple config files to python scripts.
Directory structure as below.
dau
|-APPS
|---kafka
|---brokers
|-ENVS
As per the above, my base directory is dau. I'm planing to hold the script in Kafka and Broker directories. All global environments store in ENVS directory with ".ini" format. I want to load those ini files to all the script without adding one by one, because we may have to add more environments files in the future , in that case we don't have to add them manually on each and every scripts.
Sample env.ini
[DEV]
SERVER_NAME = dev123.abcd.net
i was trying to use the answer of below link, but still we have to add them manually, or if the parent path change in the dau directory, we have to edit the code.
Stack-flow-answer
Hi I came up with below solution, Thanks for the support.
Below code will get the all .ini file as list and return.
import os
def All_env_files():
try:
BASE_PATH = os.path.abspath(os.path.join(__file__,"../.."))
ENV_INI_FILES = [os.path.join(BASE_PATH + '/ENVS/',each) for each in os.listdir(BASE_PATH + '/ENVS') if each.endswith('.ini')]
return ENV_INI_FILES
except ValueError:
raise ValueError('Issue with Gathering Files from ENVS Directory')
Below code will take the list ini files and provide it to ConfigParser.
import ConfigParser, sys , os
"""
This is for kafka broker status check
"""
#Get Base path
Base_PATH = os.path.abspath(os.path.join(__file__,"../../.."))
sys.path.insert(0, Base_PATH)
#Importing configs python file on ../Configs.py
import Configs, edpCMD
#Taking All the ENVS ini file as list
List_ENVS = Configs.All_env_files()
Feel free to provide any shorter way to this.
Not sure about unix, but in windows you can add attributes to files, like a location on a photo file or a duration on a video file.
Is there a way to do this in node js. Would be very handy with my currently project. It would save me having to create separate attribute data files.
You may use the WinAttr package to do that.
See this module:
https://www.npmjs.com/package/winattr
But it's for attributes like archive, hidden, readonly, system.
I don't think you can add an attribute of of duration to the video file - the duration is written in the container/codec of the video itself. The location for images is in the EXIF data - which can be manipulated with other modules on npm - see:
https://www.npmjs.com/search?q=EXIF
For the location on a photo file or a duration on a video file you need to use whatever information is appropriate for that given image or video format.
You can run a SHELL command:
var execSync = require('child_process').execSync;
// Remove Hidden and system attributes:
execSync("attrib -h -s " + yourFolder);
// Add Hidden attribute:
execSync("attrib +h " + yourFolder);
Here is how I set up flask-assets for scss:
def configure_extensions(app):
# Web Assets
from app.extensions import assets
scss = Bundle(
'scss/all.scss',
filters='scss',
output='scss_all.css'
)
assets.register('scss_all', scss)
assets.init_app(app)
In my config, I set ASSETS_DEBUG = True
This works, and generates the file app/static/scss_all.scss and the folder app/static/.webassets.cache. The styles appear on the site as intended.
The problem, though, is if I want to regenerate the scss style sheet, I must delete the files mentioned above. This is tedious when playing around with scss.
Is there a way to regenerate these files automatically with the reloader when app.debug is set to True?
(aside: I'm using the dev version of flask)
This should ideally work. But incase you are using #imports to import other scss files within the main file then you need to add the depends option. Something like;
mycss = Bundle(
'app.scss',
filters='pyscss,cssprefixer', depends=('/path/to/scss/files/**/*.scss'), output='generated/css/app.css')
assets.register('mycss)
The answer by Joe should be accepted. I also implemented it in a similar way:
scss = Bundle('css/main.scss',
filters='libsass',
depends='css/*',
output='gen/home%(version)s.css')
app = Flask(__name__)
assets = Environment(app)
assets.register('scss_all', scss)
all scss files are in the css directory. The main.scss imports the others. Now any change to any file in that directory will force a refresh.